Skip to content

Commit

Permalink
resolve comments - saveFakeStudent
Browse files Browse the repository at this point in the history
  • Loading branch information
lewischen-aot committed Nov 8, 2024
1 parent d9cb75c commit 95859cf
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "../../../../services/cas-supplier/cas-supplier.models";
import {
createFakeCASCreateSupplierAndSiteResponse,
createFakeCASCreateSupplierNoSiteResponse,
createFakeCASSiteForExistingSupplierResponse,
createFakeCASNotFoundSupplierResponse,
createFakeCASSupplierResponse,
} from "../../../../../test/helpers/mock-utils/cas-response.factory";
Expand Down Expand Up @@ -370,25 +370,41 @@ describe(describeProcessorRootTest(QueueNames.CASSupplierIntegration), () => {

it("Should create a new site and update the student CAS supplier when an active CAS supplier exists with no match addresses.", async () => {
// Arrange
// Created a student with same address line 1 and postal code from the expected CAS mocked result.
// Postal code has a white space that is expected to be removed.
const student = await saveFakeStudent(db.dataSource, undefined, {
initialValue: {
contactInfo: {
address: {
addressLine1: "3350 DOUGLAS ST",
city: "Victoria",
country: "Canada",
selectedCountry: COUNTRY_CANADA,
provinceState: "BC",
postalCode: "V8Z 7X9",
},
} as ContactInfo,
},
});
const referenceDate = new Date();
const savedCASSupplier = await saveFakeCASSupplier(db);
const savedCASSupplier = await saveFakeCASSupplier(db, { student });

// Configure CAS mock to return a result for the GetSupplier
// with the same supplier number and address line 1 from the
// saved CAS supplier but a different postal code.
casServiceMock.getSupplierInfoFromCAS = jest.fn(() =>
Promise.resolve(
createFakeCASSupplierResponse({
initialValues: {
supplierNumber: savedCASSupplier.supplierNumber,
addressLine1: savedCASSupplier.supplierAddress.addressLine1,
postalCode: "V1V1V1", // The postal code is added to mismatch the address.
},
}),
),
);

// Configure CAS mock to return a successful result for the CreateSiteForExistingSupplier.
const createSupplierNoSiteResponse =
createFakeCASCreateSupplierNoSiteResponse({
createFakeCASSiteForExistingSupplierResponse({
initialValues: {
supplierNumber: savedCASSupplier.supplierNumber,
supplierAddress: savedCASSupplier.supplierAddress,
Expand Down Expand Up @@ -478,8 +494,24 @@ describe(describeProcessorRootTest(QueueNames.CASSupplierIntegration), () => {

it("Should create a new site and update the student CAS supplier when an inactive CAS supplier exists with matching addresses.", async () => {
// Arrange
// Created a student with same address line 1 and postal code from the expected CAS mocked result.
// Postal code has a white space that is expected to be removed.
const student = await saveFakeStudent(db.dataSource, undefined, {
initialValue: {
contactInfo: {
address: {
addressLine1: "3350 DOUGLAS ST",
city: "Victoria",
country: "Canada",
selectedCountry: COUNTRY_CANADA,
provinceState: "BC",
postalCode: "V8Z 7X9",
},
} as ContactInfo,
},
});
const referenceDate = new Date();
const savedCASSupplier = await saveFakeCASSupplier(db);
const savedCASSupplier = await saveFakeCASSupplier(db, { student });
// Configure CAS mock to return a result for the GetSupplier
// with the same supplier number and address line 1 from the
// saved CAS supplier but an inactive status.
Expand All @@ -488,8 +520,6 @@ describe(describeProcessorRootTest(QueueNames.CASSupplierIntegration), () => {
createFakeCASSupplierResponse({
initialValues: {
status: "INACTIVE", // The status is set to "INACTIVE" to mismatch the address.
addressLine1: savedCASSupplier.supplierAddress.addressLine1,
postalCode: savedCASSupplier.supplierAddress.postalCode,
},
}),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import {
formatAddress,
formatCity,
formatPostalCode,
} from "@sims/integrations/cas";
import {
CASSupplierResponse,
CreateExistingSupplierSiteResponse,
CreateSupplierAddressSubmittedData,
CreateSupplierAndSiteResponse,
} from "@sims/integrations/cas/models/cas-service.model";
import { CASSupplierRecordStatus, SupplierAddress } from "@sims/sims-db";
Expand All @@ -14,16 +20,14 @@ import * as faker from "faker";
*/
export function createFakeCASSupplierResponse(options?: {
initialValues: {
supplierNumber?: string;
status?: CASSupplierRecordStatus;
addressLine1?: string;
postalCode?: string;
};
}): CASSupplierResponse {
return {
items: [
{
suppliernumber: options?.initialValues?.supplierNumber ?? "2006124",
suppliernumber: "2006124",
suppliername: "SMITH, MELANIE",
subcategory: "INDIVIDUAL",
sin: "000000000",
Expand All @@ -36,8 +40,7 @@ export function createFakeCASSupplierResponse(options?: {
supplieraddress: [
{
suppliersitecode: "001",
addressline1:
options?.initialValues?.addressLine1 ?? "3350 DOUGLAS ST",
addressline1: "3350 DOUGLAS ST",
addressline2: null,
addressline3: null,
city: "VICTORIA",
Expand Down Expand Up @@ -89,25 +92,15 @@ export function createFakeCASCreateSupplierAndSiteResponse(options?: {
supplierAddress: SupplierAddress;
};
}): CreateSupplierAndSiteResponse {
const supplierAddress = createFakeCASSupplierAddress(
options?.initialValues?.supplierAddress,
);
return {
submittedData: {
SupplierName: "DOE, JOHN",
SubCategory: "Individual",
Sin: faker.datatype.number({ min: 100000000, max: 999999999 }).toString(),
SupplierAddress: [
{
AddressLine1:
options?.initialValues?.supplierAddress.addressLine1 ??
faker.address.streetAddress(false).toUpperCase(),
City: options?.initialValues?.supplierAddress.city ?? "Victoria",
Province:
options?.initialValues?.supplierAddress.provinceState ?? "BC",
Country: options?.initialValues?.supplierAddress.country ?? "CA",
PostalCode:
options?.initialValues?.supplierAddress.postalCode ?? "H1H1H1",
EmailAddress: faker.internet.email(),
},
],
SupplierAddress: [supplierAddress],
},
response: {
supplierNumber: faker.datatype
Expand All @@ -124,7 +117,7 @@ export function createFakeCASCreateSupplierAndSiteResponse(options?: {
* - `initialValues` fake CAS create supplier without site response values.
* @returns fake CreateSupplierNoSite response.
*/
export function createFakeCASCreateSupplierNoSiteResponse(options?: {
export function createFakeCASSiteForExistingSupplierResponse(options?: {
initialValues: {
supplierNumber: string;
supplierAddress: SupplierAddress;
Expand All @@ -133,28 +126,36 @@ export function createFakeCASCreateSupplierNoSiteResponse(options?: {
const supplierNumber =
options?.initialValues?.supplierNumber ??
faker.datatype.number({ min: 1000000, max: 9999999 }).toString();
const supplierAddress = createFakeCASSupplierAddress(
options?.initialValues?.supplierAddress,
);
return {
submittedData: {
SupplierNumber: supplierNumber,
SupplierAddress: [
{
AddressLine1:
options?.initialValues?.supplierAddress.addressLine1 ??
faker.address.streetAddress(false).toUpperCase(),
City: options?.initialValues?.supplierAddress.city ?? "Victoria",
Province:
options?.initialValues?.supplierAddress.provinceState ?? "BC",
Country: options?.initialValues?.supplierAddress.country ?? "CA",
PostalCode:
options?.initialValues?.supplierAddress.postalCode ?? "H1H1H1",
EmailAddress: faker.internet.email(),
},
],
SupplierAddress: [supplierAddress],
},
response: {
supplierNumber: supplierNumber,
supplierSiteCode:
options?.initialValues?.supplierAddress.supplierSiteCode ?? "001",
supplierSiteCode: "001",
},
};
}

export function createFakeCASSupplierAddress(
supplierAddress?: SupplierAddress,
): CreateSupplierAddressSubmittedData {
return {
AddressLine1: supplierAddress?.addressLine1
? formatAddress(supplierAddress?.addressLine1)
: faker.address.streetAddress(false).toUpperCase(),
City: supplierAddress?.city
? formatCity(supplierAddress?.city)
: "Victoria",
Province: supplierAddress?.provinceState ?? "BC",
Country: supplierAddress?.country ?? "CA",
PostalCode: supplierAddress?.postalCode
? formatPostalCode(supplierAddress?.postalCode)
: "H1H1H1",
EmailAddress: faker.internet.email(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("CASService-createSiteForExistingSupplier", () => {
jest.resetAllMocks();
});

it("Should invoke CAS API createSiteForExistingSupplier with formatted payload when all data was provided as expected.", async () => {
it("Should invoke CAS API to create site for existing supplier with formatted payload when all data was provided as expected.", async () => {
// Arrange
mockAuthenticationResponseOnce(httpService).mockResolvedValue({
data: {
Expand Down
31 changes: 21 additions & 10 deletions sources/packages/backend/libs/integrations/src/cas/cas.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CreateSupplierAndSiteData,
CreateSupplierAndSiteResponse,
CreateSupplierAndSiteSubmittedData,
CreateSupplierSite,
} from "./models/cas-service.model";
import { AxiosError, AxiosRequestConfig } from "axios";
import { HttpService } from "@nestjs/axios";
Expand Down Expand Up @@ -135,14 +136,18 @@ export class CASService {
const url = `${this.casIntegrationConfig.baseUrl}/cfs/supplier/`;
try {
const config = await this.getAuthConfig();
const supplierAddress = this.getSupplierAddress(
supplierData.supplierSite,
supplierData.emailAddress,
);
const submittedData: CreateSupplierAndSiteSubmittedData = {
SupplierName: formatUserName(
supplierData.firstName,
supplierData.lastName,
),
SubCategory: "Individual",
Sin: supplierData.sin,
SupplierAddress: [this.getSupplierAddress(supplierData)],
SupplierAddress: [supplierAddress],
};
const response = await this.httpService.axiosRef.post(
url,
Expand Down Expand Up @@ -188,9 +193,13 @@ export class CASService {
const url = `${this.casIntegrationConfig.baseUrl}/cfs/supplier/${supplierData.supplierNumber}/site`;
try {
const config = await this.getAuthConfig();
const supplierAddress = this.getSupplierAddress(
supplierData.supplierSite,
supplierData.emailAddress,
);
const submittedData: CreateExistingSupplierAndSiteSubmittedData = {
SupplierNumber: supplierData.supplierNumber,
SupplierAddress: [this.getSupplierAddress(supplierData)],
SupplierAddress: [supplierAddress],
};
const response = await this.httpService.axiosRef.post(
url,
Expand Down Expand Up @@ -239,19 +248,21 @@ export class CASService {
/**
* Obtains the supplier address object for based on supplierData being either
* the CreateSupplierAndSiteData or CreateExistingSupplierSiteData class.
* @param supplierData
* @returns
* @param supplierSite supplier site data to get supplier address.
* @param emailAddress email address for the supplier address.
* @returns formatted supplier address data.
*/
private getSupplierAddress(
supplierData: CreateSupplierAndSiteData | CreateExistingSupplierSiteData,
supplierSite: CreateSupplierSite,
emailAddress: string,
): CreateSupplierAddressSubmittedData {
const supplierAddress = {
AddressLine1: formatAddress(supplierData.supplierSite.addressLine1),
City: formatCity(supplierData.supplierSite.city),
Province: supplierData.supplierSite.provinceCode,
AddressLine1: formatAddress(supplierSite.addressLine1),
City: formatCity(supplierSite.city),
Province: supplierSite.provinceCode,
Country: "CA",
PostalCode: formatPostalCode(supplierData.supplierSite.postalCode),
EmailAddress: supplierData.emailAddress,
PostalCode: formatPostalCode(supplierSite.postalCode),
EmailAddress: emailAddress,
};
return supplierAddress;
}
Expand Down

0 comments on commit 95859cf

Please sign in to comment.