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

55 lines
1.1 KiB
JavaScript

const path = require('path');
const rimraf = require('rimraf');
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
},
board: {
type: 'ref',
required: true,
},
card: {
type: 'ref',
required: true,
},
request: {
type: 'ref',
},
},
async fn(inputs) {
if (inputs.record.id === inputs.card.coverAttachmentId) {
await sails.helpers.cards.updateOne.with({
record: inputs.card,
values: {
coverAttachmentId: null,
},
request: inputs.request,
});
}
const attachment = await Attachment.archiveOne(inputs.record.id);
if (attachment) {
try {
rimraf.sync(path.join(sails.config.custom.attachmentsPath, attachment.dirname));
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
sails.sockets.broadcast(
`board:${inputs.board.id}`,
'attachmentDelete',
{
item: attachment,
},
inputs.request,
);
}
return attachment;
},
};