Skip to content

Commit

Permalink
Merge branch 'test/forms-1114-cypress-workflow' of https://github.com…
Browse files Browse the repository at this point in the history
…/nimya-aot/common-hosted-form-service into test/forms-1114-cypress-workflow
  • Loading branch information
nimya-aot committed Jun 3, 2024
2 parents a27f274 + bfd4934 commit f58c842
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 23 deletions.
10 changes: 5 additions & 5 deletions app/src/docs/v1.api-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -615,28 +615,28 @@ paths:
name: preference
schema:
type: object
example: '{ minDate=2020-12-17T08:00:00Z, maxDate=2020-12-17T08:00:00Z, updatedMinDate=2020-12-17T08:00:00Z, updatedMaxDate=2020-12-17T08:00:00Z }'
example: '{minDate=2020-12-17T08:00:00Z,maxDate=2020-12-18T08:00:00Z,updatedMinDate=2020-12-17T08:00:00Z,updatedMaxDate=2020-12-18T08:00:00Z}'
description: form submissions export preferences
- in: query
name: deleted
schema:
type: boolean
description: (optional) This optional parameter should be set to true if deleted records (submissions) need to be fetched
description: Set to true to return deleted submissions
example: false
- in: query
name: drafts
schema:
type: boolean
description: (optional) This optional parameter should be set to true if draft records (submissions) need to be fetched
description: Set to true to return draft submissions
example: false
- in: query
name: columns
schema:
type: array
description: (optional) List of form level columns (Only Allowed draft, deleted, updatedAt columns) to be include. Other then allowed columns will be ignored
description: List of form level columns (Only Allowed `deleted`, `draft`, `updatedAt`) to be included, others will be ignored
example:
- draft,
- deleted,
- draft,
- updatedAt
- in: query
name: status
Expand Down
33 changes: 27 additions & 6 deletions app/src/forms/form/exportService.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,35 @@ const service = {
});
},

_getSubmissions: async (form, params, version) => {
//let preference = params.preference ? JSON.parse(params.preference) : undefined;
let preference;
if (params.preference && _.isString(params.preference)) {
preference = JSON.parse(params.preference);
/**
* Parse the preferences that come in on the request.
*
* @param {any} preferences the preferences - probably a string or undefined
* but unsure exactly what this could be.
* @returns {any} the preferences. If a string was given as an argument then
* it's treated as JSON and an object is returned.
*/
_parsePreferences: (preferences) => {
let parsedPreferences;
if (preferences && _.isString(preferences)) {
try {
parsedPreferences = JSON.parse(preferences);
} catch (error) {
throw new Problem(400, {
detail: 'Bad preferences: ' + error.message,
});
}
} else {
preference = params.preference;
// What is this? How is it not a string? Is this just handling undefined?
parsedPreferences = preferences;
}

return parsedPreferences;
},

_getSubmissions: async (form, params, version) => {
const preference = service._parsePreferences(params.preference);

// let submissionData;
// params for this export include minDate and maxDate (full timestamp dates).
return SubmissionData.query()
Expand Down
24 changes: 13 additions & 11 deletions app/src/forms/form/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ const service = {

const selection = ['confirmationId', 'createdAt', 'formId', 'formSubmissionStatusCode', 'submissionId', 'deleted', 'createdBy', 'formVersionId'];

let fields = [];
if (params.fields && params.fields.length) {
let fields = [];
if (typeof params.fields !== 'string' && params.fields.includes('updatedAt')) {
selection.push('updatedAt');
}
Expand All @@ -383,19 +383,21 @@ const service = {
// columns. Also remove empty values to handle the case of trailing commas
// and other malformed data too.
fields = fields.filter((f) => f !== 'updatedAt' && f !== 'updatedBy' && f.trim() !== '');
}

fields.push('lateEntry');
query.select(
selection,
fields.map((f) => ref(`submission:data.${f}`).as(f.split('.').slice(-1)))
);
} else {
query.select(
selection,
['lateEntry'].map((f) => ref(`submission:data.${f}`).as(f.split('.').slice(-1)))
);
fields.push('lateEntry');

if (params.sortBy?.column && !selection.includes(params.sortBy.column) && !fields.includes(params.sortBy.column)) {
throw new Problem(400, {
details: `orderBy column '${params.sortBy.column}' not in selected columns`,
});
}

query.select(
selection,
fields.map((f) => ref(`submission:data.${f}`).as(f.split('.').slice(-1)))
);

if (params.paginationEnabled) {
return await service.processPaginationData(query, parseInt(params.page), parseInt(params.itemsPerPage), params.totalSubmissions, params.search, params.searchEnabled);
}
Expand Down
9 changes: 9 additions & 0 deletions app/tests/unit/forms/form/exportService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ describe('export', () => {
};
exportService._getForm = jest.fn().mockReturnValue(form);

describe('400 response when', () => {
test('invalid preferences json', async () => {
const params = { preference: '{' };
exportService._readLatestFormSchema = jest.fn().mockReturnValueOnce();

await expect(exportService.export(formId, params, currentUser)).rejects.toThrow('400');
});
});

describe('type 1', () => {
const params = {
emailExport: false,
Expand Down
12 changes: 12 additions & 0 deletions app/tests/unit/forms/form/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ describe('_findFileIds', () => {
});

describe('listFormSubmissions', () => {
describe('400 response when', () => {
test('sort by column not in select', async () => {
await expect(
service.listFormSubmissions(formId, {
sortBy: {
column: 'x',
},
})
).rejects.toThrow('400');
});
});

it('should not error if fields has a trailing commma', async () => {
await service.listFormSubmissions(formId, { fields: 'x,' });

Expand Down
2 changes: 1 addition & 1 deletion openshift/app.cronjob.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ objects:
spec:
containers:
- name: my-container
image: docker.io/curlimages/curl:8.1.0
image: docker.io/curlimages/curl:8.8.0
env:
- name: APITOKEN
valueFrom:
Expand Down

0 comments on commit f58c842

Please sign in to comment.