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

75 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const Errors = {
PROJECT_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
projectNotFound: 'Project not found',
2019-08-31 04:07:25 +05:00
},
USER_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
userNotFound: 'User not found',
2019-08-31 04:07:25 +05:00
},
USER_ALREADY_PROJECT_MANAGER: {
userAlreadyProjectManager: 'User already project manager',
},
2019-08-31 04:07:25 +05:00
};
module.exports = {
inputs: {
projectId: {
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: {
2020-04-03 00:35:25 +05:00
projectNotFound: {
responseType: 'notFound',
2019-08-31 04:07:25 +05:00
},
2020-04-03 00:35:25 +05:00
userNotFound: {
responseType: 'notFound',
},
userAlreadyProjectManager: {
responseType: 'conflict',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs) {
const { currentUser } = this.req;
2019-08-31 04:07:25 +05:00
const project = await Project.findOne(inputs.projectId);
if (!project) {
throw Errors.PROJECT_NOT_FOUND;
}
const isProjectManager = await sails.helpers.users.isProjectManager(currentUser.id, project.id);
if (!isProjectManager) {
throw Errors.PROJECT_NOT_FOUND; // Forbidden
}
const user = await sails.helpers.users.getOne(inputs.userId);
2019-08-31 04:07:25 +05:00
if (!user) {
throw Error.USER_NOT_FOUND;
}
2022-12-26 21:10:50 +01:00
const projectManager = await sails.helpers.projectManagers.createOne
.with({
values: {
project,
user,
},
request: this.req,
})
.intercept('userAlreadyProjectManager', () => Errors.USER_ALREADY_PROJECT_MANAGER);
2019-08-31 04:07:25 +05:00
return {
item: projectManager,
};
},
2019-08-31 04:07:25 +05:00
};