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

84 lines
1.7 KiB
JavaScript
Raw Normal View History

/*!
* Copyright (c) 2024 PLANKA Software GmbH
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
*/
const { idInput } = require('../../../utils/inputs');
2020-04-21 05:04:34 +05:00
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
2020-04-21 05:04:34 +05:00
ATTACHMENT_NOT_FOUND: {
attachmentNotFound: 'Attachment not found',
},
};
module.exports = {
inputs: {
id: {
...idInput,
2020-04-21 05:04:34 +05:00
required: true,
},
name: {
type: 'string',
isNotEmptyString: true,
maxLength: 128,
2020-04-21 05:04:34 +05:00
},
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-21 05:04:34 +05:00
attachmentNotFound: {
responseType: 'notFound',
},
},
async fn(inputs) {
2020-04-21 05:04:34 +05:00
const { currentUser } = this.req;
const pathToProject = await sails.helpers.attachments
.getPathToProjectById(inputs.id)
2020-04-21 05:04:34 +05:00
.intercept('pathNotFound', () => Errors.ATTACHMENT_NOT_FOUND);
let { attachment } = pathToProject;
const { card, list, board, project } = pathToProject;
2020-04-21 05:04:34 +05:00
const boardMembership = await BoardMembership.qm.getOneByBoardIdAndUserId(
board.id,
currentUser.id,
);
2020-04-21 05:04:34 +05:00
if (!boardMembership) {
2020-04-21 05:04:34 +05:00
throw Errors.ATTACHMENT_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
2020-04-21 05:04:34 +05:00
const values = _.pick(inputs, ['name']);
2022-12-26 21:10:50 +01:00
attachment = await sails.helpers.attachments.updateOne.with({
values,
project,
2022-12-26 21:10:50 +01:00
board,
list,
card,
2022-12-26 21:10:50 +01:00
record: attachment,
actorUser: currentUser,
2022-12-26 21:10:50 +01:00
request: this.req,
});
2020-04-21 05:04:34 +05:00
if (!attachment) {
throw Errors.ATTACHMENT_NOT_FOUND;
}
return {
item: sails.helpers.attachments.presentOne(attachment),
};
2020-04-21 05:04:34 +05:00
},
};