1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 21:29:43 +02:00
planka/server/api/helpers/attachments/create-one.js

94 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
if (!_.isPlainObject(value.card)) {
return false;
}
if (!_.isPlainObject(value.creatorUser)) {
return false;
}
return true;
};
2020-04-21 05:04:34 +05:00
module.exports = {
inputs: {
values: {
type: 'ref',
2022-12-26 21:10:50 +01:00
custom: valuesValidator,
2020-04-21 05:04:34 +05:00
required: true,
},
project: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
list: {
type: 'ref',
required: true,
},
2020-04-23 05:56:02 +05:00
requestId: {
type: 'string',
isNotEmptyString: true,
},
2020-04-21 05:04:34 +05:00
request: {
type: 'ref',
},
},
async fn(inputs) {
const { values } = inputs;
2022-12-26 21:10:50 +01:00
2020-04-21 05:04:34 +05:00
const attachment = await Attachment.create({
2022-12-26 21:10:50 +01:00
...values,
cardId: values.card.id,
creatorUserId: values.creatorUser.id,
2020-04-21 05:04:34 +05:00
}).fetch();
sails.sockets.broadcast(
2022-12-26 21:10:50 +01:00
`board:${values.card.boardId}`,
2020-04-21 05:04:34 +05:00
'attachmentCreate',
{
item: attachment,
2020-04-23 05:56:02 +05:00
requestId: inputs.requestId,
2020-04-21 05:04:34 +05:00
},
inputs.request,
);
sails.helpers.utils.sendWebhooks.with({
event: 'attachmentCreate',
data: {
item: attachment,
included: {
projects: [inputs.project],
boards: [inputs.board],
lists: [inputs.list],
cards: [values.card],
},
},
user: values.creatorUser,
});
2022-12-26 21:10:50 +01:00
if (!values.card.coverAttachmentId && attachment.image) {
await sails.helpers.cards.updateOne.with({
record: values.card,
values: {
coverAttachmentId: attachment.id,
},
project: inputs.project,
board: inputs.board,
list: inputs.list,
actorUser: values.creatorUser,
2020-04-23 03:02:53 +05:00
});
}
return attachment;
2020-04-21 05:04:34 +05:00
},
};