1
- import { LoggerService , InjectLogger } from "@sims/utilities/logger" ;
1
+ import {
2
+ LoggerService ,
3
+ InjectLogger ,
4
+ ProcessSummary ,
5
+ } from "@sims/utilities/logger" ;
2
6
import { Injectable } from "@nestjs/common" ;
3
7
import { FedRestrictionIntegrationService } from "./fed-restriction.integration.service" ;
4
8
import { DataSource , Repository } from "typeorm" ;
5
9
import { FederalRestriction , Restriction } from "@sims/sims-db" ;
6
- import {
7
- getISODateOnlyString ,
8
- parseJSONError ,
9
- processInParallel ,
10
- } from "@sims/utilities" ;
10
+ import { getISODateOnlyString , processInParallel } from "@sims/utilities" ;
11
11
import { FedRestrictionFileRecord } from "./fed-restriction-files/fed-restriction-file-record" ;
12
- import { ProcessSFTPResponseResult } from "../models/esdc-integration.model" ;
13
12
import { ConfigService , ESDCIntegrationConfig } from "@sims/utilities/config" ;
14
13
import { FEDERAL_RESTRICTIONS_BULK_INSERT_AMOUNT } from "@sims/services/constants" ;
15
14
import {
@@ -48,9 +47,10 @@ export class FedRestrictionProcessingService {
48
47
* but it is not present on federal data, deactivate it;
49
48
* 3. If the restriction is present on federal data and it is also
50
49
* present and active on student data, update the updated_at only.
50
+ * @param processSummary process summary for logging.
51
51
* @returns process response.
52
52
*/
53
- async process ( ) : Promise < ProcessSFTPResponseResult > {
53
+ async process ( processSummary : ProcessSummary ) : Promise < void > {
54
54
const auditUser = this . systemUsersService . systemUser ;
55
55
// Get the list of all files from SFTP ordered by file name.
56
56
const fileSearch = new RegExp (
@@ -63,12 +63,12 @@ export class FedRestrictionProcessingService {
63
63
fileSearch ,
64
64
) ;
65
65
66
- let result : ProcessSFTPResponseResult ;
67
66
if ( filePaths . length > 0 ) {
68
67
// Process only the most updated file.
69
- result = await this . processAllRestrictions (
68
+ await this . processAllRestrictions (
70
69
filePaths [ filePaths . length - 1 ] ,
71
70
auditUser . id ,
71
+ processSummary ,
72
72
) ;
73
73
// If there are more than one file, archive it.
74
74
// Only the most updated file matters because it represents the entire data snapshot.
@@ -77,48 +77,40 @@ export class FedRestrictionProcessingService {
77
77
await this . integrationService . archiveFile ( remoteFilePath ) ;
78
78
} catch ( error ) {
79
79
const logMessage = `Error while archiving federal restrictions file: ${ remoteFilePath } ` ;
80
- result . errorsSummary . push ( logMessage ) ;
81
- result . errorsSummary . push ( parseJSONError ( error ) ) ;
80
+ processSummary . error ( logMessage , error ) ;
82
81
this . logger . error ( logMessage , error ) ;
83
82
}
84
83
}
85
84
} else {
86
- result = new ProcessSFTPResponseResult ( ) ;
87
- result . processSummary . push (
88
- "No files found to be processed at this time." ,
89
- ) ;
85
+ processSummary . info ( "No files found to be processed at this time." ) ;
90
86
}
91
-
92
- return result ;
93
87
}
94
88
95
89
/**
96
90
* Process all the federal restrictions records in the file.
97
91
* @param remoteFilePath remote file to be processed.
98
92
* @param auditUserId user that should be considered the one that is causing the changes.
93
+ * @param processSummary process summary for logging.
99
94
* @returns result of the processing, summary and errors.
100
95
*/
101
96
private async processAllRestrictions (
102
97
remoteFilePath : string ,
103
98
auditUserId : number ,
104
- ) : Promise < ProcessSFTPResponseResult > {
105
- const result = new ProcessSFTPResponseResult ( ) ;
106
- result . processSummary . push ( `Processing file ${ remoteFilePath } .` ) ;
107
-
108
- let downloadResult : FedRestrictionFileRecord [ ] ;
99
+ processSummary : ProcessSummary ,
100
+ ) : Promise < void > {
101
+ processSummary . info ( `Processing file ${ remoteFilePath } .` ) ;
109
102
this . logger . log ( `Starting download of file ${ remoteFilePath } .` ) ;
103
+ let downloadResult : FedRestrictionFileRecord [ ] ;
110
104
try {
111
105
// Download the Federal Restrictions file with the full snapshot of the data.
112
106
downloadResult = await this . integrationService . downloadResponseFile (
113
107
remoteFilePath ,
114
108
) ;
115
109
this . logger . log ( "File download finished." ) ;
116
110
} catch ( error ) {
117
- this . logger . error ( error ) ;
118
- result . errorsSummary . push (
119
- `Error downloading file ${ remoteFilePath } . Error: ${ error } ` ,
120
- ) ;
121
- return result ;
111
+ const errorMessage = `Error downloading file ${ remoteFilePath } .` ;
112
+ processSummary . error ( errorMessage , error ) ;
113
+ this . logger . error ( errorMessage , error ) ;
122
114
}
123
115
124
116
let insertedRestrictionsIDs : number [ ] ;
@@ -139,7 +131,7 @@ export class FedRestrictionProcessingService {
139
131
const invalidDataMessage = restriction . getInvalidDataMessage ( ) ;
140
132
if ( invalidDataMessage ) {
141
133
const errorMessage = `Found record with invalid data at line number ${ restriction . lineNumber } : ${ invalidDataMessage } ` ;
142
- result . errorsSummary . push ( errorMessage ) ;
134
+ processSummary . error ( errorMessage ) ;
143
135
this . logger . error ( errorMessage ) ;
144
136
} else {
145
137
sanitizedRestrictions . push ( restriction ) ;
@@ -168,7 +160,7 @@ export class FedRestrictionProcessingService {
168
160
const logMessage = `New restrictions created: ${ federalRestrictions . createdRestrictionsCodes . join (
169
161
", " ,
170
162
) } `;
171
- result . processSummary . push ( logMessage ) ;
163
+ processSummary . warn ( logMessage ) ;
172
164
this . logger . warn ( logMessage ) ;
173
165
}
174
166
@@ -203,10 +195,8 @@ export class FedRestrictionProcessingService {
203
195
} catch ( error : unknown ) {
204
196
const logMessage =
205
197
"Aborting process due to an error on the bulk insert." ;
206
- result . errorsSummary . push ( logMessage ) ;
207
- result . errorsSummary . push ( parseJSONError ( error ) ) ;
208
198
this . logger . error ( logMessage , error ) ;
209
- return result ;
199
+ throw new Error ( logMessage , { cause : error } ) ;
210
200
}
211
201
212
202
this . logger . log ( "Bulk data insert finished." ) ;
@@ -222,11 +212,9 @@ export class FedRestrictionProcessingService {
222
212
} catch ( error ) {
223
213
const logMessage =
224
214
"Unexpected error while processing federal restrictions. Executing rollback." ;
225
- result . errorsSummary . push ( logMessage ) ;
226
- result . errorsSummary . push ( error . message ) ;
227
- this . logger . error ( logMessage ) ;
228
- this . logger . error ( error ) ;
215
+ this . logger . error ( logMessage , error ) ;
229
216
await queryRunner . rollbackTransaction ( ) ;
217
+ throw new Error ( logMessage , { cause : error } ) ;
230
218
} finally {
231
219
await queryRunner . release ( ) ;
232
220
}
@@ -242,21 +230,19 @@ export class FedRestrictionProcessingService {
242
230
insertedRestrictionsIDs ,
243
231
auditUserId ,
244
232
) ;
245
- result . processSummary . push (
233
+ processSummary . info (
246
234
`${ insertedRestrictionsIDs . length } notification(s) generated.` ,
247
235
) ;
248
236
} else {
249
- result . processSummary . push (
237
+ processSummary . info (
250
238
"No notifications were generated because no new student restriction record was created." ,
251
239
) ;
252
240
}
253
241
} catch ( error : unknown ) {
254
- result . errorsSummary . push (
255
- "Error while generating notifications. See logs for details." ,
256
- ) ;
257
- this . logger . error ( `Error while generating notifications. ${ error } ` ) ;
242
+ const errorMessage = "Error while generating notifications." ;
243
+ this . logger . error ( errorMessage , error ) ;
244
+ throw new Error ( errorMessage , { cause : error } ) ;
258
245
}
259
- return result ;
260
246
}
261
247
262
248
/**
0 commit comments