Skip to content

Commit

Permalink
feat: ready to use email after terraform applies the realm and improv…
Browse files Browse the repository at this point in the history
…ed consistency for emails (#142)

* feat: send ready to use email after realm is applied

* feat: minor tweaks
  • Loading branch information
NithinKuruba authored Dec 18, 2023
1 parent be7c5f1 commit 2ca2642
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 40 deletions.
19 changes: 7 additions & 12 deletions app/page-partials/my-dashboard/RealmURIs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import styled from 'styled-components';
import Link from '@button-inc/bcgov-theme/Link';
import { RealmProfile } from 'types/realm-profile';
import { generateRealmLinksByEnv } from 'utils/helpers';

const Title = styled.div`
font-weight: 700;
Expand All @@ -14,27 +15,21 @@ interface Props {
}

function RealmURIs({ realm }: Props) {
const devURL = generateRealmLinksByEnv('dev', realm.realm);
const testURL = generateRealmLinksByEnv('test', realm.realm);
const prodURL = generateRealmLinksByEnv('prod', realm.realm);
return (
<>
<Title>Development</Title>
<Link
href={`https://dev.loginproxy.gov.bc.ca/auth/admin/${realm.realm}/console`}
external
>{`https://dev.loginproxy.gov.bc.ca/auth/admin/${realm.realm}/console`}</Link>
<Link href={`${devURL}`} external>{`${devURL}`}</Link>
<br />
<br />
<Title>Test</Title>
<Link
href={`https://test.loginproxy.gov.bc.ca/auth/admin/${realm.realm}/console`}
external
>{`https://test.loginproxy.gov.bc.ca/auth/admin/${realm.realm}/console`}</Link>
<Link href={`${testURL}`} external>{`${testURL}`}</Link>
<br />
<br />
<Title>Production</Title>
<Link
href={`https://loginproxy.gov.bc.ca/auth/admin/${realm.realm}/console`}
external
>{`https://loginproxy.gov.bc.ca/auth/admin/${realm.realm}/console`}</Link>
<Link href={`${prodURL}`} external>{`${prodURL}`}</Link>
</>
);
}
Expand Down
4 changes: 3 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 { sendDeleteEmail, sendReadyToUseEmail, sendUpdateEmail } from 'utils/mailer';

interface ErrorData {
success: boolean;
Expand Down Expand Up @@ -212,9 +212,11 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse<
details: getUpdatedProperties(currentRequest, updatedRealm),
});
updatedRealm = !isAdmin ? omit(updatedRealm, adminOnlyFields) : updatedRealm;

sendUpdateEmail(updatedRealm, session, updatingApprovalStatus).catch((err) =>
console.error(`Error sending email for ${updatedRealm.realm}`, err),
);

return res.send(updatedRealm);
} catch (err) {
await createEvent({
Expand Down
3 changes: 2 additions & 1 deletion app/pages/api/realms/pending.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import prisma from 'utils/prisma';
import { ActionEnum, EventEnum, StatusEnum, realmPlanAndApplySchema } from 'validators/create-realm';
import { ValidationError } from 'yup';
import { removeUserAsRealmAdmin } from 'controllers/keycloak';
import { sendDeletionCompleteEmail } from 'utils/mailer';
import { sendDeletionCompleteEmail, sendReadyToUseEmail } from 'utils/mailer';

const { serverRuntimeConfig = {} } = getConfig() || {};
const { gh_api_token, idir_requestor_user_guid } = serverRuntimeConfig;
Expand Down Expand Up @@ -121,6 +121,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}
},
);
await sendReadyToUseEmail(currentRequest!);
} catch (err) {
console.trace(err);
console.error(err);
Expand Down
14 changes: 14 additions & 0 deletions app/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import prisma from './prisma';
import { diff } from 'deep-diff';
import getConfig from 'next/config';

const { serverRuntimeConfig = {} } = getConfig() || {};
const { dev_kc_url, test_kc_url, prod_kc_url } = serverRuntimeConfig;

export enum RoleEnum {
ADMIN = 'admin',
Expand Down Expand Up @@ -57,3 +61,13 @@ export const createEvent = async (data: any) => {
export const getUpdatedProperties = (originalData: any, newData: any) => {
return diff(originalData, newData);
};

export const generateRealmLinksByEnv = (env: string, realmName: string) => {
const domain = env === 'dev' ? dev_kc_url : env === 'test' ? test_kc_url : prod_kc_url;
return `${domain}/auth/admin/${realmName}/console/`;
};

export const generateMasterRealmLinksByEnv = (env: string, realmName: string) => {
const domain = env === 'dev' ? dev_kc_url : env === 'test' ? test_kc_url : prod_kc_url;
return `${domain}/auth/admin/master/console/#/realms/${realmName}`;
};
164 changes: 138 additions & 26 deletions app/utils/mailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import getConfig from 'next/config';
import { Session } from 'next-auth';
import { RealmProfile } from 'types/realm-profile';
import { Roster } from '@prisma/client';
import { generateRealmLinksByEnv, generateMasterRealmLinksByEnv } from './helpers';

const { serverRuntimeConfig = {} } = getConfig() || {};
const { app_env } = serverRuntimeConfig;
Expand Down Expand Up @@ -122,50 +123,53 @@ const emailFooter = `
</footer>
`;

export const sendUpdateEmail = (realm: RealmProfile, session: Session, updatingApprovalStatus: boolean) => {
let message: string = `<h2>Your Realm Registry has been updated.</h2>
<p>
<strong>Project name: </strong>${realm.realm}<br /><strong>Updated by: </strong>${session.user?.given_name} ${session.user?.family_name}
</p>`;
let subject = `${subjectPrefix}Realm Registry has been updated`;
export const sendUpdateEmail = (realm: any, session: any, updatingApprovalStatus: boolean) => {
const prefix = app_env === 'development' ? '[DEV] ' : '';
let message: string = `
<main>
<p>The information for your Custom Realm has been successfully updated within our Realm Registry.</p>
<p><strong>Realm: </strong>${realm.realm}<br /><strong>Updated by: </strong>${session.user?.given_name} ${session.user?.family_name}</p>
</main>`;
let subject = `${prefix}Notification: Realm ${realm.realm} Information Updated in Realm Registry`;

if (updatingApprovalStatus && realm.approved === true) {
message = `
<p>This custom realm request has been approved and is under processing. Please wait up to 24 hours to get an update from us.</p>
`;
subject = `${subjectPrefix}Update: Custom Realm Request is in process`;
<main>
<p>We're pleased to inform you that your request for the Custom Realm ${realm.realm} has been approved and is currently being processed. Kindly anticipate an update from us within the next 24 hours.</p>
</main>`;
subject = `${prefix}Important: Your request for Custom Realm ${realm.realm} has been Approved`;
} else if (updatingApprovalStatus && realm.approved === false) {
message = `
<p>An SSO team member will be in touch with you to explain why this was declined.</p>
`;
subject = `${subjectPrefix}Update: Custom Realm Request has been declined`;
<main>
<p>We regret to inform you that your request for the Custom Realm ${realm.realm} has been declined. A member of our SSO team will reach out to you shortly to provide further details on the reasons behind this decision.</p>
</main>
`;
subject = `${prefix}Important: Your request for Custom Realm ${realm.realm} has been Declined`;
}

return sendEmail({
to: [realm.technicalContactEmail, realm.productOwnerEmail],
body: `
${emailHeader}
${message}
${emailFooter}
`,
${emailHeader}
${message}
${emailFooter}
`,
subject,
});
};

export const sendCreateEmail = (realm: Roster) => {
const prefix = app_env === 'development' ? '[DEV] ' : '';
return sendEmail({
to: [realm.technicalContactEmail!, realm.productOwnerEmail!],
body: `
${emailHeader}
<main style="padding: 0 30px;">
<p>We have received your request for a Custom Realm. Please be assured that someone from our team is looking
into
your request and will reach out soon.</p>
<br />
</main>
${emailFooter}
`,
subject: `${subjectPrefix}Realm Registry request received`,
${emailHeader}
<main style="padding: 0 30px;">
<p>We've received a request for the Custom Realm <strong>${realm.realm}</strong>, submitted by ${realm.requestor}. Rest assured, our team is actively reviewing your request and will be in touch shortly.</p>
</main>
${emailFooter}
`,
subject: `${prefix}Confirmation: Your Request for Custom Realm ${realm.realm} Has Been Received`,
});
};

Expand Down Expand Up @@ -199,3 +203,111 @@ export const sendDeletionCompleteEmail = (realm: Roster) => {
subject: `${subjectPrefix}Notification: Custom Realm ${realm.realm} has now been Deleted.`,
});
};

export const sendReadyToUseEmail = (realm: Roster) => {
const prefix = app_env === 'development' ? '[DEV] ' : '';
const realmName = realm.realm!;
return sendEmail({
to: [realm.technicalContactEmail!, realm.productOwnerEmail!],
body: `
${emailHeader}
<main>
<p>Your custom realm <strong>${realm.realm}</strong> is ready to be accessed.</p>
<p>Please follow the instructions below to add Realm Admins:</p>
<ol>
<li>
<p>You are a Realm Admin: Please save these links URLs of your custom realm</p>
<ul>
<li><p><code><a href="${generateRealmLinksByEnv('dev', realmName)}">${generateRealmLinksByEnv(
'dev',
realmName,
)}</a></p></code></li>
<li><p><code><a href="${generateRealmLinksByEnv('test', realmName)}"></a>${generateRealmLinksByEnv(
'test',
realmName,
)}</p></code></li>
<li><p><code><a href="${generateRealmLinksByEnv('prod', realmName)}">${generateRealmLinksByEnv(
'prod',
realmName,
)}</a></p></code></li>
</ul>
</li>
<li>
<p>You must have your identity provider configured. Please follow these <a href="https://stackoverflow.developer.gov.bc.ca/questions/864">instructions</a></strong></p>
</li>
<li>
<p> User friendly URLS and configure additional realm admins. To add yourself and others as realm admins via a user friendly url:</p>
<ol type="a">
<li>
<p>Log into your custom realm using below links so that you and other admins can be imported into the custom realm</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>At this point you will see a <code>forbidden</code> message</p>
</li>
<li>
<p>One of the existing Realm Admins will need to add the user that logged in to #1 above to the custom realm admin group via the <strong>master</strong> links</p>
<ul>
<li>
<p><code><a href="${generateMasterRealmLinksByEnv(
'dev',
realmName,
)}">${generateMasterRealmLinksByEnv('dev', realmName)}</a></code></p>
</li>
<li><p><code><a href="${generateMasterRealmLinksByEnv(
'test',
realmName,
)}">${generateMasterRealmLinksByEnv('test', realmName)}</a></code></p></li>
<li><p><code><a href="${generateMasterRealmLinksByEnv(
'prod',
realmName,
)}">${generateMasterRealmLinksByEnv('prod', realmName)}</a></code></p></li>
</ul>
</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>
</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>
</main>
${emailFooter}
`,
subject: `${prefix}Important: Custom Realm ${realmName} Created and Action Required for Realm Admin Configuration`,
});
};

0 comments on commit 2ca2642

Please sign in to comment.