Skip to content

Commit 559b2c6

Browse files
committed
chore: [AB#13058] add health check for tax filing API
1 parent 6bb341e commit 559b2c6

File tree

11 files changed

+92
-7
lines changed

11 files changed

+92
-7
lines changed

api/src/client/ApiTaxFilingClient.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
ApiTaxFilingOnboardingResponse,
1717
} from "@client/ApiTaxFilingClient";
1818
import { dateToShortISO } from "@domain/tax-filings/taxIdHelper";
19-
import { StatusCodes } from "http-status-codes";
19+
import { randomElementFromArray } from "@test/helpers";
20+
import { ReasonPhrases, StatusCodes } from "http-status-codes";
2021

2122
jest.mock("axios");
2223
jest.mock("winston");
@@ -363,4 +364,37 @@ describe("ApiTaxFilingClient", () => {
363364
expect(response).toEqual({ state: "API_ERROR" });
364365
});
365366
});
367+
368+
describe("health", () => {
369+
it("returns a passing health check if the service is available", async () => {
370+
const stubResponse = generateSuccessfulApiTaxFilingLookupResponse({});
371+
mockAxios.post.mockResolvedValue({ data: stubResponse });
372+
373+
expect(await client.health()).toEqual({ success: true, data: { message: ReasonPhrases.OK } });
374+
});
375+
376+
it("returns a passing health check if the service returns a known validation error", async () => {
377+
const state: "PENDING" | "FAILED" | "UNREGISTERED" = randomElementFromArray([
378+
"PENDING",
379+
"FAILED",
380+
"UNREGISTERED",
381+
]);
382+
383+
const stubResponse = generateErroredApiTaxFilingLookupResponse({}, state);
384+
mockAxios.post.mockRejectedValue({
385+
response: { data: stubResponse, status: StatusCodes.BAD_REQUEST },
386+
});
387+
388+
expect(await client.health()).toEqual({ success: true, data: { message: ReasonPhrases.OK } });
389+
});
390+
391+
it("returns a failing health check if the service is unavailable", async () => {
392+
mockAxios.post.mockRejectedValue({});
393+
394+
expect(await client.health()).toEqual({
395+
success: false,
396+
data: { message: ReasonPhrases.INTERNAL_SERVER_ERROR },
397+
});
398+
});
399+
});
366400
});

api/src/client/ApiTaxFilingClient.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { flattenDeDupAndConvertTaxFilings } from "@domain/tax-filings/taxIdHelper";
22
import {
3+
HealthCheckMetadata,
34
TaxFilingClient,
45
TaxFilingLookupResponse,
56
TaxFilingOnboardingResponse,
@@ -8,7 +9,7 @@ import {
89
} from "@domain/types";
910
import { LogWriterType } from "@libs/logWriter";
1011
import axios, { AxiosError } from "axios";
11-
import { StatusCodes } from "http-status-codes";
12+
import { ReasonPhrases, StatusCodes } from "http-status-codes";
1213

1314
type ApiConfig = {
1415
apiKey: string;
@@ -186,9 +187,51 @@ export const ApiTaxFilingClient = (config: ApiConfig, logger: LogWriterType): Ta
186187
}
187188
};
188189

190+
const health = async (): Promise<HealthCheckMetadata> => {
191+
return lookup({ taxId: "777777777771", businessName: "RUBB" })
192+
.then((response) => {
193+
if (response.state === "API_ERROR") {
194+
logger.LogError(
195+
`Tax Registration Health Check - Id:${logId} - Error received: ${JSON.stringify(response.state)}`,
196+
);
197+
return {
198+
success: false,
199+
data: {
200+
message: ReasonPhrases.INTERNAL_SERVER_ERROR,
201+
},
202+
};
203+
} else {
204+
// Note: The API may return a 400 and a validation error message. This still indicates the service is available.
205+
logger.LogInfo(
206+
`Tax Registration Health Check - Id:${logId} - Response received: ${JSON.stringify(
207+
response.state,
208+
)}`,
209+
);
210+
return {
211+
success: true,
212+
data: {
213+
message: ReasonPhrases.OK,
214+
},
215+
};
216+
}
217+
})
218+
.catch((error) => {
219+
logger.LogError(
220+
`Tax Registration Health Check - Id:${logId} - Error received: ${JSON.stringify(error)}`,
221+
);
222+
return {
223+
success: false,
224+
data: {
225+
message: ReasonPhrases.INTERNAL_SERVER_ERROR,
226+
},
227+
};
228+
});
229+
};
230+
189231
return {
190232
lookup,
191233
onboarding,
234+
health,
192235
};
193236
};
194237

api/src/domain/tax-filings/taxFilingsInterfaceFactory.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe("TaxFilingsInterfaceFactory", () => {
3838
taxFilingClient = {
3939
lookup: jest.fn(),
4040
onboarding: jest.fn(),
41+
health: jest.fn(),
4142
};
4243
const dateNowStub = jest.fn(() => {
4344
return dateNow;

api/src/domain/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export interface TaxFilingClient {
140140
email: string;
141141
businessName: string;
142142
}) => Promise<TaxFilingOnboardingResponse>;
143+
health: () => Promise<HealthCheckMetadata>;
143144
}
144145

145146
export interface TaxFilingInterface {

api/src/functions/express/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,11 @@ app.use(
536536
["webservice/formation", formationHealthCheckClient],
537537
["tax-clearance", taxClearanceHealthCheckClient],
538538
["xray-registration", xrayRegistrationHealthCheckClient],
539+
// TODO: The following need CloudWatch metrics
539540
["cigarette-license", cigaretteLicenseHealthCheckClient],
540541
["cigarette-email-client", cigaretteLicenseEmailClient.health],
541542
["messaging-service", messagingServiceClient.health],
543+
["tax-filing-client", taxFilingClient.health],
542544
]),
543545
logger,
544546
),

api/src/libs/healthCheck.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe("healthCheck", () => {
2222
xrayRegistration: "PASS",
2323
cigaretteLicense: "PASS",
2424
cigaretteEmailClient: "PASS",
25+
taxFilingClient: "PASS",
2526
});
2627
});
2728

@@ -39,6 +40,7 @@ describe("healthCheck", () => {
3940
xrayRegistration: "FAIL",
4041
cigaretteLicense: "FAIL",
4142
cigaretteEmailClient: "FAIL",
43+
taxFilingClient: "FAIL",
4244
});
4345
});
4446

@@ -56,6 +58,7 @@ describe("healthCheck", () => {
5658
xrayRegistration: "ERROR",
5759
cigaretteLicense: "ERROR",
5860
cigaretteEmailClient: "ERROR",
61+
taxFilingClient: "ERROR",
5962
});
6063
});
6164
});

api/src/libs/healthCheck.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const healthCheckEndPoints: Record<string, string> = {
1919
xrayRegistration: "xray-registration",
2020
cigaretteEmailClient: "cigarette-email-client",
2121
cigaretteLicense: "cigarette-license",
22+
taxFilingClient: "tax-filing-client",
2223
};
2324

2425
const healthCheck = async (type: string, url: string, logger: LogWriterType): Promise<Status> => {

api/wiremock/mappings/get-tax-calendar-lookup-success.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"url": "/lookup",
55
"bodyPatterns": [
66
{
7-
"contains": "123456789098"
7+
"contains": "777777777771"
88
}
99
]
1010
},

api/wiremock/mappings/get-tax-calendar-onboarding-success.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"url": "/onboard",
55
"bodyPatterns": [
66
{
7-
"contains": "123456789098"
7+
"contains": "777777777771"
88
}
99
]
1010
},

web/cypress/e2e/auto-tax-filing.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe.skip(
3737
clickModalSaveButton();
3838

3939
onDashboardPage.registerForTaxes();
40-
cy.get('input[name="taxId"]').type("123456789098");
40+
cy.get('input[name="taxId"]').type("777777777771");
4141
cy.get("button").contains("Save").click();
4242
cy.get(`[data-testid="back-to-dashboard"]`).first().click({ force: true });
4343
cy.get('[data-testid="cta-funding-nudge"]').first().click();
@@ -58,7 +58,7 @@ describe.skip(
5858
clickModalSaveButton();
5959

6060
onDashboardPage.registerForTaxes();
61-
cy.get('input[name="taxId"]').type("123456789098");
61+
cy.get('input[name="taxId"]').type("777777777771");
6262
cy.get("button").contains("Save").click();
6363
cy.get(`[data-testid="back-to-dashboard"]`).first().click({ force: true });
6464
cy.get('[data-testid="cta-funding-nudge"]').first().click();

0 commit comments

Comments
 (0)