jormungandr-bite/src/api/models/auth-session.ts

46 lines
872 B
TypeScript
Raw Normal View History

2018-02-01 16:06:01 -07:00
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
2017-01-16 17:12:33 -07:00
import db from '../../db/mongodb';
2018-02-01 16:06:01 -07:00
import { pack as packApp } from './app';
2017-01-16 17:12:33 -07:00
2018-02-01 16:06:01 -07:00
const AuthSession = db.get('auth_sessions');
export default AuthSession;
export interface IAuthSession {
_id: mongo.ObjectID;
}
/**
* Pack an auth session for API response
*
* @param {any} session
* @param {any} me?
* @return {Promise<any>}
*/
export const pack = (
session: any,
me?: any
) => new Promise<any>(async (resolve, reject) => {
let _session: any;
// TODO: Populate session if it ID
_session = deepcopy(session);
// Me
if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) {
if (typeof me === 'string') {
me = new mongo.ObjectID(me);
} else {
me = me._id;
}
}
delete _session._id;
// Populate app
_session.app = await packApp(_session.app_id, me);
resolve(_session);
});