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/delete-one.js

87 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-04-21 05:04:34 +05:00
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
project: {
type: 'ref',
required: true,
},
board: {
2020-04-23 03:02:53 +05:00
type: 'ref',
required: true,
},
list: {
type: 'ref',
required: true,
},
card: {
2020-04-21 05:04:34 +05:00
type: 'ref',
required: true,
},
actorUser: {
type: 'ref',
required: true,
},
2020-04-21 05:04:34 +05:00
request: {
type: 'ref',
},
},
async fn(inputs) {
2020-04-23 03:02:53 +05:00
if (inputs.record.id === inputs.card.coverAttachmentId) {
await sails.helpers.cards.updateOne.with({
2020-04-23 03:02:53 +05:00
record: inputs.card,
values: {
coverAttachmentId: null,
},
project: inputs.project,
board: inputs.board,
list: inputs.list,
actorUser: inputs.actorUser,
2020-04-23 03:02:53 +05:00
request: inputs.request,
});
}
2020-04-21 05:04:34 +05:00
const attachment = await Attachment.archiveOne(inputs.record.id);
if (attachment) {
const fileManager = sails.hooks['file-manager'].getInstance();
try {
await fileManager.deleteFolder(
`${sails.config.custom.attachmentsPathSegment}/${attachment.dirname}`,
);
2020-04-21 05:04:34 +05:00
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'attachmentDelete',
{
item: attachment,
},
inputs.request,
);
sails.helpers.utils.sendWebhooks.with({
event: 'attachmentDelete',
data: {
item: attachment,
included: {
projects: [inputs.project],
boards: [inputs.board],
lists: [inputs.list],
cards: [inputs.card],
},
},
user: inputs.actorUser,
});
2020-04-21 05:04:34 +05:00
}
return attachment;
2020-04-21 05:04:34 +05:00
},
};