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

85 lines
1.8 KiB
JavaScript
Raw Normal View History

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
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
};
module.exports = {
inputs: {
cardId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
2020-04-23 05:56:02 +05:00
requestId: {
type: 'string',
isNotEmptyString: true,
},
2020-04-21 05:04:34 +05:00
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-21 05:04:34 +05:00
cardNotFound: {
responseType: 'notFound',
},
uploadError: {
responseType: 'unprocessableEntity',
},
},
async fn(inputs, exits) {
const { currentUser } = this.req;
const { card } = await sails.helpers.cards
.getProjectPath(inputs.cardId)
2020-04-21 05:04:34 +05:00
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
const boardMembership = await BoardMembership.findOne({
boardId: card.boardId,
userId: currentUser.id,
});
2020-04-21 05:04:34 +05:00
if (!boardMembership) {
2020-04-21 05:04:34 +05:00
throw Errors.CARD_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
this.req
.file('file')
.upload(sails.helpers.utils.createAttachmentReceiver(), async (error, files) => {
if (error) {
return exits.uploadError(error.message);
}
2020-04-21 05:04:34 +05:00
if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
2020-04-21 05:04:34 +05:00
const file = files[0];
2020-04-21 05:04:34 +05:00
const attachment = await sails.helpers.attachments.createOne(
{
...file.extra,
filename: file.filename,
},
currentUser,
card,
inputs.requestId,
this.req,
);
2020-04-21 05:04:34 +05:00
return exits.success({
item: attachment.toJSON(),
});
2020-04-21 05:04:34 +05:00
});
},
};