-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.ts
41 lines (29 loc) · 1.11 KB
/
cache.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
import { readJSON } from "fs-extra";
import { stat } from "fs/promises";
import { join } from "path";
import { equals } from 'ramda'
import { RELATIVE_STORAGE_DIRECTORY } from "./constants";
import { VaccinationCentre, RawVaccinationCentre } from "../VaccinationCentre";
export interface Cache {
getExtendedVaccinationCentre: (rawVaccinationCentre: RawVaccinationCentre) => VaccinationCentre | undefined
}
/**
* Reads a cache file so it's contents can be used
* @param filename
*/
export async function readCacheFile(filename: string): Promise<Cache> {
const fullPath = join(RELATIVE_STORAGE_DIRECTORY, filename)
let cachedCentres: VaccinationCentre[]
if (await stat(fullPath)) {
cachedCentres = await readJSON(fullPath)
}
function getExtendedVaccinationCentre(rawVaccinationCentre: RawVaccinationCentre): VaccinationCentre | undefined {
if (!Array.isArray(cachedCentres)) {
return undefined
}
return cachedCentres.find(centre => equals(centre.baseVaccinationCentre, rawVaccinationCentre))
}
return {
getExtendedVaccinationCentre
}
}