Skip to content

Commit 11854e1

Browse files
committed
Added conditional routing to get approved members route
1 parent 9263d8c commit 11854e1

3 files changed

Lines changed: 44 additions & 6 deletions

File tree

src/controllers/member.controller.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,31 @@ import * as memberService from "../services/member.service";
33
import { ApiError } from "../utils/apiError";
44
import { deleteImage, uploadImage } from "../utils/imageUtils";
55
import { SupabaseClient } from "@supabase/supabase-js";
6+
import { password } from "bun";
67

78
// List all approved members
89
export const listAllApprovedMembers = async (req: Request, res: Response) => {
10+
11+
const body = req.body;
12+
13+
if(body.email) {
14+
15+
const user = memberService.getUserByEmail(body.email);
16+
17+
if(!user) throw new ApiError('Incorrect email', 400);
18+
19+
res.status(200).json({
20+
success: true,
21+
user
22+
})
23+
}
24+
else {
925
const user = await memberService.approvedMembers();
1026
res
1127
.status(200)
1228
.json({ user, success: true, message: "Fetched approved users" });
29+
}
30+
1331
};
1432

1533
// Get details of a single user

src/routes/members.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,17 @@ export default function membersRouter(
2727
* @apiName ListAllApprovedMembers
2828
* @apiGroup Member
2929
*
30-
* @apiSuccess {Object[]} user List of approved members.
30+
* * @apiDescription
31+
* - Returns a list of all approved members if no email is provided.
32+
* - If `email` is provided in the request body, returns the member associated with that email.
33+
*
34+
* @apiBody {String} [email] Optional email to fetch a specific member.
35+
*
36+
* @apiSuccess {Boolean} success Whether the operation was successful.
37+
* @apiSuccess {Object|Object[]} user The user object (if email provided) or list of approved members.
38+
* @apiSuccess {String} [message] Message in case of full list fetch.
39+
*
40+
* @apiError (400) IncorrectEmail The provided email does not match any user.
3141
*/
3242
router.get("/", memberCtrl.listAllApprovedMembers);
3343

src/services/member.service.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import { prisma } from "../db/client";
22
import { ApiError } from "../utils/apiError";
33

4-
export const checkAdmin = async (adminId: string) => {
4+
export const getUserByEmail = async(email: string) => {
55
return await prisma.member.findUnique({
66
where: {
7-
id: adminId,
8-
isManager: true,
7+
email: email
98
},
10-
});
11-
};
9+
select: {
10+
id: true,
11+
accounts: {
12+
where: {
13+
provider: 'credentials',
14+
},
15+
select: {
16+
password: true,
17+
},
18+
},
19+
}
20+
})
21+
}
1222

1323
export const approvedMembers = async () => {
1424
return await prisma.member.findMany({

0 commit comments

Comments
 (0)