From aac199d275360d8aca6d6695595764f00cd07529 Mon Sep 17 00:00:00 2001 From: Pontus Abrahamsson Date: Tue, 3 Dec 2024 11:20:26 +0100 Subject: [PATCH] wip --- .../{bank-sync.ts => bank-scheduler.ts} | 0 .../jobs/tasks/bank/setup/initial.ts | 2 +- .../tasks/rates/scheduler/rates-scheduler.ts | 23 ++++++++ apps/engine/src/routes/connections/index.ts | 52 +++++++++++++++++++ apps/engine/src/routes/connections/schema.ts | 23 ++++++++ packages/jobs/src/index.ts | 1 - packages/jobs/src/rates/index.ts | 1 - packages/jobs/src/rates/update.ts | 38 -------------- 8 files changed, 99 insertions(+), 41 deletions(-) rename apps/dashboard/jobs/tasks/bank/scheduler/{bank-sync.ts => bank-scheduler.ts} (100%) create mode 100644 apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts delete mode 100644 packages/jobs/src/rates/index.ts delete mode 100644 packages/jobs/src/rates/update.ts diff --git a/apps/dashboard/jobs/tasks/bank/scheduler/bank-sync.ts b/apps/dashboard/jobs/tasks/bank/scheduler/bank-scheduler.ts similarity index 100% rename from apps/dashboard/jobs/tasks/bank/scheduler/bank-sync.ts rename to apps/dashboard/jobs/tasks/bank/scheduler/bank-scheduler.ts diff --git a/apps/dashboard/jobs/tasks/bank/setup/initial.ts b/apps/dashboard/jobs/tasks/bank/setup/initial.ts index eff075bf78..71ad8301d9 100644 --- a/apps/dashboard/jobs/tasks/bank/setup/initial.ts +++ b/apps/dashboard/jobs/tasks/bank/setup/initial.ts @@ -1,7 +1,7 @@ import { schedules, schemaTask } from "@trigger.dev/sdk/v3"; import { generateCronTag } from "jobs/utils/generate-cron-tag"; import { z } from "zod"; -import { bankSyncScheduler } from "../scheduler/bank-sync"; +import { bankSyncScheduler } from "../scheduler/bank-scheduler"; import { syncConnection } from "../sync/connection"; // This task sets up the bank sync for a new team on a daily schedule and diff --git a/apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts b/apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts new file mode 100644 index 0000000000..db703543c4 --- /dev/null +++ b/apps/dashboard/jobs/tasks/rates/scheduler/rates-scheduler.ts @@ -0,0 +1,23 @@ +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); + }, +}); diff --git a/apps/engine/src/routes/connections/index.ts b/apps/engine/src/routes/connections/index.ts index b192e8f238..af718d43c3 100644 --- a/apps/engine/src/routes/connections/index.ts +++ b/apps/engine/src/routes/connections/index.ts @@ -13,6 +13,7 @@ import { ConnectionStatusQuerySchema, ConnectionStatusSchema, DeleteConnectionBodySchema, + GoCardLessConnectionsSchema, } from "./schema"; const app = new OpenAPIHono<{ Bindings: Bindings }>() @@ -139,6 +140,57 @@ const app = new OpenAPIHono<{ Bindings: Bindings }>() } }, ) + .openapi( + createRoute({ + method: "get", + path: "/gocardless", + summary: "Get GoCardless Connections", + responses: { + 200: { + content: { + "application/json": { + schema: GoCardLessConnectionsSchema, + }, + }, + description: "Retrieve GoCardless connections", + }, + 400: { + content: { + "application/json": { + schema: ErrorSchema, + }, + }, + description: "Returns an error", + }, + }, + }), + async (c) => { + const envs = env(c); + + const api = new GoCardLessApi({ + kv: c.env.KV, + envs, + }); + + try { + const data = await api.getRequisitions(); + + return c.json( + { + count: data.count, + next: data.next, + previous: data.previous, + results: data.results, + }, + 200, + ); + } catch (error) { + const errorResponse = createErrorResponse(error, c.get("requestId")); + + return c.json(errorResponse, 400); + } + }, + ) .openapi( createRoute({ method: "get", diff --git a/apps/engine/src/routes/connections/schema.ts b/apps/engine/src/routes/connections/schema.ts index 487255062e..7d94e25853 100644 --- a/apps/engine/src/routes/connections/schema.ts +++ b/apps/engine/src/routes/connections/schema.ts @@ -89,3 +89,26 @@ export const ConnectionByReferenceSchema = z.object({ accounts: z.array(z.string()), }), }); + +export const GoCardLessConnectionSchema = z.object({ + id: z.string(), + created: z.string(), + redirect: z.string(), + status: z.enum(["CR", "GC", "UA", "RJ", "SA", "GA", "LN", "EX"]), + institution_id: z.string(), + agreement: z.string(), + reference: z.string(), + accounts: z.array(z.string()), + user_language: z.string(), + link: z.string(), + ssn: z.string(), + account_selection: z.boolean(), + redirect_immediate: z.boolean(), +}); + +export const GoCardLessConnectionsSchema = z.object({ + count: z.number(), + next: z.string(), + previous: z.string(), + results: z.array(GoCardLessConnectionSchema), +}); diff --git a/packages/jobs/src/index.ts b/packages/jobs/src/index.ts index 02b0a19b7b..486b7d7203 100644 --- a/packages/jobs/src/index.ts +++ b/packages/jobs/src/index.ts @@ -2,4 +2,3 @@ export * from "./transactions"; export * from "./inbox"; export * from "./client"; export * from "./constants"; -export * from "./rates"; diff --git a/packages/jobs/src/rates/index.ts b/packages/jobs/src/rates/index.ts deleted file mode 100644 index 635be6440d..0000000000 --- a/packages/jobs/src/rates/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./update"; diff --git a/packages/jobs/src/rates/update.ts b/packages/jobs/src/rates/update.ts deleted file mode 100644 index 1b0083cb9c..0000000000 --- a/packages/jobs/src/rates/update.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { cronTrigger } from "@trigger.dev/sdk"; -import { client, supabase } from "../client"; -import { Jobs } from "../constants"; -import { engine } from "../utils/engine"; -import { processBatch } from "../utils/process"; - -client.defineJob({ - id: Jobs.EXCHANGE_RATES_UPDATE, - name: "Exchange Rates - Update", - version: "0.1.2", - 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 io.logger.info("Updating exchange rates", { data }); - - await processBatch(data, 500, async (batch) => { - await io.supabase.client.from("exchange_rates").upsert(batch, { - onConflict: "base, target", - ignoreDuplicates: false, - }); - }); - }, -});