1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/controllers/comment-actions/create.js

79 lines
1.8 KiB
JavaScript
Raw Normal View History

2023-12-23 10:52:15 -06:00
const services = require('../../services/slack');
2019-08-31 04:07:25 +05:00
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
2019-08-31 04:07:25 +05:00
CARD_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
cardNotFound: 'Card not found',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
cardId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
2019-08-31 04:07:25 +05:00
},
text: {
type: 'string',
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
notEnoughRights: {
responseType: 'forbidden',
},
2020-04-03 00:35:25 +05:00
cardNotFound: {
responseType: 'notFound',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
const { card } = await sails.helpers.cards
.getProjectPath(inputs.cardId)
2020-04-03 00:35:25 +05:00
.intercept('pathNotFound', () => Errors.CARD_NOT_FOUND);
2019-08-31 04:07:25 +05:00
const boardMembership = await BoardMembership.findOne({
boardId: card.boardId,
userId: currentUser.id,
});
2019-08-31 04:07:25 +05:00
if (!boardMembership) {
2019-08-31 04:07:25 +05:00
throw Errors.CARD_NOT_FOUND; // Forbidden
}
if (boardMembership.role !== BoardMembership.Roles.EDITOR && !boardMembership.canComment) {
throw Errors.NOT_ENOUGH_RIGHTS;
}
2019-08-31 04:07:25 +05:00
const values = {
type: Action.Types.COMMENT_CARD,
data: _.pick(inputs, ['text']),
2019-08-31 04:07:25 +05:00
};
2022-12-26 21:10:50 +01:00
const action = await sails.helpers.actions.createOne.with({
values: {
...values,
card,
user: currentUser,
},
request: this.req,
});
2019-08-31 04:07:25 +05:00
const cardUrl = services.buildCardUrl(card);
const messageText = '*' + currentUser.username + '* commented on ' + cardUrl + ':\n>' + inputs.text;
services.sendSlackMessage(messageText)
.then(() => { console.log('Slack message sent successfully.'); })
.catch((error) => { console.error('Failed to send Slack message:', error.message); });
return {
item: action,
};
},
2019-08-31 04:07:25 +05:00
};