Skip to content

Commit 889337f

Browse files
Galeaf11kostysh
authored andcommitted
feat(lpms-server): add search space service and
Availability db FacilityRepository SpaceAvailabilityRepository
1 parent 6467148 commit 889337f

File tree

5 files changed

+205
-475
lines changed

5 files changed

+205
-475
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import DBService from '../services/DBService';
2+
import { Level } from 'level';
3+
import { Facility } from '../proto/facility';
4+
5+
export class FacilityRepository {
6+
private dbService: DBService;
7+
private db: Level<string, string | string[]>;
8+
9+
constructor() {
10+
this.dbService = DBService.getInstance();
11+
this.db = DBService.getInstance().getDB();
12+
}
13+
14+
public async getFacilityIds(): Promise<string[]> {
15+
try {
16+
return await this.db.get<string, string[]>(
17+
'facilities',
18+
{ valueEncoding: 'json' }
19+
);
20+
} catch (e) {
21+
if (e.status !== 404) {
22+
throw e;
23+
}
24+
}
25+
return [];
26+
}
27+
28+
public async createFacility(facilityId: string, facility: Facility): Promise<void> {
29+
const facilitySublevel = this.dbService.getFacilitySublevelDB(facilityId);
30+
31+
await facilitySublevel.put('metadata', facility);
32+
33+
await this.createFacilityIndex(facilityId);
34+
}
35+
36+
private async createFacilityIndex(facilityId: string) {
37+
const facilitiesIds = await this.getFacilityIds();
38+
39+
if (facilitiesIds.length > 0) {
40+
const idsSet = new Set<string>(facilitiesIds);
41+
idsSet.add(facilityId);
42+
await this.db.put('facilities', Array.from(idsSet));
43+
} else {
44+
await this.db.put('facilities', [facilityId]);
45+
}
46+
}
47+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import DBService, { DateType, FacilityItemValues, LevelDefaultTyping } from '../services/DBService';
2+
import { AbstractLevel, AbstractSublevel } from 'abstract-level';
3+
import { Availability } from '../proto/lpms';
4+
5+
export class SpaceAvailabilityRepository {
6+
private dbService: DBService;
7+
private availableDB: AbstractSublevel<AbstractLevel<LevelDefaultTyping, string, FacilityItemValues>, LevelDefaultTyping, "default" | DateType, Availability>;
8+
9+
constructor(facilityId, spaceId) {
10+
this.dbService = DBService.getInstance();
11+
this.availableDB = this.dbService.getSpaceAvailabilityDB(facilityId, spaceId);
12+
}
13+
14+
public async getSpaceAvailabilityNumSpaces(key): Promise<number> {
15+
try {
16+
const availability: Availability = await this.availableDB.get(key);
17+
return availability.numSpaces;
18+
} catch (e) {
19+
if (e.status !== 404) {
20+
throw e;
21+
}
22+
}
23+
24+
return 0;
25+
}
26+
27+
public async createAvailabilityByDate(key: DateType): Promise<void> {
28+
const count = await this.getSpaceAvailabilityNumSpaces(key);
29+
30+
const availability: Availability = {
31+
numSpaces: count + 1
32+
};
33+
34+
await this.availableDB.put(key, availability);
35+
}
36+
}

src/services/DBService.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import {
88
DayOfWeekRateModifer,
99
LOSRateModifier,
1010
NoticeRequiredRule,
11-
OccupancyRateModifier,
12-
Rates
11+
OccupancyRateModifier
1312
} from '../proto/lpms';
14-
import { Bytes } from 'ethers';
15-
import { Person } from '@windingtree/stays-models/dist/cjs/proto/person';
1613

1714
export type LevelDefaultTyping = string | Buffer | Uint8Array;
1815
export type DBLevel = Level<string, string | string[]>;
@@ -31,6 +28,7 @@ export type FacilityLevelValues = Facility | string[];
3128
export type FacilitySpaceLevelValues = Item | Space;
3229
export type FacilityItemType = 'spaces' | 'otherItems';
3330
export type FacilityItemValues = Item | FacilitySpaceLevelValues;
31+
export type DateType = `${number}-${number}-${number}`
3432

3533
export default class DBService {
3634
protected db: DBLevel;
@@ -114,4 +112,11 @@ export default class DBService {
114112
FacilityItemValues
115113
>(key, { valueEncoding: 'json' });
116114
}
115+
116+
public getSpaceAvailabilityDB(facilityId: string, itemId: string) {
117+
return this.getFacilityItemDB(facilityId, 'spaces', itemId).sublevel<'default' | DateType, Availability>(
118+
'availability',
119+
{ valueEncoding: 'json' }
120+
);
121+
}
117122
}

0 commit comments

Comments
 (0)