16945 welsh template for mailing list
This commit is contained in:
+168
-26
@@ -42,6 +42,8 @@ const flattenWatchlistEntry = (entry) => {
|
|||||||
flattened.contact_email = entry.pinswg_Contact.emailaddress1;
|
flattened.contact_email = entry.pinswg_Contact.emailaddress1;
|
||||||
flattened.firstname = entry.pinswg_Contact.firstname;
|
flattened.firstname = entry.pinswg_Contact.firstname;
|
||||||
flattened.lastname = entry.pinswg_Contact.lastname;
|
flattened.lastname = entry.pinswg_Contact.lastname;
|
||||||
|
flattened.pinswg_preferredlanguage =
|
||||||
|
entry.pinswg_Contact.pinswg_preferredlanguage;
|
||||||
delete flattened.pinswg_Contact;
|
delete flattened.pinswg_Contact;
|
||||||
}
|
}
|
||||||
return flattened;
|
return flattened;
|
||||||
@@ -52,46 +54,125 @@ const buildNotifyPayloads = (watchlistByEmail) => {
|
|||||||
const reference = "PEDW-CASE-UPDATES";
|
const reference = "PEDW-CASE-UPDATES";
|
||||||
const templateId = "217f7f96-2f8a-4034-a27f-9ffcf8d89edb";
|
const templateId = "217f7f96-2f8a-4034-a27f-9ffcf8d89edb";
|
||||||
|
|
||||||
const formatDocuments = (docs = []) =>
|
const templateIdCY = "09d5cece-65d3-4717-84cd-edab99c8870c";
|
||||||
docs
|
|
||||||
|
// const formatDocuments = (docs = []) =>
|
||||||
|
// docs
|
||||||
|
// .map((doc) => {
|
||||||
|
// const ref = doc.pinswg_isharedocumentreference || "No Ref";
|
||||||
|
// const name = doc.pinswg_name || "Untitled";
|
||||||
|
// const date =
|
||||||
|
// formatDates(
|
||||||
|
// doc.pinswg_documentpublisheddate?.split("T")[0]
|
||||||
|
// ) || "No Date";
|
||||||
|
// const link =
|
||||||
|
// process.env.NEXTAUTH_URL + doc.pinswg_hashlink ||
|
||||||
|
// "[No Link]";
|
||||||
|
// return ` - ${name} (${date})\n Link: ${link}`;
|
||||||
|
// })
|
||||||
|
// .join("\n");
|
||||||
|
|
||||||
|
const formatDocuments = (docs = [], language = "en") => {
|
||||||
|
// Labels based on language
|
||||||
|
const labels = {
|
||||||
|
en: {
|
||||||
|
noRef: "No Ref",
|
||||||
|
untitled: "Untitled",
|
||||||
|
noDate: "No Date",
|
||||||
|
link: "Link",
|
||||||
|
},
|
||||||
|
cy: {
|
||||||
|
noRef: "Dim Cyfeirnod",
|
||||||
|
untitled: "Heb Deitl",
|
||||||
|
noDate: "Dim Dyddiad",
|
||||||
|
link: "Dolen",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const t = labels[language] || labels.en;
|
||||||
|
|
||||||
|
return docs
|
||||||
.map((doc) => {
|
.map((doc) => {
|
||||||
const ref = doc.pinswg_isharedocumentreference || "No Ref";
|
const ref = doc.pinswg_isharedocumentreference || t.noRef;
|
||||||
const name = doc.pinswg_name || "Untitled";
|
const name = doc.pinswg_name || t.untitled;
|
||||||
const date =
|
const date =
|
||||||
formatDates(
|
formatDates(
|
||||||
doc.pinswg_documentpublisheddate?.split("T")[0]
|
doc.pinswg_documentpublisheddate?.split("T")[0],
|
||||||
) || "No Date";
|
language
|
||||||
|
) || t.noDate;
|
||||||
const link =
|
const link =
|
||||||
process.env.NEXTAUTH_URL + doc.pinswg_hashlink ||
|
(process.env.NEXTAUTH_URL || "") + doc.pinswg_hashlink ||
|
||||||
"[No Link]";
|
"[No Link]";
|
||||||
return ` - ${name} (${date})\n Link: ${link}`;
|
|
||||||
|
return ` - ${name} (${date})\n ${t.link}: ${link}`;
|
||||||
})
|
})
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
};
|
||||||
|
|
||||||
const formatSipEvents = (events = []) =>
|
const formatSipEvents = (events = [], language = "en") => {
|
||||||
events
|
const labels = {
|
||||||
|
en: {
|
||||||
|
unnamed: "Unnamed Event",
|
||||||
|
noDate: "No Date",
|
||||||
|
on: "on",
|
||||||
|
},
|
||||||
|
cy: {
|
||||||
|
unnamed: "Digwyddiad Heb Enw",
|
||||||
|
noDate: "Dim Dyddiad",
|
||||||
|
on: "ar",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const t = labels[language] || labels.en;
|
||||||
|
|
||||||
|
return events
|
||||||
.map((event) => {
|
.map((event) => {
|
||||||
const name = event.pinswg_name || "Unnamed Event";
|
const name = event.pinswg_name || t.unnamed;
|
||||||
const date =
|
const date =
|
||||||
formatDates(
|
formatDates(
|
||||||
event.pinswg_dateeventrequested?.split("T")[0]
|
event.pinswg_dateeventrequested?.split("T")[0],
|
||||||
) || "No Date";
|
language
|
||||||
return ` - ${name} on ${date}`;
|
) || t.noDate;
|
||||||
|
return ` - ${name} ${t.on} ${date}`;
|
||||||
})
|
})
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
};
|
||||||
|
|
||||||
const formatReps = (reps = []) =>
|
const formatReps = (reps = [], language = "en") => {
|
||||||
reps
|
const labels = {
|
||||||
|
en: {
|
||||||
|
open: "Open",
|
||||||
|
to: "to",
|
||||||
|
close: "Close",
|
||||||
|
na: "N/A",
|
||||||
|
},
|
||||||
|
cy: {
|
||||||
|
open: "Agor",
|
||||||
|
to: "i",
|
||||||
|
close: "Cau",
|
||||||
|
na: "Dim ar gael",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const t = labels[language] || labels.en;
|
||||||
|
|
||||||
|
return reps
|
||||||
.map((rep) => {
|
.map((rep) => {
|
||||||
const start =
|
const start =
|
||||||
formatDates(rep.pinswg_consultationopen?.split("T")[0]) ||
|
formatDates(
|
||||||
"N/A";
|
rep.pinswg_consultationopen?.split("T")[0],
|
||||||
|
language
|
||||||
|
) || t.na;
|
||||||
const end =
|
const end =
|
||||||
formatDates(rep.pinswg_consultationclose?.split("T")[0]) ||
|
formatDates(
|
||||||
"N/A";
|
rep.pinswg_consultationclose?.split("T")[0],
|
||||||
return ` - Open: ${start}to Close: ${end}`;
|
language
|
||||||
|
) || t.na;
|
||||||
|
|
||||||
|
return ` - ${t.open}: ${start} ${t.to} ${t.close}: ${end}`;
|
||||||
})
|
})
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
};
|
||||||
|
|
||||||
const extractNameFromEmail = (email) =>
|
const extractNameFromEmail = (email) =>
|
||||||
email
|
email
|
||||||
@@ -115,6 +196,10 @@ const buildNotifyPayloads = (watchlistByEmail) => {
|
|||||||
);
|
);
|
||||||
if (filteredEntries.length === 0) return null;
|
if (filteredEntries.length === 0) return null;
|
||||||
|
|
||||||
|
const prefLanguage = entries[0].pinswg_preferredlanguage;
|
||||||
|
|
||||||
|
console.log("Language to use for email tmeplace:", prefLanguage);
|
||||||
|
|
||||||
const personalisation = {
|
const personalisation = {
|
||||||
contact_name: contactName,
|
contact_name: contactName,
|
||||||
case_sections: filteredEntries
|
case_sections: filteredEntries
|
||||||
@@ -126,7 +211,8 @@ const buildNotifyPayloads = (watchlistByEmail) => {
|
|||||||
|
|
||||||
const docSection = entry.documents?.length
|
const docSection = entry.documents?.length
|
||||||
? `Documents added or updated:\n${formatDocuments(
|
? `Documents added or updated:\n${formatDocuments(
|
||||||
entry.documents
|
entry.documents,
|
||||||
|
"en"
|
||||||
)}\n`
|
)}\n`
|
||||||
: "";
|
: "";
|
||||||
const sipSection = entry.sipEvents?.length
|
const sipSection = entry.sipEvents?.length
|
||||||
@@ -159,13 +245,67 @@ const buildNotifyPayloads = (watchlistByEmail) => {
|
|||||||
}`,
|
}`,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const personalisationCY = {
|
||||||
|
contact_name: contactName,
|
||||||
|
case_sections: filteredEntries
|
||||||
|
.map((entry) => {
|
||||||
|
const caseRef =
|
||||||
|
entry[
|
||||||
|
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
|
||||||
|
] || "Achos Anhysbys";
|
||||||
|
|
||||||
|
const docSection = entry.documents?.length
|
||||||
|
? `Dogfennau wedi'u hychwanegu neu eu diweddaru:\n${formatDocuments(
|
||||||
|
entry.documents,
|
||||||
|
"cy"
|
||||||
|
)}\n`
|
||||||
|
: "";
|
||||||
|
const sipSection = entry.sipEvents?.length
|
||||||
|
? `Digwyddiadau SIP wedi'u creu:\n${formatSipEvents(
|
||||||
|
entry.sipEvents,
|
||||||
|
"cy"
|
||||||
|
)}\n`
|
||||||
|
: "";
|
||||||
|
const repsSection = entry.repsPeriods?.length
|
||||||
|
? `Cyfnodau Ymgynghori:\n${formatReps(
|
||||||
|
entry.repsPeriods,
|
||||||
|
"cy"
|
||||||
|
)}\n`
|
||||||
|
: "";
|
||||||
|
|
||||||
|
const caseUnsubscribelink = `Dad-danysgrifio i ddiweddariadau ar yr achos hwn ${process.env.NEXTAUTH_URL}/unsubscribe/${entry.pinswg_watchlistid}`;
|
||||||
|
|
||||||
|
return `Achos: ${caseRef}\nDolen i'r achos: ${
|
||||||
|
process.env.NEXTAUTH_URL
|
||||||
|
}/case/${caseRef}\n\n${[
|
||||||
|
docSection,
|
||||||
|
sipSection,
|
||||||
|
repsSection,
|
||||||
|
caseUnsubscribelink,
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join("\n")}`;
|
||||||
|
})
|
||||||
|
.join("\n\n"),
|
||||||
|
unsubscribeLink: `Dad-danysgrifiwch drwy ddewis y ddolen hon os nad ydych chi eisiau derbyn unrhyw ddiweddariadau mwyach ${
|
||||||
|
process.env.NEXTAUTH_URL + "/unsubscribeall/" + contactId
|
||||||
|
}`,
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
email_address: email,
|
email_address: email,
|
||||||
template_id: templateId,
|
template_id:
|
||||||
personalisation,
|
prefLanguage == 846040001 ? templateId : templateIdCY,
|
||||||
|
personalisation:
|
||||||
|
prefLanguage == 846040001
|
||||||
|
? personalisation
|
||||||
|
: personalisationCY,
|
||||||
reference,
|
reference,
|
||||||
oneClickUnsubscribeURL:
|
oneClickUnsubscribeURL:
|
||||||
process.env.NEXTAUTH_URL + "/unsubscribeall/" + contactId,
|
process.env.NEXTAUTH_URL +
|
||||||
|
(prefLanguage == 846040001 ? "en" : "cy") +
|
||||||
|
"/unsubscribeall/" +
|
||||||
|
contactId,
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.filter(Boolean); // Remove any nulls from filtered-out users
|
.filter(Boolean); // Remove any nulls from filtered-out users
|
||||||
@@ -176,7 +316,7 @@ export default async function CombinedApiProxy(req, res) {
|
|||||||
const token = await getToken();
|
const token = await getToken();
|
||||||
|
|
||||||
// Step 1: Fetch all watchlist entries
|
// Step 1: Fetch all watchlist entries
|
||||||
const watchlistQuery = `pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications ne null&$expand=pinswg_Contact($select=contactid,emailaddress1,firstname,lastname)&$select=pinswg_emailnotifications,_pinswg_watchedcase_value`;
|
const watchlistQuery = `pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications ne null&$expand=pinswg_Contact($select=contactid,emailaddress1,firstname,lastname,pinswg_preferredlanguage)&$select=pinswg_emailnotifications,_pinswg_watchedcase_value`;
|
||||||
const watchlistRes = await axios.get(
|
const watchlistRes = await axios.get(
|
||||||
WEBAPI_URL + watchlistQuery + hashAPIPath(watchlistQuery),
|
WEBAPI_URL + watchlistQuery + hashAPIPath(watchlistQuery),
|
||||||
azureHeaders(token.access_token)
|
azureHeaders(token.access_token)
|
||||||
@@ -287,6 +427,8 @@ export default async function CombinedApiProxy(req, res) {
|
|||||||
email: payload.email_address,
|
email: payload.email_address,
|
||||||
reference: payload.reference,
|
reference: payload.reference,
|
||||||
template_id: payload.template_id,
|
template_id: payload.template_id,
|
||||||
|
language:
|
||||||
|
payload.pinswg_preferredlanguage == 846040001 ? "en" : "cy",
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user