Initial commit 🍀
This commit is contained in:
53
src/api/endpoints/i/appdata/get.js
Normal file
53
src/api/endpoints/i/appdata/get.js
Normal file
@@ -0,0 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import Appdata from '../../../models/appdata';
|
||||
|
||||
/**
|
||||
* Get app data
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @param {Object} app
|
||||
* @param {Boolean} isSecure
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
module.exports = (params, user, app, isSecure) =>
|
||||
new Promise(async (res, rej) =>
|
||||
{
|
||||
// Get 'key' parameter
|
||||
let key = params.key;
|
||||
if (key === undefined) {
|
||||
key = null;
|
||||
}
|
||||
|
||||
if (isSecure) {
|
||||
if (!user.data) {
|
||||
return res();
|
||||
}
|
||||
if (key !== null) {
|
||||
const data = {};
|
||||
data[key] = user.data[key];
|
||||
res(data);
|
||||
} else {
|
||||
res(user.data);
|
||||
}
|
||||
} else {
|
||||
const select = {};
|
||||
if (key !== null) {
|
||||
select['data.' + key] = true;
|
||||
}
|
||||
const appdata = await Appdata.findOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, select);
|
||||
|
||||
if (appdata) {
|
||||
res(appdata.data);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
}
|
||||
});
|
55
src/api/endpoints/i/appdata/set.js
Normal file
55
src/api/endpoints/i/appdata/set.js
Normal file
@@ -0,0 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies
|
||||
*/
|
||||
import Appdata from '../../../models/appdata';
|
||||
import User from '../../../models/user';
|
||||
|
||||
/**
|
||||
* Set app data
|
||||
*
|
||||
* @param {Object} params
|
||||
* @param {Object} user
|
||||
* @param {Object} app
|
||||
* @param {Boolean} isSecure
|
||||
* @return {Promise<object>}
|
||||
*/
|
||||
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 set = {
|
||||
$set: {
|
||||
data: Object.assign(user.data || {}, JSON.parse(data))
|
||||
}
|
||||
};
|
||||
await User.updateOne({ _id: user._id }, set);
|
||||
res(204);
|
||||
} else {
|
||||
const appdata = await Appdata.findOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
});
|
||||
const set = {
|
||||
$set: {
|
||||
data: Object.assign((appdata || {}).data || {}, JSON.parse(data))
|
||||
}
|
||||
};
|
||||
await Appdata.updateOne({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, Object.assign({
|
||||
app_id: app._id,
|
||||
user_id: user._id
|
||||
}, set), {
|
||||
upsert: true
|
||||
});
|
||||
res(204);
|
||||
}
|
||||
});
|
Reference in New Issue
Block a user