Skip to content

Commit

Permalink
updated names of functions and hash/md5 attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
ktuite committed Nov 12, 2024
1 parent e5818e7 commit a9e3e05
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lib/model/frames.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Form.Attachment = class extends Frame.define(
const data = {
name: this.name,
type: this.type,
md5: this.md5,
hash: this.blobHash,
exists: (this.blobId != null || this.datasetId != null),
blobExists: this.blobId != null,
datasetExists: this.datasetId != null
Expand Down
25 changes: 13 additions & 12 deletions lib/model/query/form-attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,33 +133,34 @@ update.audit = (form, fa, blobId, datasetId = null) => (log) => log('form.attach
// This unjoiner pulls md5 from blob table (if it exists) and adds it to attachment frame
const _unjoinMd5 = unjoiner(Form.Attachment, Frame.define(into('openRosa'), 'md5'));

// This function returns the blob md5 if it exists, otherwise it returns null.
// This is used for listing the form attachments within Central and unlike `_chooseHash`
// below, doesn't need to look at the dataset timestamp to compute a synthetic hash.
const _chooseSimpleHash = (attachment) => {
if (attachment.blobId) return attachment.with({ md5: attachment.aux.openRosa.md5 });
return attachment.with({ md5: null });
// This function returns the blob's md5 hash if it exists, otherwise it returns null.
// This is used for listing the form attachments within Central and unlike `_chooseDynamicHash`
// below, doesn't need to look at the dataset timestamp to compute a dynamic hash.
const _chooseBlobHash = (attachment) => {
if (attachment.blobId) return attachment.with({ blobHash: attachment.aux.openRosa.md5 });
return attachment.with({ blobHash: null });
};

const getAllByFormDefId = (formDefId) => ({ all }) =>
all(sql`select ${_unjoinMd5.fields} from form_attachments
left outer join (select id, md5 from blobs) as blobs on form_attachments."blobId"=blobs.id
where "formDefId"=${formDefId} order by name asc`)
.then(map(_unjoinMd5))
.then(map(_chooseSimpleHash));
.then(map(_chooseBlobHash));

const getByFormDefIdAndName = (formDefId, name) => ({ maybeOne }) => maybeOne(sql`
select ${_unjoinMd5.fields} from form_attachments
left outer join (select id, md5 from blobs) as blobs on form_attachments."blobId"=blobs.id
where "formDefId"=${formDefId} and "name"=${name}`)
.then(map(_unjoinMd5))
.then(map(_chooseSimpleHash));
.then(map(_chooseBlobHash));

// This function decides on the openrosa hash (functionally equivalent to an http Etag)
// This function decides on the OpenRosa hash (functionally equivalent to an http Etag)
// It uses the blob md5 directly if it exists.
// If the attachment is actually an entity list, it looks up the last modified time
// in the database, which is computed from the latest dataset/entity audit timestamp.
const _chooseHash = (attachment) => async ({ Datasets }) => {
// It is dynamic because it can change when a dataset's data is updated.
const _chooseDynamicHash = (attachment) => async ({ Datasets }) => {
const { md5 } = attachment.aux.openRosa;
if (attachment.blobId) return attachment.with({ openRosaHash: md5, md5 });

Expand All @@ -176,14 +177,14 @@ select ${_unjoinMd5.fields} from form_attachments
left outer join (select id, md5 from blobs) as blobs on form_attachments."blobId"=blobs.id
where "formDefId"=${formDefId}`)
.then(map(_unjoinMd5))
.then((attachments) => Promise.all(attachments.map(FormAttachments._chooseHash)));
.then((attachments) => Promise.all(attachments.map(FormAttachments._chooseDynamicHash)));


module.exports = {
createNew, createVersion,
update,
getAllByFormDefId, getByFormDefIdAndName,
getAllByFormDefIdForOpenRosa,
_chooseHash
_chooseDynamicHash
};

16 changes: 8 additions & 8 deletions test/integration/api/forms/draft.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,8 @@ describe('api: /projects/:id/forms (drafts)', () => {
.expect(200)
.then(({ body }) => {
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: false, blobExists: false, datasetExists: false, md5: null },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: false, blobExists: false, datasetExists: false, hash: null },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand Down Expand Up @@ -572,8 +572,8 @@ describe('api: /projects/:id/forms (drafts)', () => {
// eslint-disable-next-line no-param-reassign
delete body[0].updatedAt;
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'greattwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'greattwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand Down Expand Up @@ -604,8 +604,8 @@ describe('api: /projects/:id/forms (drafts)', () => {
// eslint-disable-next-line no-param-reassign
delete body[0].updatedAt;
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'greattwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'greattwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand Down Expand Up @@ -1139,8 +1139,8 @@ describe('api: /projects/:id/forms (drafts)', () => {
// eslint-disable-next-line no-param-reassign
delete body[0].updatedAt;
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand Down
8 changes: 4 additions & 4 deletions test/integration/api/forms/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,8 @@ describe('api: /projects/:id/forms (create, read, update)', () => {
.expect(200)
.then(({ body }) => {
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: false, blobExists: false, datasetExists: false, md5: null },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: false, blobExists: false, datasetExists: false, hash: null },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand All @@ -1252,8 +1252,8 @@ describe('api: /projects/:id/forms (create, read, update)', () => {
delete body[0].updatedAt;

body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand Down
4 changes: 2 additions & 2 deletions test/integration/api/forms/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,8 @@ describe('api: /projects/:id/forms (versions)', () => {
delete body[0].updatedAt;

body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2af2751b79eccfaa8f452331e76e679e' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
})))));

Expand Down
12 changes: 6 additions & 6 deletions test/integration/other/form-entities-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ describe('Update / migrate entities-version within form', () => {
// eslint-disable-next-line no-param-reassign
delete body[0].updatedAt;
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
});

Expand Down Expand Up @@ -318,8 +318,8 @@ describe('Update / migrate entities-version within form', () => {
// eslint-disable-next-line no-param-reassign
delete body[0].updatedAt;
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
});

Expand All @@ -329,8 +329,8 @@ describe('Update / migrate entities-version within form', () => {
// eslint-disable-next-line no-param-reassign
delete body[0].updatedAt;
body.should.eql([
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, md5: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, md5: null }
{ name: 'goodone.csv', type: 'file', exists: true, blobExists: true, datasetExists: false, hash: '2241de57bbec8144c8ad387e69b3a3ba' },
{ name: 'goodtwo.mp3', type: 'audio', exists: false, blobExists: false, datasetExists: false, hash: null }
]);
});
}));
Expand Down

0 comments on commit a9e3e05

Please sign in to comment.