Skip to content

Commit c7498c2

Browse files
making routes and saving chats to db
1 parent 7572f97 commit c7498c2

File tree

13 files changed

+252
-189
lines changed

13 files changed

+252
-189
lines changed

.env.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ USE_SECURE_COOKIES=
2222
SPECIAL_TOKEN=
2323

2424
OLLAMA_ENDPOINT=
25-
DATABASE_URL=
25+
MONGO_DATABASE_URL=

prisma/schema.prisma

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
datasource db {
22
provider = "mongodb"
3-
url = env("DATABASE_URL")
3+
url = env("MONGO_DATABASE_URL")
44
}
55

66
generator client {
77
provider = "prisma-client-js"
88
}
99

10-
model Email {
11-
id String @id @default(auto()) @map("_id") @db.ObjectId
12-
email String @unique
13-
user User?
14-
}
15-
1610
model User {
17-
id String @id @default(auto()) @map("_id") @db.ObjectId
18-
uid String @unique
19-
isLead Boolean
20-
email Email @relation(fields: [emailId], references: [id])
21-
emailId String @unique @db.ObjectId
22-
chats Chat[]
11+
email String @id @map("_id")
12+
isAppDev Boolean
13+
chats Chat[]
2314
}
2415

2516
model Chat {
26-
uuid String @id @map("_id")
27-
summary String
28-
user User @relation(fields: [userId], references: [uid])
29-
userId String
30-
messages Message[]
17+
id String @id @map("_id")
18+
summary String
19+
updatedAt DateTime
20+
user User @relation(fields: [userEmail], references: [email], onDelete: Cascade)
21+
userEmail String
22+
messages Message[]
3123
}
3224

3325
model Message {
3426
id String @id @default(auto()) @map("_id") @db.ObjectId
3527
content String
3628
images String[]
37-
timestamp DateTime @default(now())
29+
timestamp DateTime
3830
sender String
39-
chat Chat @relation(fields: [chatId], references: [uuid])
31+
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
4032
chatId String
4133
}

scripts/prepopulateEmails.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import { PrismaClient } from '@prisma/client';
2+
import { upsertAppDevUser } from '@/app/utils/databaseUtils';
23

34
const prisma = new PrismaClient();
45

56
const appDevEmails = ['a@cornell.edu', 'b@cornell.edu', 'c@cornell.edu'];
67

78
async function main() {
89
for (const email of appDevEmails) {
9-
await prisma.email.upsert({
10-
where: { email: email },
11-
create: { email: email },
12-
update: {},
13-
});
10+
await upsertAppDevUser(email);
1411
}
1512
}
1613

src/app/api/authenticate/route.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1+
import { getUserByEmail, upsertUser } from '@/app/utils/databaseUtils';
12
import { NextRequest, NextResponse } from 'next/server';
2-
// import { getEmail, upsertUserFromEmail } from '@/app/utils/databaseUtils';
33

44
export async function GET(request: NextRequest) {
5-
// const uid = request.headers.get('uid');
6-
// if (!uid) {
7-
// return NextResponse.json({ message: 'No uid found' }, { status: 400 });
8-
// }
5+
const email = request.headers.get('email');
6+
if (!email) {
7+
return NextResponse.json({ message: 'Invalid request' }, { status: 400 });
8+
}
99

10-
// const userEmail = request.headers.get('email')!;
11-
// const email = await getEmail(userEmail);
12-
// if (!email) {
13-
// return NextResponse.json(
14-
// { message: 'Only Cornell AppDev members can use AI Dev' },
15-
// { status: 401 },
16-
// );
17-
// }
18-
19-
// const newUser = await upsertUserFromEmail(email, uid);
20-
// return NextResponse.json({ success: true, user: newUser });
21-
return NextResponse.json({ success: true });
10+
const user = await getUserByEmail(email);
11+
if (!user) {
12+
await upsertUser(email);
13+
}
14+
// TODO: Return back user to frontend to store, should contain name, pfp, email, isAppDev
15+
return NextResponse.json({});
2216
}
Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
11
import { NextRequest, NextResponse } from 'next/server';
2-
import { getMessagesByChatId } from '@/app/utils/databaseUtils';
2+
import {
3+
createChat,
4+
createMessage,
5+
deleteChatById,
6+
getChatById,
7+
getChatsByEmail,
8+
getMessagesByChatId,
9+
updateChatTimestamp,
10+
} from '@/app/utils/databaseUtils';
311

412
export async function GET(request: NextRequest) {
5-
// TODO: Do Authorization check
6-
const uid = request.headers.get('uid');
13+
const email = request.headers.get('email');
14+
if (!email) {
15+
return NextResponse.json({ message: 'Invalid request' }, { status: 400 });
16+
}
17+
718
const chatId = request.nextUrl.pathname.split('/')[3];
19+
const chat = await getChatById(chatId);
20+
if (!chat) {
21+
return NextResponse.json({ message: 'Chat not found' }, { status: 404 });
22+
}
23+
24+
if (chat.userEmail !== email) {
25+
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
26+
}
27+
828
const messages = await getMessagesByChatId(chatId);
929
return NextResponse.json({ messages: messages });
1030
}
31+
32+
export async function POST(request: NextRequest) {
33+
const email = request.headers.get('email');
34+
if (!email) {
35+
return NextResponse.json({ message: 'No email' }, { status: 400 });
36+
}
37+
38+
const chatId = request.nextUrl.pathname.split('/')[3];
39+
const { content, images, timestamp, sender } = await request.json();
40+
if (!content) {
41+
return NextResponse.json({ message: 'Invalid request' }, { status: 400 });
42+
}
43+
44+
const chat = await getChatById(chatId);
45+
if (!chat) {
46+
await createChat(email, chatId, content, timestamp);
47+
}
48+
await createMessage(chatId, content, images, timestamp, sender);
49+
await updateChatTimestamp(chatId, timestamp);
50+
return NextResponse.json({});
51+
}
52+
53+
export async function DELETE(request: NextRequest) {
54+
const email = request.headers.get('email');
55+
if (!email) {
56+
return NextResponse.json({ message: 'Invalid request' }, { status: 400 });
57+
}
58+
59+
const chatId = request.nextUrl.pathname.split('/')[3];
60+
const chat = await getChatById(chatId);
61+
if (!chat) {
62+
return NextResponse.json({ message: 'Chat not found' }, { status: 404 });
63+
}
64+
65+
if (chat.userEmail !== email) {
66+
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
67+
}
68+
69+
await deleteChatById(chatId);
70+
return NextResponse.json({ chats: await getChatsByEmail(email) });
71+
}

src/app/api/chats/route.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,12 @@
11
import { NextRequest, NextResponse } from 'next/server';
2-
import {
3-
deleteChatById,
4-
getChatsByUserId,
5-
getChatById,
6-
createChatWithMessage,
7-
} from '@/app/utils/databaseUtils';
2+
import { getChatsByEmail } from '@/app/utils/databaseUtils';
83

94
export async function GET(request: NextRequest) {
10-
const uid = request.headers.get('uid');
11-
const chats = await getChatsByUserId(uid!);
12-
return NextResponse.json({ chats: chats });
13-
}
14-
15-
export async function POST(request: NextRequest) {
16-
const uid = request.headers.get('uid');
17-
const { chatId, message } = await request.json();
18-
if (!chatId || !message) {
5+
const email = request.headers.get('email');
6+
if (!email) {
197
return NextResponse.json({ message: 'Invalid request' }, { status: 400 });
208
}
219

22-
await createChatWithMessage(uid!, chatId, message);
23-
return NextResponse.json({});
24-
}
25-
26-
export async function DELETE(request: NextRequest) {
27-
const uid = request.headers.get('uid');
28-
const { chatId } = await request.json();
29-
if (!chatId) {
30-
return NextResponse.json({ message: 'Invalid request' }, { status: 400 });
31-
}
32-
33-
const chat = await getChatById(chatId);
34-
if (!chat) {
35-
return NextResponse.json({ message: 'Chat not found' }, { status: 404 });
36-
}
37-
38-
if (chat.userId !== uid) {
39-
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
40-
}
41-
42-
await deleteChatById(chatId);
43-
return NextResponse.json({ chats: await getChatsByUserId(uid!) });
10+
const chats = await getChatsByEmail(email);
11+
return NextResponse.json({ chats: chats });
4412
}

src/app/api/users/route.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)