KrishnaCosmic commited on
Commit
701c053
·
1 Parent(s): 8081fe9

bug fix 26

Browse files
Files changed (1) hide show
  1. src/app/api/mentor/request/route.ts +55 -8
src/app/api/mentor/request/route.ts CHANGED
@@ -1,8 +1,8 @@
1
  import { NextRequest, NextResponse } from "next/server";
2
  import { getCurrentUser } from "@/lib/auth";
3
  import { db } from "@/db";
4
- import { mentorshipRequests, mentors } from "@/db/schema";
5
- import { eq } from "drizzle-orm";
6
  import { v4 as uuidv4 } from "uuid";
7
 
8
  export async function POST(request: NextRequest) {
@@ -31,18 +31,65 @@ export async function POST(request: NextRequest) {
31
  return NextResponse.json({ error: "Mentor ID required. Please provide mentor_id in request body." }, { status: 400 });
32
  }
33
 
34
- // Get mentor details to confirm existence
35
- const mentor = await db.select().from(mentors).where(eq(mentors.id, mentorId)).limit(1);
36
- if (!mentor[0]) {
37
- return NextResponse.json({ error: "Mentor not found" }, { status: 404 });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  }
39
 
40
  await db.insert(mentorshipRequests).values({
41
  id: uuidv4(),
42
  menteeId: user.id,
43
  menteeUsername: user.username,
44
- mentorId: mentorId,
45
- mentorUsername: mentor[0].username,
46
  message: message || "I would like to be your mentee.",
47
  createdAt: new Date().toISOString(),
48
  });
 
1
  import { NextRequest, NextResponse } from "next/server";
2
  import { getCurrentUser } from "@/lib/auth";
3
  import { db } from "@/db";
4
+ import { mentorshipRequests, mentors, profiles, users } from "@/db/schema";
5
+ import { eq, and } from "drizzle-orm";
6
  import { v4 as uuidv4 } from "uuid";
7
 
8
  export async function POST(request: NextRequest) {
 
31
  return NextResponse.json({ error: "Mentor ID required. Please provide mentor_id in request body." }, { status: 400 });
32
  }
33
 
34
+ // First try to find mentor in the legacy mentors table
35
+ let mentorUsername: string | null = null;
36
+ let mentorUserId: string = mentorId;
37
+
38
+ const mentorRecord = await db.select().from(mentors).where(eq(mentors.id, mentorId)).limit(1);
39
+ if (mentorRecord[0]) {
40
+ mentorUsername = mentorRecord[0].username;
41
+ mentorUserId = mentorRecord[0].userId || mentorId;
42
+ } else {
43
+ // Try finding in profiles table (mentorId could be userId)
44
+ const profileRecord = await db
45
+ .select({ username: profiles.username, userId: profiles.userId, availableForMentoring: profiles.availableForMentoring })
46
+ .from(profiles)
47
+ .where(and(
48
+ eq(profiles.userId, mentorId),
49
+ eq(profiles.availableForMentoring, true)
50
+ ))
51
+ .limit(1);
52
+
53
+ if (profileRecord[0]) {
54
+ mentorUsername = profileRecord[0].username;
55
+ mentorUserId = profileRecord[0].userId;
56
+ } else {
57
+ // Last resort: check users table directly
58
+ const userRecord = await db
59
+ .select({ id: users.id, username: users.username })
60
+ .from(users)
61
+ .where(eq(users.id, mentorId))
62
+ .limit(1);
63
+
64
+ if (userRecord[0]) {
65
+ mentorUsername = userRecord[0].username;
66
+ mentorUserId = userRecord[0].id;
67
+ } else {
68
+ return NextResponse.json({ error: "Mentor not found" }, { status: 404 });
69
+ }
70
+ }
71
+ }
72
+
73
+ // Check if request already exists
74
+ const existingRequest = await db
75
+ .select()
76
+ .from(mentorshipRequests)
77
+ .where(and(
78
+ eq(mentorshipRequests.menteeId, user.id),
79
+ eq(mentorshipRequests.mentorId, mentorUserId)
80
+ ))
81
+ .limit(1);
82
+
83
+ if (existingRequest[0]) {
84
+ return NextResponse.json({ error: "You already have a pending request to this mentor" }, { status: 409 });
85
  }
86
 
87
  await db.insert(mentorshipRequests).values({
88
  id: uuidv4(),
89
  menteeId: user.id,
90
  menteeUsername: user.username,
91
+ mentorId: mentorUserId,
92
+ mentorUsername: mentorUsername || 'Unknown',
93
  message: message || "I would like to be your mentee.",
94
  createdAt: new Date().toISOString(),
95
  });