1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 13:19:44 +02:00
planka/server/api/helpers/attachments/create-one.js
2022-12-26 21:10:50 +01:00

63 lines
1.1 KiB
JavaScript

const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isPlainObject(value.card)) {
return false;
}
if (!_.isPlainObject(value.creatorUser)) {
return false;
}
return true;
};
module.exports = {
inputs: {
values: {
type: 'ref',
custom: valuesValidator,
required: true,
},
requestId: {
type: 'string',
isNotEmptyString: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
const { values } = inputs;
const attachment = await Attachment.create({
...values,
cardId: values.card.id,
creatorUserId: values.creatorUser.id,
}).fetch();
sails.sockets.broadcast(
`board:${values.card.boardId}`,
'attachmentCreate',
{
item: attachment,
requestId: inputs.requestId,
},
inputs.request,
);
if (!values.card.coverAttachmentId && attachment.image) {
await sails.helpers.cards.updateOne.with({
record: values.card,
values: {
coverAttachmentId: attachment.id,
},
});
}
return attachment;
},
};