Skip to content

Commit

Permalink
Merge pull request #65 from cepdnaclk/main
Browse files Browse the repository at this point in the history
deploy
  • Loading branch information
KATTA-00 authored Jan 26, 2024
2 parents 5f5778f + 491e84e commit 82bef19
Show file tree
Hide file tree
Showing 66 changed files with 1,403 additions and 298 deletions.
26 changes: 24 additions & 2 deletions code/backend/src/controllers/manager.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import managersInTeamService from "../services/managers.in.team.service";
import { sendVerificationEmail } from "../email/managerVerifyEmail";
import { sendInvitationEmail } from "../email/managerInviteEmail";
import { v4 as uuidv4 } from "uuid";
import { TeamPlayerResponse } from "../types/types";
import { AnalyticsSummaryTeam, TeamPlayerResponse } from "../types/types";

class ManagerController {
async createManager(
Expand All @@ -16,7 +16,7 @@ class ManagerController {
// Create a manager with an invitation token
const invitationToken = generateInvitationToken();
manager.invitationToken = invitationToken;
manager.isVerified = false; // Initially set to false
manager.isVerified = "pending"; // Initially set to pending

// const teamName = teamResponse.teamName;

Expand Down Expand Up @@ -178,6 +178,28 @@ class ManagerController {
}

}

//get Team Analytics
async getTeamAnalytics(teamId: string, duration:string): Promise<AnalyticsSummaryTeam> {

// 'Last Week' , 'Last Month' , 'All Time'
let durationNumber: number = 0;

if (duration == "All Time"){
durationNumber = Date.now();
} else if (duration == "Last Month"){
durationNumber = 30 * 24 * 60 * 60 * 1000;
} else if (duration == "Last Week"){
durationNumber = 7 * 24 * 60 * 60 * 1000;
}
try {
const response = await managersInTeamService.getTeamAnalytics(teamId, durationNumber);
return response;
} catch (error) {
console.error(error);
throw error;
}
}
}

function generateInvitationToken(): string {
Expand Down
26 changes: 26 additions & 0 deletions code/backend/src/controllers/team.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,32 @@ class TeamController {
}
return false;
}

async getAnalyticsSummary(
teamId: string,
duration: string
): Promise<void>{
// 'Last Week' , 'Last Month' , 'All Time'
let durationNumber: number = 0;

if (duration == "All Time"){
durationNumber = Date.now();
} else if (duration == "Last Month"){
durationNumber = 30 * 24 * 60 * 60 * 1000;
} else if (duration == "Last Week"){
durationNumber = 7 * 24 * 60 * 60 * 1000;
}
// console.log(durationNumber);

try {
const analyticsSummary = await teamService.getAnalyticsSummary(teamId, durationNumber);
// return analyticsSummary;
} catch (error) {
console.error(error);
throw new Error("Error in player service");
}

}
}

export default new TeamController();
9 changes: 7 additions & 2 deletions code/backend/src/db/manager.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ interface ManagerDocument extends Document {
lastName: string;
email: string;
invitationToken: string;
isVerified: boolean;
isVerified: string;
}
const verificationStatusValidator = function(value: string) {
if (!['pending', 'verified', 'rejected'].includes(value)) {
throw new Error('Invalid verification status');
}
};

const managerSchema = new Schema({
teamId: String,
firstName: String,
lastName: String,
email: String,
invitationToken: String,
isVerified: Boolean,
isVerified: { type: String, validate: verificationStatusValidator },
});

const ManagerModel = mongoose.model<ManagerDocument>("Manager", managerSchema);
Expand Down
10 changes: 8 additions & 2 deletions code/backend/src/db/managers.in.team.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ import mongoose, { Document, Schema } from "mongoose";
interface ManagerTeamDocument extends Document {
managerEmail: string;
teamId: string;
accepted: boolean;
accepted: string;
}

const verificationStatusValidator = function(value: string) {
if (!['pending', 'verified', 'rejected'].includes(value)) {
throw new Error('Invalid verification status');
}
};

const managerTeamSchema = new Schema({
managerEmail: String,
teamId: String,
accepted: Boolean,
accepted: { type: String, validate: verificationStatusValidator },
});

const ManagerTeamModel = mongoose.model<ManagerTeamDocument>(
Expand Down
8 changes: 7 additions & 1 deletion code/backend/src/db/player.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ interface PlayerDocument extends Document {
isVerified: string;
}

const verificationStatusValidator = function(value: string) {
if (!['pending', 'verified', 'rejected'].includes(value)) {
throw new Error('Invalid verification status');
}
};

const playerSchema = new Schema({
email: String,
invitationToken: String,
isVerified: String,
isVerified: { type: String, validate: verificationStatusValidator },
});

const PlayerModel = mongoose.model<PlayerDocument>("Player", playerSchema);
Expand Down
7 changes: 6 additions & 1 deletion code/backend/src/db/players.in.team.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ interface PlayerTeamDocument extends Document {
isVerified: string;
}

const verificationStatusValidator = function(value: string) {
if (!['pending', 'verified', 'rejected'].includes(value)) {
throw new Error('Invalid verification status');
}
};
const playerTeamSchema = new Schema({
playerEmail: String,
teamId: String,
jerseyId: Number,
fullName: String,
invitationToken: String,
isVerified: String,
isVerified: { type: String, validate: verificationStatusValidator },

});

Expand Down
2 changes: 1 addition & 1 deletion code/backend/src/email/managerInviteEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function sendInvitationEmail(
<p>Hello,</p>
<p>You've been invited to join the team ${teamName}! Click the following link to accept the invitation:</p>
<a href="http://localhost:5000/manager/accept-invitation/token/${invitationToken}"
<a href="http://16.170.235.219:5000/manager/accept-invitation/token/${invitationToken}"
style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: #ffffff;
text-decoration: none; border-radius: 5px;">Accept Invitation</a><br><br>
Expand Down
4 changes: 2 additions & 2 deletions code/backend/src/email/managerVerifyEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export async function sendVerificationEmail(
html: `
<p>Hello,</p>
<p>You've been created the team ${teamName}! Click the following link to verify your email:</p>
<p>To confirm creation of the team ${teamName}, click the button and verify your email address</p>
<a href="http://localhost:5000/manager/accept-invitation/token/${invitationToken}"
<a href="http://16.170.235.219:5000/manager/accept-invitation/token/${invitationToken}"
style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: #ffffff;
text-decoration: none; border-radius: 5px;">Verify Email Here</a><br><br>
Expand Down
4 changes: 2 additions & 2 deletions code/backend/src/email/playerInviteEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export async function sendInvitationEmail(
html: `
<p>Hello ${fullName},</p>
<p>You've been invited to join the team ${teamName}! </p>
<p>You've been invited to join the team ${teamName}! Click the following link to accept the invitation:</p>
<a href="http://localhost:5000/player/accept-invitation/token/${invitationToken}"
<a href="http://16.170.235.219:5000/player/accept-invitation/token/${invitationToken}"
style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: #ffffff;
text-decoration: none; border-radius: 5px;">Accept Invitation</a><br><br>
Expand Down
5 changes: 2 additions & 3 deletions code/backend/src/email/playerVerifyEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export async function sendVerificationEmail(
html: `
<p>Hello,</p>
<p>Thank you for registering with Impax!</p>
<p>You've been created the account successfully! </p>
<p>Your account has been created successfully! To log in to account verify your email address</p>
<a href="http://localhost:5000/player/verify-email/token/${invitationToken}"
<a href="http://16.170.235.219:5000/player/verify-email/token/${invitationToken}"
style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: #ffffff;
text-decoration: none; border-radius: 5px;">Verify Email Here</a><br><br>
Expand Down
1 change: 1 addition & 0 deletions code/backend/src/exceptions/http.codes.mgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum HttpMsg {
MANAGER_LOGIN_FAILED = "Login failed",
MANAGER_ADD_SUCCESS = "Manager added successfully",
MANAGER_ADD_FAILED = "Manager added failed",
MANAGER_NOT_FOUND = "Manager not found",
TEAM_NOT_FOUND = "Team not found",
MANAGER_DEOS_NOT_EXIST = "Manager does not exist",
PLAYER_EXIT_ERROR = "Player exited error",
Expand Down
10 changes: 5 additions & 5 deletions code/backend/src/models/manager.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Manager {
public email: string;
public password: string;
public invitationToken: string;
public isVerified: boolean;
public isVerified: string;

public constructor(
teamId: string,
Expand All @@ -22,7 +22,7 @@ class Manager {
email: string,
password: string,
invitationToken: string,
isVerified: boolean
isVerified: string
) {
this.teamId = teamId;
this.firstName = firstName;
Expand All @@ -39,7 +39,7 @@ class ManagerResponse {
public firstName: string;
public lastName: string;
public email: string;
public isVerified: boolean;
public isVerified: string;

public constructor(manager: ManagerResponse) {
this.teamId = manager.teamId;
Expand All @@ -57,7 +57,7 @@ class ManagerRequestBody {
public email: string;
public password: string;
public invitationToken: string;
public isVerified: boolean;
public isVerified: string;

public constructor(
teamId: string,
Expand All @@ -66,7 +66,7 @@ class ManagerRequestBody {
email: string,
password: string,
invitationToken: string,
isVerified: boolean
isVerified: string
) {
this.teamId = teamId;
this.firstName = firstName;
Expand Down
51 changes: 46 additions & 5 deletions code/backend/src/routes/manager.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ router.post("/", async (req: Request, res: Response) => {
email,
password,
"", // Initially set to empty string
false // Initially set to false
"pending" // Initially set to pending
);

// Create the manager and get the response
Expand Down Expand Up @@ -255,19 +255,60 @@ router.get("/getTeamPlayers",async (req:Request, res: Response) => {
}
});

// Endpoint to get Team Analytics
router.get("/analytics-summary/:duration", async (req: Request, res: Response) => {
const managerEmail = req.body.userName;
const teamId = req.body.teamId;
// check the request comes from the manager
if (req.body.role != ROLES.MANAGER) {
console.log(HttpMsg.UNAUTHORIZED);
res.status(HttpCode.UNAUTHORIZED).send({ message: HttpMsg.BAD_REQUEST });
return;
}

if (!managerEmail) {
console.log(HttpMsg.BAD_REQUEST);
res.status(HttpCode.BAD_REQUEST).send({ message: HttpMsg.BAD_REQUEST });
return;
}

try {
const managerExists = await managerController.checkManagerExistsInTeam(
managerEmail,
teamId
);

if (managerExists) {
const teamAnalyticsResponse =
await managerController.getTeamAnalytics(teamId, req.params.duration);
res.send(teamAnalyticsResponse);
} else {
throw new Error(HttpMsg.MANAGER_DEOS_NOT_EXIST);
}
} catch (err) {
if (err instanceof Error) {
// If 'err' is an instance of Error, send the error message
res.status(HttpCode.BAD_REQUEST).send({ message: err.message });
} else {
// If 'err' is of unknown type, send a generic error message
res.status(HttpCode.BAD_REQUEST).send({ message: HttpMsg.BAD_REQUEST });
}
}
});

// Endpoint Accept Invitation
router.get("/accept-invitation/token/:token", async (req, res) => {
const token = req.params.token;
const manager = await ManagerModel.findOne({ invitationToken: token });
const managerInTeam = await ManagerTeamModel.findOne({ invitationToken: token });
if (manager && !manager.isVerified) {
if (manager && (manager.isVerified == "pending")) {
// Update manager status
manager.isVerified = true;
manager.isVerified = "verified";
await manager.save();
res.send("Invitation accepted successfully!");
} else if (managerInTeam && !managerInTeam.accepted){
} else if (managerInTeam && (managerInTeam.accepted == "pending")){
// Update manager status
managerInTeam.accepted = true;
managerInTeam.accepted = "verified";
await managerInTeam.save();
} else{
res.status(400).send("Invalid or expired token.");
Expand Down
12 changes: 7 additions & 5 deletions code/backend/src/routes/player.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ router.put("/update", async (req: Request, res: Response) => {
}

try {
const player = await PlayerTeamModel.findOne({
const player = await PlayerTeamModel.find({
jesryId: jerseyId,
teamId: teamId });

console.log(player );

const playerTeamRequest = new PlayerTeamRequest(
newPlayerEmail,
jerseyId,
Expand Down Expand Up @@ -308,9 +310,9 @@ router.get("/accept-invitation/token/:token", async (req, res) => {
const token = req.params.token;
// const player = await PlayerModel.findOne({ invitationToken: token });
const playerInTeam = await PlayerTeamModel.findOne({ invitationToken: token });
if (playerInTeam && playerInTeam.isVerified == "Pending") {
if (playerInTeam && playerInTeam.isVerified == "pending") {
// Update player status
playerInTeam.isVerified = "Accepted";
playerInTeam.isVerified = "verified";
await playerInTeam.save();
res.send("Invitation accepted successfully!");
}
Expand All @@ -324,9 +326,9 @@ router.get("/verify-email/token/:token", async (req, res) => {
const token = req.params.token;
const player = await PlayerModel.findOne({ invitationToken: token });
// const playerInTeam = await PlayerTeamModel.findOne({ invitationToken: token });
if (player && player.isVerified == "Pending") {
if (player && player.isVerified == "pending") {
// Update player status
player.isVerified = "Accepted";
player.isVerified = "verified";
await player.save();
res.send("Invitation accepted successfully!");
}
Expand Down
3 changes: 2 additions & 1 deletion code/backend/src/routes/team.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ router.post("/manager", async (req, res) => {
email,
password,
"",
false
"pending"
);

// Create the Team and get the response
Expand Down Expand Up @@ -277,5 +277,6 @@ router.get("/:id", async (req: Request, res: Response) => {
}
});


// Export the router for use in other files
export default router;
Loading

0 comments on commit 82bef19

Please sign in to comment.