Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | 8x 8x 8x 4x 4x 4x 1x 3x 8x 8x 8x 7x 2x 8x 8x 8x 7x 7x 6x 6x 7x 1x 8x 8x 8x 8x 12x 12x 11x 11x 11x 11x 11x 11x 11x 1x 1x 8x | import { getUserRoleTemplate } from "libs/email";
import { UserRoleEmailType } from "libs/email/content";
import { EmailAddresses } from "shared-types";
import { getSecret } from "shared-utils";
import {
createEmailParams,
ProcessEmailConfig,
sendEmail,
validateEmailTemplate,
} from "./processEmails";
import { getApproversByRoleState, getUserByEmail } from "./user-management/userManagementService";
export async function sendUserRoleEmails(
valueParsed: UserRoleEmailType,
timestamp: number,
config: ProcessEmailConfig,
) {
const record = {
...valueParsed,
timestamp,
applicationEndpointUrl: config.applicationEndpointUrl,
};
// if the status = pending -> AdminPendingNotice & AccessPendingNotice
const templates = [];
if (record.status === "pending") {
templates.push(await getUserRoleTemplate("AccessPendingNotice"));
templates.push(await getUserRoleTemplate("AdminPendingNotice"));
}
// if the status = denied AND doneByEmail = email -> SelfRevokeAdminChangeEmail
else if (record.status === "denied" && record.doneByEmail === record.email) {
templates.push(await getUserRoleTemplate("SelfRevokeAdminChangeEmail"));
}
// else -> AccessChangeNotice
else {
templates.push(await getUserRoleTemplate("AccessChangeNotice"));
}
// get the user's name
Eif (templates.length) {
try {
const userInfo = await getUserByEmail(record.email, {
domain: config.osDomain,
index: `${config.indexNamespace}users`,
});
record.fullName = userInfo.fullName;
} catch (error) {
console.error("Error trying to get user name:", error);
}
}
// get the approver list
Eif (templates.length) {
try {
const approverList: { email: string } | null[] = await getApproversByRoleState(
record.role,
record.territory,
{
domain: config.osDomain,
index: `${config.indexNamespace}roles`,
},
{
domain: config.osDomain,
index: `${config.indexNamespace}users`,
},
);
if (!approverList.length) console.log("NO APPROVERS FOUND");
const approverListFormated = approverList.map(
(approver: { email: string; fullName: string } | null) => {
Iif (!approver) return "";
return `${approver.fullName} <${approver.email}>`;
},
);
record.approverList = approverListFormated.filter((approver) => approver !== "");
} catch (error) {
console.log("Error trying to get approver list: ", error);
}
}
const results = [];
const secret = await getSecret(config.emailAddressLookupSecretName);
const emails: EmailAddresses = JSON.parse(secret);
for (const template of templates) {
try {
const filledTemplate = await template(record);
const currentCcEmails = filledTemplate?.cc ?? [];
// This is used for injecting a test email for higher environments. If this
// secret exists it will inject that secrets cc email into the user roles
// email templates that get sent out (useful for testing email sending in dev, and val)
const userRoleCc = emails.userRoleCc ? [emails.userRoleCc] : [];
filledTemplate.cc = [...currentCcEmails, ...userRoleCc];
validateEmailTemplate(filledTemplate);
const params = createEmailParams(
filledTemplate,
emails.accessEmail,
config.osDomain,
config.isDev,
);
const result = await sendEmail(params, config.region);
results.push({ success: true, result });
} catch (error) {
console.error("Error processing template:", error);
results.push({ success: false, error });
// Continue with next template instead of throwing
}
}
return;
}
|