summaryrefslogtreecommitdiff
path: root/src/api/models/othello-matching.ts
blob: 89fcd6df6aec15b7f5aecbab96b88729ca07a98d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as mongo from 'mongodb';
import deepcopy = require('deepcopy');
import db from '../../db/mongodb';
import { IUser, pack as packUser } from './user';

const Matching = db.get<IMatching>('othello_matchings');
export default Matching;

export interface IMatching {
	_id: mongo.ObjectID;
	created_at: Date;
	parent_id: mongo.ObjectID;
	child_id: mongo.ObjectID;
}

/**
 * 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);

	delete _matching._id;

	// Populate user
	_matching.parent = await packUser(_matching.parent_id, meId);
	_matching.child = await packUser(_matching.child_id, meId);

	resolve(_matching);
});