Skip to content

Commit

Permalink
Export CSV respecting inherited values (#4117)
Browse files Browse the repository at this point in the history
* Export correctly inherited fields

* Use generated ids

* Fix linting

* Modify json schema

* Fix schema

* Declare new schema for inherited value

* Exclude ts declaration files from code climate
  • Loading branch information
Sergio Revilla authored Jan 10, 2022
1 parent d0b22fd commit 3d0238d
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 26 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ plugins:
exclude_patterns:
- '**/index.js'
- '**/fixtures.js'
- '**/*.d.ts'
5 changes: 3 additions & 2 deletions app/api/csv/specs/csvExporter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,14 @@ describe('csvExporter', () => {
});

describe('headers', () => {
it('processHeaders hould return the de-duplicated union of the templates properties', () => {
it('processHeaders should return the de-duplicated union of the templates properties', () => {
const headers: ExportHeader[] = processHeaders(testTemplates);
const headersLabels = headers.map((header: ExportHeader) => header.label);

[
'company',
'Nemesis',
'Country',
'Costume',
'Super powers',
'Allies',
Expand All @@ -100,7 +101,7 @@ describe('csvExporter', () => {
expect(headersLabels).toContain(label);
});

expect(headers.length).toBe(9);
expect(headers.length).toBe(10);
});

it('prependCommonHeaders should add entries tagged with common at the beginning', () => {
Expand Down
25 changes: 22 additions & 3 deletions app/api/csv/specs/exportCsvFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export const templates: any = {
type: 'relationship',
label: 'Nemesis',
},
{
_id: '61d5894c03be491b8b2c2937',
relationType: '61d588f4263df5aa829ddb97',
id: 'd0ab3a88-2e47-4991-9ac1-1b2273f9b765',
name: 'country',
content: '61d5891421eaff4de462c8ee',
type: 'relationship',
label: 'Country',
},
{
_id: '5e3d19ccdeeb2652690a1258',
label: 'Costume',
Expand Down Expand Up @@ -120,6 +129,16 @@ export const searchResults: SearchResults = {
template: '58ad7d240d44252fee4e61fd',
metadata: {
nemesis: [{ icon: null, label: 'Thanos', type: 'entity', value: 'tf4laogfdcf8ncdi' }],
country: [
{
icon: null,
label: 'Spain',
type: 'entity',
value: 'tf4laogfdcf8ncdi111111,',
inheritedValue: [{ value: 'ES' }],
inheritedType: 'text',
},
],
company: [{ value: 'Marvel' }],
allies: [],
costume: [],
Expand Down Expand Up @@ -191,6 +210,6 @@ export const searchResults: SearchResults = {
},
};

export const csvExample = `Title,Date added,Template,company,Nemesis,Costume,Super powers,Allies,AutoId,Sidekick,Planets conquered,DOB,Geolocation,Documents,Attachments,Published
Star Lord Wikipedia,2017-01-05,Comic character,Marvel,Thanos,,"tricky weapons|fly",,FTF8988-8015,,,,"45.974236866039696|2.154785156250431",/files/1483623310306rxeimbblc6u323xr.pdf,,Published
Scarecrow,2017-01-05,Super Villian,,,Black,"create chaos|tricky weapons",,,Man-bat,39,1941-09-23,,,/api/attachments/download?_id=58ad7d250d44252fee4e62f0&file=filename.pdf,Published`;
export const csvExample = `Title,Date added,Template,company,Nemesis,Country,Costume,Super powers,Allies,AutoId,Sidekick,Planets conquered,DOB,Geolocation,Documents,Attachments,Published
Star Lord Wikipedia,2017-01-05,Comic character,Marvel,Thanos,ES,,"tricky weapons|fly",,FTF8988-8015,,,,"45.974236866039696|2.154785156250431",/files/1483623310306rxeimbblc6u323xr.pdf,,Published
Scarecrow,2017-01-05,Super Villian,,,,Black,"create chaos|tricky weapons",,,Man-bat,39,1941-09-23,,,/api/attachments/download?_id=58ad7d250d44252fee4e62f0&file=filename.pdf,Published`;
60 changes: 47 additions & 13 deletions app/api/csv/specs/typeFormatters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,53 @@ describe('csvExporter typeFormatters', () => {
});
});

it('should return the correct RELATIONSHIP value', () => {
const singleField = [{ label: 'Entity 1', value: null }];
const multipleField = [
{ label: 'Entity 1', value: null },
{ label: 'Entity 2', value: null },
];

const singleValue = typeFormatters.relationship(singleField, {});
const multipleValue = typeFormatters.relationship(multipleField, {});

expect(singleValue).toBe('Entity 1');
expect(multipleValue).toBe('Entity 1|Entity 2');
testEmptyField(typeFormatters.relationship);
describe('RELATIONSHIP', () => {
it('should return the entity label when has no inherited value', () => {
const singleField = [{ label: 'Entity 1', value: null }];
const multipleField = [
{ label: 'Entity 1', value: null },
{ label: 'Entity 2', value: null },
];

const singleValue = typeFormatters.relationship(singleField, {});
const multipleValue = typeFormatters.relationship(multipleField, {});

expect(singleValue).toBe('Entity 1');
expect(multipleValue).toBe('Entity 1|Entity 2');
testEmptyField(typeFormatters.relationship);
});

it('should return the inherited entity value when has inherited value', () => {
const singleField = [
{
label: 'Entity 1',
value: null,
inheritedValue: [{ value: 'E1' }],
inheritedType: 'text',
},
];
const multipleField = [
{
label: 'Entity 1',
value: null,
inheritedValue: [{ value: 'E1' }],
inheritedType: 'text',
},
{
label: 'Entity 2',
value: null,
inheritedValue: [{ value: 'E2' }],
inheritedType: 'text',
},
];

const singleValue = typeFormatters.relationship(singleField, {});
const multipleValue = typeFormatters.relationship(multipleField, {});

expect(singleValue).toBe('E1');
expect(multipleValue).toBe('E1|E2');
testEmptyField(typeFormatters.relationship);
});
});

describe('FILES', () => {
Expand Down
12 changes: 11 additions & 1 deletion app/api/csv/typeFormatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export const formatFile = (fileName: string) => `/files/${fileName}`;
export const formatAttachment = (fileName: string, entityId: string) =>
`/api/attachments/download?_id=${entityId}&file=${fileName}`;

const formatRelationship = (field: MetadataObjectSchema[]) =>
field
.map(relationship => {
if (relationship.inheritedValue) {
return relationship.inheritedValue[0].value;
}
return relationship.label;
})
.join('|');

export type FormatterOptions = {
dateFormat?: string;
};
Expand Down Expand Up @@ -45,7 +55,7 @@ export const formatters: {
field.map(item => formatters.daterange([item], options)).join('|'),
numeric: field =>
field[0] && (field[0].value || field[0].value === 0) ? <string>field[0].value : '',
relationship: field => field.map(relationship => relationship.label).join('|'),
relationship: field => formatRelationship(field),
default: field => (field[0] && field[0].value ? <string>field[0].value : ''),
};

Expand Down
11 changes: 10 additions & 1 deletion app/shared/types/commonSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ export const propertyValueSchema = {
],
};

export const inheritedValueSchema = {
type: 'object',
required: ['value'],
additionalProperties: false,
properties: {
value: { type: 'string' },
},
};

export const metadataObjectSchema = {
type: 'object',
definitions: { propertyValueSchema },
Expand All @@ -102,7 +111,7 @@ export const metadataObjectSchema = {
suggestion_confidence: { type: 'number' },
suggestion_model: { type: 'string' },
provenance: { type: 'string', enum: Object.values(provenanceTypes) },
inheritedValue: { value: propertyValueSchema },
inheritedValue: { type: 'array', items: inheritedValueSchema },
inheritedType: { type: 'string' },
},
};
Expand Down
8 changes: 6 additions & 2 deletions app/shared/types/commonTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ export type PropertyValueSchema =
| LatLonSchema
| LatLonSchema[];

export interface InheritedValueSchema {
value: string;
}

export interface MetadataObjectSchema {
value: PropertyValueSchema;
label?: string;
suggestion_confidence?: number;
suggestion_model?: string;
provenance?: '' | 'BULK_ACCEPT';
inheritedValue?: {
[k: string]: unknown | undefined;
};
value: string;
}[];
inheritedType?: string;
[k: string]: unknown | undefined;
}
Expand Down
8 changes: 4 additions & 4 deletions app/shared/types/connectionType.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export interface ConnectionSchema {
suggestion_model?: string;
provenance?: '' | 'BULK_ACCEPT';
inheritedValue?: {
[k: string]: unknown | undefined;
};
value: string;
}[];
inheritedType?: string;
[k: string]: unknown | undefined;
}[]
Expand Down Expand Up @@ -97,8 +97,8 @@ export interface ConnectionSchema {
suggestion_model?: string;
provenance?: '' | 'BULK_ACCEPT';
inheritedValue?: {
[k: string]: unknown | undefined;
};
value: string;
}[];
inheritedType?: string;
[k: string]: unknown | undefined;
}[]
Expand Down

0 comments on commit 3d0238d

Please sign in to comment.