@@ -103,6 +103,7 @@ client.defineJob({
103
103
connectionMap . set ( account . bank_connection . id , {
104
104
success : true ,
105
105
errorRetries : 0 ,
106
+ status : "connected" ,
106
107
} ) ;
107
108
108
109
return {
@@ -113,30 +114,31 @@ client.defineJob({
113
114
} catch ( error ) {
114
115
// Handle errors and update connection status
115
116
let errorDetails = "Unknown error occurred" ;
117
+ let errorCode = "unknown" ;
116
118
117
119
if ( error instanceof Midday . APIError ) {
118
120
const parsedError = parseAPIError ( error ) ;
119
121
errorDetails = parsedError . message ;
122
+ errorCode = parsedError . code ;
120
123
} else if ( error instanceof Error ) {
121
124
errorDetails = error . message ;
122
125
}
123
126
124
127
await io . logger . error ( `Error processing account ${ account . id } ` , {
125
128
error : errorDetails ,
129
+ errorCode,
126
130
} ) ;
127
131
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
+ } ) ;
135
136
136
137
return {
137
138
success : false ,
138
139
accountId : account . id ,
139
140
error : errorDetails ,
141
+ errorCode,
140
142
} ;
141
143
}
142
144
} ) ;
@@ -156,23 +158,23 @@ client.defineJob({
156
158
await io . logger . error ( "Some accounts failed to sync" , failedResults ) ;
157
159
158
160
// 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
+ }
168
170
}
169
171
170
172
// Update bank connections status
171
- for ( const [ connectionId ] of connectionMap ) {
173
+ for ( const [ connectionId , connectionStatus ] of connectionMap ) {
172
174
let updateData : {
173
175
last_accessed ?: string ;
174
176
status : string ;
175
- error_details ?: null ;
177
+ error_details ?: string | null ;
176
178
error_retries ?: number ;
177
179
} ;
178
180
@@ -196,10 +198,11 @@ client.defineJob({
196
198
const newErrorRetries = currentErrorRetries + 1 ;
197
199
198
200
updateData = {
199
- status : newErrorRetries >= 3 ? "disconnected" : "unknown" ,
201
+ status : connectionStatus . status ,
202
+ error_details : connectionStatus . errorDetails ,
200
203
} ;
201
204
202
- if ( updateData . status === "disconnected ") {
205
+ if ( connectionStatus . status !== "unknown ") {
203
206
updateData . error_retries = newErrorRetries ;
204
207
}
205
208
}
0 commit comments