Skip to content

Commit 63d6e09

Browse files
Federal restrictions refactor
1 parent af0d8ff commit 63d6e09

File tree

2 files changed

+54
-84
lines changed

2 files changed

+54
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import { InjectQueue, Process, Processor } from "@nestjs/bull";
1+
import { InjectQueue, Processor } from "@nestjs/bull";
22
import { FedRestrictionProcessingService } from "@sims/integrations/esdc-integration";
33
import { QueueService } from "@sims/services/queue";
44
import { QueueNames } from "@sims/utilities";
55
import { Job, Queue } from "bull";
6-
import { QueueProcessSummary } from "../../../models/processors.models";
76
import { BaseScheduler } from "../../base-scheduler";
8-
import { ESDCFileResponse } from "../models/esdc.models";
7+
import {
8+
InjectLogger,
9+
LoggerService,
10+
ProcessSummary,
11+
} from "@sims/utilities/logger";
912

1013
@Processor(QueueNames.FederalRestrictionsIntegration)
1114
export class FederalRestrictionsIntegrationScheduler extends BaseScheduler<void> {
@@ -19,46 +22,27 @@ export class FederalRestrictionsIntegrationScheduler extends BaseScheduler<void>
1922
}
2023

2124
/**
22-
* To be removed once the method {@link process} is implemented.
23-
* This method "hides" the {@link Process} decorator from the base class.
24-
*/
25-
async processQueue(): Promise<string | string[]> {
26-
throw new Error("Method not implemented.");
27-
}
28-
29-
/**
30-
* When implemented in a derived class, process the queue job.
31-
* To be implemented.
25+
* Federal restriction import.
26+
* @param job process job.
27+
* @param processSummary process summary for logging.
28+
* @returns processing result.
3229
*/
33-
protected async process(): Promise<string | string[]> {
34-
throw new Error("Method not implemented.");
30+
protected async process(
31+
job: Job<void>,
32+
processSummary: ProcessSummary,
33+
): Promise<string> {
34+
processSummary.info("Starting federal restrictions import.");
35+
await this.fedRestrictionProcessingService.process(processSummary);
36+
return "Federal restrictions import process finished.";
3537
}
3638

3739
/**
38-
* Federal restriction import.
39-
* @params job job details.
40-
* @returns Summary details of processing.
40+
* Logger for SFAS integration scheduler.
41+
* Setting the logger here allows the correct context to be set
42+
* during the property injection.
43+
* Even if the logger is not used, it is required to be set, to
44+
* allow the base classes to write logs using the correct context.
4145
*/
42-
@Process()
43-
async processFedRestrictionsImport(
44-
job: Job<void>,
45-
): Promise<ESDCFileResponse> {
46-
const summary = new QueueProcessSummary({
47-
appLogger: this.logger,
48-
jobLogger: job,
49-
});
50-
await summary.info(
51-
`Processing federal restriction integration job ${job.id} of type ${job.name}.`,
52-
);
53-
await summary.info("Starting federal restrictions import...");
54-
const uploadResult = await this.fedRestrictionProcessingService.process();
55-
await summary.info("Federal restrictions import process finished.");
56-
await summary.info(
57-
`Completed federal restriction integration job ${job.id} of type ${job.name}.`,
58-
);
59-
return {
60-
processSummary: uploadResult.processSummary,
61-
errorsSummary: uploadResult.errorsSummary,
62-
};
63-
}
46+
@InjectLogger()
47+
logger: LoggerService;
6448
}

Diff for: sources/packages/backend/libs/integrations/src/esdc-integration/fed-restriction-integration/fed-restriction.processing.service.ts

+30-44
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { LoggerService, InjectLogger } from "@sims/utilities/logger";
1+
import {
2+
LoggerService,
3+
InjectLogger,
4+
ProcessSummary,
5+
} from "@sims/utilities/logger";
26
import { Injectable } from "@nestjs/common";
37
import { FedRestrictionIntegrationService } from "./fed-restriction.integration.service";
48
import { DataSource, Repository } from "typeorm";
59
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";
1111
import { FedRestrictionFileRecord } from "./fed-restriction-files/fed-restriction-file-record";
12-
import { ProcessSFTPResponseResult } from "../models/esdc-integration.model";
1312
import { ConfigService, ESDCIntegrationConfig } from "@sims/utilities/config";
1413
import { FEDERAL_RESTRICTIONS_BULK_INSERT_AMOUNT } from "@sims/services/constants";
1514
import {
@@ -48,9 +47,10 @@ export class FedRestrictionProcessingService {
4847
* but it is not present on federal data, deactivate it;
4948
* 3. If the restriction is present on federal data and it is also
5049
* present and active on student data, update the updated_at only.
50+
* @param processSummary process summary for logging.
5151
* @returns process response.
5252
*/
53-
async process(): Promise<ProcessSFTPResponseResult> {
53+
async process(processSummary: ProcessSummary): Promise<void> {
5454
const auditUser = this.systemUsersService.systemUser;
5555
// Get the list of all files from SFTP ordered by file name.
5656
const fileSearch = new RegExp(
@@ -63,12 +63,12 @@ export class FedRestrictionProcessingService {
6363
fileSearch,
6464
);
6565

66-
let result: ProcessSFTPResponseResult;
6766
if (filePaths.length > 0) {
6867
// Process only the most updated file.
69-
result = await this.processAllRestrictions(
68+
await this.processAllRestrictions(
7069
filePaths[filePaths.length - 1],
7170
auditUser.id,
71+
processSummary,
7272
);
7373
// If there are more than one file, archive it.
7474
// Only the most updated file matters because it represents the entire data snapshot.
@@ -77,48 +77,40 @@ export class FedRestrictionProcessingService {
7777
await this.integrationService.archiveFile(remoteFilePath);
7878
} catch (error) {
7979
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);
8281
this.logger.error(logMessage, error);
8382
}
8483
}
8584
} 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.");
9086
}
91-
92-
return result;
9387
}
9488

9589
/**
9690
* Process all the federal restrictions records in the file.
9791
* @param remoteFilePath remote file to be processed.
9892
* @param auditUserId user that should be considered the one that is causing the changes.
93+
* @param processSummary process summary for logging.
9994
* @returns result of the processing, summary and errors.
10095
*/
10196
private async processAllRestrictions(
10297
remoteFilePath: string,
10398
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}.`);
109102
this.logger.log(`Starting download of file ${remoteFilePath}.`);
103+
let downloadResult: FedRestrictionFileRecord[];
110104
try {
111105
// Download the Federal Restrictions file with the full snapshot of the data.
112106
downloadResult = await this.integrationService.downloadResponseFile(
113107
remoteFilePath,
114108
);
115109
this.logger.log("File download finished.");
116110
} 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);
122114
}
123115

124116
let insertedRestrictionsIDs: number[];
@@ -139,7 +131,7 @@ export class FedRestrictionProcessingService {
139131
const invalidDataMessage = restriction.getInvalidDataMessage();
140132
if (invalidDataMessage) {
141133
const errorMessage = `Found record with invalid data at line number ${restriction.lineNumber}: ${invalidDataMessage}`;
142-
result.errorsSummary.push(errorMessage);
134+
processSummary.error(errorMessage);
143135
this.logger.error(errorMessage);
144136
} else {
145137
sanitizedRestrictions.push(restriction);
@@ -168,7 +160,7 @@ export class FedRestrictionProcessingService {
168160
const logMessage = `New restrictions created: ${federalRestrictions.createdRestrictionsCodes.join(
169161
", ",
170162
)}`;
171-
result.processSummary.push(logMessage);
163+
processSummary.warn(logMessage);
172164
this.logger.warn(logMessage);
173165
}
174166

@@ -203,10 +195,8 @@ export class FedRestrictionProcessingService {
203195
} catch (error: unknown) {
204196
const logMessage =
205197
"Aborting process due to an error on the bulk insert.";
206-
result.errorsSummary.push(logMessage);
207-
result.errorsSummary.push(parseJSONError(error));
208198
this.logger.error(logMessage, error);
209-
return result;
199+
throw new Error(logMessage, { cause: error });
210200
}
211201

212202
this.logger.log("Bulk data insert finished.");
@@ -222,11 +212,9 @@ export class FedRestrictionProcessingService {
222212
} catch (error) {
223213
const logMessage =
224214
"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);
229216
await queryRunner.rollbackTransaction();
217+
throw new Error(logMessage, { cause: error });
230218
} finally {
231219
await queryRunner.release();
232220
}
@@ -242,21 +230,19 @@ export class FedRestrictionProcessingService {
242230
insertedRestrictionsIDs,
243231
auditUserId,
244232
);
245-
result.processSummary.push(
233+
processSummary.info(
246234
`${insertedRestrictionsIDs.length} notification(s) generated.`,
247235
);
248236
} else {
249-
result.processSummary.push(
237+
processSummary.info(
250238
"No notifications were generated because no new student restriction record was created.",
251239
);
252240
}
253241
} 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 });
258245
}
259-
return result;
260246
}
261247

262248
/**

0 commit comments

Comments
 (0)