diff --git a/src/controllers/TeamController.ts b/src/controllers/TeamController.ts index bffce1f..f404e45 100644 --- a/src/controllers/TeamController.ts +++ b/src/controllers/TeamController.ts @@ -227,7 +227,7 @@ export const updateTeam = async ( if (description) { update.description = description; } - if (visible) { + if (visible !== undefined) { update.visible = visible; } @@ -237,6 +237,38 @@ export const updateTeam = async ( res.json(updatedTeam); }; +export const updateTeamAdmin = async ( + req: Request, + res: Response +): Promise => { + const { id, name, description, visible } = req.body; + const team = await getTeamById(new ObjectId(id)); + if (!team) { + return bad(res, "This team does not exist!"); + } + + if (name) { + const existingTeam = await findTeamByName(name); + if (existingTeam && existingTeam._id.toString() !== team._id.toString()) { + return bad(res, "That team name is already taken!"); + } + } + + const update: { name?: string; description?: string; visible?: boolean } = {}; + if (name) { + update.name = name; + } + if (description) { + update.description = description; + } + if (visible !== undefined) { + update.visible = visible; + } + + await Team.updateOne({ _id: team._id }, { $set: update }); + res.status(200).send(); +}; + /** * Join a team */ diff --git a/src/routes/team.ts b/src/routes/team.ts index 678c6bd..b902b35 100644 --- a/src/routes/team.ts +++ b/src/routes/team.ts @@ -10,10 +10,11 @@ import { leaveTeam, promoteUser, updateTeam, + updateTeamAdmin, } from "../controllers/TeamController"; import { getProjectByTeamID } from "../controllers/ProjectsController"; import { asyncCatch } from "../util/asyncCatch"; -import { isAuthenticated } from "./middleware"; +import { isAdmin, isAuthenticated } from "./middleware"; const router: Router = express.Router(); @@ -88,7 +89,7 @@ router.post("/", isAuthenticated, asyncCatch(createTeam)); * @swagger * /team/: * patch: - * summary: Update a team's information + * summary: Update the user's team's information * tags: [Teams Module] * description: Update a team's name, description, or visibility. All specified fields will be updated. Access - User, Team Admin * security: @@ -118,6 +119,42 @@ router.post("/", isAuthenticated, asyncCatch(createTeam)); */ router.patch("/", isAuthenticated, asyncCatch(updateTeam)); +/** + * @swagger + * /team/update-admin: + * patch: + * summary: Update a team's information + * tags: [Teams Module] + * description: Update the name, description, or visibility of an arbitrary team. All specified fields will be updated. Access - Admin + * security: + * - apiKeyAuth: [] + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * id: + * type: string + * name: + * type: string + * description: + * type: string + * visible: + * type: boolean + * responses: + * 200: + * description: Success. + * 400: + * description: Bad request + * 403: + * description: Unauthorized. + * 500: + * description: Internal Server Error. + */ +router.patch("/update-admin", isAdmin, asyncCatch(updateTeamAdmin)); + /** * @swagger * /team/join/{teamId}: