mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Fix remaining issues with tests, and add implementation within InMemoryParkingRepository.ts
This commit is contained in:
@@ -8,7 +8,7 @@ export interface IParkingStructure extends IEntityWithTimestamp, IEntityWithId {
|
|||||||
name: string;
|
name: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IParkingStructureTimestampRecord extends IEntityWithTimestamp {
|
export interface IParkingStructureTimestampRecord {
|
||||||
timestampMs: number;
|
timestampMs: number;
|
||||||
id: string;
|
id: string;
|
||||||
spotsAvailable: number;
|
spotsAvailable: number;
|
||||||
|
|||||||
@@ -20,8 +20,33 @@ export class InMemoryParkingRepository implements ParkingGetterSetterRepository
|
|||||||
|
|
||||||
addOrUpdateParkingStructure = async (structure: IParkingStructure): Promise<void> => {
|
addOrUpdateParkingStructure = async (structure: IParkingStructure): Promise<void> => {
|
||||||
this.structures.set(structure.id, { ...structure });
|
this.structures.set(structure.id, { ...structure });
|
||||||
|
await this.addHistoricalDataForStructure(structure);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private addHistoricalDataForStructure = async (structure: IParkingStructure): Promise<void> => {
|
||||||
|
const now = Date.now();
|
||||||
|
const lastAdded = this.dataLastAdded.get(structure.id);
|
||||||
|
|
||||||
|
function parkingLoggingIntervalExceeded() {
|
||||||
|
return !lastAdded || (now - lastAdded.getTime()) >= PARKING_LOGGING_INTERVAL_MS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parkingLoggingIntervalExceeded()) {
|
||||||
|
const timestampRecord: IParkingStructureTimestampRecord = {
|
||||||
|
id: structure.id,
|
||||||
|
spotsAvailable: structure.spotsAvailable,
|
||||||
|
timestampMs: now,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!this.historicalData.has(structure.id)) {
|
||||||
|
this.historicalData.set(structure.id, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.historicalData.get(structure.id)?.push(timestampRecord);
|
||||||
|
this.dataLastAdded.set(structure.id, new Date(now));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
clearParkingStructureData = async (): Promise<void> => {
|
clearParkingStructureData = async (): Promise<void> => {
|
||||||
this.structures.clear();
|
this.structures.clear();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ describe("InMemoryParkingRepository", () => {
|
|||||||
jest.setSystemTime(now + PARKING_LOGGING_INTERVAL_MS + 60);
|
jest.setSystemTime(now + PARKING_LOGGING_INTERVAL_MS + 60);
|
||||||
await repository.addOrUpdateParkingStructure(testStructure);
|
await repository.addOrUpdateParkingStructure(testStructure);
|
||||||
|
|
||||||
expect(historicalData.get(testStructure.id)).toContain(expectedTimestampRecordMatcher);
|
expect(historicalData.get(testStructure.id)).toContainEqual(expectedTimestampRecordMatcher);
|
||||||
expect(historicalData.get(testStructure.id)).toContain({
|
expect(historicalData.get(testStructure.id)).toContainEqual({
|
||||||
...expectedTimestampRecordMatcher,
|
...expectedTimestampRecordMatcher,
|
||||||
timestampMs: now + PARKING_LOGGING_INTERVAL_MS + 60,
|
timestampMs: now + PARKING_LOGGING_INTERVAL_MS + 60,
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user