Skip to content

Commit 5a75bf7

Browse files
committed
add reset api endpoint
1 parent 4a8838d commit 5a75bf7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

backend/main.ts

+32
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,38 @@ router.get("/wind-history/:stationId", async (ctx) => {
271271
ctx.response.body = stream;
272272
});
273273

274+
router.delete("/reset-wind-history", authMiddleware, async (ctx) => {
275+
const entries = await kv.list<TableDataItem>({
276+
prefix: ["windHistoryData"],
277+
});
278+
const stations = await kv.list<StationStats>({
279+
prefix: ["station"],
280+
});
281+
const transaction = kv.atomic();
282+
for await (const entry of entries) {
283+
transaction.delete(entry.key);
284+
}
285+
for await (const station of stations) {
286+
transaction.set(["station", station.value.id], {
287+
...station.value,
288+
totalEntries: 0,
289+
months: {},
290+
});
291+
}
292+
293+
const result = await transaction.commit();
294+
if (result.ok) {
295+
ctx.response.body = { msg: "Deleted all wind history data" };
296+
ctx.response.status = 200;
297+
} else {
298+
ctx.response.body = {
299+
msg: "Failed to delete wind history data",
300+
result,
301+
};
302+
ctx.response.status = 500;
303+
}
304+
});
305+
274306
const app = new Application();
275307
app.use(oakCors({ origin: Deno.env.get("ORIGIN") || "*" }));
276308
app.use(router.routes());

0 commit comments

Comments
 (0)