mirror of
https://github.com/plankanban/planka.git
synced 2025-12-19 09:13:20 +03:00
@@ -1,3 +1,10 @@
|
||||
/*!
|
||||
* Copyright (c) 2024 PLANKA Software GmbH
|
||||
* Licensed under the Fair Use License: https://github.com/plankanban/planka/blob/master/LICENSE.md
|
||||
*/
|
||||
|
||||
const { idInput } = require('../../../utils/inputs');
|
||||
|
||||
const Errors = {
|
||||
PROJECT_NOT_FOUND: {
|
||||
projectNotFound: 'Project not found',
|
||||
@@ -7,8 +14,7 @@ const Errors = {
|
||||
module.exports = {
|
||||
inputs: {
|
||||
id: {
|
||||
type: 'string',
|
||||
regex: /^[0-9]+$/,
|
||||
...idInput,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
@@ -22,43 +28,67 @@ module.exports = {
|
||||
async fn(inputs) {
|
||||
const { currentUser } = this.req;
|
||||
|
||||
const project = await Project.findOne(inputs.id);
|
||||
const project = await Project.qm.getOneById(inputs.id);
|
||||
|
||||
if (!project) {
|
||||
throw Errors.PROJECT_NOT_FOUND;
|
||||
}
|
||||
|
||||
let boards = await sails.helpers.projects.getBoards(project.id);
|
||||
let boardIds = sails.helpers.utils.mapRecords(boards);
|
||||
|
||||
const boardMemberships = await sails.helpers.boardMemberships.getMany({
|
||||
boardId: boardIds,
|
||||
userId: currentUser.id,
|
||||
});
|
||||
|
||||
const isProjectManager = await sails.helpers.users.isProjectManager(currentUser.id, project.id);
|
||||
|
||||
if (!isProjectManager) {
|
||||
if (boardMemberships.length === 0) {
|
||||
throw Errors.PROJECT_NOT_FOUND; // Forbidden
|
||||
}
|
||||
const boardMemberships = await BoardMembership.qm.getByProjectIdAndUserId(
|
||||
project.id,
|
||||
currentUser.id,
|
||||
);
|
||||
|
||||
boardIds = sails.helpers.utils.mapRecords(boardMemberships, 'boardId');
|
||||
boards = boards.filter((board) => boardIds.includes(board.id));
|
||||
let boards;
|
||||
if (currentUser.role !== User.Roles.ADMIN || project.ownerProjectManagerId) {
|
||||
if (!isProjectManager) {
|
||||
if (boardMemberships.length === 0) {
|
||||
throw Errors.PROJECT_NOT_FOUND; // Forbidden
|
||||
}
|
||||
|
||||
const boardIds = sails.helpers.utils.mapRecords(boardMemberships, 'boardId');
|
||||
boards = await Board.qm.getByIds(boardIds);
|
||||
}
|
||||
}
|
||||
|
||||
const projectManagers = await sails.helpers.projects.getProjectManagers(project.id);
|
||||
if (!boards) {
|
||||
boards = await Board.qm.getByProjectId(project.id);
|
||||
}
|
||||
|
||||
project.isFavorite = await sails.helpers.users.isProjectFavorite(currentUser.id, project.id);
|
||||
|
||||
const projectManagers = await ProjectManager.qm.getByProjectId(project.id);
|
||||
|
||||
const userIds = sails.helpers.utils.mapRecords(projectManagers, 'userId');
|
||||
const users = await sails.helpers.users.getMany(userIds);
|
||||
const users = await User.qm.getByIds(userIds);
|
||||
|
||||
const backgroundImages = await BackgroundImage.qm.getByProjectId(project.id);
|
||||
|
||||
const baseCustomFieldGroups = await BaseCustomFieldGroup.qm.getByProjectId(project.id);
|
||||
const baseCustomFieldGroupsIds = sails.helpers.utils.mapRecords(baseCustomFieldGroups);
|
||||
|
||||
const customFields =
|
||||
await CustomField.qm.getByBaseCustomFieldGroupIds(baseCustomFieldGroupsIds);
|
||||
|
||||
let notificationServices = [];
|
||||
if (isProjectManager) {
|
||||
boardIds = sails.helpers.utils.mapRecords(boards);
|
||||
notificationServices = await NotificationService.qm.getByBoardIds(boardIds);
|
||||
}
|
||||
|
||||
return {
|
||||
item: project,
|
||||
included: {
|
||||
users,
|
||||
projectManagers,
|
||||
baseCustomFieldGroups,
|
||||
boards,
|
||||
boardMemberships,
|
||||
customFields,
|
||||
notificationServices,
|
||||
users: sails.helpers.users.presentMany(users, currentUser),
|
||||
backgroundImages: sails.helpers.backgroundImages.presentMany(backgroundImages),
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user