Skip to content

Commit

Permalink
Exclude form drafts from linkedForms (#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-white committed Apr 29, 2024
1 parent 22e799f commit b4754cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/model/query/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,10 @@ const getPublishedBySimilarName = (projectId, name) => ({ maybeOne }) => {
const getMetadata = (dataset) => async ({ all, Datasets }) => {

const _getLinkedForms = (datasetName, projectId) => sql`
SELECT DISTINCT f."xmlFormId", coalesce(fd.name, f."xmlFormId") "name" FROM form_attachments fa
JOIN forms f ON f.id = fa."formId" AND f."deletedAt" IS NULL
JOIN form_defs fd ON f."currentDefId" = fd.id
SELECT DISTINCT f."xmlFormId", coalesce(current_def.name, f."xmlFormId") "name" FROM form_attachments fa
JOIN form_defs fd ON fd.id = fa."formDefId" AND fd."publishedAt" IS NOT NULL
JOIN forms f ON f.id = fd."formId" AND f."deletedAt" IS NULL
JOIN form_defs current_def ON f."currentDefId" = current_def.id
JOIN datasets ds ON ds.id = fa."datasetId"
WHERE ds.name = ${datasetName}
AND ds."projectId" = ${projectId}
Expand Down
43 changes: 43 additions & 0 deletions test/integration/api/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,49 @@ describe('datasets and entities', () => {
]);
}));

it('should not return a form draft as a linked form', testService(async (service) => {
const asAlice = await service.login('alice');
await asAlice.post('/v1/projects/1/forms')
.send(testData.forms.withAttachments
.replace('goodone.csv', 'people.csv'))
.set('Content-Type', 'application/xml')
.expect(200);
await asAlice.post('/v1/projects/1/forms/withAttachments/draft/attachments/people.csv')
.send('test,csv\n1,2')
.set('Content-Type', 'text/csv')
.expect(200);
await asAlice.post('/v1/projects/1/forms/withAttachments/draft/publish')
.expect(200);
await asAlice.post('/v1/projects/1/forms?publish=true')
.send(testData.forms.simpleEntity)
.set('Content-Type', 'application/xml')
.expect(200);
await asAlice.post('/v1/projects/1/forms/withAttachments/draft')
.expect(200);
await asAlice.patch('/v1/projects/1/forms/withAttachments/draft/attachments/people.csv')
.send({ dataset: true })
.expect(200);
const { body: publishedAttachments } = await asAlice.get('/v1/projects/1/forms/withAttachments/attachments')
.expect(200);
const publishedCSV = publishedAttachments.find(attachment =>
attachment.name === 'people.csv');
publishedCSV.should.containEql({
blobExists: true,
datasetExists: false
});
const { body: draftAttachments } = await asAlice.get('/v1/projects/1/forms/withAttachments/draft/attachments')
.expect(200);
const draftCSV = draftAttachments.find(attachment =>
attachment.name === 'people.csv');
draftCSV.should.containEql({
blobExists: false,
datasetExists: true
});
const { body: dataset } = await asAlice.get('/v1/projects/1/datasets/people')
.expect(200);
dataset.linkedForms.length.should.equal(0);
}));

it('should return properties of a dataset in order', testService(async (service) => {
const asAlice = await service.login('alice');

Expand Down

0 comments on commit b4754cf

Please sign in to comment.