Skip to content

Commit

Permalink
Add last name translations and new enum type.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsignori-aot committed Oct 7, 2024
1 parent 22c9313 commit d2d6eaa
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { getSQLFileData } from "../utilities/sqlLoader";

export class AddManualInterventionCASSupplierType1728327203709
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
getSQLFileData("Add-manual-intervention-cas-supplier-type.sql", "Types"),
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
getSQLFileData(
"Rollback-add-manual-intervention-cas-supplier-type.sql",
"Types",
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- Postgres allows adding new types to an enum but it causes issues when the new types added are
-- used in another query in the same transaction, hence the team decision was to recreate the enums
-- types when a new item must be added following the same approach already used for rollbacks.
CREATE TYPE sims.supplier_status_to_be_updated AS ENUM (
'Pending supplier verification',
'Pending address verification',
'Verified',
'Verified manually',
'Manual intervention'
);

-- Update the dependent column to start using the new enum with the expected values.
ALTER TABLE
sims.cas_suppliers
ALTER COLUMN
supplier_status TYPE sims.supplier_status_to_be_updated USING (supplier_status :: TEXT) :: sims.supplier_status_to_be_updated;

-- Remove the entire enum that is no longer being used.
DROP TYPE sims.supplier_status;

-- Rename the enum to what it was in the beginning.
ALTER TYPE sims.supplier_status_to_be_updated RENAME TO supplier_status;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- Postgres allows adding new types to an enum but it causes issues when the new types added are
-- used in another query in the same transaction, hence the team decision was to recreate the enums
-- types when a new item must be added following the same approach already used for rollbacks.
CREATE TYPE sims.supplier_status_to_be_updated AS ENUM (
'Pending supplier verification',
'Pending address verification',
'Verified',
'Verified manually'
);

-- Update the dependent column to start using the new enum with the expected values.
ALTER TABLE
sims.cas_suppliers
ALTER COLUMN
supplier_status TYPE sims.supplier_status_to_be_updated USING (supplier_status :: TEXT) :: sims.supplier_status_to_be_updated;

-- Remove the entire enum that is no longer being used.
DROP TYPE sims.supplier_status;

-- Rename the enum to what it was in the beginning.
ALTER TYPE sims.supplier_status_to_be_updated RENAME TO supplier_status;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AxiosRequestConfig } from "axios";
import { HttpService } from "@nestjs/axios";
import { CASIntegrationConfig, ConfigService } from "@sims/utilities/config";
import { stringify } from "querystring";
import { CustomNamedError } from "@sims/utilities";
import { CustomNamedError, translateToASCII } from "@sims/utilities";
import { CAS_AUTH_ERROR } from "@sims/integrations/constants";
import { InjectLogger } from "@sims/utilities/logger";

Expand Down Expand Up @@ -63,7 +63,8 @@ export class CASService {
sin: string,
lastName: string,
): Promise<CASSupplierResponse> {
const url = `${this.casIntegrationConfig.baseUrl}/cfs/supplier/${lastName}/lastname/${sin}/sin`;
const convertedLastName = translateToASCII(lastName);
const url = `${this.casIntegrationConfig.baseUrl}/cfs/supplier/${convertedLastName}/lastname/${sin}/sin`;
let response: { data: CASSupplierResponse };
try {
const headers = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FixedFormatFileLine } from "./sftp-integration-base.models";
import {
END_OF_LINE,
getFileNameAsExtendedCurrentTimestamp,
translateToASCII,
} from "@sims/utilities";
import { FILE_DEFAULT_ENCODING } from "@sims/services/constants";
import { LINE_BREAK_SPLIT_REGEX } from "@sims/integrations/constants";
Expand Down Expand Up @@ -58,120 +59,14 @@ export abstract class SFTPIntegrationBase<DownloadType> {
const client = await this.getClient();
try {
this.logger.log(`Uploading ${remoteFilePath}`);
return await client.put(
this.convertRawContent(rawContent),
remoteFilePath,
);
return await client.put(translateToASCII(rawContent), remoteFilePath);
} finally {
this.logger.log("Finalizing SFTP client...");
await SshService.closeQuietly(client);
this.logger.log("SFTP client finalized.");
}
}

/**
* Converts input into an ASCII 7 bit buffer.
* @param rawContent Raw content in string format.
* @returns a buffer of the input with extended ASCII accented characters converted to unaccented characters.
*/
private convertRawContent(rawContent: string): Buffer {
const content = Buffer.from(rawContent, FILE_DEFAULT_ENCODING);
for (const [index, char] of content.entries()) {
if (char > 127) {
// if extended ascii
switch (char) {
case 192:
case 193:
case 194:
case 195:
case 196:
case 197: // ÀÁÂÃÄÅ
content[index] = 65; // replace with A
break;
case 199: // Ç
content[index] = 67; // replace with C
break;
case 200:
case 201:
case 202:
case 203: // ÈÉÊË
content[index] = 69; // replace with E
break;
case 204:
case 205:
case 206:
case 207: // ÌÍÎÏ
content[index] = 73; // replace with I
break;
case 209: // Ñ
content[index] = 78; // replace with N
break;
case 210:
case 211:
case 212:
case 213:
case 214: // ÒÓÔÕÖ
content[index] = 79; // replace with O
break;
case 217:
case 218:
case 219:
case 220: // ÙÚÛÜ
content[index] = 85; // replace with U
break;
case 224:
case 225:
case 226:
case 227:
case 228:
case 229: // àáâãäå
content[index] = 97; // replace with a
break;
case 231: // ç
content[index] = 99; // replace with c
break;
case 232:
case 233:
case 234:
case 235: // èéêë
content[index] = 101; // replace with e
break;
case 236:
case 237:
case 238:
case 239: // ìíîï
content[index] = 105; // replace with i
break;
case 241: // ñ
content[index] = 110; // replace with n
break;
case 242:
case 243:
case 244:
case 245:
case 246: // óòôõö
content[index] = 111; // replace with o
break;
case 249:
case 250:
case 251:
case 252: // ùúûü
content[index] = 117; // replace with u
break;
case 253:
case 255: // ýÿ
content[index] = 121; // replace with y
break;
default:
content[index] = 63; // replace with ? by default
break;
}
}
// else keep the same character
}
return content;
}

/**
* Get the list of all files waiting to be downloaded from the
* SFTP filtering by the the regex pattern.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ export enum SupplierStatus {
* Verified manually.
*/
VerifiedManually = "Verified manually",
/**
* Manual intervention is required.
*/
ManualIntervention = "Manual intervention",
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { translateToASCII } from "@sims/utilities/string-utils";

describe("StringUtils-translateToASCII", () => {
it("Should replace the special characters when equivalent ones are present.", () => {
// Arrange
const textWithSpecialCharacters =
"Some text with special characters: ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜ—àáâãäåçèéêëìíîï-ñóòôõöùúûüýÿ";

// Act
const translatedData = translateToASCII(textWithSpecialCharacters);

// Assert
expect(translatedData).toBe(
"Some text with special characters: AAAAAACEEEEIIIINOOOOOUUUU-aaaaaaceeeeiiii-nooooouuuuyy",
);
});

it("Should return null when null is provided.", () => {
// Arrange
const textWithSpecialCharacters = null;

// Act
const translatedData = translateToASCII(textWithSpecialCharacters);

// Assert
expect(translatedData).toBeNull();
});

it("Should return null when undefined is provided.", () => {
// Arrange
const textWithSpecialCharacters = undefined;

// Act
const translatedData = translateToASCII(textWithSpecialCharacters);

// Assert
expect(translatedData).toBeNull();
});
});
Loading

0 comments on commit d2d6eaa

Please sign in to comment.