Files
planka/server/api/controllers/attachments/update.js

134 lines
3.1 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
*/
2025-09-08 16:20:27 +02:00
/**
* @swagger
* /attachments/{id}:
2025-09-08 16:20:27 +02:00
* patch:
* summary: Update attachment
* description: Updates an attachment. Requires board editor permissions.
* tags:
* - Attachments
* parameters:
* - name: id
* in: path
* required: true
* description: ID of the attachment to update
* schema:
* type: string
2025-09-08 19:14:31 +02:00
* example: "1357158568008091264"
2025-09-08 16:20:27 +02:00
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name:
* type: string
* maxLength: 128
* description: Name/title of the attachment
* example: Google Link
* responses:
* 200:
* description: Attachment updated successfully
* content:
* application/json:
* schema:
* type: object
* required:
* - item
* properties:
* item:
* $ref: '#/components/schemas/Attachment'
* 400:
* $ref: '#/components/responses/ValidationError'
* 401:
* $ref: '#/components/responses/Unauthorized'
* 403:
* $ref: '#/components/responses/Forbidden'
* 404:
* $ref: '#/components/responses/NotFound'
*/
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
},
};