This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
WfmRestApi.ts
54 lines (48 loc) · 1.74 KB
/
WfmRestApi.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
/**
* @module @raincatcher/wfm-rest-api
*/
import { getLogger } from '@raincatcher/logger';
import { WorkFlow, WorkOrder } from '@raincatcher/wfm';
import * as Promise from 'bluebird';
import * as express from 'express';
import * as _ from 'lodash';
import { Db } from 'mongodb';
import { ApiConfig, defaultConfiguration } from './ApiConfig';
import { ApiController } from './impl/ApiController';
import { MongoDbRepository } from './impl/MongoDbRepository';
/**
* RESTfull api handlers for Workorders, Workflows and Results (WFM objects)
*/
export class WfmRestApi {
private config: ApiConfig;
private workorderService: MongoDbRepository<WorkOrder>;
private workflowService: MongoDbRepository<WorkFlow>;
constructor(userConfig?: Partial<ApiConfig>) {
this.config = _.defaults(defaultConfiguration, userConfig);
this.createWFMServices();
}
/**
* Create new router for hosting WFM http api.
*/
public createWFMRouter() {
const router: express.Router = express.Router();
const { workorderApiName, workflowApiName, resultApiName } = this.config;
getLogger().info('WFM web api initialization');
router.use(`/${workorderApiName}`, new ApiController<WorkOrder>(this.workorderService).buildRouter());
router.use(`/${workflowApiName}`, new ApiController<WorkFlow>(this.workflowService).buildRouter());
return router;
}
/**
* Inject database connection to services
*
* @param db - mongodb driver
*/
public setDb(db: Db) {
this.workorderService.setDb(db);
this.workflowService.setDb(db);
}
protected createWFMServices() {
this.workorderService = new MongoDbRepository(this.config.workorderCollectionName);
this.workflowService = new MongoDbRepository(this.config.workflowCollectionName);
}
}