Skip to content

Commit

Permalink
chore(types.ts): remove NotifyResult interface
Browse files Browse the repository at this point in the history
chore(govuk-notify.ts): change return value of sendAuthenticationEmail, sendApplicationConfirmationEmail, sendDataPublishedEmail, and sendEditDetailsEmail to check for "id" in result and result.id !== undefined
chore(notifications-node-client.ts): change statusText to id in mockResolvedValue
  • Loading branch information
RichardBray committed Aug 16, 2023
1 parent 1969ccc commit a42e5e5
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from "scheduler/prismaClient";
import { AuditCreateInput, ListEventJsonData } from "server/models/types";
import { AuditEvent } from "@prisma/client";
import type { AuditCreateInput, ListEventJsonData } from "server/models/types";
import type { AuditEvent } from "@prisma/client";

export async function addUnpublishPostReminderAudit(eventData: ListEventJsonData, auditEvent: AuditEvent) {
const data: AuditCreateInput = {
Expand Down
1 change: 1 addition & 0 deletions src/scheduler/workers/unpublish/delete/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default async function deleteItemsAfterAYear() {

await prisma.$transaction([
prisma.audit.createMany({
// Data added to Audit table for list item since it's been removed therefore does not have any items in the Event table to connect to
data: itemsUnpublishedByAR.map((item) => ({
auditEvent: "DELETED",
type: "listItem",
Expand Down
2 changes: 1 addition & 1 deletion src/server/services/__mocks__/notifications-node-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sendEmail = jest.fn().mockResolvedValue({ statusText: "Created" });
const sendEmail = jest.fn().mockResolvedValue({ id: "Created" });

export const NotifyClient = jest.fn().mockReturnValue({
sendEmail,
Expand Down
34 changes: 17 additions & 17 deletions src/server/services/__tests__/govuk-notify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const emailAddress = "testemail@gov.uk";
Expand Down Expand Up @@ -109,7 +109,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const contactName = "Ada Lovelace";
Expand Down Expand Up @@ -156,7 +156,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const contactName = "Ada Lovelace";
Expand Down Expand Up @@ -204,7 +204,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const contactName = "Ada Lovelace";
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const emailAddress = "testemail@gov.uk";
Expand All @@ -268,7 +268,7 @@ describe("GOVUK Notify service:", () => {
annualReviewDate
);

expect(result).toBe(true);
expect(result.id).toBeTruthy();
expect(notifyClient.sendEmail).toHaveBeenCalledWith(
mockNotify.templates.annualReviewNotices.postOneMonth,
"testemail@gov.uk",
Expand All @@ -288,7 +288,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const emailAddress = "testemail@gov.uk";
Expand All @@ -304,7 +304,7 @@ describe("GOVUK Notify service:", () => {
annualReviewDate
);

expect(result).toBe(true);
expect(result.id).toBeTruthy();
expect(notifyClient.sendEmail).toHaveBeenCalledWith(
mockNotify.templates.annualReviewNotices.postOneWeek,
"testemail@gov.uk",
Expand All @@ -324,7 +324,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const emailAddress = "testemail@gov.uk";
Expand All @@ -340,7 +340,7 @@ describe("GOVUK Notify service:", () => {
annualReviewDate
);

expect(result).toBe(true);
expect(result.id).toBeTruthy();
expect(notifyClient.sendEmail).toHaveBeenCalledWith(
mockNotify.templates.annualReviewNotices.postOneDay,
"testemail@gov.uk",
Expand All @@ -360,7 +360,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const emailAddress = "testemail@gov.uk";
Expand All @@ -370,7 +370,7 @@ describe("GOVUK Notify service:", () => {

const { result } = await sendAnnualReviewPostEmail("START", emailAddress, typePlural, country, annualReviewDate);

expect(result).toBe(true);
expect(result.id).toBeTruthy();
expect(notifyClient.sendEmail).toHaveBeenCalledWith(
mockNotify.templates.annualReviewNotices.postStart,
"testemail@gov.uk",
Expand Down Expand Up @@ -414,7 +414,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

const emailAddress = "testemail@gov.uk";
Expand All @@ -433,7 +433,7 @@ describe("GOVUK Notify service:", () => {
changeLink
);

expect(result).toBe(true);
expect(result.id).toBeTruthy();
expect(notifyClient.sendEmail).toHaveBeenCalledWith(
mockNotify.templates.annualReviewNotices.providerStart,
"testemail@gov.uk",
Expand Down Expand Up @@ -500,7 +500,7 @@ describe("GOVUK Notify service:", () => {
const notifyClient = getNotifyClient();

jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce({
statusText: "Created",
id: "Created",
});

await sendManualActionNotificationToPost(1, trigger);
Expand All @@ -514,13 +514,13 @@ describe("GOVUK Notify service:", () => {
test("returns resolved value when at least one email succeeds", async () => {
const notifyClient = getNotifyClient();
const successfulSend = {
statusText: "Created",
id: "Created",
};
const rejectedSend = "Failed to send email";
jest.spyOn(notifyClient, "sendEmail").mockResolvedValueOnce(successfulSend).mockRejectedValueOnce(rejectedSend);

const settled = await sendManualActionNotificationToPost(1, "CHANGED_DETAILS");
expect(settled).toEqual({ statusText: "Created" });
expect(settled).toEqual({ id: "Created" });
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/server/services/govuk-notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function sendAuthenticationEmail(email: string, authenticationLink:
reference: "",
});

return (result as NotifyResult).statusText === "Created";
return "id" in result && result.id !== undefined;
} catch (error) {
logger.error(`sendAuthenticationEmail Error: ${error.message}`);
return false;
Expand All @@ -61,7 +61,7 @@ export async function sendApplicationConfirmationEmail(
reference: "",
});

return (result as NotifyResult).statusText === "Created";
return "id" in result && result.id !== undefined;
} catch (error) {
logger.error(`sendApplicationConfirmationEmail Error: ${error.message}`);
return false;
Expand All @@ -88,7 +88,7 @@ export async function sendDataPublishedEmail(
reference: "",
});

return (result as NotifyResult).statusText === "Created";
return "id" in result && result.id !== undefined;
} catch (error) {
logger.error(`sendDataPublishedEmail Error: ${error.message}`);
return false;
Expand Down Expand Up @@ -121,7 +121,7 @@ export async function sendEditDetailsEmail(
reference: "",
});

return { result: (result as NotifyResult).statusText === "Created" };
return { result: "id" in result && result.id !== undefined };
} catch (error) {
const message = `sendEditDetailsEmail: Unable to send change request email: ${error.message}`;
logger.error(message);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/getNotifyClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function getNotifyClient() {

class FakeNotifyClient {
sendEmail() {
return { statusText: "Created" };
return { id: "Created" };
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ export interface ListUpdateInput extends PrismaClient.Prisma.ListUpdateInput {
jsonData: ListJsonData;
}

export interface NotifyResult {
statusText: string;
}

export interface Event extends PrismaClient.Event {
jsonData: EventJsonData;
}

0 comments on commit a42e5e5

Please sign in to comment.