2016-12-28 23:49:51 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import Appdata from '../../../models/appdata';
|
|
|
|
import User from '../../../models/user';
|
2017-01-21 13:08:40 +01:00
|
|
|
import serialize from '../../../serializers/user';
|
|
|
|
import event from '../../../event';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set app data
|
|
|
|
*
|
2017-03-01 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @param {any} app
|
2016-12-28 23:49:51 +01:00
|
|
|
* @param {Boolean} isSecure
|
2017-03-01 09:37:01 +01:00
|
|
|
* @return {Promise<any>}
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
|
|
|
module.exports = (params, user, app, isSecure) =>
|
|
|
|
new Promise(async (res, rej) =>
|
|
|
|
{
|
|
|
|
const data = params.data;
|
|
|
|
if (data == null) {
|
|
|
|
return rej('data is required');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isSecure) {
|
2017-01-21 13:08:40 +01:00
|
|
|
const _user = await User.findOneAndUpdate(user._id, {
|
2016-12-28 23:49:51 +01:00
|
|
|
$set: {
|
|
|
|
data: Object.assign(user.data || {}, JSON.parse(data))
|
|
|
|
}
|
2017-01-21 13:08:40 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
res(204);
|
2017-01-21 13:08:40 +01:00
|
|
|
|
|
|
|
// Publish i updated event
|
|
|
|
event(user._id, 'i_updated', await serialize(_user, user, {
|
|
|
|
detail: true,
|
|
|
|
includeSecrets: true
|
|
|
|
}));
|
2016-12-28 23:49:51 +01:00
|
|
|
} else {
|
|
|
|
const appdata = await Appdata.findOne({
|
|
|
|
app_id: app._id,
|
|
|
|
user_id: user._id
|
|
|
|
});
|
2017-01-17 03:11:22 +01:00
|
|
|
await Appdata.update({
|
2016-12-28 23:49:51 +01:00
|
|
|
app_id: app._id,
|
|
|
|
user_id: user._id
|
|
|
|
}, Object.assign({
|
|
|
|
app_id: app._id,
|
|
|
|
user_id: user._id
|
2017-01-21 13:08:40 +01:00
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
data: Object.assign((appdata || {}).data || {}, JSON.parse(data))
|
|
|
|
}
|
|
|
|
}), {
|
2016-12-28 23:49:51 +01:00
|
|
|
upsert: true
|
|
|
|
});
|
|
|
|
res(204);
|
|
|
|
}
|
|
|
|
});
|