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;
|
||||
}
|
||||
|
||||
export interface IParkingStructureTimestampRecord extends IEntityWithTimestamp {
|
||||
export interface IParkingStructureTimestampRecord {
|
||||
timestampMs: number;
|
||||
id: string;
|
||||
spotsAvailable: number;
|
||||
|
||||
@@ -20,8 +20,33 @@ export class InMemoryParkingRepository implements ParkingGetterSetterRepository
|
||||
|
||||
addOrUpdateParkingStructure = async (structure: IParkingStructure): Promise<void> => {
|
||||
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> => {
|
||||
this.structures.clear();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user