Skip to content

Commit

Permalink
endpoint for getting forms by enketoId
Browse files Browse the repository at this point in the history
  • Loading branch information
brontolosone committed Jan 22, 2025
1 parent 7574030 commit 3d4e8d2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/model/query/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ const getByProjectId = (auth, projectId, xml, version, options = QueryOptions.no
_get(all, options.withCondition({ projectId }), xml, version, deleted, auth.actor.map((actor) => actor.id).orElse(-1));
const getByProjectAndXmlFormId = (projectId, xmlFormId, xml, version, options = QueryOptions.none, deleted = false) => ({ maybeOne }) =>
_get(maybeOne, options.withCondition({ projectId, xmlFormId }), xml, version, deleted);
const getByEnketoId = (enketoId, xml, version, options = QueryOptions.none, deleted = false) => ({ maybeOne }) => {
const enketoIdFilter = [{ 'form_defs.enketoId': enketoId }, { 'forms.enketoId': enketoId }]; // hack: the array makes lib/util/db.sqlEquals turn this into a disjunction (OR)
return _get(maybeOne, options.withCondition(enketoIdFilter), xml, version, deleted);
};
const getByProjectAndNumericId = (projectId, id, xml, version, options = QueryOptions.none, deleted = false) => ({ maybeOne }) =>
_get(maybeOne, options.withCondition({ projectId, 'forms.id': id }), xml, version, deleted);
const getAllByAuth = (auth, options = QueryOptions.none) => ({ all }) =>
Expand Down Expand Up @@ -843,7 +847,7 @@ module.exports = {
setManagedKey,
getByAuthForOpenRosa,
getVersions, getByActeeIdForUpdate, getByActeeId,
getByProjectId, getByProjectAndXmlFormId, getByProjectAndNumericId,
getByProjectId, getByProjectAndXmlFormId, getByEnketoId, getByProjectAndNumericId,
getAllByAuth,
getFields, getBinaryFields, getStructuralFields, getMergedFields,
rejectIfWarnings, checkMeta, checkDeletedForms, checkStructuralChange, checkFieldDowncast, checkDatasetWarnings,
Expand Down
4 changes: 3 additions & 1 deletion lib/resources/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ module.exports = (service, endpoint) => {
formResource('/projects/:projectId/forms/:id', (Forms, params, withXml = false, options = QueryOptions.none) =>
Forms.getByProjectAndXmlFormId(params.projectId, params.id, withXml, null, options)
.then(getOrNotFound));

formResource('/forms/:enketoId', (Forms, params, withXml = true, options = QueryOptions.none) =>
Forms.getByEnketoId(params.enketoId, withXml, null, options)
.then(getOrNotFound));
formResource('/projects/:projectId/forms/:id/versions/:version', (Forms, params, withXml = false, options = QueryOptions.none) =>
Forms.getByProjectAndXmlFormId(params.projectId, params.id, withXml, params.version, options)
.then(getOrNotFound)
Expand Down
13 changes: 12 additions & 1 deletion lib/util/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ const markUndeleted = (obj) =>
// query fragment util

const sqlEquals = (obj) => {

let connective = sql` AND `;

if (Object.entries(obj).every(([k, v]) => !Number.isNaN(parseInt(k, 10) && typeof v === 'object'))) {
// hack: passing an array to options.withCondition (eg `[{ id: 10 }, { name: 'foo' }]`)
// will create a disjunction (OR) rather than a conjunction (AND) of these conditions.
connective = sql` OR `;
// eslint-disable-next-line no-param-reassign
obj = Object.assign({}, ...Object.values(obj));
}

const keys = Object.keys(obj);
if (keys.length === 0) return sql`true`;

Expand All @@ -224,7 +235,7 @@ const sqlEquals = (obj) => {
parts[i] = (v === null) ? sql`${sql.identifier(k.split('.'))} is null`
: sql`${sql.identifier(k.split('.'))}=${obj[k]}`;
}
return sql.join(parts, sql` and `);
return sql.join(parts, connective);
};

const page = (options) => {
Expand Down

0 comments on commit 3d4e8d2

Please sign in to comment.