Skip to content

Commit d9b331e

Browse files
committed
At least one account succedded status
1 parent b9b52a1 commit d9b331e

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

packages/jobs/src/transactions/manual-sync.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -174,21 +174,18 @@ client.defineJob({
174174
await io.logger.error("Some accounts failed to sync", failedAccounts);
175175

176176
// Update failed accounts
177-
// for (const failedAccount of failedAccounts) {
178-
// await supabase
179-
// .from("bank_accounts")
180-
// .update({
181-
// // enabled: false, // TODO: Disable if the account id is not found in the bank connection
182-
// // error_details: failedAccount.error instanceof Error
183-
// // ? failedAccount.error.message
184-
// // : String(failedAccount.error),
185-
// })
186-
// .eq("id", failedAccount.accountId);
187-
188-
// await io.logger.info("Disabled failed account", {
189-
// accountId: failedAccount.accountId,
190-
// });
191-
// }
177+
for (const failedAccount of failedAccounts) {
178+
await supabase
179+
.from("bank_accounts")
180+
.update({
181+
// enabled: false, // TODO: Disable if the account id is not found in the bank connection
182+
error_details:
183+
failedAccount.error instanceof Error
184+
? failedAccount.error.message
185+
: String(failedAccount.error),
186+
})
187+
.eq("id", failedAccount.accountId);
188+
}
192189
}
193190

194191
// Update bank connection status based on sync results

packages/jobs/src/transactions/sync.ts

+23-20
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ client.defineJob({
103103
connectionMap.set(account.bank_connection.id, {
104104
success: true,
105105
errorRetries: 0,
106+
status: "connected",
106107
});
107108

108109
return {
@@ -113,30 +114,31 @@ client.defineJob({
113114
} catch (error) {
114115
// Handle errors and update connection status
115116
let errorDetails = "Unknown error occurred";
117+
let errorCode = "unknown";
116118

117119
if (error instanceof Midday.APIError) {
118120
const parsedError = parseAPIError(error);
119121
errorDetails = parsedError.message;
122+
errorCode = parsedError.code;
120123
} else if (error instanceof Error) {
121124
errorDetails = error.message;
122125
}
123126

124127
await io.logger.error(`Error processing account ${account.id}`, {
125128
error: errorDetails,
129+
errorCode,
126130
});
127131

128-
const connectionStatus = connectionMap.get(account.bank_connection.id);
129-
if (!connectionStatus) {
130-
connectionMap.set(account.bank_connection.id, {
131-
success: false,
132-
errorRetries: account.bank_connection.error_retries + 1,
133-
});
134-
}
132+
connectionMap.set(account.bank_connection.id, {
133+
success: false,
134+
status: errorCode,
135+
});
135136

136137
return {
137138
success: false,
138139
accountId: account.id,
139140
error: errorDetails,
141+
errorCode,
140142
};
141143
}
142144
});
@@ -156,23 +158,23 @@ client.defineJob({
156158
await io.logger.error("Some accounts failed to sync", failedResults);
157159

158160
// Update failed accounts
159-
// for (const failedResult of failedResults) {
160-
// await supabase
161-
// .from("bank_accounts")
162-
// .update({
163-
// // enabled: false, // TODO: Disable if the account id is not found in the bank connection
164-
// // error_details: failedResult.error,
165-
// })
166-
// .eq("id", failedResult.accountId);
167-
// }
161+
for (const failedResult of failedResults) {
162+
await supabase
163+
.from("bank_accounts")
164+
.update({
165+
// enabled: false, // TODO: Disable if the account id is not found in the bank connection
166+
error_details: failedResult.error,
167+
})
168+
.eq("id", failedResult.accountId);
169+
}
168170
}
169171

170172
// Update bank connections status
171-
for (const [connectionId] of connectionMap) {
173+
for (const [connectionId, connectionStatus] of connectionMap) {
172174
let updateData: {
173175
last_accessed?: string;
174176
status: string;
175-
error_details?: null;
177+
error_details?: string | null;
176178
error_retries?: number;
177179
};
178180

@@ -196,10 +198,11 @@ client.defineJob({
196198
const newErrorRetries = currentErrorRetries + 1;
197199

198200
updateData = {
199-
status: newErrorRetries >= 3 ? "disconnected" : "unknown",
201+
status: connectionStatus.status,
202+
error_details: connectionStatus.errorDetails,
200203
};
201204

202-
if (updateData.status === "disconnected") {
205+
if (connectionStatus.status !== "unknown") {
203206
updateData.error_retries = newErrorRetries;
204207
}
205208
}

0 commit comments

Comments
 (0)