Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OORT-239 - Add the possibility to set createdBy.user on excel import #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"invalidEditRolesArguments": "Either permissions or channels must be provided.",
"invalidEditStepArguments": "Either name, type, content or permissions must be provided.",
"invalidEditWorkflowArguments": "Either name or steps must be provided.",
"invalidEmailsInput": "Wrong format detected. Please provide valid emails.",
"invalidEmailsInput": "Wrong format detected. Following emails are invalid: {{emails}}",
"invalidPaginationArguments": "Please provider valid integers for first and offset arguments.",
"invalidPublishNotificationArguments": "Action, content and channel arguments must all be provided.",
"invalidSeeNotificationArguments": "Notification ID must be provided.",
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"invalidEditRolesArguments": "******",
"invalidEditStepArguments": "******",
"invalidEditWorkflowArguments": "******",
"invalidEmailsInput": "******",
"invalidEmailsInput": "****** {{emails}}",
"invalidPaginationArguments": "******",
"invalidPublishNotificationArguments": "******",
"invalidSeeNotificationArguments": "******",
Expand Down
15 changes: 12 additions & 3 deletions src/routes/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ async function insertRecords(
// }
if (canCreate) {
const records: Record[] = [];
const dataSets: { data: any; positionAttributes: PositionAttribute[] }[] =
[];
const dataSets: Promise<{
data: any;
positionAttributes: PositionAttribute[];
user: mongoose.Types.ObjectId;
}>[] = [];
const workbook = new Workbook();
await workbook.xlsx.load(file.data);
const worksheet = workbook.getWorksheet(1);
Expand All @@ -75,8 +78,13 @@ async function insertRecords(
dataSets.push(loadRow(columns, values));
}
});
const resolvedDataSets: {
data: any;
positionAttributes: PositionAttribute[];
user: mongoose.Types.ObjectId;
}[] = await Promise.all(dataSets);
// Create records one by one so the incrementalId works correctly
for (const dataSet of dataSets) {
for (const dataSet of resolvedDataSets) {
records.push(
new Record({
incrementalId: await getNextId(
Expand All @@ -89,6 +97,7 @@ async function insertRecords(
resource: form.resource ? form.resource : null,
createdBy: {
positionAttributes: dataSet.positionAttributes,
user: dataSet.user,
},
})
);
Expand Down
9 changes: 7 additions & 2 deletions src/schema/mutation/addUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ export default {
);
}
}
if (args.users.filter((x) => !validateEmail(x.email)).length > 0) {
throw new GraphQLError(context.i18next.t('errors.invalidEmailsInput'));
const invalidEmails = args.users.filter((x) => !validateEmail(x.email));
if (invalidEmails.length > 0) {
throw new GraphQLError(
context.i18next.t('errors.invalidEmailsInput', {
emails: invalidEmails.map((x) => x.email),
})
);
}
// Separate registered users and new users
const invitedUsers: User[] = [];
Expand Down
10 changes: 10 additions & 0 deletions src/utils/files/getUploadColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const getUploadColumns = (fields: any[], headers: any[]): any[] => {
}
}
}
// Create position attributes columns
for (const name of headers.filter((x) => x && x.startsWith('$attribute.'))) {
const index = headers.indexOf(name);
columns.push({
Expand All @@ -155,5 +156,14 @@ export const getUploadColumns = (fields: any[], headers: any[]): any[] => {
category: name.replace('$attribute.', ''),
});
}
// Create user column
for (const name of headers.filter((x: string) => x && x === '$user')) {
const index = headers.indexOf(name);
columns.push({
name,
index,
type: '$user',
});
}
return columns;
};
36 changes: 31 additions & 5 deletions src/utils/files/loadRow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isArray } from 'lodash';
import set from 'lodash/set';
import { PositionAttribute } from '../../models';
import { PositionAttribute, User } from '../../models';
import mongoose from 'mongoose';

/**
* Transforms uploaded row into record data, using fiels definition.
Expand All @@ -9,14 +10,19 @@ import { PositionAttribute } from '../../models';
* @param row list of records
* @returns list of export rows.
*/
export const loadRow = (
export const loadRow = async (
columns: any[],
row: any
): { data: any; positionAttributes: PositionAttribute[] } => {
): Promise<{
data: any;
positionAttributes: PositionAttribute[];
user: mongoose.Types.ObjectId;
}> => {
const data = {};
const positionAttributes = [];
let user;
for (const column of columns) {
const value = row[column.index];
let value = row[column.index];
if (value !== undefined) {
switch (column.type) {
case 'boolean': {
Expand Down Expand Up @@ -71,12 +77,32 @@ export const loadRow = (
});
break;
}
case '$user': {
if (typeof value === 'object' && value.text) {
value = value.text;
}
let filter;
if (mongoose.Types.ObjectId.isValid(value)) {
filter = {
$or: [
{ username: value },
{
_id: mongoose.Types.ObjectId(value),
},
],
};
} else {
filter = { username: value };
}
user = (await User.findOne(filter, '_id'))._id;
break;
}
default: {
data[column.field] = value;
break;
}
}
}
}
return { data, positionAttributes };
return { data, positionAttributes, user };
};