-
Notifications
You must be signed in to change notification settings - Fork 2
/
tenants.service.ts
37 lines (32 loc) · 1.06 KB
/
tenants.service.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
import { Injectable, InternalServerErrorException, Logger } from "@nestjs/common";
import { IDb } from "./interfaces/connection.interface";
import { RequestContext } from "./models/request-context.model";
const MockConnection: IDb = {
master: {
name: 'master-connection',
repositories: {
tenants: [{ tenantId: 'foo', name: 'bar', host: 'localhost:3003' }],
},
},
bar: {
name: 'tenant-connection',
repositories: {
users: [{ id: 'user-123', firstName: 'first', lastName: 'last', email: 'first-last@mail.com' }],
},
},
};
@Injectable()
export class TenantsService {
public getRepository(name: string) {
const ctx = this.currentRequest; // <-- UNDEFINED
const tenant = MockConnection.master.repositories.tenants.find(t => t.host === ctx?.tenantName);
if (!tenant) {
throw new InternalServerErrorException('Tenant not found');
}
return MockConnection[tenant].repositories[name];
}
private get currentRequest() {
const requestContext = RequestContext.currentContext;
return requestContext;
}
}