|
|
|
@ -14,30 +14,40 @@ export const conversations = router({
|
|
|
|
|
}),
|
|
|
|
|
fetchOne: publicProcedure
|
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
|
.query(async ({ input: { id }, ctx: { dbClient } }) => {
|
|
|
|
|
.query(async ({ input: { id }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return null;
|
|
|
|
|
const row = await dbClient
|
|
|
|
|
.selectFrom("conversations")
|
|
|
|
|
.selectAll()
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
.execute();
|
|
|
|
|
return row[0];
|
|
|
|
|
}),
|
|
|
|
|
start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const row = {
|
|
|
|
|
title: "New Conversation",
|
|
|
|
|
userId: jwt?.id as string,
|
|
|
|
|
};
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return null;
|
|
|
|
|
const insertedRows = await dbClient
|
|
|
|
|
.insertInto("conversations")
|
|
|
|
|
.values(row)
|
|
|
|
|
.values({
|
|
|
|
|
title: "New Conversation",
|
|
|
|
|
userId: jwt?.id as string,
|
|
|
|
|
})
|
|
|
|
|
.returningAll()
|
|
|
|
|
.execute();
|
|
|
|
|
return insertedRows[0];
|
|
|
|
|
}),
|
|
|
|
|
deleteOne: publicProcedure
|
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
|
.mutation(async ({ input: { id }, ctx: { dbClient } }) => {
|
|
|
|
|
await dbClient.deleteFrom("conversations").where("id", "=", id).execute();
|
|
|
|
|
.mutation(async ({ input: { id }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return { ok: false };
|
|
|
|
|
await dbClient
|
|
|
|
|
.deleteFrom("conversations")
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
.execute();
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}),
|
|
|
|
|
updateTitle: publicProcedure
|
|
|
|
@ -48,21 +58,32 @@ export const conversations = router({
|
|
|
|
|
title: string;
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
.mutation(async ({ input: { id, title }, ctx: { dbClient } }) => {
|
|
|
|
|
.mutation(async ({ input: { id, title }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return { ok: false };
|
|
|
|
|
await dbClient
|
|
|
|
|
.updateTable("conversations")
|
|
|
|
|
.set({ title })
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
.execute();
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}),
|
|
|
|
|
fetchMessages: publicProcedure
|
|
|
|
|
.input((x) => x as { conversationId: string })
|
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => {
|
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return [];
|
|
|
|
|
const rows = await dbClient
|
|
|
|
|
.selectFrom("messages")
|
|
|
|
|
.selectAll()
|
|
|
|
|
.innerJoin(
|
|
|
|
|
"conversations",
|
|
|
|
|
"conversations.id",
|
|
|
|
|
"messages.conversationId"
|
|
|
|
|
)
|
|
|
|
|
.selectAll("messages")
|
|
|
|
|
.where("conversationId", "=", conversationId)
|
|
|
|
|
.where("conversations.userId", "=", userId)
|
|
|
|
|
.execute();
|
|
|
|
|
return rows as Array<CommittedMessage>;
|
|
|
|
|
}),
|
|
|
|
|