Skip to content

Commit

Permalink
feat: email for onboarding and offboarding team mems
Browse files Browse the repository at this point in the history
  • Loading branch information
NithinKuruba committed Dec 20, 2023
1 parent fb4c735 commit 0556984
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 2 deletions.
36 changes: 35 additions & 1 deletion app/pages/api/realms/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
mergePullRequest,
} from 'utils/github';
import omit from 'lodash.omit';
import { sendDeleteEmail, sendUpdateEmail } from 'utils/mailer';
import { offboardRealmAdmin, onboardNewRealmAdmin, sendDeleteEmail, sendUpdateEmail } from 'utils/mailer';

interface ErrorData {
success: boolean;
Expand Down Expand Up @@ -217,6 +217,40 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
console.error(`Error sending email for ${updatedRealm.realm}`, err),
);

let typeOfContactUpdate = '';

if (
currentRequest.approved &&
currentRequest.status === StatusEnum.APPLIED &&
currentRequest.productOwnerEmail !== updateRequest.productOwnerEmail
) {
typeOfContactUpdate = 'Product Owner';
onboardNewRealmAdmin(
session,
updatedRealm,
currentRequest.productOwnerEmail!,
updatedRealm.productOwnerEmail,
typeOfContactUpdate,
);
offboardRealmAdmin(session, updatedRealm, currentRequest.productOwnerEmail!, typeOfContactUpdate);
}

if (
currentRequest.approved &&
currentRequest.status === StatusEnum.APPLIED &&
currentRequest.technicalContactEmail !== updateRequest.technicalContactEmail
) {
typeOfContactUpdate = 'Technical Contact';
onboardNewRealmAdmin(
session,
updatedRealm,
currentRequest.technicalContactEmail!,
updatedRealm.technicalContactEmail,
typeOfContactUpdate,
);
offboardRealmAdmin(session, updatedRealm, currentRequest.technicalContactEmail!, typeOfContactUpdate);
}

return res.send(updatedRealm);
} catch (err) {
await createEvent({
Expand Down
Binary file added app/public/offboard-realm-admin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/public/onboard-realm-admin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
141 changes: 140 additions & 1 deletion app/utils/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Roster } from '@prisma/client';
import { generateRealmLinksByEnv, generateMasterRealmLinksByEnv } from './helpers';

const { serverRuntimeConfig = {} } = getConfig() || {};
const { app_env } = serverRuntimeConfig;
const { app_env, sso_logout_redirect_uri } = serverRuntimeConfig;
const subjectPrefix = app_env === 'development' ? '[DEV] ' : '';

const emailHeader = `
Expand Down Expand Up @@ -312,3 +312,142 @@ export const sendReadyToUseEmail = (realm: Roster) => {
subject: `${prefix}Important: Custom Realm ${realmName} Created and Action Required for Realm Admin Configuration`,
});
};

export const onboardNewRealmAdmin = (
session: Session,
realm: Roster,
oldContact: string,
newContact: string,
contactType: string,
) => {
const nonUpdatedTeamMemberEmail = contactType.startsWith('Product')
? realm.technicalContactEmail
: realm.productOwnerEmail;
const username = `${session.user.given_name} ${session.user.family_name}`;
const realmName = realm.realm!;
const to = [newContact!, nonUpdatedTeamMemberEmail!].filter((email) => email);
if (!to.length) return;
return sendEmail({
to,
body: `
${emailHeader}
<p>
We're reaching out to update you on changes to the ${contactType} details within the ${
realm.realm
} Custom realm. As of
${new Date().toLocaleDateString()}, ${username} has modified the contact information in the Realm Registry, replacing ${oldContact} with the
updated information for the ${realm.realm}. We want to ensure you're informed about these recent updates.
</p>
<p>To ensure a smooth transition, please follow instructions below to make ${newContact} the Realm Admin</p>
<strong>Grant Realm Admin Access to the new team members</strong>
<p>To add others as realm admins via a user friendly URL:</p>
<ol type="a">
<li>
<p>Ask your new team member to login at</p>
<ul>
<li><p><code><a href="${generateRealmLinksByEnv('dev', realmName)}">${generateRealmLinksByEnv(
'dev',
realmName,
)}</a></code></p></li>
<li><p><code><a href="${generateRealmLinksByEnv('test', realmName)}">${generateRealmLinksByEnv(
'test',
realmName,
)}</a></code></p></li>
<li><p><code><a href="${generateRealmLinksByEnv('prod', realmName)}">${generateRealmLinksByEnv(
'prod',
realmName,
)}</a></code></p></li>
</ul>
</li>
<li>
<p>They will see a forbidden message <code>Forbidden: You don't have access to the requested resource</code></p>
</li>
<li>
<p>You or one of the existing Realm Admins will need to add the user that logged in. See image below.</p>
<img src="${sso_logout_redirect_uri}/onboard-realm-admin.png" alt="OnBoardNewRealmAdmin" style="width:650px">
</li>
<li>
<p>Once you&rsquo;ve done this, you and your realm admins can access your realm via a more user friendly url</p>
<p>
<span style="color: #ff0000"><strong>PLEASE SAVE THIS USER FRIENDLY LINK</strong></span> as User Friendly Realm
Admin Links
</p>
<ul>
<li>
<p>
<code
><a href="${generateRealmLinksByEnv('dev', realmName)}"
>${generateRealmLinksByEnv('dev', realmName)}</a
></code
>
</p>
</li>
<li>
<p>
<code
><a href="${generateRealmLinksByEnv('test', realmName)}"
>${generateRealmLinksByEnv('test', realmName)}</a
></code
>
</p>
</li>
<li>
<p>
<code
><a href="${generateRealmLinksByEnv('prod', realmName)}"
>${generateRealmLinksByEnv('prod', realmName)}</a
></code
>
</p>
</li>
</ul>
</li>
</ol>
<p>
If you have any questions or require further assistance, feel free to reach out to us by Rocket.Chat or email at:
<a href="mailto:[email protected]">[email protected]</a>
</p>
${emailFooter}
`,
subject: `${subjectPrefix}Important: Custom Realm ${realm.realm} contact information has been updated. Action required to Onboard New Realm Admin.`,
});
};

export const offboardRealmAdmin = (session: Session, realm: Roster, oldContact: string, contactType: string) => {
const nonUpdatedTeamMemberEmail = contactType.startsWith('Product')
? realm.technicalContactEmail
: realm.productOwnerEmail;
const username = `${session.user.given_name} ${session.user.family_name}`;
const to = [oldContact!, nonUpdatedTeamMemberEmail!].filter((email) => email);
if (!to.length) return;
return sendEmail({
to,
body: `
${emailHeader}
<p>
We're reaching out to update you on changes to the ${contactType} details within the ${
realm.realm
} Custom realm. As of
${new Date().toLocaleDateString()}, ${username} has modified the contact information in the Realm Registry, replacing ${oldContact} with the
updated information for the ${realm.realm}. We want to ensure you're informed about these recent updates.
</p>
<p>Please follow instructions below to remove ${oldContact} as the Realm Admin</p>
<strong>Offboarding instructions to remove an existing realm admin</strong>
<ol type="a">
<li>
<p>We recommend deleting the offboarded team member from your custom realm. See image below.</p>
<img src="${sso_logout_redirect_uri}/offboard-realm-admin.png" alt="OffBoardRealmAdmin" style="width:650px">
</li>
<li>
<p>As you and your realm admins may have configured the user to a realm level role or realm level group, please remove the user accordingly.</p>
</li>
</ol>
<p>
If you have any questions or require further assistance, feel free to reach out to us by Rocket.Chat or email at:
<a href="mailto:[email protected]">[email protected]</a>
</p>
${emailFooter}
`,
subject: `${subjectPrefix}Important: Custom Realm ${realm.realm} contact information has been updated. Action required to Offboard existing Realm Admin.`,
});
};

0 comments on commit 0556984

Please sign in to comment.