Skip to content

Commit f488ef6

Browse files
committed
add getting a batch for the user's team
1 parent 44a03cd commit f488ef6

1 file changed

Lines changed: 34 additions & 3 deletions

File tree

services/hexathons/src/routes/food-batch.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ foodBatchRouter.route("/").post(
4343
})
4444
);
4545

46-
foodBatchRouter.route("/:id").get(
46+
foodBatchRouter.route("/batch/:id").get(
4747
checkAbility("read", "FoodBatch"),
4848
asyncHandler(async (req, res) => {
4949
const batch = await FoodBatchModel.findById(req.params.id);
@@ -56,7 +56,7 @@ foodBatchRouter.route("/:id").get(
5656
})
5757
);
5858

59-
foodBatchRouter.route("/:id").put(
59+
foodBatchRouter.route("/batch/:id").put(
6060
checkAbility("update", "FoodBatch"),
6161
asyncHandler(async (req, res) => {
6262
const updatedBatch = await FoodBatchModel.findByIdAndUpdate(req.params.id, req.body, {
@@ -67,14 +67,45 @@ foodBatchRouter.route("/:id").put(
6767
})
6868
);
6969

70-
foodBatchRouter.route("/:id").delete(
70+
foodBatchRouter.route("/batch/:id").delete(
7171
checkAbility("delete", "FoodBatch"),
7272
asyncHandler(async (req, res) => {
7373
await FoodBatchModel.findByIdAndDelete(req.params.id);
7474
return res.sendStatus(204);
7575
})
7676
);
7777

78+
foodBatchRouter.route("/my-batch").get(
79+
checkAbility("read", "FoodBatch"),
80+
asyncHandler(async (req, res) => {
81+
const team = await TeamModel.findById(req.body.teamId);
82+
83+
if (!team) {
84+
throw new BadRequestError("Team not found.");
85+
}
86+
87+
if (!team.batch) {
88+
return res.json({});
89+
}
90+
91+
const requestingUser = await HexathonUserModel.findOne({
92+
userId: req.user?.uid,
93+
});
94+
95+
if (!requestingUser) {
96+
throw new BadRequestError("User not found (not authenticated?)");
97+
}
98+
99+
if (!req.user || !team.members.includes(requestingUser.id)) {
100+
throw new ForbiddenError("User is not a member of the team.");
101+
}
102+
103+
const assignedBatch = await FoodBatchModel.findById(team.batch);
104+
105+
return res.json(assignedBatch || {});
106+
})
107+
);
108+
78109
foodBatchRouter.route("/join").post(
79110
checkAbility("update", "FoodBatch"),
80111
asyncHandler(async (req, res) => {

0 commit comments

Comments
 (0)