Sharkey/src/api/endpoints/i/appdata/get.ts

51 lines
954 B
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
/**
* Module dependencies
*/
import $ from 'cafy';
2016-12-28 23:49:51 +01:00
import Appdata from '../../../models/appdata';
/**
* Get 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
*/
2017-03-03 20:28:38 +01:00
module.exports = (params, user, app, isSecure) => new Promise(async (res, rej) => {
2016-12-28 23:49:51 +01:00
// Get 'key' parameter
const [key = null, keyError] = $(params.key).optional.nullable.string().match(/[a-z_]+/).$;
if (keyError) return rej('invalid key param');
2016-12-28 23:49:51 +01:00
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) {
2017-04-14 13:45:37 +02:00
select[`data.${key}`] = true;
2016-12-28 23:49:51 +01:00
}
const appdata = await Appdata.findOne({
app_id: app._id,
user_id: user._id
}, {
2017-04-14 13:45:37 +02:00
fields: select
});
2016-12-28 23:49:51 +01:00
if (appdata) {
res(appdata.data);
} else {
res();
}
}
});