diff --git a/lib/model/query/datasets.js b/lib/model/query/datasets.js index dac2ed8e5..6a64c59e0 100644 --- a/lib/model/query/datasets.js +++ b/lib/model/query/datasets.js @@ -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} diff --git a/test/integration/api/datasets.js b/test/integration/api/datasets.js index 2bfedd86e..8d678359d 100644 --- a/test/integration/api/datasets.js +++ b/test/integration/api/datasets.js @@ -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');