-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecalculate.ts
81 lines (65 loc) · 2.02 KB
/
recalculate.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { createClient } from "@supabase/supabase-js";
import { getAccessToken } from "./osuRequests";
import { Database } from "./database.types";
import {
adjustAllRankDates,
getFormattedMapsFromDatabase,
getUpdatedMaps,
storeMapProperties,
} from "./osuHelpers";
import { Redis } from "@upstash/redis";
require("dotenv").config();
const supabase = createClient<Database>(
process.env.SUPABASE_URL!,
process.env.SERVICE_ROLE!,
);
const recalculate = async () => {
const { data: res, error } = await supabase.from("app_data").select("*");
if (!res || error) throw new Error(`failed to get app_data. Error: ${error}`);
const appData = res[0];
let accessToken = appData.access_token;
let expireDate = appData.expire_date;
if (accessToken === null || Date.now() >= expireDate) {
[accessToken, expireDate] = await getAccessToken();
const { error } = await supabase
.from("app_data")
.update(
{
access_token: accessToken,
expire_date: expireDate,
},
)
.eq("id", 1);
if (error) console.log(error);
}
const { qualifiedMaps, rankedMaps, qualifiedData } =
await getFormattedMapsFromDatabase(supabase);
const previousData = storeMapProperties(qualifiedData);
adjustAllRankDates(qualifiedMaps, rankedMaps);
const { mapsToUpdate, updatedMapIds } = getUpdatedMaps(
qualifiedMaps,
previousData,
);
console.log(updatedMapIds);
const { error: errorBeatmapSets } = await supabase.from("beatmapsets").upsert(
mapsToUpdate,
);
if (errorBeatmapSets) console.log(errorBeatmapSets);
if (updatedMapIds.length > 0) {
const redis = Redis.fromEnv();
const timestamp = Date.now();
redis.set(`updates-${timestamp}`, JSON.stringify(mapsToUpdate), { ex: 60 });
const { error } = await supabase
.from("updates")
.upsert({
id: 1,
timestamp,
updated_maps: updatedMapIds,
deleted_maps: [],
});
if (error) console.log(error);
}
};
if (require.main === module) {
recalculate();
}