diff --git a/.github/workflows/preview-dashboard.yaml b/.github/workflows/preview-dashboard.yaml index a467763d65..16157b813f 100644 --- a/.github/workflows/preview-dashboard.yaml +++ b/.github/workflows/preview-dashboard.yaml @@ -35,11 +35,11 @@ jobs: run: bunx vercel pull --yes --environment=preview --token=${{ secrets.VERCEL_TOKEN }} - name: 🏗 Build Project Artifacts run: bunx vercel build --token=${{ secrets.VERCEL_TOKEN }} - # - name: 🔄 Deploy Background Jobs - # env: - # TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_ACCESS_TOKEN }} - # run: | - # TRIGGER_PROJECT_ID=${{ secrets.TRIGGER_PROJECT_ID }} bunx trigger.dev@latest deploy --env staging - # working-directory: apps/dashboard + - name: 🔄 Deploy Background Jobs + env: + TRIGGER_ACCESS_TOKEN: ${{ secrets.TRIGGER_ACCESS_TOKEN }} + run: | + TRIGGER_PROJECT_ID=${{ secrets.TRIGGER_PROJECT_ID }} bunx trigger.dev@latest deploy --env staging + working-directory: apps/dashboard - name: 🚀 Deploy to Vercel run: bunx vercel deploy --prebuilt --archive=tgz --token=${{ secrets.VERCEL_TOKEN }} diff --git a/apps/dashboard/jobs/bank/scheduler/bank-sync.ts b/apps/dashboard/jobs/bank/scheduler/bank-sync.ts index c69e13ccea..eecebbf595 100644 --- a/apps/dashboard/jobs/bank/scheduler/bank-sync.ts +++ b/apps/dashboard/jobs/bank/scheduler/bank-sync.ts @@ -3,8 +3,8 @@ import { schedules } from "@trigger.dev/sdk/v3"; import { syncConnection } from "../sync/connection"; // This is a fan-out pattern. We want to trigger a task for each bank connection. -export const scheduleBankSync = schedules.task({ - id: "schedule-bank-sync", +export const bankSyncScheduler = schedules.task({ + id: "bank-sync-scheduler", run: async (payload) => { const supabase = createClient(); diff --git a/apps/dashboard/jobs/bank/setup/initial.ts b/apps/dashboard/jobs/bank/setup/initial.ts index 58bc518e29..4eb1036ac9 100644 --- a/apps/dashboard/jobs/bank/setup/initial.ts +++ b/apps/dashboard/jobs/bank/setup/initial.ts @@ -1,7 +1,8 @@ import { schedules, schemaTask } from "@trigger.dev/sdk/v3"; import { generateCron } from "jobs/utils/generate-cron"; import { z } from "zod"; -import { scheduleBankSync } from "../scheduler/bank-sync"; +import { bankSyncScheduler } from "../scheduler/bank-sync"; +import { syncConnection } from "../sync/connection"; // This task sets up the bank sync for a new team on a daily schedule and // runs the initial sync for transactions and balance @@ -9,27 +10,29 @@ export const initialBankSetup = schemaTask({ id: "initial-bank-setup", schema: z.object({ teamId: z.string().uuid(), + connectionId: z.string().uuid(), }), maxDuration: 600, queue: { concurrencyLimit: 20, }, run: async (payload) => { - const { teamId } = payload; + const { teamId, connectionId } = payload; // Schedule the bank sync task to run daily at a random time to distribute load // Use a deduplication key to prevent duplicate schedules for the same team - // Add teamId as externalId to use it in the scheduleBankSync task + // Add teamId as externalId to use it in the bankSyncScheduler task await schedules.create({ - task: scheduleBankSync.id, + task: bankSyncScheduler.id, cron: generateCron(teamId), timezone: "UTC", externalId: teamId, - deduplicationKey: `${teamId}-${scheduleBankSync.id}`, + deduplicationKey: `${teamId}-${bankSyncScheduler.id}`, }); - // Run manual sync - // --> Transactions - // --> Balance + // Run initial sync for transactions and balance for the connection + await syncConnection.trigger({ + connectionId, + }); }, }); diff --git a/apps/dashboard/jobs/bank/sync/balance.ts b/apps/dashboard/jobs/bank/sync/balance.ts index d32020de2c..517de5ec0d 100644 --- a/apps/dashboard/jobs/bank/sync/balance.ts +++ b/apps/dashboard/jobs/bank/sync/balance.ts @@ -13,8 +13,9 @@ export const syncBalance = schemaTask({ accountId: z.string(), accessToken: z.string().optional(), provider: z.enum(["gocardless", "plaid", "teller"]), + manual: z.boolean().optional(), }), - run: async ({ accountId, accessToken, provider }) => { + run: async ({ accountId, accessToken, provider, manual }) => { const supabase = createClient(); try { diff --git a/apps/dashboard/jobs/bank/sync/connection.ts b/apps/dashboard/jobs/bank/sync/connection.ts index bb2cbe1550..8441274944 100644 --- a/apps/dashboard/jobs/bank/sync/connection.ts +++ b/apps/dashboard/jobs/bank/sync/connection.ts @@ -24,6 +24,7 @@ export const syncConnection = schemaTask({ }); if (!connectionResponse.ok) { + logger.error("Failed to get connection status", { connectionId }); throw new Error("Failed to get connection status"); } @@ -47,11 +48,12 @@ export const syncConnection = schemaTask({ .eq("manual", false); if (bankAccountsError) { + logger.error("Failed to get bank accounts", { connectionId }); throw new Error("Failed to get bank accounts"); } if (!bankAccountsData) { - logger.info("No bank accounts found"); + logger.info("No bank accounts found", { connectionId }); return; } @@ -88,6 +90,8 @@ export const syncConnection = schemaTask({ } if (connectionData.status === "disconnected") { + logger.info("Connection disconnected", { connectionId }); + await supabase .from("bank_connections") .update({ status: "disconnected" }) diff --git a/apps/dashboard/jobs/bank/sync/manual.ts b/apps/dashboard/jobs/bank/sync/manual.ts deleted file mode 100644 index 8b13789179..0000000000 --- a/apps/dashboard/jobs/bank/sync/manual.ts +++ /dev/null @@ -1 +0,0 @@ - diff --git a/apps/dashboard/jobs/bank/sync/provider.ts b/apps/dashboard/jobs/bank/sync/provider.ts index e69de29bb2..c59f1fe1ba 100644 --- a/apps/dashboard/jobs/bank/sync/provider.ts +++ b/apps/dashboard/jobs/bank/sync/provider.ts @@ -0,0 +1,13 @@ +import { schemaTask } from "@trigger.dev/sdk/v3"; +import { z } from "zod"; + +// This task trigger a sync for a specific provider +export const syncProvider = schemaTask({ + id: "sync-provider", + schema: z.object({ + provider: z.enum(["gocardless", "plaid", "teller"]), + }), + run: async ({ provider }) => { + // TODO: Implement + }, +}); diff --git a/apps/dashboard/jobs/bank/sync/transactions.ts b/apps/dashboard/jobs/bank/sync/transactions.ts index 0780f8cb18..cba897c7d9 100644 --- a/apps/dashboard/jobs/bank/sync/transactions.ts +++ b/apps/dashboard/jobs/bank/sync/transactions.ts @@ -22,8 +22,16 @@ export const syncTransactions = schemaTask({ ]), accessToken: z.string().optional(), provider: z.enum(["gocardless", "plaid", "teller"]), + manual: z.boolean().optional(), }), - run: async ({ teamId, accountId, accountType, accessToken, provider }) => { + run: async ({ + teamId, + accountId, + accountType, + accessToken, + provider, + manual, + }) => { const supabase = createClient(); const classification = getClassification(accountType);