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-05-26 00:46:04 +05:00
|
|
|
custom: (value) =>
|
|
|
|
_.isPlainObject(value) &&
|
2020-06-04 17:52:30 +05:00
|
|
|
(_.isUndefined(value.background) ||
|
|
|
|
_.isNull(value.background) ||
|
|
|
|
_.isPlainObject(value.background)) &&
|
2020-05-26 00:46:04 +05:00
|
|
|
(_.isUndefined(value.backgroundImage) || _.isNull(value.backgroundImage)),
|
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: {
|
|
|
|
invalidParams: {},
|
|
|
|
},
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
async fn(inputs, exits) {
|
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)) {
|
|
|
|
throw 'invalidParams';
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 18:01:42 +05:00
|
|
|
const userIds = await sails.helpers.getMembershipUserIdsForProject(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
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return exits.success(project);
|
2019-11-05 18:01:42 +05:00
|
|
|
},
|
2019-08-31 04:07:25 +05:00
|
|
|
};
|