Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusab committed Nov 24, 2024
1 parent 046b762 commit da823ca
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 20 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/preview-dashboard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
4 changes: 2 additions & 2 deletions apps/dashboard/jobs/bank/scheduler/bank-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
19 changes: 11 additions & 8 deletions apps/dashboard/jobs/bank/setup/initial.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
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
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,
});
},
});
3 changes: 2 additions & 1 deletion apps/dashboard/jobs/bank/sync/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 5 additions & 1 deletion apps/dashboard/jobs/bank/sync/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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" })
Expand Down
1 change: 0 additions & 1 deletion apps/dashboard/jobs/bank/sync/manual.ts

This file was deleted.

13 changes: 13 additions & 0 deletions apps/dashboard/jobs/bank/sync/provider.ts
Original file line number Diff line number Diff line change
@@ -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
},
});
10 changes: 9 additions & 1 deletion apps/dashboard/jobs/bank/sync/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit da823ca

Please sign in to comment.