1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 12:49:43 +02:00
planka/server/api/helpers/attachments/create-one.js
HannesOberreiter 193daf6cfb feat: Events via webhook (#771)
Closes #215, closes #656
2024-06-06 20:22:14 +02:00

78 lines
1.5 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,
},
board: {
type: 'ref',
required: true,
},
requestId: {
type: 'string',
isNotEmptyString: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
const { values, board } = 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,
},
board,
request: inputs.request,
});
}
await sails.helpers.utils.sendWebhook.with({
event: 'ATTACHMENT_CREATE',
data: attachment,
projectId: board.projectId,
user: inputs.request.currentUser,
card: values.card,
board,
});
return attachment;
},
};