-
Notifications
You must be signed in to change notification settings - Fork 3
/
ControllerFactory.ts
45 lines (37 loc) · 2.08 KB
/
ControllerFactory.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
import { PatientController } from "./PatientController";
import { Db, MongoClient } from "mongodb";
import { ISettings } from "../Models/ISettings";
import { ICollection } from "../Services/ICollection";
import { PatientDataService } from "../Services/PatientDataService";
import { EnvironmentSettings } from "../Models/EnvironmentSettings";
import { RetryCollection } from "../Services/RetryCollection";
import { LoggingCollection } from "../Services/LoggingCollection";
import { AppInsightsService } from "../Services/app-insights/app-insights-service";
import { TraceContext, HttpRequest } from "@azure/functions";
export class ControllerFactory {
private static mongoDb: Promise<Db>;
private readonly settings: ISettings;
constructor () {
this.settings = new EnvironmentSettings();
}
public async createPatientController(functionContext: TraceContext, request: HttpRequest): Promise<PatientController> {
const appInsightsService = new AppInsightsService(functionContext, request);
const collection = await this.CreateCollection(this.settings.patientCollection, appInsightsService);
const dataService: PatientDataService = new PatientDataService(collection);
return new PatientController(dataService);
}
private async CreateCollection(collectionName: string, appInsightsService: AppInsightsService): Promise<ICollection> {
if (ControllerFactory.mongoDb == null) {
ControllerFactory.mongoDb = this.createMongoDb();
}
const mongoCollection = (await ControllerFactory.mongoDb).collection(collectionName);
const retryCollection = new RetryCollection(mongoCollection);
return new LoggingCollection(retryCollection, appInsightsService, collectionName, this.settings.patientTestDatabase);
}
private async createMongoDb(): Promise<Db> {
// connect and select database
const mongoClient = await MongoClient.connect(this.settings.mongoConnectionString,
{ useUnifiedTopology: true, useNewUrlParser: true, tlsAllowInvalidCertificates: this.settings.allowSelfSignedMongoCert });
return mongoClient.db(this.settings.patientTestDatabase);
}
}