From b0db1e711e6ca143e1a7fe6e27b70209876ecbe8 Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Tue, 3 Dec 2024 12:05:31 +0100 Subject: [PATCH] Add rates scheduler --- .../jobs/tasks/rates/rates-scheduler.ts | 42 +++++++++++++++++++ .../tasks/rates/scheduler/rates-scheduler.ts | 23 ---------- 2 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 apps/dashboard/jobs/tasks/rates/rates-scheduler.ts delete mode 100644 apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts diff --git a/apps/dashboard/jobs/tasks/rates/rates-scheduler.ts b/apps/dashboard/jobs/tasks/rates/rates-scheduler.ts new file mode 100644 index 0000000000..3a9a720f5a --- /dev/null +++ b/apps/dashboard/jobs/tasks/rates/rates-scheduler.ts @@ -0,0 +1,42 @@ +import { client } from "@midday/engine/client"; +import { createClient } from "@midday/supabase/job"; +import { logger, schedules } from "@trigger.dev/sdk/v3"; +import { processBatch } from "jobs/utils/process-batch"; + +export const ratesScheduler = schedules.task({ + id: "rates-scheduler", + cron: "0 0,12 * * *", + run: async () => { + // Only run in production (Set in Trigger.dev) + // if (process.env.TRIGGER_ENVIRONMENT !== "production") return; + + const supabase = createClient(); + + const ratesResponse = await client.rates.$get(); + + if (!ratesResponse.ok) { + logger.error("Failed to get rates"); + throw new Error("Failed to get rates"); + } + + const { data: ratesData } = await ratesResponse.json(); + + const data = ratesData.flatMap((rate) => { + return Object.entries(rate.rates).map(([target, value]) => ({ + base: rate.source, + target: target, + rate: value, + updated_at: rate.date, + })); + }); + + await processBatch(data, 500, async (batch) => { + await supabase.from("exchange_rates").upsert(batch, { + onConflict: "base, target", + ignoreDuplicates: false, + }); + + return batch; + }); + }, +}); diff --git a/apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts b/apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts deleted file mode 100644 index db703543c4..0000000000 --- a/apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { client } from "@midday/engine/client"; -import { createClient } from "@midday/supabase/job"; -import { logger, schedules } from "@trigger.dev/sdk/v3"; - -export const ratesScheduler = schedules.task({ - id: "rates-scheduler", - cron: "0 0,12 * * *", - run: async () => { - // Only run in production (Set in Trigger.dev) - if (process.env.TRIGGER_ENVIRONMENT !== "production") return; - - const ratesResponse = await client.rates.$get(); - - if (!ratesResponse.ok) { - logger.error("Failed to get rates"); - throw new Error("Failed to get rates"); - } - - const { data: ratesData } = await ratesResponse.json(); - - console.log(ratesData); - }, -});