Spaces:
Sleeping
Sleeping
Commit ·
b80664e
1
Parent(s): a257b82
finalising
Browse files
src/app/api/health/route.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Health Check Route
|
| 3 |
+
*
|
| 4 |
+
* GET /api/health
|
| 5 |
+
* Check if the backend is running and database is accessible
|
| 6 |
+
*/
|
| 7 |
+
|
| 8 |
+
import { NextRequest, NextResponse } from "next/server";
|
| 9 |
+
import { db } from "@/db";
|
| 10 |
+
import { users } from "@/db/schema";
|
| 11 |
+
|
| 12 |
+
export async function GET(request: NextRequest) {
|
| 13 |
+
try {
|
| 14 |
+
// Test database connection
|
| 15 |
+
const testQuery = await db.select().from(users).limit(1);
|
| 16 |
+
|
| 17 |
+
return NextResponse.json({
|
| 18 |
+
status: "healthy",
|
| 19 |
+
database: "connected",
|
| 20 |
+
timestamp: new Date().toISOString(),
|
| 21 |
+
env: {
|
| 22 |
+
jwt_secret_set: !!process.env.JWT_SECRET,
|
| 23 |
+
database_url_set: !!process.env.DATABASE_URL,
|
| 24 |
+
}
|
| 25 |
+
});
|
| 26 |
+
} catch (error) {
|
| 27 |
+
console.error("Health check failed:", error);
|
| 28 |
+
return NextResponse.json({
|
| 29 |
+
status: "unhealthy",
|
| 30 |
+
database: "disconnected",
|
| 31 |
+
error: error instanceof Error ? error.message : "Unknown error",
|
| 32 |
+
timestamp: new Date().toISOString(),
|
| 33 |
+
}, { status: 500 });
|
| 34 |
+
}
|
| 35 |
+
}
|
src/app/api/messaging/conversations/route.ts
CHANGED
|
@@ -25,7 +25,7 @@ export async function GET(request: NextRequest) {
|
|
| 25 |
username: c.partnerUsername,
|
| 26 |
avatar_url: c.partnerAvatar,
|
| 27 |
last_message: c.lastMessage?.content?.substring(0, 50) || "",
|
| 28 |
-
|
| 29 |
unread_count: c.unreadCount
|
| 30 |
}))
|
| 31 |
});
|
|
|
|
| 25 |
username: c.partnerUsername,
|
| 26 |
avatar_url: c.partnerAvatar,
|
| 27 |
last_message: c.lastMessage?.content?.substring(0, 50) || "",
|
| 28 |
+
last_message_timestamp: c.lastMessage?.timestamp || null,
|
| 29 |
unread_count: c.unreadCount
|
| 30 |
}))
|
| 31 |
});
|
src/app/api/messaging/poll/[userId]/route.ts
CHANGED
|
@@ -28,6 +28,13 @@ export async function GET(
|
|
| 28 |
return NextResponse.json(newMessages);
|
| 29 |
} catch (error) {
|
| 30 |
console.error("Poll messages error:", error);
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
}
|
|
|
|
| 28 |
return NextResponse.json(newMessages);
|
| 29 |
} catch (error) {
|
| 30 |
console.error("Poll messages error:", error);
|
| 31 |
+
console.error("Error details:", {
|
| 32 |
+
message: error instanceof Error ? error.message : "Unknown error",
|
| 33 |
+
stack: error instanceof Error ? error.stack : undefined
|
| 34 |
+
});
|
| 35 |
+
return NextResponse.json({
|
| 36 |
+
error: "Internal server error",
|
| 37 |
+
detail: error instanceof Error ? error.message : "Unknown error"
|
| 38 |
+
}, { status: 500 });
|
| 39 |
}
|
| 40 |
}
|
src/app/api/messaging/send/route.ts
CHANGED
|
@@ -35,6 +35,13 @@ export async function POST(request: NextRequest) {
|
|
| 35 |
return NextResponse.json(message);
|
| 36 |
} catch (error) {
|
| 37 |
console.error("Send message error:", error);
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
}
|
|
|
|
| 35 |
return NextResponse.json(message);
|
| 36 |
} catch (error) {
|
| 37 |
console.error("Send message error:", error);
|
| 38 |
+
console.error("Error details:", {
|
| 39 |
+
message: error instanceof Error ? error.message : "Unknown error",
|
| 40 |
+
stack: error instanceof Error ? error.stack : undefined
|
| 41 |
+
});
|
| 42 |
+
return NextResponse.json({
|
| 43 |
+
error: "Internal server error",
|
| 44 |
+
detail: error instanceof Error ? error.message : "Unknown error"
|
| 45 |
+
}, { status: 500 });
|
| 46 |
}
|
| 47 |
}
|
src/lib/db/queries/messages.ts
CHANGED
|
@@ -135,19 +135,12 @@ export async function getChatHistory(currentUserId: string, otherUserId: string,
|
|
| 135 |
content: msg.content,
|
| 136 |
read: msg.read,
|
| 137 |
timestamp: msg.timestamp,
|
|
|
|
| 138 |
}));
|
| 139 |
}
|
| 140 |
|
| 141 |
export async function pollNewMessages(currentUserId: string, otherUserId: string, lastMessageId?: string) {
|
| 142 |
-
let
|
| 143 |
-
.from(messages)
|
| 144 |
-
.where(
|
| 145 |
-
and(
|
| 146 |
-
eq(messages.senderId, otherUserId),
|
| 147 |
-
eq(messages.receiverId, currentUserId)
|
| 148 |
-
)
|
| 149 |
-
)
|
| 150 |
-
.orderBy(asc(messages.timestamp));
|
| 151 |
|
| 152 |
// If we have a last message ID, only get newer messages
|
| 153 |
if (lastMessageId) {
|
|
@@ -157,7 +150,7 @@ export async function pollNewMessages(currentUserId: string, otherUserId: string
|
|
| 157 |
.limit(1);
|
| 158 |
|
| 159 |
if (lastMessage[0]) {
|
| 160 |
-
|
| 161 |
.from(messages)
|
| 162 |
.where(
|
| 163 |
and(
|
|
@@ -167,10 +160,31 @@ export async function pollNewMessages(currentUserId: string, otherUserId: string
|
|
| 167 |
)
|
| 168 |
)
|
| 169 |
.orderBy(asc(messages.timestamp));
|
|
|
|
|
|
|
| 170 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
}
|
| 172 |
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
}
|
| 175 |
|
| 176 |
// =============================================================================
|
|
|
|
| 135 |
content: msg.content,
|
| 136 |
read: msg.read,
|
| 137 |
timestamp: msg.timestamp,
|
| 138 |
+
edited_at: msg.editedAt || null,
|
| 139 |
}));
|
| 140 |
}
|
| 141 |
|
| 142 |
export async function pollNewMessages(currentUserId: string, otherUserId: string, lastMessageId?: string) {
|
| 143 |
+
let results;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
// If we have a last message ID, only get newer messages
|
| 146 |
if (lastMessageId) {
|
|
|
|
| 150 |
.limit(1);
|
| 151 |
|
| 152 |
if (lastMessage[0]) {
|
| 153 |
+
results = await db.select()
|
| 154 |
.from(messages)
|
| 155 |
.where(
|
| 156 |
and(
|
|
|
|
| 160 |
)
|
| 161 |
)
|
| 162 |
.orderBy(asc(messages.timestamp));
|
| 163 |
+
} else {
|
| 164 |
+
results = [];
|
| 165 |
}
|
| 166 |
+
} else {
|
| 167 |
+
results = await db.select()
|
| 168 |
+
.from(messages)
|
| 169 |
+
.where(
|
| 170 |
+
and(
|
| 171 |
+
eq(messages.senderId, otherUserId),
|
| 172 |
+
eq(messages.receiverId, currentUserId)
|
| 173 |
+
)
|
| 174 |
+
)
|
| 175 |
+
.orderBy(asc(messages.timestamp));
|
| 176 |
}
|
| 177 |
|
| 178 |
+
// Transform to snake_case for frontend compatibility
|
| 179 |
+
return results.map(msg => ({
|
| 180 |
+
id: msg.id,
|
| 181 |
+
sender_id: msg.senderId,
|
| 182 |
+
receiver_id: msg.receiverId,
|
| 183 |
+
content: msg.content,
|
| 184 |
+
read: msg.read,
|
| 185 |
+
timestamp: msg.timestamp,
|
| 186 |
+
edited_at: msg.editedAt || null,
|
| 187 |
+
}));
|
| 188 |
}
|
| 189 |
|
| 190 |
// =============================================================================
|