Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions server/src/integrations/email/balance-change-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import ejs from "ejs"
import { centsToDollarString } from "../currency/currency.js"

const templateSource: string = `
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />

<style>
body {
font-family: 'Courier New', Courier, monospace;
padding: 1%;
text-align: center;
}

@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
.email-body{
margin: 0px auto;
width: 90%;
}

</style>
<div class="email-body">
<img src="https://d1msoab4sbdxmc.cloudfront.net/email-images/SHED_all_horizontal_black_orange_white_bg.png"
alt="RIT SHED Logo" width="600px">
<h1>Construct Credit Account Balance Modification Notice</h1>

<h2>
<% if (info.type == "credit") { %>
Your Construct Credit balance has been credited <%= formatCents(info.amount) %>.
<% } else { %>
<h2> Your Construct Credit balance has been charged <%= formatCents(info.amount) %>.
<% } %>
This is not your Tiger Bucks balance.
</h2>
<h4> Reason: <%= info.desc %> </h4>

</div>
`

const template = ejs.compile(templateSource, { async: false })

function generateHTMLChange(desc:BalanceChangeInfo) {
const data = {
info: desc,
formatCents: centsToDollarString,
};
return template(data);
}

function generateTextChange(desc: BalanceChangeInfo) {
return desc.type
}

export type BalanceChangeInfo ={
amount: number
type: "credit" | "charge"
desc: string
}

export function generateBalanceChangeEmail(desc: BalanceChangeInfo): {text: string, html: string} {
const text = generateTextChange(desc);
const html = generateHTMLChange(desc);
return {text, html}
}
40 changes: 34 additions & 6 deletions server/src/integrations/email/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import FormData from "form-data";
import * as Mailgun from "mailgun.js"
import { generateReceiptEmail } from "./receipt-template.js"
import { generateExpiryEmail, ExpiryDescription } from "./training-expiry-template.js"
import { BalanceChangeInfo, generateBalanceChangeEmail } from "./balance-change-template.js";
import { generateHoldPlacedEmail } from "./hold-placed-template.js";
const mailgun = new Mailgun.default(FormData);
const mg = mailgun.client({ username: 'api', key: process.env.MAILGUN_API_KEY || 'key-yourkeyhere' });
const MAIL_DOMAIN = process.env.MAIL_DOMAIN ?? "";
Expand All @@ -13,12 +15,19 @@ type MessageSendResult = {
status: number;
details?: string;
}
const OVERRIDE_EMAIL_TARGET = process.env.OVERRIDE_EMAIL_TARGET;


export async function send_generic_email(args: { fromAccount: string, to: string[], subject: string, htmlContent: string, textContent: string }): Promise<MessageSendResult> {
if (process.env.NODE_ENV !== "development") {
let emailAddresses = args.to
if (OVERRIDE_EMAIL_TARGET) {
emailAddresses = [OVERRIDE_EMAIL_TARGET];
}
return mg.messages.create(MAIL_DOMAIN, {
from: `make@rit.edu <${args.fromAccount}@${MAIL_DOMAIN}>`,
bcc: ((process.env.NODE_ENV !== "development") ? ['make@rit.edu'] : []),
to: args.to,
to: emailAddresses,
subject: args.subject,
text: args.textContent,
html: args.htmlContent,
Expand All @@ -32,7 +41,6 @@ export async function send_generic_email(args: { fromAccount: string, to: string
}
}

const OVERRIDE_RECEIPT_EMAIL = process.env.OVERRIDE_RECEIPT_EMAIL;

/**
* Send an email describing a transaction of tigerbucks and or construct credits
Expand All @@ -45,10 +53,8 @@ export async function send_transaction_email(transactionId: number) {
console.error("Failed to create transaction receipt, id =", transactionId);
return;
}
let emailAddress = content.to;
if (OVERRIDE_RECEIPT_EMAIL) {
emailAddress = OVERRIDE_RECEIPT_EMAIL;
}
const emailAddress = content.to;

await send_generic_email({
fromAccount: 'receipts',
to: [emailAddress],
Expand All @@ -69,4 +75,26 @@ export async function send_training_expiry_email(email: string, desc: ExpiryDesc
textContent: content.text,
htmlContent: content.html,
})
}

export async function send_cc_balance_change_email(email: string, desc: BalanceChangeInfo) {
const content = generateBalanceChangeEmail(desc);
send_generic_email({
fromAccount: "cc",
to: [email],
subject: "RIT SHED: Account Balance Adjusted - " + new Date().toLocaleDateString(),
textContent: content.text,
htmlContent: content.html,
})
}

export async function send_hold_placed_email(email: string, desc: string) {
const content = generateHoldPlacedEmail(desc);
send_generic_email({
fromAccount: "holds",
to: [email],
subject: "RIT SHED: Hold Placed On Account - " + new Date().toLocaleDateString(),
textContent: content.text,
htmlContent: content.html,
})
}
47 changes: 47 additions & 0 deletions server/src/integrations/email/hold-placed-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import ejs from "ejs"

const templateSource: string = `
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
<style>
body {
font-family: 'Courier New', Courier, monospace;
padding: 1%;
text-align: center;
}
@media (prefers-color-scheme: dark) {
body {
background-color: black;
color: white;
}
}
.email-body{
margin: 0px auto;
width: 90%;
}
</style>
<div class="email-body">
<img src="https://d1msoab4sbdxmc.cloudfront.net/email-images/SHED_all_horizontal_black_orange_white_bg.png"
alt="RIT SHED Logo" width="600px">
<h2> A hold has been placed on your account for the following reason:</h2>
<blockquote> <%= desc %> </blockquote>
<p>
You wont be able to use makerspace equipment until the hold is resolved. Email <a href="mailto:make@rit.edu">make@rit.edu</a> to talk to professional staff for more information.
</p>


</div>
`

const template = ejs.compile(templateSource, { async: false })

function generateHTMLHold(desc: string) {
const data = {
desc: desc,
};
return template(data);
}

export function generateHoldPlacedEmail(desc: string): {text: string, html: string} {
const html = generateHTMLHold(desc);
return {text: html, html}
}
7 changes: 7 additions & 0 deletions server/src/resolvers/currencyAccountResolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApolloContext } from "../context.js";
import { CurrencyAccountsRow } from "../db/tables.js";
import { send_cc_balance_change_email } from "../integrations/email/email.js";
import * as CurrencyAccountRepo from "../repositories/Currency/CurrencyAccountsRepository.js"

export const CurrencyAccountResolvers = {
Expand Down Expand Up @@ -45,6 +46,12 @@ export const CurrencyAccountResolvers = {
},
{ isManager }: ApolloContext
) => {
const owner = await CurrencyAccountRepo.getAccountOwner(args.accountID);
send_cc_balance_change_email(owner?.username + "@rit.edu", {
amount: Math.abs(args.amount),
type: args.amount > 0 ? "credit" : "charge",
desc: args.description,
});
return isManager(async (user) => (
await CurrencyAccountRepo.adjustAccountBalanceCents(args.accountID, args.amount, "make-website", `adjustment by ${user.ritUsername}: ${args.description}`)
))
Expand Down
2 changes: 2 additions & 0 deletions server/src/resolvers/holdsResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as UsersRepo from "../repositories/Users/UserRepository.js";
import { createLog } from "../repositories/AuditLogs/AuditLogRepository.js";
import { getUsersFullName } from "../repositories/Users/UserRepository.js";
import { HoldRow } from "../db/tables.js";
import { send_hold_placed_email } from "../integrations/email/email.js";

const HoldsResolvers = {
Hold: {
Expand Down Expand Up @@ -51,6 +52,7 @@ const HoldsResolvers = {
);

await UsersRepo.setActiveHold(Number(args.userID), true)
send_hold_placed_email(user.ritUsername + "@rit.edu", args.description);
return HoldsRepo.createHold(user.id, Number(args.userID), args.description);
}),

Expand Down
Loading