Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending email functionality #97

Merged
merged 32 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1b3e0b5
Sending email functionality
YashRavipati1 Apr 24, 2024
91d681e
Resize Images (#96)
Azhou2023 May 9, 2024
02ad2f7
V2 filters frontend (#90)
RamtinTJB May 13, 2024
a805420
Profile Page: Manage Permissions (#91)
PoliteUnicorn May 15, 2024
4b5fa7d
Renter Candidate Page (#95)
nbpillai May 19, 2024
3519be5
Sending email functionality
YashRavipati1 Apr 24, 2024
16008e0
Removing email route
YashRavipati1 May 22, 2024
91719f8
hooking email functionality
YashRavipati1 May 22, 2024
0d60f9d
Merge branch 'feature/Yash/EmailSending' of github.com:TritonSE/USHS-…
YashRavipati1 May 22, 2024
281de8d
Sending email functionality
YashRavipati1 Apr 24, 2024
619e9ae
Removing email route
YashRavipati1 May 22, 2024
91f63ac
hooking email functionality
YashRavipati1 May 22, 2024
0ee3a71
Sending email functionality
YashRavipati1 Apr 24, 2024
f22c124
Demote User
YashRavipati1 May 22, 2024
4b28732
Merge branch 'feature/Yash/EmailSending' of github.com:TritonSE/USHS-…
YashRavipati1 May 22, 2024
c0e3d20
Removing email route
YashRavipati1 May 22, 2024
dc2ce34
Sending email functionality
YashRavipati1 Apr 24, 2024
b19e628
Merge branch 'feature/Yash/EmailSending' of github.com:TritonSE/USHS-…
YashRavipati1 May 23, 2024
9ebabe1
Revert "Demote User"
YashRavipati1 May 23, 2024
c330484
Demote User Email
YashRavipati1 May 23, 2024
7de4a9a
Removing route for email
YashRavipati1 May 23, 2024
4dd63e7
Sending email functionality
YashRavipati1 Apr 24, 2024
481340c
Revert "Sending email functionality"
YashRavipati1 May 23, 2024
60b9dfc
Revert "Resize Images (#96)"
YashRavipati1 May 23, 2024
ea24022
Revert "V2 filters frontend (#90)"
YashRavipati1 May 23, 2024
c539087
Revert "Profile Page: Manage Permissions (#91)"
YashRavipati1 May 23, 2024
1547380
Revert "Renter Candidate Page (#95)"
YashRavipati1 May 23, 2024
7a252ff
demoting user
YashRavipati1 May 23, 2024
59f5989
Cleaning up/removing frontend routes
YashRavipati1 May 23, 2024
d487fea
Fixing referall leased emails
YashRavipati1 May 28, 2024
e828607
assignedHousingLocator instead of renterCandidate
YashRavipati1 May 28, 2024
a05ffa5
build failure
petabite May 29, 2024
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
Binary file modified backend/bun.lockb
Binary file not shown.
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"http-errors": "^2.0.0",
"module-alias": "^2.2.3",
"mongodb": "^5.7.0",
"mongoose": "^7.4.0"
"mongoose": "^7.4.0",
"nodemailer": "^6.9.13"
},
"name": "backend",
"version": "1.0.0",
Expand All @@ -32,6 +33,7 @@
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/http-errors": "^2.0.1",
"@types/nodemailer": "^6.4.15",
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "latest",
"bun-types": "latest",
Expand Down
26 changes: 26 additions & 0 deletions backend/src/services/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import "dotenv/config";

import { createTransport } from "nodemailer";

export async function sendEmail(recipient: string, subjectString: string, body: string) {
const transporter = createTransport({
port: 465, // true for 465, false for other ports
host: "smtp.gmail.com",
auth: {
user: process.env.GMAILUSER,
pass: process.env.GMAILPASS,
},
secure: true,
});

const mailData = {
from: process.env.GMAILUSER, // sender address
to: recipient, // list of receivers
subject: subjectString,
text: body,
};

const response = await transporter.sendMail(mailData);

return response;
}
43 changes: 43 additions & 0 deletions backend/src/services/referral.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ObjectId } from "mongoose";

import { sendEmail } from "./email";
import { getUserByID } from "./user";

import { ReferralModel } from "@/models/referral";

export async function getUnitReferrals(id: string) {
Expand Down Expand Up @@ -27,17 +30,57 @@ export async function createReferral(
return referral;
}

const INACTIVE_REFERRAL_STATUSES = ["Leased", "Denied", "Canceled"];

export async function editReferral(
id: string,
assignedHousingLocatorId: string,
assignedReferringStaffId: string,
status: string,
) {
const Ref = await ReferralModel.findById(id);
const updateHL = Ref?.assignedHousingLocator?.toString() !== assignedHousingLocatorId;
const setLeased =
status === "Leased" &&
Ref?.status !== "Leased" &&
Ref?.status !== "Denied" &&
Ref?.status !== "Canceled";
await ReferralModel.findByIdAndUpdate(id, {
assignedHousingLocator: assignedHousingLocatorId,
assignedReferringStaff: assignedReferringStaffId,
status,
});
const referral = await ReferralModel.findById(id);
const HL = await getUserByID(referral?.assignedHousingLocator?.toString() ?? "");

if (updateHL) {
if (HL !== null) {
await sendEmail(
HL.email,
"Referral Update",
`You have been assigned a new referral. Please login to the portal to view the update.`,
);
}
}
if (setLeased) {
const refs = await getUnitReferrals(referral?.unitId.toString() ?? "");
const userPromises = [];
const emailPromises = [];
for (const ref of refs) {
if (!INACTIVE_REFERRAL_STATUSES.includes(ref.status)) {
userPromises.push(getUserByID(ref.assignedHousingLocator?.toString() ?? ""));
}
}
const users = await Promise.all(userPromises);
for (const user of users) {
if (user?.email) {
emailPromises.push(
sendEmail(user.email, "Referral Update", "One of your referrals has been leased."),
);
}
}
await Promise.all(emailPromises);
}

return referral;
}
22 changes: 22 additions & 0 deletions backend/src/services/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import { UserModel } from "../models/user";

import { sendEmail } from "./email";

// Fetch users from the database
export async function getUsers() {
const users = await UserModel.find({});
Expand Down Expand Up @@ -46,5 +48,25 @@ export async function getUserByID(id: string) {
}

export async function elevateUser(id: string) {
const RS = await getUserByID(id);
if (RS !== null) {
await sendEmail(
RS.email,
"Elevation to Housing Locator",
"You have been elevated to a Housing Locator",
);
}
return await UserModel.findByIdAndUpdate(id, { isHousingLocator: true });
}

export async function demoteUser(id: string) {
const RS = await getUserByID(id);
if (RS !== null) {
await sendEmail(
RS.email,
"Housing Locator Demotion",
"You have been demoted from a Housing Locator",
);
}
return await UserModel.findByIdAndUpdate(id, { isHousingLocator: false });
}
Binary file modified frontend/bun.lockb
Binary file not shown.
Loading