Spaces:
Sleeping
Sleeping
Commit ·
83652c0
1
Parent(s): 7ded4d6
bug fix 42
Browse files
src/app/api/messaging/mentees/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 { mentorMatches, users } from "@/db/schema";
|
| 5 |
import { eq, and } from "drizzle-orm";
|
| 6 |
|
| 7 |
export async function GET(request: NextRequest) {
|
|
@@ -11,6 +11,21 @@ export async function GET(request: NextRequest) {
|
|
| 11 |
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
const mentees = await db
|
| 15 |
.select({
|
| 16 |
id: mentorMatches.id,
|
|
@@ -24,11 +39,13 @@ export async function GET(request: NextRequest) {
|
|
| 24 |
.leftJoin(users, eq(mentorMatches.menteeId, users.id))
|
| 25 |
.where(
|
| 26 |
and(
|
| 27 |
-
eq(mentorMatches.mentorId,
|
| 28 |
eq(mentorMatches.status, "active")
|
| 29 |
)
|
| 30 |
);
|
| 31 |
|
|
|
|
|
|
|
| 32 |
// Transform to match frontend expectations
|
| 33 |
const formattedMentees = mentees.map(m => ({
|
| 34 |
user_id: m.menteeId,
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
import { getCurrentUser } from "@/lib/auth";
|
| 3 |
import { db } from "@/db";
|
| 4 |
+
import { mentorMatches, mentors, users } 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
|
| 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 (!mentorRecord[0]) {
|
| 22 |
+
console.log("[Get Mentees] No mentor record for user:", user.id);
|
| 23 |
+
return NextResponse.json({ mentees: [] });
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
const mentorId = mentorRecord[0].id;
|
| 27 |
+
console.log("[Get Mentees] Found mentor:", mentorId, "for user:", user.id);
|
| 28 |
+
|
| 29 |
const mentees = await db
|
| 30 |
.select({
|
| 31 |
id: mentorMatches.id,
|
|
|
|
| 39 |
.leftJoin(users, eq(mentorMatches.menteeId, users.id))
|
| 40 |
.where(
|
| 41 |
and(
|
| 42 |
+
eq(mentorMatches.mentorId, mentorId),
|
| 43 |
eq(mentorMatches.status, "active")
|
| 44 |
)
|
| 45 |
);
|
| 46 |
|
| 47 |
+
console.log("[Get Mentees] Found", mentees.length, "active mentees");
|
| 48 |
+
|
| 49 |
// Transform to match frontend expectations
|
| 50 |
const formattedMentees = mentees.map(m => ({
|
| 51 |
user_id: m.menteeId,
|
src/app/api/messaging/mentorship/accept/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, mentors, messages } from "@/db/schema";
|
| 5 |
import { eq, and } from "drizzle-orm";
|
| 6 |
import { v4 as uuidv4 } from "uuid";
|
| 7 |
|
|
@@ -22,8 +22,9 @@ export async function POST(request: NextRequest) {
|
|
| 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 |
|
|
@@ -58,13 +59,26 @@ export async function POST(request: NextRequest) {
|
|
| 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:
|
| 68 |
read: false,
|
| 69 |
timestamp: new Date().toISOString(),
|
| 70 |
});
|
|
|
|
| 1 |
import { NextRequest, NextResponse } from "next/server";
|
| 2 |
import { getCurrentUser } from "@/lib/auth";
|
| 3 |
import { db } from "@/db";
|
| 4 |
+
import { mentorshipRequests, mentors, mentorMatches, messages, users } from "@/db/schema";
|
| 5 |
import { eq, and } from "drizzle-orm";
|
| 6 |
import { v4 as uuidv4 } from "uuid";
|
| 7 |
|
|
|
|
| 22 |
|
| 23 |
// Find the mentor record for this user
|
| 24 |
const mentorRecord = await db
|
| 25 |
+
.select({ id: mentors.id, username: users.username })
|
| 26 |
.from(mentors)
|
| 27 |
+
.leftJoin(users, eq(mentors.userId, users.id))
|
| 28 |
.where(eq(mentors.userId, user.id))
|
| 29 |
.limit(1);
|
| 30 |
|
|
|
|
| 59 |
.set({ status: "accepted" })
|
| 60 |
.where(eq(mentorshipRequests.id, requestId));
|
| 61 |
|
| 62 |
+
// Create mentor-mentee relationship in mentorMatches
|
| 63 |
+
await db.insert(mentorMatches).values({
|
| 64 |
+
id: uuidv4(),
|
| 65 |
+
mentorId: mentorId,
|
| 66 |
+
mentorUsername: mentorRecord[0].username || user.username || "",
|
| 67 |
+
menteeId: req.menteeId,
|
| 68 |
+
menteeUsername: req.menteeUsername || "",
|
| 69 |
+
compatibilityScore: 1.0,
|
| 70 |
+
matchReason: "Mentorship request accepted",
|
| 71 |
+
status: "active",
|
| 72 |
+
createdAt: new Date().toISOString(),
|
| 73 |
+
});
|
| 74 |
+
|
| 75 |
// Send welcome message
|
| 76 |
if (welcomeMessage) {
|
| 77 |
await db.insert(messages).values({
|
| 78 |
id: uuidv4(),
|
| 79 |
senderId: user.id,
|
| 80 |
receiverId: req.menteeId,
|
| 81 |
+
content: welcomeMessage,
|
| 82 |
read: false,
|
| 83 |
timestamp: new Date().toISOString(),
|
| 84 |
});
|