Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove submissionCreate from entity audit events #1072

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13436,7 +13436,7 @@ components:
source:
type: object
description: The source of the Entity version, such as a Submission or an API request. This property is experimental and may change in the future.
example: {event: {}, submissionCreate: {}, submission: {}}
example: {event: {}, submission: {}}
baseDiff:
type: array
description: List of properties that are different between this version and its base version. Includes the label if it differs between the two versions.
Expand Down
4 changes: 2 additions & 2 deletions lib/data/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ const getWithConflictDetails = (defs, audits, relevantToConflict) => {
const relevantBaseVersions = new Set();

for (const def of defs) {
const { sourceEvent, submissionCreate, submission } = auditMap.get(def.id);
const { source = {} } = auditMap.get(def.id);

const v = mergeLeft(def.forApi(),
{
conflict: null,
resolved: false,
baseDiff: [],
serverDiff: [],
source: { event: sourceEvent, submissionCreate, submission },
source,
lastGoodVersion: false
});

Expand Down
38 changes: 23 additions & 15 deletions lib/model/query/audits.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,29 @@ const getByEntityId = (entityId, options) => ({ all }) => {
.map(a => a.withAux('actor', audit.aux.triggeringEventActor.orNull()))
.map(a => a.forApi());

const submissionCreate = audit.aux.submissionCreateEvent
.map(a => a.withAux('actor', audit.aux.submissionCreateEventActor.orNull()))
.map(a => a.forApi());

const submission = audit.aux.submission
.map(s => s.withAux('submitter', audit.aux.submissionActor.orNull()))
.map(s => s.withAux('currentVersion', audit.aux.currentVersion.map(v => v.withAux('submitter', audit.aux.currentSubmissionActor.orNull()))))
.map(s => s.forApi())
.map(s => mergeLeft(s, { xmlFormId: audit.aux.form.map(f => f.xmlFormId).orNull() }));

const details = mergeLeft(audit.details, {
sourceEvent: sourceEvent.orElse(undefined),
submissionCreate: submissionCreate.orElse(undefined),
submission: submission.orElse(undefined)
});
// If the entity event is based on a submission, get submission details from the submission create event,
// which should always exist, even if the entity has been deleted.
const submission = audit.aux.submissionCreateEvent.map((createEvent) => {
const baseSubmission = {
instanceId: createEvent.details.instanceId,
createdAt: createEvent.loggedAt,
submitter: audit.aux.submissionCreateEventActor.get().forApi()
};

// If the submission has not been deleted, return full submission details, not just what
// was in the submission create event details.
const fullSubmission = audit.aux.submission
.map(s => s.withAux('submitter', audit.aux.submissionActor.orNull()))
.map(s => s.withAux('currentVersion', audit.aux.currentVersion.map(v => v.withAux('submitter', audit.aux.currentSubmissionActor.orNull()))))
.map(s => s.forApi())
.map(s => mergeLeft(s, { xmlFormId: audit.aux.form.map(f => f.xmlFormId).orNull() }));
return mergeLeft(baseSubmission, fullSubmission.orElse(undefined));
})
.orElse(undefined);

const details = mergeLeft(audit.details, sourceEvent
.map(event => ({ source: { event, submission } }))
.orElse(undefined));

return new Audit({ ...audit, details }, { actor: audit.aux.actor });
}));
Expand Down
20 changes: 10 additions & 10 deletions test/integration/api/datasets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3825,10 +3825,10 @@ describe('datasets and entities', () => {
logs[0].action.should.be.eql('entity.create');
logs[0].actor.displayName.should.be.eql('Alice');

logs[0].details.submission.should.be.a.Submission();
logs[0].details.submission.xmlFormId.should.be.eql('simpleEntity');
logs[0].details.submission.currentVersion.instanceName.should.be.eql('one');
logs[0].details.submission.currentVersion.submitter.displayName.should.be.eql('Alice');
logs[0].details.source.submission.should.be.a.Submission();
logs[0].details.source.submission.xmlFormId.should.be.eql('simpleEntity');
logs[0].details.source.submission.currentVersion.instanceName.should.be.eql('one');
logs[0].details.source.submission.currentVersion.submitter.displayName.should.be.eql('Alice');
});


Expand Down Expand Up @@ -4159,8 +4159,8 @@ describe('datasets and entities', () => {
.then(({ body: logs }) => {
const updateDetails = logs.filter(log => log.action === 'entity.update.version').map(log => log.details);
updateDetails.length.should.equal(4);
updateDetails.filter(d => d.sourceEvent.action === 'submission.create').length.should.equal(4);
updateDetails.map(d => d.submission.instanceId).should.eql([
updateDetails.filter(d => d.source.event.action === 'submission.create').length.should.equal(4);
updateDetails.map(d => d.source.submission.instanceId).should.eql([
'six', 'five', 'four', 'three'
]);
});
Expand All @@ -4170,9 +4170,9 @@ describe('datasets and entities', () => {
.expect(200)
.then(({ body: logs }) => {
logs[0].action.should.equal('entity.create');
logs[0].details.submission.xmlFormId.should.equal('simpleEntity');
logs[0].details.submission.instanceId.should.equal('two');
logs[0].details.sourceEvent.action.should.equal('submission.create');
logs[0].details.source.submission.xmlFormId.should.equal('simpleEntity');
logs[0].details.source.submission.instanceId.should.equal('two');
logs[0].details.source.event.action.should.equal('submission.create');
});

// only one entity def should have a source with a non-null parent id
Expand Down Expand Up @@ -4486,7 +4486,7 @@ describe('datasets and entities', () => {
// Observe that the entity's audit says it was updated by the new edited submission
await asAlice.get('/v1/projects/1/datasets/people/entities/12345678-1234-4123-8234-123456789abc/audits')
.then(({ body: logs }) => {
logs[0].details.submission.instanceId.should.equal('one2');
logs[0].details.source.submission.instanceId.should.equal('one2');
});

// Observe that the submission's audit log makes sense
Expand Down
Loading
Loading