Skip to content

Commit

Permalink
Merge pull request #55 from cepdnaclk/main
Browse files Browse the repository at this point in the history
deploy
  • Loading branch information
KATTA-00 authored Jan 21, 2024
2 parents 366e292 + 2c6df81 commit 66320df
Show file tree
Hide file tree
Showing 93 changed files with 4,549 additions and 1,254 deletions.
2 changes: 1 addition & 1 deletion code/backend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testTimeout: 10000, // or any other value in milliseconds
testTimeout: 30000, // or any other value in milliseconds
maxWorkers: 4,

clearMocks: true,
Expand Down
9 changes: 9 additions & 0 deletions code/backend/src/config/allowEndPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ const excludedRoutes = [
{ method: "POST", path: "/team" },
{ method: "POST", path: "/manager" },
{ method: "POST", path: "/team/manager" },
{ method: "GET", path: "/manager/accept-invitation/token" },
{ method: "GET", path: "/player/accept-invitation/token" },
{ method: "GET", path: "/player/verify-email/token" },
{ method: "POST", path: "/player" },

// Add more entries as needed
];

const excludedRoutesStartWith = [
{ method: "GET", path: "/team/exists/teamId" },
{ method: "GET", path: "/manager/exists/email" },
{ method: "GET", path: "/manager/accept-invitation/token" },
{ method: "GET", path: "/player/accept-invitation/token" },
{ method: "GET", path: "/player/verify-email/token" },

// Add more entries as needed
];

Expand Down
28 changes: 19 additions & 9 deletions code/backend/src/controllers/manager.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sendInvitationEmail } from "../email/managerInviteEmail";
import { Manager, ManagerResponse } from "../models/manager.model";
import TeamModel from "../db/team.schema";
import managerService from "../services/manager.service";
import managersInTeamService from "../services/managers.in.team.service";
import { sendVerificationEmail } from "../email/managerVerifyEmail";
import { sendInvitationEmail } from "../email/managerInviteEmail";
import { v4 as uuidv4 } from "uuid";

class ManagerController {
Expand All @@ -14,8 +14,8 @@ class ManagerController {
try {
// Create a manager with an invitation token
const invitationToken = generateInvitationToken();
manager.acceptInvitation = false;
manager.invitationToken = invitationToken;
manager.isVerified = false; // Initially set to false

// const teamName = teamResponse.teamName;

Expand Down Expand Up @@ -109,23 +109,33 @@ class ManagerController {
// acceptInvitation: false, // Initially set to false
// };

const createdManagerResponse =
await managersInTeamService.addManagerToTeam(newManagerEmail, teamId);
const managerTeamAdded = await managersInTeamService.addManagerToTeam(
newManagerEmail,
teamId
);

const teamInstance = await TeamModel.findOne({ teamId });
const teamName = teamInstance?.teamName; // Add null check using optional chaining operator

// Send an invitation email
await sendInvitationEmail(newManagerEmail, invitationToken, teamName!);

// Add the new manager to the team
const managerTeamAdded = await managersInTeamService.addManagerToTeam(
return managerTeamAdded;
} catch (error) {
console.error(error);

const exist = await managersInTeamService.checkManagerExistsInTeamDetails(
newManagerEmail,
teamId
);

return managerTeamAdded;
} catch (error) {
console.error(error);
if (exist) {
await managersInTeamService.deleteManagerFromTeamDetails(
newManagerEmail,
teamId
);
}

throw error;
}
return false;
Expand Down
137 changes: 124 additions & 13 deletions code/backend/src/controllers/player.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,139 @@ import playersInTeamService from "../services/players.in.team.service";
import managerService from "../services/manager.service";
import playerService from "../services/player.service";
import { HttpMsg } from "../exceptions/http.codes.mgs";
import { TeamIdEmailExistsResponse} from "../models/team.model";
import teamService from "../services/team.service";
import { v4 as uuidv4 } from "uuid";
import PlayerModel from "../db/player.schema";
import TeamModel from "../db/team.schema";
import { sendInvitationEmail } from "../email/playerInviteEmail";
import { sendVerificationEmail } from "../email/playerVerifyEmail";
import { findSourceMap } from "module";
import { PlayerInTeamResponse, PlayerRequestBody, PlayerResponse } from "../models/player.model";

class PlayerController {

// add player to the player team collection
async addNewPlayer(
jersyId: string,
fullName: string,
newPlayerEmail: string,
teamId: string,
managerEmail: string,
newManagerEmail: string,
teamId: string
): Promise<boolean> {

): Promise<PlayerInTeamResponse> {
try {
// check the manager exits in that team
// check the manager exits in that team
const managerExists = await managerService.checkManagerExistsInTeam(
managerEmail,
teamId
// check the team exist
const teamIdEmailExistsResponse: TeamIdEmailExistsResponse = await teamService.checkTeamEmailExist(teamId,managerEmail);
if (!teamIdEmailExistsResponse.teamExists) {
throw new Error(HttpMsg.TEAM_NOT_FOUND);
}
if (!teamIdEmailExistsResponse.managerExists) {
throw new Error(HttpMsg.MANAGER_DEOS_NOT_EXIST);
}
// check if player already exists in the team
const playerExistsInTeam = await playersInTeamService.checkPlayerExistsInTeam(
newPlayerEmail,
teamId,
);

if (!managerExists) {
throw new Error(HttpMsg.MANAGER_DEOS_NOT_EXIST);

if (playerExistsInTeam) {
throw new Error(HttpMsg.PLAYER_ALREADY_EXISTS_IN_TEAM);

// If player not exist in that team => added to the team (player in teams)
}else{
// Create a player with an invitation token
const invitationToken = generateInvitationToken();
const teamInstance = await TeamModel.findOne({ teamId });
const teamName = teamInstance?.teamName; // Add null check using optional chaining operator

const playerInTeamResponse = await playersInTeamService.addPlayerToTeam(
newPlayerEmail,
teamId,
jersyId,
fullName,
invitationToken
);

// Send the invitation email
await sendInvitationEmail(fullName, newPlayerEmail, invitationToken, teamName!);
return playerInTeamResponse
}

await playersInTeamService.addPlayerToTeam(newManagerEmail, teamId);

// // check if new player already has an account
// //TODO: Remove if user will not be able to create an account within 30 days
// const playerExists = await playerService.checkPlayerExists(newPlayerEmail);

// // If player not exist in the player collection => added to the player collection
// if (!playerExists) {

// await playerService.addPlayer(
// firstName,
// lastName,
// newPlayerEmail
// );
// }


} catch (error) {
console.error(error);
throw error;
}

}

// create player
async createPlayer(
email: string,
password: string
): Promise<PlayerResponse> {
try {
// check if player already has an account
const exists: boolean = await playerService.checkPlayerExists(email);

if (exists) {
throw new Error(HttpMsg.PLAYER_ALREADY_HAS_ACCOUNT);

} else {
// Create a new player account
// const playerRequestBody: PlayerRequestBody = new PlayerRequestBody(
// email,
// password
// );

// Create a player with an invitation token
const invitationToken = generateInvitationToken();
const playerResponse = await playerService.createPlayer(email, password, invitationToken);
// Send the verification email
await sendVerificationEmail(email, invitationToken);

return true;
return playerResponse;

}
} catch (error) {
console.error(error);
throw error;
}
return false;

}

//get player details
async getPlayer(
playerEmail: string
): Promise<PlayerResponse> {
try {
const playerResponse = await playerService.getPlayer(
playerEmail
);
return playerResponse;
} catch (error) {
console.error(error);
// Handle the error, either by returning a default value or throwing an error
throw error;
}
}
// player exists
async checkPlayerExists(email: string): Promise<boolean> {
try {
Expand All @@ -44,6 +148,13 @@ class PlayerController {
throw error;
}
}


}
function generateInvitationToken(): string {
// Generate a UUID (v4) using the uuid library
const uniqueToken = uuidv4();

return uniqueToken;
}
export default new PlayerController();
4 changes: 2 additions & 2 deletions code/backend/src/db/connectdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const mongoose = require("mongoose");
const connectToDatabase = async () => {
try {
await mongoose.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
// useNewUrlParser: true,
// useUnifiedTopology: true,
});
} catch (err) {
console.error(err);
Expand Down
4 changes: 4 additions & 0 deletions code/backend/src/db/manager.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ interface ManagerDocument extends Document {
firstName: string;
lastName: string;
email: string;
invitationToken: string;
isVerified: boolean;
}

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

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

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

const ManagerTeamModel = mongoose.model<ManagerTeamDocument>(
Expand Down
10 changes: 4 additions & 6 deletions code/backend/src/db/player.schema.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import mongoose, { Document, Schema } from "mongoose";

interface PlayerDocument extends Document {
teamId: string;
firstName: string;
lastName: string;
email: string;
invitationToken: string;
isVerified: string;
}

const playerSchema = new Schema({
teamId: String,
firstName: String,
lastName: String,
email: String,
invitationToken: String,
isVerified: String,
});

const PlayerModel = mongoose.model<PlayerDocument>("Player", playerSchema);
Expand Down
11 changes: 11 additions & 0 deletions code/backend/src/db/players.in.team.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ import mongoose, { Document, Schema } from "mongoose";
interface PlayerTeamDocument extends Document {
playerEmal: string;
teamId: string;
jesryId: string;
firstName: string;
lastName: string;
invitationToken: string;
isVerified: string;
}

const playerTeamSchema = new Schema({
playerEmail: String,
teamId: String,
jesryId: String,
firstName: String,
lastName: String,
invitationToken: String,
isVerified: String,

});

const PlayerTeamModel = mongoose.model<PlayerTeamDocument>(
Expand Down
7 changes: 6 additions & 1 deletion code/backend/src/email/managerInviteEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export async function sendInvitationEmail(

const signature = "IMPAX TEAM";

// <a href="http://16.170.235.219:5000/accept-invitation/${invitationToken}">Accept Invitation</a>
// Compose the email
const mailOptions = {
from: "[email protected]",
Expand All @@ -25,7 +26,11 @@ export async function sendInvitationEmail(
html: `
<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://16.170.235.219:5000/accept-invitation/${invitationToken}">Accept Invitation</a>
<a href="http://localhost: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>
<div>
<img src="cid:Impax" alt="Impax Team" style="width: 100px; height: auto; max-width: 100%;" />
</div>
Expand Down
14 changes: 10 additions & 4 deletions code/backend/src/email/managerVerifyEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ export async function sendVerificationEmail(

const signature = "IMPAX TEAM";

// <a href="http://16.170.235.219:5000/accept-invitation/${invitationToken}">Verify Email Here</a>
// <p>You've been created the team ${teamName}! Click the following link to verify your email:</p>
// <a href="http://16.170.235.219:5000/accept-invitation/${invitationToken}">Verify Email Here</a><br><br>
const mailOptions = {
from: "[email protected]",
to: recipientEmail,
subject: "Notification Email",
subject: "Verification Email",
html: `
<p>Hello,</p>
<p>You've been created the team ${teamName}!</p>
<p>You've been created the team ${teamName}! Click the following link to verify your email:</p>
<a href="http://localhost: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>
<div>
<img src="cid:Impax" alt="Impax Team" style="width: 100px; height: auto; max-width: 100%;" />
</div>
Expand All @@ -40,4 +45,5 @@ export async function sendVerificationEmail(
};

await transporter.sendMail(mailOptions);
console.log("email sent successfully");
}
Loading

0 comments on commit 66320df

Please sign in to comment.