From 27ef6a3f1408e2435fbafd0a9433c4bf0d110a18 Mon Sep 17 00:00:00 2001 From: Vivian Nobrega <43480681+ShadeWyrm@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:45:21 -0400 Subject: [PATCH 1/2] * Add ability to process address complete in emails. --- lambda-code/reliability/lib/dataLayer.ts | 39 ++++++++++++++++++++++-- lambda-code/reliability/lib/types.ts | 18 +++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lambda-code/reliability/lib/dataLayer.ts b/lambda-code/reliability/lib/dataLayer.ts index d6f474607..ff95251c6 100644 --- a/lambda-code/reliability/lib/dataLayer.ts +++ b/lambda-code/reliability/lib/dataLayer.ts @@ -14,6 +14,8 @@ import { Response, DateFormat, DateObject, + AddressElements, + AddressCompleteProps, } from "./types.js"; import { getFormattedDateFromObject } from "./utils.js"; @@ -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); @@ -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; @@ -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) => { @@ -326,6 +332,9 @@ function handleDynamicForm( rowCollector ); break; + case "addressComplete": + handleAddressCompleteResponse(qTitle, (row as Record)[qIndex], rowCollector, language, qItem.properties.addressComponents); + break; default: // Do not try to handle form elements like richText that do not have responses break; @@ -394,3 +403,29 @@ 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 = response as Record; + //Convert address to 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`); +} \ No newline at end of file diff --git a/lambda-code/reliability/lib/types.ts b/lambda-code/reliability/lib/types.ts index f9cba69fa..f679d0166 100644 --- a/lambda-code/reliability/lib/types.ts +++ b/lambda-code/reliability/lib/types.ts @@ -47,12 +47,14 @@ export interface ElementProperties { maxNumberOfRows?: number; autoComplete?: string; dateFormat?: string; + addressComponents?: AddressCompleteProps | undefined; [key: string]: | string | number | boolean | Array | Array + | AddressCompleteProps | undefined; } @@ -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 { @@ -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"; From 0055bc9ea3290e72501759c949d7523d0f575351 Mon Sep 17 00:00:00 2001 From: Vivian Nobrega <43480681+ShadeWyrm@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:07:45 -0400 Subject: [PATCH 2/2] Update lambda-code/reliability/lib/dataLayer.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clément JANIN --- lambda-code/reliability/lib/dataLayer.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lambda-code/reliability/lib/dataLayer.ts b/lambda-code/reliability/lib/dataLayer.ts index ff95251c6..d8c8a45da 100644 --- a/lambda-code/reliability/lib/dataLayer.ts +++ b/lambda-code/reliability/lib/dataLayer.ts @@ -406,8 +406,7 @@ function handleFormattedDateResponse( function handleAddressCompleteResponse(title: string, response: Response, collector: string[], language: string, adddressComponents?: AddressCompleteProps) { if (response !== undefined && response !== null && response !== "") { - const address = response as Record; - //Convert address to AddressElements + 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}`);