2018-03-07 11:40:40 +09:00
|
|
|
import * as mongo from 'mongodb';
|
2018-03-07 17:48:32 +09:00
|
|
|
import deepcopy = require('deepcopy');
|
2018-03-29 01:20:40 +09:00
|
|
|
import db from '../../../db/mongodb';
|
2018-03-07 17:48:32 +09:00
|
|
|
import { IUser, pack as packUser } from './user';
|
2018-03-07 11:40:40 +09:00
|
|
|
|
|
|
|
const Matching = db.get<IMatching>('othello_matchings');
|
|
|
|
export default Matching;
|
|
|
|
|
|
|
|
export interface IMatching {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-07 17:48:32 +09:00
|
|
|
created_at: Date;
|
2018-03-07 11:40:40 +09:00
|
|
|
parent_id: mongo.ObjectID;
|
|
|
|
child_id: mongo.ObjectID;
|
|
|
|
}
|
2018-03-07 17:48:32 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack an othello matching for API response
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
matching: any,
|
|
|
|
me?: string | mongo.ObjectID | IUser
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
|
|
|
|
// Me
|
|
|
|
const meId: mongo.ObjectID = me
|
|
|
|
? mongo.ObjectID.prototype.isPrototypeOf(me)
|
|
|
|
? me as mongo.ObjectID
|
|
|
|
: typeof me === 'string'
|
|
|
|
? new mongo.ObjectID(me)
|
|
|
|
: (me as IUser)._id
|
|
|
|
: null;
|
|
|
|
|
|
|
|
const _matching = deepcopy(matching);
|
|
|
|
|
2018-03-11 18:08:26 +09:00
|
|
|
// Rename _id to id
|
|
|
|
_matching.id = _matching._id;
|
2018-03-07 17:48:32 +09:00
|
|
|
delete _matching._id;
|
|
|
|
|
|
|
|
// Populate user
|
|
|
|
_matching.parent = await packUser(_matching.parent_id, meId);
|
|
|
|
_matching.child = await packUser(_matching.child_id, meId);
|
|
|
|
|
|
|
|
resolve(_matching);
|
|
|
|
});
|