Skip to content

Commit

Permalink
Account for no form id on new observation forms
Browse files Browse the repository at this point in the history
  • Loading branch information
newmanw committed Aug 16, 2021
1 parent 646c4d3 commit 471520c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion api/field/attachmentField.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ AttachmentField.prototype.validate = function() {
return { error: 'value', message: `${this.definition.title} must be an Array` }
}

const observationFormId = this.observationForm._id ? this.observationForm._id.toString() : null
const attachments = this.attachments.filter(attachment => {
return attachment.observationFormId.toString() === this.observationForm._id.toString() && attachment.fieldName === this.definition.name;
return attachment.observationFormId.toString() === observationFormId && attachment.fieldName === this.definition.name;
});

const add = fieldValue.reduce((count, attachment) => { attachment.action === 'add' ? count + 1 : count }, 0)
Expand Down
8 changes: 4 additions & 4 deletions migrations/029-attachment-form-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function createAttachmentField(db) {
log.info(`creating attachment fields for event ${event.name}`);

event.forms.forEach(form => {
let id = 1;
let id = 0;
const fields = form.fields.slice() || [];
if (fields.length) {
id = fields.sort((a, b) => b.id - a.id)[0].id + 1;
Expand All @@ -23,7 +23,7 @@ async function createAttachmentField(db) {
fields.forEach(field => field.id = ++field.id);

fields.push({
id: 1,
id: 0,
name: `field${id}`,
required: false,
type: 'attachment',
Expand All @@ -41,7 +41,7 @@ async function createAttachmentField(db) {
log.info(`Event ${event.name} has no forms`);

const observationCollection = await db.collection(`observations${event._id}`);
const count = await observationCollection.count({attachments: { $exists: true, $type: 'array', $ne: [] } })
const count = await observationCollection.count({attachments: { $exists: true, $ne: [] } })
if (count > 0) {
log.info(`Event ${event.name} has no forms, but does contain attachments, create new form w/ attachment field`);

Expand All @@ -62,7 +62,7 @@ async function createAttachmentField(db) {
type: 'attachment',
title: 'Attachments',
choices: [],
allowedAttachmentTypes: ['image', 'video', 'voice']
allowedAttachmentTypes: ['image', 'video', 'audio']
}]
}];
}
Expand Down

0 comments on commit 471520c

Please sign in to comment.