-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
133 additions
and
87 deletions.
There are no files selected for viewing
42 changes: 0 additions & 42 deletions
42
apps/dashboard/jobs/tasks/bank/accounts/update-reference.ts
This file was deleted.
Oops, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { client } from "@midday/engine/client"; | ||
import { createClient } from "@midday/supabase/job"; | ||
import { schemaTask } from "@trigger.dev/sdk/v3"; | ||
import { syncConnection } from "jobs/tasks/bank/sync/connection"; | ||
import { z } from "zod"; | ||
|
||
export const reconnectConnection = schemaTask({ | ||
id: "reconnect-connection", | ||
maxDuration: 1000, | ||
retry: { | ||
maxAttempts: 2, | ||
}, | ||
schema: z.object({ | ||
teamId: z.string().uuid(), | ||
connectionId: z.string().uuid(), | ||
provider: z.string(), | ||
}), | ||
run: async ({ teamId, connectionId, provider }) => { | ||
const supabase = createClient(); | ||
|
||
if (provider === "gocardless") { | ||
// We need to update the reference of the connection | ||
const connection = await client.connections[":reference"].$get({ | ||
param: { reference: teamId }, | ||
}); | ||
|
||
const connectionResponse = await connection.json(); | ||
const referenceId = connectionResponse?.data.id; | ||
|
||
// Update the reference_id of the new connection | ||
if (referenceId) { | ||
await supabase | ||
.from("bank_connections") | ||
.update({ | ||
reference_id: referenceId, | ||
}) | ||
.eq("id", connectionId); | ||
} | ||
|
||
// The account_ids can be different between the old and new connection | ||
// So we need to check for account_reference and update | ||
const accounts = await client.accounts.$get({ | ||
query: { | ||
id: referenceId, | ||
provider: "gocardless", | ||
}, | ||
}); | ||
|
||
const accountsResponse = await accounts.json(); | ||
|
||
await Promise.all( | ||
accountsResponse.data.map(async (account) => { | ||
await supabase | ||
.from("bank_accounts") | ||
.update({ | ||
account_id: account.id, | ||
}) | ||
.eq("account_reference", account.resource_id); | ||
}), | ||
); | ||
} | ||
|
||
await syncConnection.trigger({ | ||
connectionId, | ||
manualSync: true, | ||
}); | ||
}, | ||
}); |
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
27 changes: 27 additions & 0 deletions
27
apps/dashboard/src/actions/transactions/reconnect-connection-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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"use server"; | ||
|
||
import { authActionClient } from "@/actions/safe-action"; | ||
import { reconnectConnectionSchema } from "@/actions/schema"; | ||
import { LogEvents } from "@midday/events/events"; | ||
import { reconnectConnection } from "jobs/tasks/reconnect/connection"; | ||
|
||
export const reconnectConnectionAction = authActionClient | ||
.schema(reconnectConnectionSchema) | ||
.metadata({ | ||
name: "reconnect-connection", | ||
track: { | ||
event: LogEvents.ReconnectConnection.name, | ||
channel: LogEvents.ReconnectConnection.channel, | ||
}, | ||
}) | ||
.action( | ||
async ({ parsedInput: { connectionId, provider }, ctx: { user } }) => { | ||
const event = await reconnectConnection.trigger({ | ||
teamId: user.team_id!, | ||
connectionId, | ||
provider, | ||
}); | ||
|
||
return event; | ||
}, | ||
); |
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