summaryrefslogtreecommitdiff
path: root/src/api/endpoints/i/appdata/set.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-03-03 08:56:07 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-03-03 08:56:07 +0900
commitdc45055f2ffcea2369f12d104999746220b22c90 (patch)
treed3ec8f4a03076fe804c906cd60656e197f3010f2 /src/api/endpoints/i/appdata/set.ts
parentwip (diff)
downloadsharkey-dc45055f2ffcea2369f12d104999746220b22c90.tar.gz
sharkey-dc45055f2ffcea2369f12d104999746220b22c90.tar.bz2
sharkey-dc45055f2ffcea2369f12d104999746220b22c90.zip
wip
Diffstat (limited to 'src/api/endpoints/i/appdata/set.ts')
-rw-r--r--src/api/endpoints/i/appdata/set.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts
new file mode 100644
index 0000000000..57001f4e8b
--- /dev/null
+++ b/src/api/endpoints/i/appdata/set.ts
@@ -0,0 +1,61 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import Appdata from '../../../models/appdata';
+import User from '../../../models/user';
+import serialize from '../../../serializers/user';
+import event from '../../../event';
+
+/**
+ * Set app data
+ *
+ * @param {any} params
+ * @param {any} user
+ * @param {any} app
+ * @param {Boolean} isSecure
+ * @return {Promise<any>}
+ */
+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) {
+ const _user = await User.findOneAndUpdate(user._id, {
+ $set: {
+ data: Object.assign(user.data || {}, JSON.parse(data))
+ }
+ });
+ res(204);
+
+ // Publish i updated event
+ event(user._id, 'i_updated', await serialize(_user, user, {
+ detail: true,
+ includeSecrets: true
+ }));
+ } else {
+ const appdata = await Appdata.findOne({
+ app_id: app._id,
+ user_id: user._id
+ });
+ await Appdata.update({
+ app_id: app._id,
+ user_id: user._id
+ }, Object.assign({
+ app_id: app._id,
+ user_id: user._id
+ }, {
+ $set: {
+ data: Object.assign((appdata || {}).data || {}, JSON.parse(data))
+ }
+ }), {
+ upsert: true
+ });
+ res(204);
+ }
+});