KrishnaCosmic commited on
Commit
7ded4d6
·
1 Parent(s): 6eb9011

bug fix 41

Browse files
src/app/api/messaging/mentorship/accept/route.ts ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { getCurrentUser } from "@/lib/auth";
3
+ import { db } from "@/db";
4
+ import { mentorshipRequests, mentors, messages } 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) {
9
+ try {
10
+ const user = await getCurrentUser(request);
11
+ if (!user) {
12
+ return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
13
+ }
14
+
15
+ const body = await request.json();
16
+ const requestId = body.request_id || body.requestId;
17
+ const welcomeMessage = body.message || "Welcome! I'm excited to be your mentor.";
18
+
19
+ if (!requestId) {
20
+ return NextResponse.json({ error: "Request ID required" }, { status: 400 });
21
+ }
22
+
23
+ // Find the mentor record for this user
24
+ const mentorRecord = await db
25
+ .select({ id: mentors.id })
26
+ .from(mentors)
27
+ .where(eq(mentors.userId, user.id))
28
+ .limit(1);
29
+
30
+ if (!mentorRecord[0]) {
31
+ return NextResponse.json({ error: "You are not registered as a mentor" }, { status: 403 });
32
+ }
33
+
34
+ const mentorId = mentorRecord[0].id;
35
+
36
+ // Find the mentorship request
37
+ const mentorshipRequest = await db
38
+ .select()
39
+ .from(mentorshipRequests)
40
+ .where(
41
+ and(
42
+ eq(mentorshipRequests.id, requestId),
43
+ eq(mentorshipRequests.mentorId, mentorId),
44
+ eq(mentorshipRequests.status, "pending")
45
+ )
46
+ )
47
+ .limit(1);
48
+
49
+ if (!mentorshipRequest[0]) {
50
+ return NextResponse.json({ error: "Request not found or already processed" }, { status: 404 });
51
+ }
52
+
53
+ const req = mentorshipRequest[0];
54
+
55
+ // Update request status to accepted
56
+ await db
57
+ .update(mentorshipRequests)
58
+ .set({ status: "accepted" })
59
+ .where(eq(mentorshipRequests.id, requestId));
60
+
61
+ // Send welcome message
62
+ if (welcomeMessage) {
63
+ await db.insert(messages).values({
64
+ id: uuidv4(),
65
+ senderId: user.id,
66
+ receiverId: req.menteeId,
67
+ content: `[Mentorship Welcome] ${welcomeMessage}`,
68
+ read: false,
69
+ timestamp: new Date().toISOString(),
70
+ });
71
+ }
72
+
73
+ console.log("[Accept Mentorship] Request", requestId, "accepted by mentor", user.id);
74
+
75
+ return NextResponse.json({
76
+ success: true,
77
+ message: "Mentorship request accepted"
78
+ });
79
+
80
+ } catch (error: any) {
81
+ console.error("POST /api/messaging/mentorship/accept error:", error);
82
+ return NextResponse.json({
83
+ error: "Internal server error",
84
+ details: error.message
85
+ }, { status: 500 });
86
+ }
87
+ }
src/app/api/messaging/mentorship/decline/route.ts ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextRequest, NextResponse } from "next/server";
2
+ import { getCurrentUser } from "@/lib/auth";
3
+ import { db } from "@/db";
4
+ import { mentorshipRequests, mentors, messages } 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) {
9
+ try {
10
+ const user = await getCurrentUser(request);
11
+ if (!user) {
12
+ return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
13
+ }
14
+
15
+ const body = await request.json();
16
+ const requestId = body.request_id || body.requestId;
17
+ const declineReason = body.reason || body.message || "";
18
+
19
+ if (!requestId) {
20
+ return NextResponse.json({ error: "Request ID required" }, { status: 400 });
21
+ }
22
+
23
+ // Find the mentor record for this user
24
+ const mentorRecord = await db
25
+ .select({ id: mentors.id })
26
+ .from(mentors)
27
+ .where(eq(mentors.userId, user.id))
28
+ .limit(1);
29
+
30
+ if (!mentorRecord[0]) {
31
+ return NextResponse.json({ error: "You are not registered as a mentor" }, { status: 403 });
32
+ }
33
+
34
+ const mentorId = mentorRecord[0].id;
35
+
36
+ // Find the mentorship request
37
+ const mentorshipRequest = await db
38
+ .select()
39
+ .from(mentorshipRequests)
40
+ .where(
41
+ and(
42
+ eq(mentorshipRequests.id, requestId),
43
+ eq(mentorshipRequests.mentorId, mentorId),
44
+ eq(mentorshipRequests.status, "pending")
45
+ )
46
+ )
47
+ .limit(1);
48
+
49
+ if (!mentorshipRequest[0]) {
50
+ return NextResponse.json({ error: "Request not found or already processed" }, { status: 404 });
51
+ }
52
+
53
+ const req = mentorshipRequest[0];
54
+
55
+ // Update request status to declined
56
+ await db
57
+ .update(mentorshipRequests)
58
+ .set({ status: "declined" })
59
+ .where(eq(mentorshipRequests.id, requestId));
60
+
61
+ // Optionally send a message explaining the decline
62
+ if (declineReason) {
63
+ await db.insert(messages).values({
64
+ id: uuidv4(),
65
+ senderId: user.id,
66
+ receiverId: req.menteeId,
67
+ content: `[Mentorship Request Declined] ${declineReason}`,
68
+ read: false,
69
+ timestamp: new Date().toISOString(),
70
+ });
71
+ }
72
+
73
+ console.log("[Decline Mentorship] Request", requestId, "declined by mentor", user.id);
74
+
75
+ return NextResponse.json({
76
+ success: true,
77
+ message: "Mentorship request declined"
78
+ });
79
+
80
+ } catch (error: any) {
81
+ console.error("POST /api/messaging/mentorship/decline error:", error);
82
+ return NextResponse.json({
83
+ error: "Internal server error",
84
+ details: error.message
85
+ }, { status: 500 });
86
+ }
87
+ }
src/app/api/messaging/mentorship/requests/route.ts CHANGED
@@ -1,7 +1,7 @@
1
  import { NextRequest, NextResponse } from "next/server";
2
  import { getCurrentUser } from "@/lib/auth";
3
  import { db } from "@/db";
4
- import { mentorshipRequests, users } from "@/db/schema";
5
  import { eq, and } from "drizzle-orm";
6
 
7
  export async function GET(request: NextRequest) {
@@ -11,10 +11,28 @@ export async function GET(request: NextRequest) {
11
  return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
12
  }
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  const requests = await db
15
  .select({
16
  id: mentorshipRequests.id,
17
  menteeId: mentorshipRequests.menteeId,
 
18
  status: mentorshipRequests.status,
19
  message: mentorshipRequests.message,
20
  createdAt: mentorshipRequests.createdAt,
@@ -25,16 +43,18 @@ export async function GET(request: NextRequest) {
25
  .leftJoin(users, eq(mentorshipRequests.menteeId, users.id))
26
  .where(
27
  and(
28
- eq(mentorshipRequests.mentorId, user.id),
29
  eq(mentorshipRequests.status, "pending")
30
  )
31
  );
32
 
 
 
33
  // Transform to match frontend expectations (snake_case)
34
  const formattedRequests = requests.map(r => ({
35
  id: r.id,
36
  mentee_id: r.menteeId,
37
- mentee_username: r.menteeName,
38
  mentee_avatar: r.menteeAvatar,
39
  message: r.message,
40
  created_at: r.createdAt,
 
1
  import { NextRequest, NextResponse } from "next/server";
2
  import { getCurrentUser } from "@/lib/auth";
3
  import { db } from "@/db";
4
+ import { mentorshipRequests, users, mentors } from "@/db/schema";
5
  import { eq, and } from "drizzle-orm";
6
 
7
  export async function GET(request: NextRequest) {
 
11
  return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
12
  }
13
 
14
+ // First, find the mentor record for this user (mentor.userId = user.id)
15
+ const mentorRecord = await db
16
+ .select({ id: mentors.id })
17
+ .from(mentors)
18
+ .where(eq(mentors.userId, user.id))
19
+ .limit(1);
20
+
21
+ // If no mentor record exists, return empty requests
22
+ if (!mentorRecord[0]) {
23
+ console.log("[Mentorship Requests] No mentor record found for user:", user.id);
24
+ return NextResponse.json({ requests: [] });
25
+ }
26
+
27
+ const mentorId = mentorRecord[0].id;
28
+ console.log("[Mentorship Requests] Found mentor record:", mentorId, "for user:", user.id);
29
+
30
+ // Now fetch requests where mentorId matches the mentor record's ID
31
  const requests = await db
32
  .select({
33
  id: mentorshipRequests.id,
34
  menteeId: mentorshipRequests.menteeId,
35
+ menteeUsername: mentorshipRequests.menteeUsername,
36
  status: mentorshipRequests.status,
37
  message: mentorshipRequests.message,
38
  createdAt: mentorshipRequests.createdAt,
 
43
  .leftJoin(users, eq(mentorshipRequests.menteeId, users.id))
44
  .where(
45
  and(
46
+ eq(mentorshipRequests.mentorId, mentorId),
47
  eq(mentorshipRequests.status, "pending")
48
  )
49
  );
50
 
51
+ console.log("[Mentorship Requests] Found", requests.length, "pending requests");
52
+
53
  // Transform to match frontend expectations (snake_case)
54
  const formattedRequests = requests.map(r => ({
55
  id: r.id,
56
  mentee_id: r.menteeId,
57
+ mentee_username: r.menteeUsername || r.menteeName,
58
  mentee_avatar: r.menteeAvatar,
59
  message: r.message,
60
  created_at: r.createdAt,