2025-05-10 02:09:06 +02:00
|
|
|
/*!
|
|
|
|
|
* Copyright (c) 2024 PLANKA Software GmbH
|
|
|
|
|
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
|
|
|
|
*/
|
|
|
|
|
|
2025-09-08 16:20:27 +02:00
|
|
|
/**
|
|
|
|
|
* @swagger
|
2025-09-08 18:25:26 +02:00
|
|
|
* /projects/{projectId}/managers:
|
2025-09-08 16:20:27 +02:00
|
|
|
* post:
|
2025-09-12 12:17:01 +02:00
|
|
|
* summary: Create project manager
|
|
|
|
|
* description: Creates a project manager within a project. Requires admin privileges for shared projects or existing project manager permissions. The user must be an admin or project owner.
|
2025-09-08 16:20:27 +02:00
|
|
|
* tags:
|
|
|
|
|
* - Project Managers
|
2025-09-12 12:17:01 +02:00
|
|
|
* operationId: createProjectManager
|
2025-09-08 16:20:27 +02:00
|
|
|
* parameters:
|
|
|
|
|
* - name: projectId
|
|
|
|
|
* in: path
|
|
|
|
|
* required: true
|
2025-09-12 12:17:01 +02:00
|
|
|
* description: ID of the project to create the project manager in
|
2025-09-08 16:20:27 +02:00
|
|
|
* schema:
|
|
|
|
|
* type: string
|
2025-09-08 19:14:31 +02:00
|
|
|
* example: "1357158568008091264"
|
2025-09-08 16:20:27 +02:00
|
|
|
* requestBody:
|
|
|
|
|
* required: true
|
|
|
|
|
* content:
|
|
|
|
|
* application/json:
|
|
|
|
|
* schema:
|
|
|
|
|
* type: object
|
|
|
|
|
* required:
|
|
|
|
|
* - userId
|
|
|
|
|
* properties:
|
|
|
|
|
* userId:
|
|
|
|
|
* type: string
|
|
|
|
|
* description: ID of the user who is assigned as project manager
|
2025-09-08 19:14:31 +02:00
|
|
|
* example: "1357158568008091265"
|
2025-09-08 16:20:27 +02:00
|
|
|
* responses:
|
|
|
|
|
* 200:
|
2025-09-12 12:17:01 +02:00
|
|
|
* description: Project manager created successfully
|
2025-09-08 16:20:27 +02:00
|
|
|
* content:
|
|
|
|
|
* application/json:
|
|
|
|
|
* schema:
|
|
|
|
|
* type: object
|
|
|
|
|
* required:
|
|
|
|
|
* - item
|
|
|
|
|
* properties:
|
|
|
|
|
* item:
|
|
|
|
|
* $ref: '#/components/schemas/ProjectManager'
|
|
|
|
|
* 400:
|
|
|
|
|
* $ref: '#/components/responses/ValidationError'
|
|
|
|
|
* 401:
|
|
|
|
|
* $ref: '#/components/responses/Unauthorized'
|
|
|
|
|
* 403:
|
|
|
|
|
* $ref: '#/components/responses/Forbidden'
|
|
|
|
|
* 404:
|
|
|
|
|
* $ref: '#/components/responses/NotFound'
|
|
|
|
|
* 409:
|
|
|
|
|
* $ref: '#/components/responses/Conflict'
|
|
|
|
|
* 422:
|
|
|
|
|
* $ref: '#/components/responses/UnprocessableEntity'
|
|
|
|
|
*/
|
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const { idInput } = require('../../../utils/inputs');
|
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
const Errors = {
|
2025-05-10 02:09:06 +02:00
|
|
|
NOT_ENOUGH_RIGHTS: {
|
|
|
|
|
notEnoughRights: 'Not enough rights',
|
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
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
|
|
|
},
|
2021-06-24 01:05:22 +05:00
|
|
|
USER_ALREADY_PROJECT_MANAGER: {
|
|
|
|
|
userAlreadyProjectManager: 'User already project manager',
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
USER_MUST_BE_ADMIN_OR_PROJECT_OWNER: {
|
|
|
|
|
userMustBeAdminOrProjectOwner: 'User must be admin or project owner',
|
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
inputs: {
|
|
|
|
|
projectId: {
|
2025-05-10 02:09:06 +02:00
|
|
|
...idInput,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
userId: {
|
2025-05-10 02:09:06 +02:00
|
|
|
...idInput,
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
exits: {
|
2025-05-10 02:09:06 +02:00
|
|
|
notEnoughRights: {
|
|
|
|
|
responseType: 'forbidden',
|
|
|
|
|
},
|
2020-04-03 00:35:25 +05:00
|
|
|
projectNotFound: {
|
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
|
|
|
userNotFound: {
|
|
|
|
|
responseType: 'notFound',
|
|
|
|
|
},
|
2021-06-24 01:05:22 +05:00
|
|
|
userAlreadyProjectManager: {
|
2019-11-05 18:01:42 +05:00
|
|
|
responseType: 'conflict',
|
|
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
userMustBeAdminOrProjectOwner: {
|
|
|
|
|
responseType: 'unprocessableEntity',
|
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
|
|
|
|
const { currentUser } = this.req;
|
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const project = await Project.qm.getOneById(inputs.projectId);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
|
throw Errors.PROJECT_NOT_FOUND;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (currentUser.role !== User.Roles.ADMIN) {
|
|
|
|
|
const isProjectManager = await sails.helpers.users.isProjectManager(
|
|
|
|
|
currentUser.id,
|
|
|
|
|
project.id,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!isProjectManager) {
|
|
|
|
|
throw Errors.PROJECT_NOT_FOUND; // Forbidden
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-24 01:05:22 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (project.ownerProjectManagerId) {
|
|
|
|
|
throw Errors.NOT_ENOUGH_RIGHTS;
|
2021-06-24 01:05:22 +05:00
|
|
|
}
|
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const user = await User.qm.getOneById(inputs.userId, {
|
|
|
|
|
withDeactivated: false,
|
|
|
|
|
});
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
|
if (!user) {
|
2025-05-10 02:09:06 +02:00
|
|
|
throw Errors.USER_NOT_FOUND;
|
2019-08-31 04:07:25 +05:00
|
|
|
}
|
|
|
|
|
|
2022-12-26 21:10:50 +01:00
|
|
|
const projectManager = await sails.helpers.projectManagers.createOne
|
|
|
|
|
.with({
|
|
|
|
|
values: {
|
|
|
|
|
project,
|
|
|
|
|
user,
|
|
|
|
|
},
|
2024-06-12 00:51:36 +02:00
|
|
|
actorUser: currentUser,
|
2022-12-26 21:10:50 +01:00
|
|
|
request: this.req,
|
|
|
|
|
})
|
2025-05-10 02:09:06 +02:00
|
|
|
.intercept('userAlreadyProjectManager', () => Errors.USER_ALREADY_PROJECT_MANAGER)
|
|
|
|
|
.intercept(
|
|
|
|
|
'userInValuesMustBeAdminOrProjectOwner',
|
|
|
|
|
() => Errors.USER_MUST_BE_ADMIN_OR_PROJECT_OWNER,
|
|
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return {
|
|
|
|
|
item: projectManager,
|
|
|
|
|
};
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|