-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Daylight-LLC/teams-api
add team and get teams api
- Loading branch information
Showing
3 changed files
with
49 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters