Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3869 - Unzip federal restrictions file #4030

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export class FedRestrictionIntegrationService extends SFTPIntegrationBase<
async downloadResponseFile(
remoteFilePath: string,
): Promise<FedRestrictionFileRecord[]> {
const fileLines = await this.downloadResponseFileLines(remoteFilePath);
const fileLines = await this.downloadResponseFileLines(remoteFilePath, {
checkIfZipFile: true,
});
return fileLines.map((line, index) => {
return new FedRestrictionFileRecord(line, index + 1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class FedRestrictionProcessingService {
const auditUser = this.systemUsersService.systemUser;
// Get the list of all files from SFTP ordered by file name.
const fileSearch = new RegExp(
`^${this.esdcConfig.environmentCode}CSLS\\.PBC\\.RESTR\\.LIST\\.D[\\w]*\\.[\\d]*$`,
`^${this.esdcConfig.environmentCode}CSLS\\.PBC\\.RESTR\\.LIST\\.D[\\w]*\\.[\\d]*.zip$`,
"i",
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
END_OF_LINE,
getFileNameAsExtendedCurrentTimestamp,
convertToASCII,
FILE_DEFAULT_ENCODING,
} from "@sims/utilities";
import {
LINE_BREAK_SPLIT_REGEX,
SFTP_ARCHIVE_DIRECTORY,
} from "@sims/integrations/constants";
import * as AdmZip from "adm-zip";

/**
* Provides the basic features to enable the SFTP integration.
Expand Down Expand Up @@ -132,6 +132,19 @@ export abstract class SFTPIntegrationBase<DownloadType> {
options: { checkIfFileExist: boolean },
): Promise<string[] | false>;

/**
* Downloads the file specified on 'fileName' parameter from the
* SFAS integration folder on the SFTP.
* @param remoteFilePath full remote file path with file name.
* @param options download file options.
* - `checkIfZipFile` when set to true, the expectation is to receive a zip file.
* @returns parsed records from the file.
*/
protected async downloadResponseFileLines(
remoteFilePath: string,
options: { checkIfZipFile: boolean },
): Promise<string[]>;

/**
* Downloads the file specified on 'fileName' parameter from the
* SFAS integration folder on the SFTP.
Expand All @@ -142,7 +155,7 @@ export abstract class SFTPIntegrationBase<DownloadType> {
*/
protected async downloadResponseFileLines(
remoteFilePath: string,
options?: { checkIfFileExist: boolean },
options?: { checkIfFileExist: boolean; checkIfZipFile: boolean },
): Promise<string[] | false> {
this.logger.log(`Downloading file ${remoteFilePath}.`);
let client: Client;
Expand All @@ -154,10 +167,20 @@ export abstract class SFTPIntegrationBase<DownloadType> {
return false;
}
}
// Check if the file is a zip file and unzip it.
if (options?.checkIfZipFile) {
const fileExtension = path.extname(remoteFilePath);
if (fileExtension !== ".zip") {
throw new Error("File is not a zip file.");
}
}
// Read all the file content and create a buffer with 'ascii' encoding.
const fileContent = await client.get(remoteFilePath, undefined, {
readStreamOptions: { encoding: FILE_DEFAULT_ENCODING },
});
let fileContent = await client.get(remoteFilePath);
if (options?.checkIfZipFile) {
const zipFile = new AdmZip(fileContent as Buffer);
const [extractedFile] = zipFile.getEntries();
fileContent = extractedFile.getData();
}
// Convert the file content to an array of text lines and remove possible blank lines.
return fileContent
.toString()
Expand Down
19 changes: 19 additions & 0 deletions sources/packages/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion sources/packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@nestjs/terminus": "^10.2.3",
"@nestjs/typeorm": "^10.0.2",
"@types/ssh2-sftp-client": "^9.0.3",
"adm-zip": "^0.5.16",
"axios": "^1.7.4",
"bull": "^4.12.2",
"clamscan": "^2.2.1",
Expand Down Expand Up @@ -99,6 +100,7 @@
"@suites/di.nestjs": "^3.0.0",
"@suites/doubles.jest": "^3.0.0",
"@suites/unit": "^3.0.0",
"@types/adm-zip": "^0.5.7",
"@types/clamscan": "^2.0.8",
"@types/express": "^4.17.8",
"@types/faker": "^5.1.5",
Expand Down Expand Up @@ -177,4 +179,4 @@
"^@sims/auth(|/.*)$": "<rootDir>/libs/auth/src/$1"
}
}
}
}
Loading