Skip to content

Commit

Permalink
Merge pull request #22 from Daylight-LLC/teams-api
Browse files Browse the repository at this point in the history
add team and get teams api
  • Loading branch information
malik-na authored Sep 11, 2024
2 parents a471808 + 1c10d41 commit 7cf8495
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
51 changes: 43 additions & 8 deletions backend/controllers/team.controller.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,74 @@
import mongoose from "mongoose";
import Project from "../models/project.model.js";

// Add a Team to a Project
export const addTeam = async (req, res) => {
const { projectId } = req.params;
const { teamName, members } = req.body;
const { teamName, teamLeadName } = req.body;

if (!mongoose.isValidObjectId(projectId)) {
return res
.status(400)
.json({ success: false, message: "Invalid Project ID" });
}

if (!teamName || !members || members.length === 0) {
if (!teamName || !teamLeadName) {
return res.status(400).json({
success: false,
message: "Please provide a team name and add members",
message: "Please provide all the fields",
});
}

try {
const project = await Project.findById(projectId);
if (!project) {
return res
.status(404)
.json({ success: false, message: "Project not found" });
}
const newTeam = { teamName, members };

// Ensure teams array is initialized
if (!project.teams) {
project.teams = [];
}

// Create a new team object
const newTeam = {
teamName,
teamLeadName,
members: [], // Initialize members if needed
};

// Add the new team to the project's teams array
project.teams.push(newTeam);

await project.save();
return res.status(200).json({ success: true, data: project.teams });
} catch (error) {
console.error("Error in adding team to project", error.message);
return res.status(500).json({ success: false, message: "Server Error" });
}
};
// Get All Teams from a Project
// Update a Team within a Project
// Delete a Team from a Project

export const getTeams = async (req, res) => {
const { projectId } = req.params;

if (!mongoose.isValidObjectId(projectId)) {
return res
.status(400)
.json({ success: false, message: "Invalid Project ID" });
}

try {
const project = await Project.findById(projectId);
if (!project) {
return res
.status(404)
.json({ success: false, message: "Project not found" });
}

res.status(200).json({ success: true, data: project.teams });
} catch (error) {
console.error("Error in fetching teams", error.message);
res.status(500).json({ success: false, message: "Server Error" });
}
};
4 changes: 4 additions & 0 deletions backend/models/team.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const teamSchema = new mongoose.Schema({
type: String,
required: true,
},
teamLeadName: {
type: String,
required: true,
},
members: [teamMemberSchema], // Each team has an array of members
});

Expand Down
3 changes: 2 additions & 1 deletion backend/routes/team.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express";
import { addTeam } from "../controllers/team.controller.js";
import { addTeam, getTeams } from "../controllers/team.controller.js";
import rateLimit from "express-rate-limit";

const router = express.Router();
Expand All @@ -11,5 +11,6 @@ const rateLimiter = rateLimit({
});

router.post("/:projectId/teams", rateLimiter, addTeam);
router.get("/:projectId/teams", rateLimiter, getTeams);

export default router;

0 comments on commit 7cf8495

Please sign in to comment.