From 90f8fe7e538bb7e52d2558152a0390e693f39b11 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Thu, 29 Mar 2018 01:20:40 +0900 Subject: Introduce processor --- src/server/api/models/othello-matching.ts | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/server/api/models/othello-matching.ts (limited to 'src/server/api/models/othello-matching.ts') diff --git a/src/server/api/models/othello-matching.ts b/src/server/api/models/othello-matching.ts new file mode 100644 index 0000000000..3c29e6a00c --- /dev/null +++ b/src/server/api/models/othello-matching.ts @@ -0,0 +1,44 @@ +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('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(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); + + // Rename _id to id + _matching.id = _matching._id; + delete _matching._id; + + // Populate user + _matching.parent = await packUser(_matching.parent_id, meId); + _matching.child = await packUser(_matching.child_id, meId); + + resolve(_matching); +}); -- cgit v1.2.3-freya