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/card-memberships/create.js

67 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const Errors = {
CARD_NOT_FOUND: {
notFound: 'Card is not found',
2019-08-31 04:07:25 +05:00
},
USER_NOT_FOUND: {
notFound: 'User is not found',
2019-08-31 04:07:25 +05:00
},
CARD_MEMBERSHIP_EXIST: {
conflict: 'Card membership is already exist',
},
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
},
userId: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
notFound: {
responseType: 'notFound',
2019-08-31 04:07:25 +05:00
},
conflict: {
responseType: 'conflict',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs, exits) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
const { card, project } = await sails.helpers
.getCardToProjectPath(inputs.cardId)
.intercept('notFound', () => Errors.CARD_NOT_FOUND);
let isUserMemberForProject = await sails.helpers.isUserMemberForProject(
project.id,
currentUser.id,
2019-08-31 04:07:25 +05:00
);
if (!isUserMemberForProject) {
throw Errors.CARD_NOT_FOUND; // Forbidden
}
isUserMemberForProject = await sails.helpers.isUserMemberForProject(project.id, inputs.userId);
2019-08-31 04:07:25 +05:00
if (!isUserMemberForProject) {
throw Errors.USER_NOT_FOUND;
}
const cardMembership = await sails.helpers
.createCardMembership(card, inputs.userId, this.req)
.intercept('conflict', () => Errors.CARD_MEMBERSHIP_EXIST);
return exits.success({
item: cardMembership,
2019-08-31 04:07:25 +05:00
});
},
2019-08-31 04:07:25 +05:00
};