Skip to content

Commit 83512d8

Browse files
committed
Merge branch 'main' of https://github.com/call-0f-code/COC-API into achievement-fixes
2 parents a7e44d7 + 2c99e6d commit 83512d8

8 files changed

Lines changed: 90 additions & 239 deletions

File tree

bun.lock

Lines changed: 27 additions & 209 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Member" ALTER COLUMN "passoutYear" DROP NOT NULL;

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ model Member {
2626
leetcode String?
2727
codechef String?
2828
codeforces String?
29-
passoutYear DateTime
29+
passoutYear DateTime?
3030
isApproved Boolean @default(false)
3131
isManager Boolean @default(false)
3232
createdAt DateTime @default(now())

src/controllers/member.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,27 @@ import { SupabaseClient } from "@supabase/supabase-js";
66

77
// List all approved members
88
export const listAllApprovedMembers = async (req: Request, res: Response) => {
9+
10+
const {email, password} = req.query;
11+
12+
if(email && password) {
13+
14+
const user = await memberService.getUserByEmail(email as string);
15+
16+
if(!user) throw new ApiError('Incorrect email or password', 400);
17+
18+
res.status(200).json({
19+
success: true,
20+
user
21+
})
22+
}
23+
else {
924
const user = await memberService.approvedMembers();
1025
res
1126
.status(200)
12-
.json({ user, success: true, message: "Fetched approved users" });
27+
.json({ user, success: true });
28+
}
29+
1330
};
1431

1532
// Get details of a single user
@@ -27,7 +44,7 @@ export const getUserDetails = async (req: Request, res: Response) => {
2744
// Create a new member
2845
export const createAMember =
2946
(supabase: SupabaseClient) => async (req: Request, res: Response) => {
30-
const { email, name, password, passoutYear } = req.body;
47+
const {email, name, password, passoutYear, provider} = req.body;
3148

3249
if (!email || !name || !password || !passoutYear) {
3350
throw new ApiError("Required fields absent", 400);
@@ -44,6 +61,7 @@ export const createAMember =
4461
password,
4562
passoutYear,
4663
imageUrl,
64+
provider,
4765
);
4866

4967
if (!user) throw new ApiError("Error creating user", 500);

src/routes/members.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default function membersRouter(
1111
const router = express.Router();
1212

1313
/**
14-
* @api {get} /members/details/:memberId Get a member's details
14+
* @api {get} /members/:memberId Get a member's details
1515
* @apiName GetUserDetails
1616
* @apiGroup Member
1717
*
@@ -23,11 +23,21 @@ export default function membersRouter(
2323
router.get("/:memberId", memberCtrl.getUserDetails);
2424

2525
/**
26-
* @api {get} /members List all approved members
26+
* @api {get} /members List all approved members or get member by email
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 query parameter is provided.
32+
* - If `email` query parameter is provided, returns the member associated with that email.
33+
*
34+
* @apiQuery {String} [email] Optional email to fetch a specific member.
35+
*
36+
* @apiSuccess {Object} user Single user object when email provided.
37+
+ * @apiSuccess {Object[]} user Array of approved members when no email provided.
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/routes/progress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default function progressRouter() {
3838
* @apiError (Error 500) InternalServerError Error while toggling question completion.
3939
*/
4040
router.patch(
41-
"/:memeberId/questions/:questionId/completed/toggle",
41+
"/:memberId/questions/:questionId/completed/toggle",
4242
toggleQuestions,
4343
);
4444

src/routes/topics.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,7 @@ export default function topicRouter() {
8787
*
8888
* @apiSuccess {String} message Confirmation message
8989
* @apiSuccess {String="SUCCESS"} status Response status.
90-
* @apiSuccess {Object} question question object.
91-
* @apiSuccess {Number} question.id Question ID.
92-
* @apiSuccess {String} question.questionName Name of the question.
93-
* @apiSuccess {String} question.link Link to the question.
94-
* @apiSuccess {String="Easy","Medium","Hard"} question.difficulty Difficulty level of the question.
95-
* @apiSuccess {Number} question.topicId Topic ID associated with the question.
96-
* @apiSuccess {number} question.createdAt Timestamp when the question was created.
97-
* @apiSuccess {number} question.updatedAt Timestamp when the question was last updated.
98-
* @apiSuccess {String} question.createdById ID of the user who created the question.
99-
* @apiSuccess {String} question.updatedById ID of the user who last updated the question.
100-
*
90+
10191
* @apiError (Error 400) BadRequest Missing required fields.
10292
*/
10393
router.delete("/:topicId", deleteTopic);

src/services/member.service.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
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+
isApproved: true,
12+
isManager: true,
13+
accounts: {
14+
where: {
15+
provider: 'credentials',
16+
},
17+
select: {
18+
password: true
19+
}
20+
},
21+
}
22+
})
23+
}
1224

1325
export const approvedMembers = async () => {
1426
return await prisma.member.findMany({
@@ -29,24 +41,25 @@ export const getDetails = async (memberId: string) => {
2941
export const createMember = async (
3042
email: string,
3143
name: string,
32-
password: string,
33-
passoutYear: number,
44+
provider: "google" | "github" | "credentials",
45+
passoutYear?: number,
3446
imageUrl?: string,
47+
password?: string
3548
) => {
3649
const newMember = await prisma.member.create({
3750
data: {
38-
email: email,
39-
name: name,
40-
passoutYear: new Date(passoutYear),
51+
email,
52+
name,
53+
passoutYear: passoutYear === undefined ? "" : new Date(passoutYear),
4154
profilePhoto: imageUrl,
4255
},
4356
});
4457

4558
await prisma.account.create({
4659
data: {
47-
provider: "credentials",
60+
provider,
4861
providerAccountId: email,
49-
password: password,
62+
password: provider === "credentials" ? password : null,
5063
memberId: newMember.id,
5164
},
5265
});

0 commit comments

Comments
 (0)