Skip to content

Commit

Permalink
updated csv to use attachments when importing image or media fields
Browse files Browse the repository at this point in the history
  • Loading branch information
konzz committed Mar 21, 2021
1 parent 61268bd commit d5334f7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
58 changes: 56 additions & 2 deletions app/api/csv/importEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import entitiesModel from 'api/entities/entitiesModel';
import { processDocument } from 'api/files/processDocument';
import { RawEntity } from 'api/csv/entityRow';
import { TemplateSchema } from 'shared/types/templateType';
import { MetadataSchema, PropertySchema } from 'shared/types/commonTypes';
import { MetadataSchema, PropertySchema, MetadataObjectSchema } from 'shared/types/commonTypes';
import { ImportFile } from 'api/csv/importFile';
import { EntitySchema } from 'shared/types/entityType';
import { ensure } from 'shared/tsUtils';
Expand Down Expand Up @@ -51,6 +51,47 @@ type Options = {
language: string;
};

const extractAttachmentsFromProp = async (valueObject: MetadataObjectSchema, entity: string) => {
const attachment = await files.save({
entity,
type: 'attachment',
url: String(valueObject.value),
});
return attachment._id.toString();
};

const extractAttachmentsFromMediaProps = async (
template: TemplateSchema,
_metadata: MetadataSchema,
sharedId: string
) => {
const metadata = { ..._metadata };

const extractions = template.properties
? template.properties.map(async prop => {
if (['image', 'media'].includes(prop.type) && metadata[prop.name] && sharedId) {
const propertyMO = metadata[prop.name];

const extractOneProp = propertyMO
? propertyMO.map(async (valueObject: MetadataObjectSchema) => {
if (valueObject.value) {
const attachmentId = await extractAttachmentsFromProp(valueObject, sharedId);
valueObject.value = attachmentId;
}
})
: [];

return Promise.all(extractOneProp);
}

return Promise.resolve();
})
: [];

await Promise.all(extractions);
return metadata;
};

const importEntity = async (
toImportEntity: RawEntity,
template: TemplateSchema,
Expand All @@ -60,7 +101,9 @@ const importEntity = async (
const attachments = toImportEntity.attachments;
delete toImportEntity.attachments;
const eo = await entityObject(toImportEntity, template, { language });
const entity = await entities.save(eo, { user, language }, true, false);
const entity = await entities.save({ ...eo, metadata: {} }, { user, language }, true, false);

// const entity = await entities.save(eo, { user, language }, true, false);

if (toImportEntity.file && entity.sharedId) {
const file = await importFile.extractFile(toImportEntity.file);
Expand All @@ -75,6 +118,17 @@ const importEntity = async (
}, Promise.resolve());
}

if (entity.sharedId) {
const metadataWithAttachments = await extractAttachmentsFromMediaProps(
template,
eo.metadata,
entity.sharedId
);

entity.metadata = metadataWithAttachments;
}

await entities.save(entity, { user, language }, true, false);
await search.indexEntities({ sharedId: entity.sharedId }, '+fullText');
return entity;
};
Expand Down
18 changes: 18 additions & 0 deletions app/api/csv/specs/csvLoader.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import db from 'api/utils/testing_db';
import entities from 'api/entities';
import { files } from 'api/files';
import path from 'path';
import translations from 'api/i18n';
import { search } from 'api/search';
Expand Down Expand Up @@ -61,6 +62,7 @@ describe('csvLoader', () => {
expect(metadataImported).toEqual([
'text_label',
'numeric_label',
'image_label',
'select_label',
'not_defined_type',
'geolocation_geolocation',
Expand Down Expand Up @@ -92,6 +94,21 @@ describe('csvLoader', () => {
});
});
});

it('should generate attachments from image and media fields urls', async () => {
await db.clearAllAndLoad(fixtures);
await loader.load(csvFile, template1Id, { user: { username: 'user' }, language: 'en' });
const importedEntities = await entities.get();
const attachments = await files.get({ type: 'attachment' });

expect(attachments.length).toBe(2);
const firstAttachment = attachments.find(a => a.url === 'http://awesomecats.org/havery.png');

const firstEntityWithImage = importedEntities.find(e => e.title === 'title1');
expect(firstEntityWithImage.metadata.image_label[0].value).toBe(
firstAttachment._id.toString()
);
});
});

describe('on error', () => {
Expand All @@ -117,6 +134,7 @@ describe('csvLoader', () => {
jest
.spyOn(entities, 'save')
.mockImplementationOnce(({ title }) => Promise.resolve({ title }))
.mockImplementationOnce(({ title }) => Promise.resolve({ title }))
.mockImplementationOnce(({ title }) => Promise.reject(new Error(`error-${title}`)));

try {
Expand Down
5 changes: 5 additions & 0 deletions app/api/csv/specs/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export default {
label: 'numeric label',
name: templateUtils.safeName('numeric label'),
},
{
type: propertyTypes.image,
label: 'image label',
name: templateUtils.safeName('image label'),
},
{
type: propertyTypes.select,
label: 'select label',
Expand Down
8 changes: 4 additions & 4 deletions app/api/csv/specs/test.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Title , text label , numeric label, non configured, select label, not defined type, geolocation_geolocation
Title , text label , numeric label, non configured, select label, not defined type, geolocation_geolocation, image label

title1, text value 1, 1977, ______________, thesauri1 , notType1 , 1|1,
title2, text value 2, 2019, ______________, thesauri2 , notType2 , ,
title3, text value 3, 2020, ______________, thesauri2 , notType3 , 0|0,
title1, text value 1, 1977, ______________, thesauri1 , notType1 , 1|1, http://awesomecats.org/havery.png,
title2, text value 2, 2019, ______________, thesauri2 , notType2 , , http://awesomecats.org/lola.png,
title3, text value 3, 2020, ______________, thesauri2 , notType3 , 0|0, ,

0 comments on commit d5334f7

Please sign in to comment.