Sharkey/src/api/endpoints/i/authorized_apps.js

60 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-02-08 17:11:16 +02:00
'use strict';
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import AccessToken from '../../models/access-token';
import serialize from '../../serializers/app';
/**
* Get authorized apps of my account
*
2017-03-01 10:37:01 +02:00
* @param {any} params
* @param {any} user
* @return {Promise<any>}
2017-02-08 17:11:16 +02:00
*/
module.exports = (params, user) =>
new Promise(async (res, rej) =>
{
// Get 'limit' parameter
let limit = params.limit;
if (limit !== undefined && limit !== null) {
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get 'offset' parameter
let offset = params.offset;
if (offset !== undefined && offset !== null) {
offset = parseInt(offset, 10);
} else {
offset = 0;
}
// Get 'sort' parameter
let sort = params.sort || 'desc';
// Get tokens
const tokens = await AccessToken
.find({
user_id: user._id
}, {
limit: limit,
skip: offset,
sort: {
_id: sort == 'asc' ? 1 : -1
}
});
// Serialize
res(await Promise.all(tokens.map(async token =>
await serialize(token.app_id))));
});