Не знаю насколько это корректно, я реализовал вот так, скорее всего можно лучше, если есть идеи, я бы почитал:
export class CallController {
private _calls?: QueryModel;
constructor(router: IRouter, route_name: string) {
this.init().then(_ => {
this.handle_http_methods_for_route(router, route_name);
});
}
public async post(req: Request, res: Response, next: NextFunction): Promise<void> {
if(req.body) {
console.log(this._calls);
const call = await this._calls?.collection.findOne({type: req.body.type, members: {$in: req.body.members}});
if(!call) {
await this._calls?.collection.insertOne(req.body);
return;
}
await this._calls?.collection.updateOne({id: req.body.id}, {time: req.body.time, expert: req.body.expert});
}
}
private handle_http_methods_for_route(router: IRouter, route_name: string) {
router.route(route_name).post(this.post.bind(this));
}
async init(): Promise<void> {
this._calls = await query('calls');
}
}