Skip to content

Commit 471520c

Browse files
committed
Account for no form id on new observation forms
1 parent 646c4d3 commit 471520c

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

api/field/attachmentField.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ AttachmentField.prototype.validate = function() {
2020
return { error: 'value', message: `${this.definition.title} must be an Array` }
2121
}
2222

23+
const observationFormId = this.observationForm._id ? this.observationForm._id.toString() : null
2324
const attachments = this.attachments.filter(attachment => {
24-
return attachment.observationFormId.toString() === this.observationForm._id.toString() && attachment.fieldName === this.definition.name;
25+
return attachment.observationFormId.toString() === observationFormId && attachment.fieldName === this.definition.name;
2526
});
2627

2728
const add = fieldValue.reduce((count, attachment) => { attachment.action === 'add' ? count + 1 : count }, 0)

migrations/029-attachment-form-field.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function createAttachmentField(db) {
1313
log.info(`creating attachment fields for event ${event.name}`);
1414

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

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

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

@@ -62,7 +62,7 @@ async function createAttachmentField(db) {
6262
type: 'attachment',
6363
title: 'Attachments',
6464
choices: [],
65-
allowedAttachmentTypes: ['image', 'video', 'voice']
65+
allowedAttachmentTypes: ['image', 'video', 'audio']
6666
}]
6767
}];
6868
}

0 commit comments

Comments
 (0)