Skip to content

Commit

Permalink
moving conditional, adding validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Oct 3, 2023
1 parent 20573bd commit d0f0680
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions lib/model/query/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ createVersion.audit.withResult = true;
// Processing submission events to create and update entities

const _createEntity = (dataset, entityData, submissionId, submissionDef, submissionDefId, event, parentEvent) => async ({ Audits, Entities }) => {
// If dataset requires approval on submission to create an entity and this event is not
// an approval event, then don't create an entity
if ((dataset.approvalRequired && event.details.reviewState !== 'approved') ||
(!dataset.approvalRequired && event.action === 'submission.update')) // don't process submission if approval is not required and submission metadata is updated
return null;

const partial = await Entity.fromParseEntityData(entityData);

const sourceDetails = { submission: { instanceId: submissionDef.instanceId }, parentEventId: parentEvent ? parentEvent.id : undefined };
Expand All @@ -150,13 +156,15 @@ const _createEntity = (dataset, entityData, submissionId, submissionDef, submiss
};

const _updateEntity = (dataset, entityData, submissionId, submissionDef, submissionDefId, event) => async ({ Audits, Entities }) => {
// Get client version of entity
const clientEntity = await Entity.fromParseEntityData(entityData); // validation happens here

// Get version of entity on the server
const serverEntity = await Entities.getById(dataset.id, entityData.system.id).then(o => o.get());
const serverEntity = await Entities.getById(dataset.id, clientEntity.uuid).then(o => o.get());

// merge data
const mergedData = mergeRight(serverEntity.aux.currentVersion.data, entityData.data);
const mergedLabel = entityData.system.label || serverEntity.aux.currentVersion.label;
const mergedData = mergeRight(serverEntity.aux.currentVersion.data, clientEntity.def.data);
const mergedLabel = clientEntity.def.label || serverEntity.aux.currentVersion.label;

// make some kind of source object
const sourceDetails = {
Expand All @@ -173,8 +181,8 @@ const _updateEntity = (dataset, entityData, submissionId, submissionDef, submiss
const entity = await Entities.createVersion(dataset, partial, submissionDef, serverEntity.aux.currentVersion.version + 1, sourceId);
return Audits.log({ id: event.actorId }, 'entity.update.version', { acteeId: dataset.acteeId },
{
entityId: entity.id, // Added in v2023.3 and backfilled
entityDefId: entity.aux.currentVersion.id, // Added in v2023.3 and backfilled
entityId: entity.id,
entityDefId: entity.aux.currentVersion.id,
entity: { uuid: entity.uuid, dataset: dataset.name },
submissionId,
submissionDefId
Expand Down Expand Up @@ -218,27 +226,18 @@ const _processSubmissionEvent = (event, parentEvent) => async ({ Datasets, Entit

const entityData = await parseSubmissionXml(fields, submissionDef.xml);

// If dataset requires approval on submission to create an entity and this event is not
// an approval event, then don't create an entity
//
// We can't simply use submissionDefId to trace back to dataset and find out if approval
// is required because in future there can be multiple entities in a single submission.
// So we have to rely on dataset name from the xml to fetch the dataset.approvalRequired flag.
// We have to look up the dataset based on the name in the XML
if (!entityData.system.dataset || entityData.system.dataset.trim() === '') {
throw Problem.user.missingParameter({ field: 'dataset' });
}

const dataset = (await Datasets.get(form.get().projectId, entityData.system.dataset, true))
.orThrow(Problem.user.datasetNotFound({ datasetName: entityData.system.dataset }));

if ((dataset.approvalRequired && event.details.reviewState !== 'approved') ||
(!dataset.approvalRequired && event.action === 'submission.update')) // don't process submission if approval is not required and submission metadata is updated
return null;

// If create is not true (either 1 or true) then we don't need to process further
// Create entity
if (entityData.system.create === '1' || entityData.system.create === 'true')
return Entities._createEntity(dataset, entityData, submissionId, submissionDef, submissionDefId, event, parentEvent);

// Or update entity
if (entityData.system.update === '1' || entityData.system.update === 'true')
return Entities._updateEntity(dataset, entityData, submissionId, submissionDef, submissionDefId, event);

Expand Down

0 comments on commit d0f0680

Please sign in to comment.