-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts
This file was deleted.
Oops, something went wrong.