Skip to content

Commit b4754cf

Browse files
Exclude form drafts from linkedForms (#1131)
Closes getodk/central#645.
1 parent 22e799f commit b4754cf

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

lib/model/query/datasets.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,9 +340,10 @@ const getPublishedBySimilarName = (projectId, name) => ({ maybeOne }) => {
340340
const getMetadata = (dataset) => async ({ all, Datasets }) => {
341341

342342
const _getLinkedForms = (datasetName, projectId) => sql`
343-
SELECT DISTINCT f."xmlFormId", coalesce(fd.name, f."xmlFormId") "name" FROM form_attachments fa
344-
JOIN forms f ON f.id = fa."formId" AND f."deletedAt" IS NULL
345-
JOIN form_defs fd ON f."currentDefId" = fd.id
343+
SELECT DISTINCT f."xmlFormId", coalesce(current_def.name, f."xmlFormId") "name" FROM form_attachments fa
344+
JOIN form_defs fd ON fd.id = fa."formDefId" AND fd."publishedAt" IS NOT NULL
345+
JOIN forms f ON f.id = fd."formId" AND f."deletedAt" IS NULL
346+
JOIN form_defs current_def ON f."currentDefId" = current_def.id
346347
JOIN datasets ds ON ds.id = fa."datasetId"
347348
WHERE ds.name = ${datasetName}
348349
AND ds."projectId" = ${projectId}

test/integration/api/datasets.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,49 @@ describe('datasets and entities', () => {
13941394
]);
13951395
}));
13961396

1397+
it('should not return a form draft as a linked form', testService(async (service) => {
1398+
const asAlice = await service.login('alice');
1399+
await asAlice.post('/v1/projects/1/forms')
1400+
.send(testData.forms.withAttachments
1401+
.replace('goodone.csv', 'people.csv'))
1402+
.set('Content-Type', 'application/xml')
1403+
.expect(200);
1404+
await asAlice.post('/v1/projects/1/forms/withAttachments/draft/attachments/people.csv')
1405+
.send('test,csv\n1,2')
1406+
.set('Content-Type', 'text/csv')
1407+
.expect(200);
1408+
await asAlice.post('/v1/projects/1/forms/withAttachments/draft/publish')
1409+
.expect(200);
1410+
await asAlice.post('/v1/projects/1/forms?publish=true')
1411+
.send(testData.forms.simpleEntity)
1412+
.set('Content-Type', 'application/xml')
1413+
.expect(200);
1414+
await asAlice.post('/v1/projects/1/forms/withAttachments/draft')
1415+
.expect(200);
1416+
await asAlice.patch('/v1/projects/1/forms/withAttachments/draft/attachments/people.csv')
1417+
.send({ dataset: true })
1418+
.expect(200);
1419+
const { body: publishedAttachments } = await asAlice.get('/v1/projects/1/forms/withAttachments/attachments')
1420+
.expect(200);
1421+
const publishedCSV = publishedAttachments.find(attachment =>
1422+
attachment.name === 'people.csv');
1423+
publishedCSV.should.containEql({
1424+
blobExists: true,
1425+
datasetExists: false
1426+
});
1427+
const { body: draftAttachments } = await asAlice.get('/v1/projects/1/forms/withAttachments/draft/attachments')
1428+
.expect(200);
1429+
const draftCSV = draftAttachments.find(attachment =>
1430+
attachment.name === 'people.csv');
1431+
draftCSV.should.containEql({
1432+
blobExists: false,
1433+
datasetExists: true
1434+
});
1435+
const { body: dataset } = await asAlice.get('/v1/projects/1/datasets/people')
1436+
.expect(200);
1437+
dataset.linkedForms.length.should.equal(0);
1438+
}));
1439+
13971440
it('should return properties of a dataset in order', testService(async (service) => {
13981441
const asAlice = await service.login('alice');
13991442

0 commit comments

Comments
 (0)