2019-08-31 04:07:25 +05:00
|
|
|
const Errors = {
|
|
|
|
CARD_NOT_FOUND: {
|
2020-04-03 00:35:25 +05:00
|
|
|
cardNotFound: 'Card not found',
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
LABEL_NOT_FOUND: {
|
2020-04-03 00:35:25 +05:00
|
|
|
labelNotFound: 'Label not found',
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
LABEL_ALREADY_IN_CARD: {
|
|
|
|
labelAlreadyInCard: 'Label already in card',
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
cardId: {
|
2019-10-10 02:51:54 +05:00
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
labelId: {
|
2019-10-10 02:51:54 +05:00
|
|
|
type: 'string',
|
|
|
|
regex: /^[0-9]+$/,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
exits: {
|
2020-04-03 00:35:25 +05:00
|
|
|
cardNotFound: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'notFound',
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
labelNotFound: {
|
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
|
|
|
labelAlreadyInCard: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'conflict',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
2019-08-31 04:07:25 +05:00
|
|
|
const { currentUser } = this.req;
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
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
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const isBoardMember = await sails.helpers.users.isBoardMember(currentUser.id, card.boardId);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
if (!isBoardMember) {
|
2019-08-31 04:07:25 +05:00
|
|
|
throw Errors.CARD_NOT_FOUND; // Forbidden
|
|
|
|
}
|
|
|
|
|
|
|
|
const label = await Label.findOne({
|
|
|
|
id: inputs.labelId,
|
2019-11-05 18:01:42 +05:00
|
|
|
boardId: card.boardId,
|
2019-08-31 04:07:25 +05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!label) {
|
|
|
|
throw Errors.LABEL_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const cardLabel = await sails.helpers.cardLabels
|
|
|
|
.createOne(label, card, this.req)
|
2020-04-03 00:35:25 +05:00
|
|
|
.intercept('labelAlreadyInCard', () => Errors.LABEL_ALREADY_IN_CARD);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return {
|
2019-11-05 18:01:42 +05:00
|
|
|
item: cardLabel,
|
2021-06-24 01:05:22 +05:00
|
|
|
};
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|