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

feat: API changes for handling new submission_email location #3582

Merged
merged 8 commits into from
Aug 30, 2024
Merged
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
4 changes: 2 additions & 2 deletions api.planx.uk/modules/send/downloadApplicationFiles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export async function downloadApplicationFiles(

try {
// Confirm that the provided email matches the stored team settings for the provided localAuthority
const { sendToEmail } = await getTeamEmailSettings(
const { notifyPersonalisation } = await getTeamEmailSettings(
req.query.localAuthority as string,
);
if (sendToEmail !== req.query.email) {
if (notifyPersonalisation.sendToEmail !== req.query.email) {
return next({
status: 403,
message:
Expand Down
22 changes: 16 additions & 6 deletions api.planx.uk/modules/send/email/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ describe(`sending an application by email to a planning office`, () => {
data: {
teams: [
{
sendToEmail: "[email protected]",
settings: { emailReplyToId: "abc123" },
notifyPersonalisation: {
emailReplyToId: "abc123",
sendToEmail: "[email protected]",
},
},
],
},
Expand Down Expand Up @@ -145,15 +147,17 @@ describe(`sending an application by email to a planning office`, () => {
});
});

it("errors if this team does not have a 'submission_email' configured in teams", async () => {
it("errors if this team does not have a 'submission_email' configured in team settings", async () => {
queryMock.mockQuery({
name: "GetTeamEmailSettings",
matchOnVariables: false,
data: {
teams: [
{
sendToEmail: null,
settings: { emailReplyToId: "abc123" },
notifyPersonalisation: {
emailReplyToId: "abc123",
sendToEmail: null,
},
},
],
},
Expand Down Expand Up @@ -200,7 +204,13 @@ describe(`downloading application data received by email`, () => {
name: "GetTeamEmailSettings",
matchOnVariables: false,
data: {
teams: [{ sendToEmail: "[email protected]" }],
teams: [
{
notifyPersonalisation: {
sendToEmail: "[email protected]",
},
},
],
},
variables: { slug: "southwark" },
});
Expand Down
19 changes: 12 additions & 7 deletions api.planx.uk/modules/send/email/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export async function sendToEmail(
}

try {
// Confirm this local authority (aka team) has an email configured in teams.submission_email
const { sendToEmail, notifyPersonalisation } =
// Confirm this local authority (aka team) has an email configured in team_settings.submission_email
const { notifyPersonalisation } =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If we're only using the sendToEmail field in this file, and not the rest of the notifyPersonalisation object, it would be cleaner / neater to destructure one level deeper here, and then not have to repeatedly write notifyPersonalisation.sendToEmail throughout the file 👍

Suggested change
const { notifyPersonalisation } =
const { notifyPersonalisation: { sendToEmail } } =

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are also using ...notifyPersonalisation for part of the code below. I'll keep as is now.

await getTeamEmailSettings(localAuthority);
if (!sendToEmail) {

if (!notifyPersonalisation.sendToEmail) {
return next({
status: 400,
message: `Send to email is not enabled for this local authority (${localAuthority})`,
Expand All @@ -47,13 +48,17 @@ export async function sendToEmail(
serviceName: flowName,
sessionId: payload.sessionId,
applicantEmail: email,
downloadLink: `${process.env.API_URL_EXT}/download-application-files/${payload.sessionId}?email=${sendToEmail}&localAuthority=${localAuthority}`,
downloadLink: `${process.env.API_URL_EXT}/download-application-files/${payload.sessionId}?email=${notifyPersonalisation.sendToEmail}&localAuthority=${localAuthority}`,
...notifyPersonalisation,
},
};

// Send the email
const response = await sendEmail("submit", sendToEmail, config);
const response = await sendEmail(
"submit",
notifyPersonalisation.sendToEmail,
config,
);

// Mark session as submitted so that reminder and expiry emails are not triggered
markSessionAsSubmitted(payload.sessionId);
Expand All @@ -62,14 +67,14 @@ export async function sendToEmail(
insertAuditEntry(
payload.sessionId,
localAuthority,
sendToEmail,
notifyPersonalisation.sendToEmail,
config,
response,
);

return res.status(200).send({
message: `Successfully sent to email`,
inbox: sendToEmail,
inbox: notifyPersonalisation.sendToEmail,
govuk_notify_template: "Submit",
});
} catch (error) {
Expand Down
6 changes: 2 additions & 4 deletions api.planx.uk/modules/send/email/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { EmailSubmissionNotifyConfig } from "../../../types.js";

interface GetTeamEmailSettings {
teams: {
sendToEmail: string;
notifyPersonalisation: NotifyPersonalisation;
notifyPersonalisation: NotifyPersonalisation & { sendToEmail: string };
}[];
}

Expand All @@ -18,12 +17,12 @@ export async function getTeamEmailSettings(localAuthority: string) {
gql`
query GetTeamEmailSettings($slug: String) {
teams(where: { slug: { _eq: $slug } }) {
sendToEmail: submission_email
notifyPersonalisation: team_settings {
helpEmail: help_email
helpPhone: help_phone
emailReplyToId: email_reply_to_id
helpOpeningHours: help_opening_hours
sendToEmail: submission_email
}
}
}
Expand All @@ -32,7 +31,6 @@ export async function getTeamEmailSettings(localAuthority: string) {
slug: localAuthority,
},
);

return response?.teams[0];
}

Expand Down
3 changes: 2 additions & 1 deletion e2e/tests/api-driven/src/globalHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export function createTeam(
$admin.team.create({
name: "E2E Test Team",
slug: "E2E",
submissionEmail: TEST_EMAIL,

settings: {
homepage: "http://www.planx.uk",
submissionEmail: TEST_EMAIL,
},
...args,
}),
Expand Down
4 changes: 2 additions & 2 deletions e2e/tests/ui-driven/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const contextDefaults: Context = {
},
settings: {
homepage: "planx.uk",
submissionEmail: "[email protected]",
},
submissionEmail: "[email protected]",
},
};

Expand All @@ -58,9 +58,9 @@ export async function setUpTestContext(
context.team.id = await $admin.team.create({
slug: context.team.slug,
name: context.team.name,
submissionEmail: context.team.submissionEmail,
settings: {
homepage: context.team.settings?.homepage,
submissionEmail: context.team.submissionEmail,
},
});
}
Expand Down
Loading