2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-19 15:21:43 +09:00
|
|
|
import $ from 'cafy';
|
2016-12-29 07:49:51 +09:00
|
|
|
import Appdata from '../../../models/appdata';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set app data
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @param {any} app
|
2016-12-29 07:49:51 +09:00
|
|
|
* @param {Boolean} isSecure
|
2017-03-01 17:37:01 +09:00
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
2017-11-08 23:43:47 +09:00
|
|
|
module.exports = (params, user, app) => new Promise(async (res, rej) => {
|
|
|
|
if (app == null) return rej('このAPIはサードパーティAppからのみ利用できます');
|
|
|
|
|
2017-03-19 15:47:36 +09:00
|
|
|
// Get 'data' parameter
|
|
|
|
const [data, dataError] = $(params.data).optional.object()
|
2017-03-19 15:21:43 +09:00
|
|
|
.pipe(obj => {
|
2017-03-19 15:47:36 +09:00
|
|
|
const hasInvalidData = Object.entries(obj).some(([k, v]) =>
|
2017-10-07 03:36:46 +09:00
|
|
|
$(k).string().match(/^[a-z_]+$/).nok() && $(v).string().nok());
|
2017-03-19 15:47:36 +09:00
|
|
|
return !hasInvalidData;
|
2017-03-19 15:21:43 +09:00
|
|
|
}).$;
|
2017-03-19 15:47:36 +09:00
|
|
|
if (dataError) return rej('invalid data param');
|
2017-03-19 15:21:43 +09:00
|
|
|
|
|
|
|
// Get 'key' parameter
|
|
|
|
const [key, keyError] = $(params.key).optional.string().match(/[a-z_]+/).$;
|
|
|
|
if (keyError) return rej('invalid key param');
|
|
|
|
|
|
|
|
// Get 'value' parameter
|
|
|
|
const [value, valueError] = $(params.value).optional.string().$;
|
|
|
|
if (valueError) return rej('invalid value param');
|
|
|
|
|
2017-05-24 20:50:17 +09:00
|
|
|
const set = {};
|
2017-03-19 15:47:36 +09:00
|
|
|
if (data) {
|
|
|
|
Object.entries(data).forEach(([k, v]) => {
|
2017-04-14 20:45:37 +09:00
|
|
|
set[`data.${k}`] = v;
|
2017-03-19 15:47:36 +09:00
|
|
|
});
|
2017-03-19 15:21:43 +09:00
|
|
|
} else {
|
2017-04-14 20:45:37 +09:00
|
|
|
set[`data.${key}`] = value;
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
2017-11-08 23:43:47 +09:00
|
|
|
await Appdata.update({
|
2018-03-29 14:48:47 +09:00
|
|
|
appId: app._id,
|
|
|
|
userId: user._id
|
2017-11-08 23:43:47 +09:00
|
|
|
}, Object.assign({
|
2018-03-29 14:48:47 +09:00
|
|
|
appId: app._id,
|
|
|
|
userId: user._id
|
2017-11-08 23:43:47 +09:00
|
|
|
}, {
|
2017-03-19 15:47:36 +09:00
|
|
|
$set: set
|
2017-11-08 23:43:47 +09:00
|
|
|
}), {
|
|
|
|
upsert: true
|
2017-01-21 21:08:40 +09:00
|
|
|
});
|
2017-03-19 15:21:43 +09:00
|
|
|
|
2017-11-08 23:43:47 +09:00
|
|
|
res(204);
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|