Skip to content

Commit

Permalink
Add rates scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Dec 3, 2024
1 parent aac199d commit b0db1e7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
42 changes: 42 additions & 0 deletions apps/dashboard/jobs/tasks/rates/rates-scheduler.ts
Original file line number Diff line number Diff line change
@@ -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;
});
},
});
23 changes: 0 additions & 23 deletions apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts

This file was deleted.

0 comments on commit b0db1e7

Please sign in to comment.