-
Notifications
You must be signed in to change notification settings - Fork 1
feature/create-team #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
729f7c7
feat: add `createTeam` controller
mdawoud27 5aabd98
feat: add route for `createTeam` controller
mdawoud27 617076d
feat: add validations for `createTeam` controller
mdawoud27 c6dc29b
feat: import team routes in `index.js`
mdawoud27 8c2fe8b
docs: add swagger docs for create team function
mdawoud27 a31b25e
fix: typo in team controller
mdawoud27 73466b8
fix: success field in response
mdawoud27 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| import prisma from '../config/prismaClient.js'; | ||
| import { createTeamValidation } from '../validations/team.validation.js'; | ||
|
|
||
| /** | ||
| * @desc Create a new team in a specific organization | ||
| * @route /api/organization | ||
| * @method POST | ||
| * @access private - admins or organization owners only | ||
| */ | ||
| export const createTeam = async (req, res, next) => { | ||
| try { | ||
| // POST /api/organization/:organizationId/department/:departmentId/team | ||
| const { organizationId, departmentId } = req.params; | ||
|
|
||
| if (!organizationId) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: 'Organization ID is required', | ||
| }); | ||
| } | ||
|
|
||
| // Check if organization exists and is not deleted | ||
| const existingOrg = await prisma.organization.findFirst({ | ||
| where: { | ||
| id: organizationId, | ||
| deletedAt: null, | ||
| }, | ||
| include: { | ||
| owners: { | ||
| select: { | ||
| userId: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (!existingOrg) { | ||
| return res.status(404).json({ | ||
| success: false, | ||
| message: 'Organization not found', | ||
| }); | ||
| } | ||
|
|
||
| if (!departmentId) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: 'Department ID is required', | ||
| }); | ||
| } | ||
|
|
||
| // Check if department exists and is not deleted | ||
| const existingDep = await prisma.department.findFirst({ | ||
| where: { | ||
| id: departmentId, | ||
| deletedAt: null, | ||
| }, | ||
| }); | ||
|
|
||
| if (!existingDep) { | ||
| return res.status(404).json({ | ||
| success: false, | ||
| message: 'Department not found', | ||
| }); | ||
| } | ||
|
|
||
| // Check permissions - only admins and organization owners | ||
| const isAdmin = req.user.role === 'ADMIN'; | ||
| const isOwner = existingOrg.owners.some( | ||
| (owner) => owner.userId === req.user.id, | ||
| ); | ||
| const isDepManager = existingDep.managerId === req.user.id; | ||
|
|
||
| if (!isAdmin && !isOwner && !isDepManager) { | ||
| return res.status(403).json({ | ||
| success: false, | ||
| message: | ||
| 'You do not have permission to create teams in this department', | ||
| }); | ||
| } | ||
|
|
||
| // Validate input | ||
| const { error } = createTeamValidation(req.body); | ||
| if (error) { | ||
| return res.status(400).json({ | ||
| success: false, | ||
| message: 'Validation failed', | ||
| errors: error.details.map((e) => e.message), | ||
| }); | ||
| } | ||
|
|
||
| const { name, description, avatar, members = [] } = req.body; | ||
|
|
||
| const existingTeam = await prisma.team.findFirst({ | ||
| where: { | ||
| name: name, | ||
| organizationId: organizationId, | ||
| deletedAt: null, | ||
| }, | ||
| }); | ||
| if (existingTeam) { | ||
|
||
| return res.status(409).json({ | ||
| success: false, | ||
| message: 'Team with this name already exists', | ||
| }); | ||
| } | ||
|
|
||
| const result = await prisma.$transaction(async (tx) => { | ||
| // 1. create the team | ||
| const team = await tx.team.create({ | ||
| data: { | ||
| name, | ||
| description, | ||
| avatar, | ||
| createdBy: req.user.id, | ||
| organizationId: organizationId, | ||
| departmentId: departmentId, | ||
| }, | ||
| }); | ||
|
|
||
| // 2. create team member | ||
| const leaderMember = await tx.teamMember.create({ | ||
| data: { | ||
| teamId: team.id, | ||
| userId: req.user.id, | ||
| role: 'LEADER', | ||
| isActive: true, | ||
| }, | ||
| }); | ||
|
|
||
| // 3. create additional team members if provided | ||
| const additionalMembers = []; | ||
| if (members && members.length > 0) { | ||
| for (const member of members) { | ||
| // Check if user exists | ||
| const userExists = await tx.user.findFirst({ | ||
| where: { id: member.userId, deletedAt: null }, | ||
| select: { id: true }, | ||
| }); | ||
|
|
||
| if (userExists) { | ||
| const newMember = await tx.teamMember.create({ | ||
| data: { | ||
| teamId: team.id, | ||
| userId: member.userId, | ||
| role: member.role || 'MEMBER', | ||
| isActive: true, | ||
| }, | ||
| }); | ||
| additionalMembers.push(newMember); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 4. fetch all team members for the response | ||
| const allTeamMembers = await tx.teamMember.findMany({ | ||
| where: { teamId: team.id }, | ||
| include: { | ||
| user: { | ||
| select: { | ||
| id: true, | ||
| name: true, | ||
| email: true, | ||
| avatar: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| return { team, leaderMember, allTeamMembers }; | ||
| }); | ||
|
|
||
| return res.status(201).json({ | ||
| success: true, | ||
| message: `Team created successfully.`, | ||
| data: { | ||
| team: { | ||
| id: result.team.id, | ||
| name: result.team.name, | ||
| description: result.team.description, | ||
| }, | ||
| teamLeader: { | ||
| id: result.team.createdBy, | ||
| leader: result.leaderMember, | ||
| }, | ||
| teamMembers: result.allTeamMembers.map((member) => ({ | ||
| id: member.id, | ||
| userId: member.userId, | ||
| role: member.role, | ||
| user: member.user, | ||
| })), | ||
| }, | ||
| }); | ||
| } catch (error) { | ||
| if (error.code === 'P2002') { | ||
| return res.status(409).json({ | ||
| success: false, | ||
| message: 'Team with this name already exists in this organization', | ||
| }); | ||
| } | ||
| next(error); | ||
| } | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.