-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprediction.ts
59 lines (57 loc) · 1.77 KB
/
prediction.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { http } from "@google-cloud/functions-framework";
import { config } from "dotenv";
import { getFixture, getPredictionsForFixture } from "./football-api";
import { fetchCachedOrFresh } from "./utils";
config();
http("prediction", async (req, res) => {
res.header("Content-Type", "application/json");
const fixture = Number(req.query.fixture?.toString());
if (!fixture || Number.isNaN(fixture)) {
res.json({
meta: { fixture },
errors: [
{
message: "query param `fixture` must be a number",
},
],
data: null,
});
return;
}
try {
const [fixtureData, fromCache] = await fetchCachedOrFresh<
Results.FixtureApi[]
>(
`prediction-api:v2:fixture:${fixture}`,
async () => getFixture(fixture),
(data) =>
!data
? 30
: data?.[0].fixture.status.long === "Match Finished"
? 0
: data?.[0].fixture.status.short === "NS"
? 60 * 60 * 4 // 4 hours if the match has not started
: 60 * 15, // 15 minutes if the match has started
);
console.info("Fetching data", fixture, Boolean(fixtureData), fromCache);
const [predictionData] = await fetchCachedOrFresh<Results.PredictionApi[]>(
`prediction-api:v2:predictions:${fixture}`,
async () => getPredictionsForFixture(fixture),
(data) =>
fixtureData?.[0].fixture.status.long === "Match Finished"
? 0 // store in perpetuity if match is finished
: Boolean(data)
? 60 * 60 * 24
: 60 * 60, // one minute if failed, 24 hours if not
);
res.json({
errors: [],
data: { fixtureData, predictionData },
meta: { fixture },
});
} catch (e) {
res.json({
errors: [e],
});
}
});