Skip to content

Commit

Permalink
feat: created email service for several operations
Browse files Browse the repository at this point in the history
  • Loading branch information
akshatmittal61 committed Sep 4, 2023
1 parent 61c0e6e commit db4155e
Show file tree
Hide file tree
Showing 3 changed files with 296 additions and 21 deletions.
46 changes: 45 additions & 1 deletion controllers/participation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import Participant from "@/models/Participant";
import Team from "@/models/Team";
import User from "@/models/User";
import { ApiRequest, ApiResponse } from "@/types/api";
import {
sendAcceptedInTeam,
sendRegistrationConfirmation,
sendRejectedInTeam,
sendRequestedInTeam,
sendWithdrawnFromEvent,
} from "@/utils/emails";
import mongoose from "mongoose";

export const getAllParticipants = async (req: ApiRequest, res: ApiResponse) => {
Expand Down Expand Up @@ -241,6 +248,10 @@ export const participateInEvent = async (req: ApiRequest, res: ApiResponse) => {
path: "user",
select: "name email avatar",
});
await sendRegistrationConfirmation(
newParticipant.user.email,
newParticipant.event.name
);
return res.status(201).json({
message: RESPONSE_MESSAGES.SUCCESS,
data: newParticipant,
Expand All @@ -267,6 +278,15 @@ export const participateInEvent = async (req: ApiRequest, res: ApiResponse) => {
user: req.user?.id,
status: TEAM_PARTICIPATION_STATUS.PENDING,
});
const foundTeamLeader = await User.findById(
foundTeam.createdBy.toString()
);
await sendRequestedInTeam(
foundTeamLeader.email,
foundEvent.name,
foundTeamLeader.name,
foundTeam.name
);
await newParticipant.populate({
path: "event",
select: "name description",
Expand Down Expand Up @@ -335,16 +355,30 @@ export const handleParticipantStatusInTeam = async (
message: "Please select a valid status to approve or reject",
});
}
const foundUser = await User.findById(foundParticipant.user.toString());
const foundEvent = await Event.findById(
foundParticipant.event.toString()
);
if (req.body.status === TEAM_PARTICIPATION_STATUS.ACCEPTED) {
foundParticipant.status = TEAM_PARTICIPATION_STATUS.ACCEPTED;
await foundParticipant.save();
await sendAcceptedInTeam(
foundUser?.email,
foundEvent.name,
foundTeam.name
);
} else if (req.body.status === TEAM_PARTICIPATION_STATUS.REJECTED) {
await Participant.findByIdAndDelete(foundParticipant._id);
await sendRejectedInTeam(
foundUser?.email,
foundEvent.name,
foundTeam.name
);
} else {
return res.status(400).json({
message: "Please select a valid status to approve",
});
}
await foundParticipant.save();
return res.status(200).json({
message: RESPONSE_MESSAGES.SUCCESS,
data: foundParticipant,
Expand Down Expand Up @@ -424,7 +458,15 @@ export const approveParticipant = async (req: ApiRequest, res: ApiResponse) => {
});
}
foundParticipant.status = TEAM_PARTICIPATION_STATUS.ACCEPTED;
const foundUser = await User.findById(
foundParticipant.user.toString()
);
await foundParticipant.save();
await sendAcceptedInTeam(
foundUser?.email,
foundEvent.name,
foundTeam.name
);
return res.status(200).json({
message: RESPONSE_MESSAGES.SUCCESS,
data: foundParticipant,
Expand Down Expand Up @@ -486,6 +528,8 @@ export const removeParticipantFromEvent = async (
}
}
await Participant.findByIdAndDelete(foundParticipant._id);
const foundUser = await User.findById(foundParticipant.user.toString());
await sendWithdrawnFromEvent(foundUser?.email, foundEvent.name);
return res.status(204).json({
message: RESPONSE_MESSAGES.SUCCESS,
data: foundParticipant,
Expand Down
13 changes: 13 additions & 0 deletions controllers/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Event from "@/models/Event";
import Participant from "@/models/Participant";
import Team from "@/models/Team";
import User from "@/models/User";
import { sendRegistrationConfirmation, sendTeamRemoved } from "@/utils/emails";

export const getAllTeams = async (req: ApiRequest, res: ApiResponse) => {
try {
Expand Down Expand Up @@ -97,6 +98,12 @@ export const createTeam = async (req: ApiRequest, res: ApiResponse) => {
user: req.user?.id,
status: TEAM_PARTICIPATION_STATUS.ACCEPTED,
});
const foundUser = await User.findById(req.user?.id);
await sendRegistrationConfirmation(
foundUser?.email,
foundEvent.name,
req.body.name
);
await newTeam.populate({
path: "createdBy",
select: "name email avatar",
Expand Down Expand Up @@ -139,8 +146,14 @@ export const removeTeam = async (req: ApiRequest, res: ApiResponse) => {
message: "Only the creator can delete this team",
});
}
const foundEvent = await Event.findById(foundTeam.event.toString());
await Participant.deleteMany({ team: teamId });
await Team.findByIdAndDelete(teamId);
await sendTeamRemoved(
foundUser?.email,
foundEvent?.name,
foundTeam.name
);
return res.status(204).json({ message: RESPONSE_MESSAGES.SUCCESS });
} catch (error: any) {
console.error(error);
Expand Down
Loading

0 comments on commit db4155e

Please sign in to comment.