-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from midday-ai/feature/connect-bank-v2
Feature/connect bank v2
- Loading branch information
Showing
24 changed files
with
587 additions
and
319 deletions.
There are no files selected for viewing
97 changes: 30 additions & 67 deletions
97
apps/dashboard/src/actions/connect-bank-account-action.ts
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 |
---|---|---|
@@ -1,90 +1,53 @@ | ||
"use server"; | ||
|
||
import { processPromisesBatch } from "@/utils/process"; | ||
import { LogEvents } from "@midday/events/events"; | ||
import { logsnag } from "@midday/events/server"; | ||
import { getTransactions, transformTransactions } from "@midday/gocardless"; | ||
import { scheduler } from "@midday/jobs"; | ||
import { Events, client } from "@midday/jobs"; | ||
import { getUser } from "@midday/supabase/cached-queries"; | ||
import { createBankAccounts } from "@midday/supabase/mutations"; | ||
import { createClient } from "@midday/supabase/server"; | ||
import { revalidateTag } from "next/cache"; | ||
import { action } from "./safe-action"; | ||
import { connectBankAccountSchema } from "./schema"; | ||
|
||
const BATCH_LIMIT = 500; | ||
|
||
export const connectBankAccountAction = action( | ||
connectBankAccountSchema, | ||
async (accounts) => { | ||
async ({ provider, accounts }) => { | ||
const user = await getUser(); | ||
const supabase = createClient(); | ||
const teamId = user.data.team_id; | ||
|
||
const { data } = await createBankAccounts(supabase, accounts); | ||
|
||
const promises = data?.map(async (account) => { | ||
// Fetch transactions for each account | ||
const { transactions } = await getTransactions({ | ||
accountId: account.account_id, | ||
}); | ||
try { | ||
const { data } = await createBankAccounts( | ||
supabase, | ||
accounts.map((account) => ({ | ||
...account, | ||
provider, | ||
})) | ||
); | ||
|
||
// Schedule sync for each account | ||
await scheduler.register(account.id, { | ||
type: "interval", | ||
options: { | ||
seconds: 3600, // every 1h | ||
const event = await client.sendEvent({ | ||
name: Events.TRANSACTIONS_SETUP, | ||
payload: { | ||
teamId: user.data.team_id, | ||
provider, | ||
accounts: data.map((account) => ({ | ||
id: account.id, | ||
account_id: account.account_id, | ||
})), | ||
}, | ||
}); | ||
|
||
// Update bank account last_accessed | ||
await supabase | ||
.from("bank_accounts") | ||
.update({ | ||
last_accessed: new Date().toISOString(), | ||
}) | ||
.eq("id", account.id); | ||
|
||
const formattedTransactions = transformTransactions( | ||
transactions?.booked, | ||
{ | ||
accountId: account.id, // Bank account row id | ||
teamId, | ||
} | ||
); | ||
|
||
// NOTE: We will get all the transactions at once so | ||
// we need to guard against massive payloads | ||
await processPromisesBatch( | ||
formattedTransactions, | ||
BATCH_LIMIT, | ||
async (batch) => { | ||
await supabase.from("transactions").upsert(batch, { | ||
onConflict: "internal_id", | ||
ignoreDuplicates: true, | ||
}); | ||
} | ||
); | ||
|
||
return; | ||
}); | ||
|
||
await Promise.all(promises); | ||
|
||
revalidateTag(`bank_connections_${teamId}`); | ||
revalidateTag(`transactions_${teamId}`); | ||
revalidateTag(`spending_${teamId}`); | ||
revalidateTag(`metrics_${teamId}`); | ||
revalidateTag(`bank_accounts_${teamId}`); | ||
revalidateTag(`insights_${teamId}`); | ||
logsnag.track({ | ||
event: LogEvents.ConnectBankCompleted.name, | ||
icon: LogEvents.ConnectBankCompleted.icon, | ||
user_id: user.data.email, | ||
channel: LogEvents.ConnectBankCompleted.channel, | ||
}); | ||
|
||
logsnag.track({ | ||
event: LogEvents.ConnectBankCompleted.name, | ||
icon: LogEvents.ConnectBankCompleted.icon, | ||
user_id: user.data.email, | ||
channel: LogEvents.ConnectBankCompleted.channel, | ||
}); | ||
return event; | ||
} catch (err) { | ||
console.log(err); | ||
|
||
return; | ||
throw new Error("Something went wrong"); | ||
} | ||
} | ||
); |
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
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
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
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
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
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
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
37 changes: 37 additions & 0 deletions
37
apps/dashboard/src/components/loading-transactions-event.tsx
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,37 @@ | ||
"use client"; | ||
|
||
import { Button } from "@midday/ui/button"; | ||
import { useEventDetails } from "@trigger.dev/react"; | ||
import { Loader2 } from "lucide-react"; | ||
import { useEffect } from "react"; | ||
|
||
export function LoadingTransactionsEvent({ | ||
eventId, | ||
setEventId, | ||
onClose, | ||
}: { | ||
eventId: string; | ||
}) { | ||
const { data } = useEventDetails(eventId); | ||
const firstRun = data?.runs?.at(0); | ||
|
||
useEffect(() => { | ||
if (firstRun?.status === "SUCCESS") { | ||
onClose(); | ||
} | ||
}, [firstRun]); | ||
|
||
if (firstRun?.status === "FAILURE") { | ||
return ( | ||
<Button onClick={() => setEventId(undefined)} className="w-full"> | ||
Try again | ||
</Button> | ||
); | ||
} | ||
|
||
return ( | ||
<Button disabled className="w-full"> | ||
<Loader2 className="w-4 h-4 animate-spin pointer-events-none" /> | ||
</Button> | ||
); | ||
} |
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
Oops, something went wrong.