2020-05-26 00:46:04 +05:00
|
|
|
const path = require('path');
|
|
|
|
const rimraf = require('rimraf');
|
|
|
|
|
2019-08-31 04:07:25 +05:00
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
record: {
|
|
|
|
type: 'ref',
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
values: {
|
|
|
|
type: 'json',
|
2020-08-04 01:32:46 +05:00
|
|
|
custom: (value) => {
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!_.isUndefined(value.background) &&
|
|
|
|
!_.isNull(value.background) &&
|
|
|
|
!_.isPlainObject(value.background)
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_.isUndefined(value.backgroundImage) && !_.isNull(value.backgroundImage)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
required: true,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
request: {
|
2019-11-05 18:01:42 +05:00
|
|
|
type: 'ref',
|
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
|
|
|
|
2020-06-03 22:27:20 +05:00
|
|
|
exits: {
|
2020-08-04 01:32:46 +05:00
|
|
|
backgroundImageDirnameMustBeNotNullInValues: {},
|
2020-06-03 22:27:20 +05:00
|
|
|
},
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
async fn(inputs) {
|
2020-05-26 00:46:04 +05:00
|
|
|
if (!_.isUndefined(inputs.values.backgroundImage)) {
|
|
|
|
/* eslint-disable no-param-reassign */
|
|
|
|
inputs.values.backgroundImageDirname = null;
|
|
|
|
delete inputs.values.backgroundImage;
|
|
|
|
/* eslint-enable no-param-reassign */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs.values.backgroundImageDirname) {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
inputs.values.background = {
|
|
|
|
type: 'image',
|
|
|
|
};
|
2020-06-03 22:27:20 +05:00
|
|
|
} else if (
|
|
|
|
_.isNull(inputs.values.backgroundImageDirname) &&
|
|
|
|
inputs.record.background &&
|
|
|
|
inputs.record.background.type === 'image'
|
|
|
|
) {
|
|
|
|
inputs.values.background = null; // eslint-disable-line no-param-reassign
|
2020-05-26 00:46:04 +05:00
|
|
|
}
|
|
|
|
|
2020-06-03 22:27:20 +05:00
|
|
|
let project;
|
|
|
|
if (inputs.values.background && inputs.values.background.type === 'image') {
|
|
|
|
if (_.isNull(inputs.values.backgroundImageDirname)) {
|
2020-08-04 01:32:46 +05:00
|
|
|
throw 'backgroundImageDirnameMustBeNotNullInValues';
|
2020-06-03 22:27:20 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isUndefined(inputs.values.backgroundImageDirname)) {
|
|
|
|
project = await Project.updateOne({
|
|
|
|
id: inputs.record.id,
|
|
|
|
backgroundImageDirname: {
|
|
|
|
'!=': null,
|
|
|
|
},
|
|
|
|
}).set(inputs.values);
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
delete inputs.values.background; // eslint-disable-line no-param-reassign
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!project) {
|
|
|
|
project = await Project.updateOne(inputs.record.id).set(inputs.values);
|
|
|
|
}
|
2019-08-31 04:07:25 +05:00
|
|
|
|
|
|
|
if (project) {
|
2020-05-26 00:46:04 +05:00
|
|
|
if (
|
|
|
|
inputs.record.backgroundImageDirname &&
|
|
|
|
project.backgroundImageDirname !== inputs.record.backgroundImageDirname
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
rimraf.sync(
|
|
|
|
path.join(
|
|
|
|
sails.config.custom.projectBackgroundImagesPath,
|
|
|
|
inputs.record.backgroundImageDirname,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
console.warn(error.stack); // eslint-disable-line no-console
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
const userIds = await sails.helpers.projects.getManagerAndBoardMemberUserIds(project.id);
|
2019-08-31 04:07:25 +05:00
|
|
|
|
2020-04-03 00:35:25 +05:00
|
|
|
userIds.forEach((userId) => {
|
2019-08-31 04:07:25 +05:00
|
|
|
sails.sockets.broadcast(
|
|
|
|
`user:${userId}`,
|
|
|
|
'projectUpdate',
|
|
|
|
{
|
2019-11-05 18:01:42 +05:00
|
|
|
item: project,
|
2019-08-31 04:07:25 +05:00
|
|
|
},
|
2019-11-05 18:01:42 +05:00
|
|
|
inputs.request,
|
2019-08-31 04:07:25 +05:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-24 01:05:22 +05:00
|
|
|
return project;
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|