Skip to content
Merged
Changes from 1 commit
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
20 changes: 17 additions & 3 deletions app/controllers/forms_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ export default class FormsController {
const event = await Event.query()
.where("id", eventId)
.preload("firstForm")
.preload("attributes")
.firstOrFail();

await bouncer.authorize("manage_form", event);
const form = await Form.query()
.where("event_id", eventId)
.where("id", formId)
.preload("attributes")
.firstOrFail();

const { attributes, ...updates } =
Expand All @@ -171,10 +173,20 @@ export default class FormsController {
});
}

form.merge(updates);
await form.save();

if (attributes !== undefined) {
const eventAttributesIdsSet = new Set(
event.attributes.map((attribute) => attribute.id),
);

const attributesFromDifferentEvent = attributes.filter(
(attribute) => !eventAttributesIdsSet.has(attribute.id),
);

if (attributesFromDifferentEvent.length > 0) {
return response.badRequest({
message: `Attributes with ids ${JSON.stringify(attributesFromDifferentEvent.map((attribute) => attribute.id))}, do not belong to this event`,
});
}
await form.related("attributes").detach();

await form.related("attributes").attach(
Expand All @@ -194,6 +206,8 @@ export default class FormsController {
),
);
}
form.merge(updates);
await form.save();

const updatedForm = await Form.query()
.where("event_id", eventId)
Expand Down
Loading