1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-18 20:59:44 +02:00
planka/server/api/helpers/projects/update-one.js

116 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-05-26 00:46:04 +05:00
const path = require('path');
const rimraf = require('rimraf');
2022-12-26 21:10:50 +01:00
const valuesValidator = (value) => {
if (!_.isPlainObject(value)) {
return false;
}
2023-01-08 22:10:41 +01:00
if (!_.isNil(value.background) && !_.isPlainObject(value.background)) {
2022-12-26 21:10:50 +01:00
return false;
}
2023-01-08 22:10:41 +01:00
if (!_.isNil(value.backgroundImage) && !_.isPlainObject(value.backgroundImage)) {
2022-12-26 21:10:50 +01:00
return false;
}
return true;
};
2019-08-31 04:07:25 +05:00
module.exports = {
inputs: {
record: {
type: 'ref',
required: true,
2019-08-31 04:07:25 +05:00
},
values: {
type: 'json',
2022-12-26 21:10:50 +01:00
custom: valuesValidator,
required: true,
2019-08-31 04:07:25 +05:00
},
request: {
type: 'ref',
},
2019-08-31 04:07:25 +05:00
},
2020-06-03 22:27:20 +05:00
exits: {
2022-12-26 21:10:50 +01:00
backgroundImageInValuesMustNotBeNull: {},
2020-06-03 22:27:20 +05:00
},
async fn(inputs) {
2022-12-26 21:10:50 +01:00
const { values } = inputs;
if (values.backgroundImage) {
values.background = {
2020-05-26 00:46:04 +05:00
type: 'image',
};
2020-06-03 22:27:20 +05:00
} else if (
2022-12-26 21:10:50 +01:00
_.isNull(values.backgroundImage) &&
2020-06-03 22:27:20 +05:00
inputs.record.background &&
inputs.record.background.type === 'image'
) {
2022-12-26 21:10:50 +01:00
values.background = null;
2020-05-26 00:46:04 +05:00
}
2020-06-03 22:27:20 +05:00
let project;
2022-12-26 21:10:50 +01:00
if (values.background && values.background.type === 'image') {
if (_.isNull(values.backgroundImage)) {
throw 'backgroundImageInValuesMustNotBeNull';
2020-06-03 22:27:20 +05:00
}
2022-12-26 21:10:50 +01:00
if (_.isUndefined(values.backgroundImage)) {
2020-06-03 22:27:20 +05:00
project = await Project.updateOne({
id: inputs.record.id,
backgroundImage: {
2020-06-03 22:27:20 +05:00
'!=': null,
},
2022-12-26 21:10:50 +01:00
}).set({ ...values });
2020-06-03 22:27:20 +05:00
if (!project) {
2022-12-26 21:10:50 +01:00
delete values.background;
2020-06-03 22:27:20 +05:00
}
}
}
if (!project) {
2022-12-26 21:10:50 +01:00
project = await Project.updateOne(inputs.record.id).set({ ...values });
2020-06-03 22:27:20 +05:00
}
2019-08-31 04:07:25 +05:00
if (project) {
2020-05-26 00:46:04 +05:00
if (
inputs.record.backgroundImage &&
(!project.backgroundImage ||
project.backgroundImage.dirname !== inputs.record.backgroundImage.dirname)
2020-05-26 00:46:04 +05:00
) {
try {
rimraf.sync(
path.join(
sails.config.custom.projectBackgroundImagesPath,
inputs.record.backgroundImage.dirname,
2020-05-26 00:46:04 +05:00
),
);
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}
}
2022-12-26 21:10:50 +01:00
const projectRelatedUserIds = await sails.helpers.projects.getManagerAndBoardMemberUserIds(
project.id,
);
2019-08-31 04:07:25 +05:00
2022-12-26 21:10:50 +01:00
projectRelatedUserIds.forEach((userId) => {
2019-08-31 04:07:25 +05:00
sails.sockets.broadcast(
`user:${userId}`,
'projectUpdate',
{
item: project,
2019-08-31 04:07:25 +05:00
},
inputs.request,
2019-08-31 04:07:25 +05:00
);
});
}
return project;
},
2019-08-31 04:07:25 +05:00
};