| import { client } from "@midday/engine-client"; |
| import type { Job } from "bullmq"; |
| import type { DeleteTeamPayload } from "../../schemas/teams"; |
| import { BaseProcessor } from "../base"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export class DeleteTeamProcessor extends BaseProcessor<DeleteTeamPayload> { |
| async process(job: Job<DeleteTeamPayload>): Promise<{ |
| teamId: string; |
| connectionsDeleted: number; |
| }> { |
| const { teamId, connections } = job.data; |
|
|
| this.logger.info("Starting team deletion cleanup", { |
| jobId: job.id, |
| teamId, |
| connectionsCount: connections.length, |
| }); |
|
|
| |
| const connectionsDeleted = await this.deleteBankConnections( |
| teamId, |
| connections, |
| ); |
|
|
| this.logger.info("Team deletion cleanup completed", { |
| teamId, |
| connectionsDeleted, |
| }); |
|
|
| return { |
| teamId, |
| connectionsDeleted, |
| }; |
| } |
|
|
| private async deleteBankConnections( |
| teamId: string, |
| connections: DeleteTeamPayload["connections"], |
| ): Promise<number> { |
| if (connections.length === 0) { |
| this.logger.info("No bank connections to delete", { teamId }); |
| return 0; |
| } |
|
|
| this.logger.info("Deleting bank connections", { |
| teamId, |
| count: connections.length, |
| }); |
|
|
| const deletePromises = connections.map(async (connection) => { |
| if (!connection.referenceId) { |
| return false; |
| } |
|
|
| try { |
| await client.connections.delete.$delete({ |
| json: { |
| id: connection.referenceId, |
| provider: connection.provider as |
| | "gocardless" |
| | "teller" |
| | "plaid" |
| | "enablebanking", |
| accessToken: connection.accessToken ?? undefined, |
| }, |
| }); |
| return true; |
| } catch (error) { |
| this.logger.warn("Failed to delete connection from provider", { |
| teamId, |
| referenceId: connection.referenceId, |
| provider: connection.provider, |
| error: error instanceof Error ? error.message : "Unknown error", |
| }); |
| return false; |
| } |
| }); |
|
|
| const results = await Promise.all(deletePromises); |
| const deletedCount = results.filter(Boolean).length; |
|
|
| this.logger.info("Bank connections deletion completed", { |
| teamId, |
| attempted: connections.length, |
| deleted: deletedCount, |
| }); |
|
|
| return deletedCount; |
| } |
| } |
|
|