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

Tweak predictProblems logic, add test for it #2117

Open
wants to merge 2 commits 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
34 changes: 34 additions & 0 deletions src/features/import/utils/problems/predictProblems.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,40 @@ describe('predictProblem()', () => {
]);
});

it('returns problem when either the first name or last name value is missing', () => {
const sheet = makeFullSheet({
columns: [
{
field: 'first_name',
kind: ColumnKind.FIELD,
selected: true,
Comment on lines +453 to +455
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try creating another test that is a copy of this one, except that this column is not configured (ColumnKind.UNKNOWN and selected: false). In my experimentation, that seems to not return a problem even though it should return UNCONFIGURED_ID_AND_NAME because not the full name has been configured.

},
{
field: 'last_name',
kind: ColumnKind.FIELD,
selected: true,
},
{
field: 'email',
kind: ColumnKind.FIELD,
selected: true,
},
],
rows: [
{ data: ['Clara', 'Zetkin', '[email protected]'] },
{ data: ['John', '', '[email protected]'] },
],
});

const problems = predictProblems(sheet, 'SE', []);
expect(problems).toEqual([
{
indices: [1],
kind: ImportProblemKind.MISSING_ID_AND_NAME,
},
]);
});

it('returns no problems when date column is configured and all cells have a value', () => {
const sheet = makeFullSheet({
columns: [
Expand Down
34 changes: 21 additions & 13 deletions src/features/import/utils/problems/predictProblems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ export function predictProblems(
(col) => col.kind == ColumnKind.FIELD && col.field == 'last_name'
);

if (!sheetHasId) {
if (sheetHasFirstName && sheetHasLastName) {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID,
});
} else {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID_AND_NAME,
});
}
}

function accumulateFieldProblem(field: string, row: number) {
const existing = problemByField[field];
if (existing) {
Expand All @@ -75,6 +63,7 @@ export function predictProblems(
customFields.forEach((field) => {
customFieldsBySlug[field.slug] = field;
});
let missingValueInName = false;

sheet.rows.forEach((row, index) => {
const rowIndex = sheet.firstRowIsHeaders ? index - 1 : index;
Expand Down Expand Up @@ -133,11 +122,18 @@ export function predictProblems(
}
}
hadImpact = true;
} else {
if (
column.kind == ColumnKind.FIELD &&
(column.field == 'first_name' || column.field == 'last_name')
) {
missingValueInName = true;
}
}
}
});

if (sheetHasId && sheetHasFirstName && sheetHasLastName) {
if (sheetHasFirstName && sheetHasLastName) {
if (!rowHasId && (!rowHasFirstName || !rowHasLastName)) {
if (!rowProblemByKind[ImportProblemKind.MISSING_ID_AND_NAME]) {
rowProblemByKind[ImportProblemKind.MISSING_ID_AND_NAME] = {
Expand All @@ -153,6 +149,18 @@ export function predictProblems(
}
});

if (!sheetHasId && !missingValueInName) {
if (sheetHasFirstName && sheetHasLastName) {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID,
});
} else {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID_AND_NAME,
});
}
}

if (!hadImpact) {
problems.push({
kind: ImportProblemKind.NO_IMPACT,
Expand Down
Loading