Skip to content

Commit

Permalink
fixes #673: dataset named itemsets and external choices form is uploa…
Browse files Browse the repository at this point in the history
…ded (#958)

* fixes #673: dataset named itemsets and external choices form is uploaded

* itemsets
sadiqkhoja authored Aug 25, 2023
1 parent bc5c432 commit 628ac0d
Showing 2 changed files with 62 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/model/query/form-attachments.js
Original file line number Diff line number Diff line change
@@ -62,7 +62,14 @@ const createNew = (xml, form, itemsets) => ({ Blobs, Datasets, run }) =>
.then(datasets => {
const dsHashtable = datasets.reduce((acc, ds) => Object.assign(acc, { [`${ds.name}.csv`]: ds }), {});
const ids = { formId: form.id, formDefId: form.def.id };
return run(insertMany(expected.map((att) => new Form.Attachment(Object.assign({}, att, ids, { datasetId: att.type === 'file' ? dsHashtable[att.name]?.id : null })))));
const attachments = expected.map((att) => {
// we don't want to set datasetId for "fast external itemsets" cb#673
const datasetId = !(itemsets && att.name === 'itemsets.csv') && att.type === 'file' ? dsHashtable[att.name]?.id : null;
return new Form.Attachment({ ...att,
...ids,
datasetId });
});
return run(insertMany(attachments));
}));

const createVersion = (xml, form, savedDef, itemsets, publish = false) => ({ Blobs, FormAttachments, Datasets, run }) =>
@@ -89,7 +96,7 @@ const createVersion = (xml, form, savedDef, itemsets, publish = false) => ({ Blo

const attachments = expecteds.map((expected) => {
const extant = Option.of(lookup[expected.name]).filter((e) => e.type === expected.type);
const matchingDsId = expected.type === 'file' ? Option.of(dsHashtable[expected.name]).map(d => d.id) : Option.none();
const matchingDsId = !(itemsets && expected.name === 'itemsets.csv') && expected.type === 'file' ? Option.of(dsHashtable[expected.name]).map(d => d.id) : Option.none();
return new Form.Attachment(mergeRight({
formId: form.id,
formDefId: savedDef.id,
53 changes: 53 additions & 0 deletions test/integration/api/datasets.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { readFileSync } = require('fs');
const appRoot = require('app-root-path');
const { testService } = require('../setup');
const testData = require('../../data/xml');
@@ -1297,6 +1298,58 @@ describe('datasets and entities', () => {
.catch(error => {
error.constraint.should.be.equal('check_datasetId_is_null_for_non_file');
}))))));

// cb#673
it('should not throw problem for fast external itemsets when there is existing "itemsets" dataset', testService(async (service) => {
const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.simpleEntity.replace(/people/, 'itemsets'))
.expect(200);

global.xlsformForm = 'itemsets';

await asAlice.post('/v1/projects/1/forms')
.send(readFileSync(appRoot + '/test/data/simple.xlsx'))
.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
.set('X-XlsForm-FormId-Fallback', 'itemsets')
.expect(200)
.then(() => asAlice.get('/v1/projects/1/forms/itemsets/draft/attachments/itemsets.csv')
.expect(200)
.then(({ text }) => {
text.should.equal('a,b,c\n1,2,3\n4,5,6');
}));
}));

// cb#673
it('should not throw problem for new version of "fast external itemsets" when there is existing "itemsets" dataset', testService(async (service) => {
const asAlice = await service.login('alice');

await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.itemsets)
.set('Content-Type', 'application/xml')
.expect(200);

await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.simpleEntity.replace(/people/, 'itemsets'))
.expect(200);

// add external choice (fast external itemsets) to an existing form
global.xlsformForm = 'itemsets';

await asAlice.post('/v1/projects/1/forms/itemsets/draft')
.send(readFileSync(appRoot + '/test/data/simple.xlsx'))
.set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
.set('X-XlsForm-FormId-Fallback', 'itemsets')
.expect(200)
.then(() => asAlice.get('/v1/projects/1/forms/itemsets/draft/attachments/itemsets.csv')
.expect(200)
.then(({ text }) => {
text.should.equal('a,b,c\n1,2,3\n4,5,6');
}));

}));

});

describe('projects/:id/forms/:formId/attachments/:name (entities dataset)', () => {

0 comments on commit 628ac0d

Please sign in to comment.