Skip to content

Commit

Permalink
Merge pull request #873 from cds-snc/fix/addresscomplete-emails
Browse files Browse the repository at this point in the history
fix: Add ability to process address complete in emails.
  • Loading branch information
ShadeWyrm authored Oct 22, 2024
2 parents e80c5e4 + 0055bc9 commit 24a430d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
38 changes: 36 additions & 2 deletions lambda-code/reliability/lib/dataLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
Response,
DateFormat,
DateObject,
AddressElements,
AddressCompleteProps,
} from "./types.js";
import { getFormattedDateFromObject } from "./utils.js";

Expand Down Expand Up @@ -272,7 +274,7 @@ function handleType(
break;
case "dynamicRow":
if (!question.properties.subElements) throw new Error("Dynamic Row must have sub elements");
handleDynamicForm(qTitle, qRowLabel, response, question.properties.subElements, collector);
handleDynamicForm(qTitle, qRowLabel, response, question.properties.subElements, collector, language);
break;
case "fileInput":
handleFileInputResponse(qTitle, response, collector);
Expand All @@ -285,6 +287,9 @@ function handleType(
collector
);
break;
case "addressComplete":
handleAddressCompleteResponse(qTitle, response, collector, language, question.properties.addressComponents);
break;
default:
// Do not try to handle form elements like richText that do not have responses
break;
Expand All @@ -296,7 +301,8 @@ function handleDynamicForm(
rowLabel = "Item",
response: Response,
question: FormElement[],
collector: string[]
collector: string[],
language: string
) {
if (!Array.isArray(response)) throw new Error("Dynamic Row responses must be in an array");
const responseCollector = response.map((row, rIndex: number) => {
Expand Down Expand Up @@ -326,6 +332,9 @@ function handleDynamicForm(
rowCollector
);
break;
case "addressComplete":
handleAddressCompleteResponse(qTitle, (row as Record<string, Response>)[qIndex], rowCollector, language, qItem.properties.addressComponents);
break;
default:
// Do not try to handle form elements like richText that do not have responses
break;
Expand Down Expand Up @@ -394,3 +403,28 @@ function handleFormattedDateResponse(

collector.push(`**${title}**${String.fromCharCode(13)}No Response`);
}

function handleAddressCompleteResponse(title: string, response: Response, collector: string[], language: string, adddressComponents?: AddressCompleteProps) {
if (response !== undefined && response !== null && response !== "") {
const address = JSON.parse(response as string) as AddressElements;
if (adddressComponents?.splitAddress) {
collector.push(`**${title} - ${language === "fr" ? "Adresse municipale" : "Street Address"}**${String.fromCharCode(13)}${address.streetAddress}`);
collector.push(`**${title} - ${language === "fr" ? "City or Town" : "Ville ou communauté"} **${String.fromCharCode(13)}${address.city}`);
collector.push(`**${title} - ${language === "fr" ? "Province, territoire ou état " : "Province, territory or state"}**${String.fromCharCode(13)}${address.province}`);
collector.push(`**${title} - ${language === "fr" ? "Code postal ou zip" : "Postal Code or zip"}**${String.fromCharCode(13)}${address.postalCode}`);
if (!adddressComponents.canadianOnly) {
collector.push(`**${title} - ${language === "fr" ? "Pays" : "Country"}**${String.fromCharCode(13)}${address.country}`);
}
} else {
const addressString = `${address.streetAddress}, ${address.city}, ${address.province}, ${address.postalCode}`;
if (!adddressComponents?.canadianOnly) {
addressString.concat(`, ${address.country}`);
}
collector.push(`**${title}**${String.fromCharCode(13)}${addressString}`);
}

return;
}

collector.push(`**${title}**${String.fromCharCode(13)}No Response`);
}
18 changes: 18 additions & 0 deletions lambda-code/reliability/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ export interface ElementProperties {
maxNumberOfRows?: number;
autoComplete?: string;
dateFormat?: string;
addressComponents?: AddressCompleteProps | undefined;
[key: string]:
| string
| number
| boolean
| Array<PropertyChoices>
| Array<FormElement>
| AddressCompleteProps
| undefined;
}

Expand All @@ -73,6 +75,7 @@ export enum FormElementTypes {
firstMiddleLastName = "firstMiddleLastName",
contact = "contact",
formattedDate = "formattedDate",
addressComplete = "addressComplete",
}
// used to define attributes for a form element or field
export interface FormElement {
Expand Down Expand Up @@ -144,4 +147,19 @@ export enum DatePart {
YYYY = "year",
}

// Props for the AddressComplete component
export interface AddressCompleteProps {
canadianOnly: boolean;
splitAddress: boolean;
}

// Address Elements for the AddressComplete component
export interface AddressElements {
streetAddress: string;
city: string;
province: string;
postalCode: string;
country: string;
}

export type DateFormat = "YYYY-MM-DD" | "DD-MM-YYYY" | "MM-DD-YYYY";

0 comments on commit 24a430d

Please sign in to comment.