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
|
|
|
|
*/
|
|
|
|
|
|
|
|
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-11-05 18:01:42 +05:00
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
OWNER_PROJECT_MANAGER_NOT_FOUND: {
|
|
|
|
ownerProjectManagerNotFound: 'Owner project manager not found',
|
|
|
|
},
|
|
|
|
BACKGROUND_IMAGE_NOT_FOUND: {
|
|
|
|
backgroundImageNotFound: 'Background image not found',
|
|
|
|
},
|
|
|
|
PROJECT_ALREADY_HAS_OWNER_PROJECT_MANAGER: {
|
|
|
|
projectAlreadyHasOwnerProjectManager: 'Project already has owner project manager',
|
|
|
|
},
|
|
|
|
OWNER_PROJECT_MANAGER_MUST_BE_LAST_MANAGER: {
|
|
|
|
ownerProjectManagerMustBeLastManager: 'Owner project manager must be last manager',
|
|
|
|
},
|
|
|
|
BACKGROUND_IMAGE_MUST_BE_PRESENT: {
|
|
|
|
backgroundImageMustBePresent: 'Background image must be present',
|
|
|
|
},
|
|
|
|
BACKGROUND_GRADIENT_MUST_BE_PRESENT: {
|
|
|
|
backgroundGradientMustBePresent: 'Background gradient must be present',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
id: {
|
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
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
ownerProjectManagerId: {
|
|
|
|
...idInput,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
backgroundImageId: {
|
|
|
|
...idInput,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
name: {
|
|
|
|
type: 'string',
|
2019-11-05 18:01:42 +05:00
|
|
|
isNotEmptyString: true,
|
2025-05-10 02:09:06 +02:00
|
|
|
maxLength: 128,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: 'string',
|
|
|
|
isNotEmptyString: true,
|
|
|
|
maxLength: 1024,
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
backgroundType: {
|
|
|
|
type: 'string',
|
|
|
|
isIn: Object.values(Project.BackgroundTypes),
|
|
|
|
allowNull: true,
|
|
|
|
},
|
|
|
|
backgroundGradient: {
|
|
|
|
type: 'string',
|
|
|
|
isIn: Project.BACKGROUND_GRADIENTS,
|
|
|
|
allowNull: true,
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
isHidden: {
|
|
|
|
type: 'boolean',
|
2020-05-26 00:46:04 +05:00
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
isFavorite: {
|
|
|
|
type: 'boolean',
|
2020-05-26 00:46:04 +05:00
|
|
|
},
|
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',
|
|
|
|
},
|
2025-05-10 02:09:06 +02:00
|
|
|
ownerProjectManagerNotFound: {
|
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
|
|
|
backgroundImageNotFound: {
|
|
|
|
responseType: 'notFound',
|
|
|
|
},
|
|
|
|
projectAlreadyHasOwnerProjectManager: {
|
|
|
|
responseType: 'conflict',
|
|
|
|
},
|
|
|
|
ownerProjectManagerMustBeLastManager: {
|
|
|
|
responseType: 'unprocessableEntity',
|
|
|
|
},
|
|
|
|
backgroundImageMustBePresent: {
|
|
|
|
responseType: 'unprocessableEntity',
|
|
|
|
},
|
|
|
|
backgroundGradientMustBePresent: {
|
|
|
|
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
|
|
|
let project = await Project.qm.getOneById(inputs.id);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
throw Errors.PROJECT_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const projectManager = await ProjectManager.qm.getOneByProjectIdAndUserId(
|
|
|
|
project.id,
|
|
|
|
currentUser.id,
|
|
|
|
);
|
|
|
|
|
|
|
|
const availableInputKeys = ['id', 'isFavorite'];
|
|
|
|
if (project.ownerProjectManagerId) {
|
|
|
|
if (projectManager) {
|
|
|
|
if (!_.isNil(inputs.ownerProjectManagerId)) {
|
|
|
|
throw Errors.NOT_ENOUGH_RIGHTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
availableInputKeys.push('ownerProjectManagerId', 'isHidden');
|
|
|
|
}
|
|
|
|
} else if (currentUser.role === User.Roles.ADMIN) {
|
|
|
|
availableInputKeys.push('ownerProjectManagerId', 'isHidden');
|
|
|
|
} else if (projectManager) {
|
|
|
|
availableInputKeys.push('isHidden');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (projectManager) {
|
|
|
|
availableInputKeys.push(
|
|
|
|
'backgroundImageId',
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'backgroundType',
|
|
|
|
'backgroundGradient',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.difference(Object.keys(inputs), availableInputKeys).length > 0) {
|
|
|
|
throw Errors.NOT_ENOUGH_RIGHTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
let nextOwnerProjectManager;
|
|
|
|
if (inputs.ownerProjectManagerId) {
|
|
|
|
nextOwnerProjectManager = await ProjectManager.qm.getOneById(inputs.ownerProjectManagerId, {
|
|
|
|
projectId: project.id,
|
|
|
|
});
|
2021-06-24 01:05:22 +05:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (!nextOwnerProjectManager) {
|
|
|
|
throw Errors.OWNER_PROJECT_MANAGER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete inputs.ownerProjectManagerId; // eslint-disable-line no-param-reassign
|
|
|
|
}
|
|
|
|
|
|
|
|
let nextBackgroundImage;
|
|
|
|
if (inputs.backgroundImageId) {
|
|
|
|
nextBackgroundImage = await BackgroundImage.qm.getOneById(inputs.backgroundImageId, {
|
|
|
|
projectId: project.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!nextBackgroundImage) {
|
|
|
|
throw Errors.BACKGROUND_IMAGE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete inputs.backgroundImageId; // eslint-disable-line no-param-reassign
|
2021-06-24 01:05:22 +05:00
|
|
|
}
|
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
if (!_.isUndefined(inputs.isFavorite)) {
|
|
|
|
if (currentUser.role !== User.Roles.ADMIN || project.ownerProjectManagerId) {
|
|
|
|
if (!projectManager) {
|
|
|
|
const boardMembershipsTotal =
|
|
|
|
await sails.helpers.projects.getBoardMembershipsTotalByIdAndUserId(
|
|
|
|
project.id,
|
|
|
|
currentUser.id,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (boardMembershipsTotal === 0) {
|
|
|
|
throw Errors.PROJECT_NOT_FOUND; // Forbidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-26 21:10:50 +01:00
|
|
|
|
2025-05-10 02:09:06 +02:00
|
|
|
const values = _.pick(inputs, [
|
|
|
|
'ownerProjectManagerId',
|
|
|
|
'backgroundImageId',
|
|
|
|
'name',
|
|
|
|
'description',
|
|
|
|
'backgroundType',
|
|
|
|
'backgroundGradient',
|
|
|
|
'isHidden',
|
|
|
|
'isFavorite',
|
|
|
|
]);
|
|
|
|
|
|
|
|
project = await sails.helpers.projects.updateOne
|
|
|
|
.with({
|
|
|
|
record: project,
|
|
|
|
values: {
|
|
|
|
...values,
|
|
|
|
ownerProjectManager: nextOwnerProjectManager,
|
|
|
|
backgroundImage: nextBackgroundImage,
|
|
|
|
},
|
|
|
|
actorUser: currentUser,
|
|
|
|
request: this.req,
|
|
|
|
})
|
|
|
|
.intercept(
|
|
|
|
'ownerProjectManagerInValuesMustBeLastManager',
|
|
|
|
() => Errors.OWNER_PROJECT_MANAGER_MUST_BE_LAST_MANAGER,
|
|
|
|
)
|
|
|
|
.intercept(
|
|
|
|
'backgroundImageInValuesMustBePresent',
|
|
|
|
() => Errors.BACKGROUND_IMAGE_MUST_BE_PRESENT,
|
|
|
|
)
|
|
|
|
.intercept(
|
|
|
|
'backgroundGradientInValuesMustBePresent',
|
|
|
|
() => Errors.BACKGROUND_GRADIENT_MUST_BE_PRESENT,
|
|
|
|
)
|
|
|
|
.intercept(
|
|
|
|
'alreadyHasOwnerProjectManager',
|
|
|
|
() => Errors.PROJECT_ALREADY_HAS_OWNER_PROJECT_MANAGER,
|
|
|
|
);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
throw Errors.PROJECT_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return {
|
2019-11-05 18:01:42 +05:00
|
|
|
item: project,
|
2021-06-24 01:05:22 +05:00
|
|
|
};
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|