Skip to content

Commit

Permalink
added endpoint to update team as admin
Browse files Browse the repository at this point in the history
  • Loading branch information
pm3512 committed Jan 29, 2024
1 parent 1faa856 commit 46ebaf8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/controllers/TeamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const updateTeam = async (
if (description) {
update.description = description;
}
if (visible) {
if (visible !== undefined) {
update.visible = visible;
}

Expand All @@ -237,6 +237,38 @@ export const updateTeam = async (
res.json(updatedTeam);
};

export const updateTeamAdmin = async (
req: Request,
res: Response
): Promise<void> => {
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
*/
Expand Down
41 changes: 39 additions & 2 deletions src/routes/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}:
Expand Down

0 comments on commit 46ebaf8

Please sign in to comment.