Skip to content

Commit

Permalink
Merge pull request #211 from midday-ai/feature/fetch-rates
Browse files Browse the repository at this point in the history
Add rates sync
  • Loading branch information
pontusab authored Aug 4, 2024
2 parents d8785b2 + 8f0fc7b commit 84518a9
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@ai-sdk/openai": "^0.0.40",
"@date-fns/utc": "^1.2.0",
"@hookform/resolvers": "^3.9.0",
"@midday-ai/engine": "^0.1.0-alpha.20",
"@midday-ai/engine": "^0.1.0-alpha.22",
"@midday/events": "workspace:*",
"@midday/inbox": "workspace:*",
"@midday/jobs": "workspace:*",
Expand Down
3 changes: 3 additions & 0 deletions apps/engine/src/routes/rates/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export const RatesSchema = z
source: z.string().openapi({
example: "USD",
}),
date: z.string().openapi({
example: "2024-02-29",
}),
rates: z.record(z.string(), z.number()).openapi({
example: {
EUR: 0.925393,
Expand Down
2 changes: 2 additions & 0 deletions apps/engine/src/utils/rates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function getRates() {
)
.map((rate) => rate.value)
.map((value) => {
const date = Object.values(value).at(0);
const currency = Object.keys(value).at(1);

if (!currency) {
Expand All @@ -50,6 +51,7 @@ export async function getRates() {

return {
source: currency.toUpperCase(),
date,
rates: transformKeysToUppercase(currencyData as Record<string, number>),
};
})
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/jobs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@langchain/openai": "^0.2.5",
"@midday/documents": "workspace:*",
"@midday/import": "workspace:*",
"@midday-ai/engine": "^0.1.0-alpha.20",
"@midday-ai/engine": "^0.1.0-alpha.22",
"@trigger.dev/react": "2.3.19",
"@trigger.dev/resend": "2.3.19",
"@trigger.dev/sdk": "2.3.19",
Expand Down
1 change: 1 addition & 0 deletions packages/jobs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./inbox";
export * from "./client";
export * from "./constants";
export * from "./bank";
export * from "./rates";
1 change: 1 addition & 0 deletions packages/jobs/src/rates/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./update";
35 changes: 35 additions & 0 deletions packages/jobs/src/rates/update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { cronTrigger } from "@trigger.dev/sdk";
import { client, supabase } from "../client";
import { engine } from "../utils/engine";
import { processBatch } from "../utils/process";

client.defineJob({
id: "exchange-rates-update",
name: "Exchange Rates - Update",
version: "0.1.1",
trigger: cronTrigger({
cron: "0 12 * * *",
}),
integrations: {
supabase,
},
run: async (_, io) => {
const rates = await engine.rates.list();

const data = rates.data.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 io.supabase.client.from("exchange_rates").upsert(batch, {
onConflict: "base, target",
ignoreDuplicates: false,
});
});
},
});
2 changes: 1 addition & 1 deletion packages/jobs/src/transactions/initial-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { processBatch } from "../utils/process";
import { getClassification, transformTransaction } from "../utils/transform";
import { scheduler } from "./scheduler";

const BATCH_LIMIT = 300;
const BATCH_LIMIT = 500;

client.defineJob({
id: Jobs.TRANSACTIONS_INITIAL_SYNC,
Expand Down
2 changes: 1 addition & 1 deletion packages/jobs/src/transactions/manual-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { parseAPIError } from "../utils/error";
import { processBatch } from "../utils/process";
import { getClassification, transformTransaction } from "../utils/transform";

const BATCH_LIMIT = 300;
const BATCH_LIMIT = 500;

client.defineJob({
id: Jobs.TRANSACTIONS_MANUAL_SYNC,
Expand Down
2 changes: 2 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"API_ROUTE_SECRET",
"TELLER_CERTIFICATE",
"TELLER_CERTIFICATE_PRIVATE_KEY",
"MIDDAY_ENGINE_ENVIRONMENT",
"MIDDAY_ENGINE_API_KEY",
"PLAID_CLIENT_ID",
"PLAID_SECRET",
"GITHUB_TOKEN",
Expand Down

0 comments on commit 84518a9

Please sign in to comment.