|
|
@ -1,26 +1,43 @@
|
|
|
|
|
|
|
|
import type { CommittedMessage } from "../../types";
|
|
|
|
import { router, publicProcedure, createCallerFactory } from "./server";
|
|
|
|
import { router, publicProcedure, createCallerFactory } from "./server";
|
|
|
|
|
|
|
|
|
|
|
|
export const conversations = router({
|
|
|
|
export const conversations = router({
|
|
|
|
fetchAll: publicProcedure.query(async ({ ctx: { db, jwt } }) => {
|
|
|
|
fetchAll: publicProcedure.query(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
console.log("jwt", jwt);
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
return await db.conversations.findAll({ userId: jwt?.id as string });
|
|
|
|
if (!userId) return [];
|
|
|
|
|
|
|
|
const rows = await dbClient
|
|
|
|
|
|
|
|
.selectFrom("conversations")
|
|
|
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
|
|
|
.selectAll()
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
fetchOne: publicProcedure
|
|
|
|
fetchOne: publicProcedure
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
.query(async ({ input: { id }, ctx: { db } }) => {
|
|
|
|
.query(async ({ input: { id }, ctx: { dbClient } }) => {
|
|
|
|
return await db.conversations.findById(id);
|
|
|
|
const row = await dbClient
|
|
|
|
|
|
|
|
.selectFrom("conversations")
|
|
|
|
|
|
|
|
.selectAll()
|
|
|
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return row[0];
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
start: publicProcedure.mutation(async ({ ctx: { db, jwt } }) => {
|
|
|
|
start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
const row = {
|
|
|
|
const row = {
|
|
|
|
title: "New Conversation",
|
|
|
|
title: "New Conversation",
|
|
|
|
userId: jwt?.id as string,
|
|
|
|
userId: jwt?.id as string,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return await db.conversations.create(row);
|
|
|
|
const insertedRows = await dbClient
|
|
|
|
|
|
|
|
.insertInto("conversations")
|
|
|
|
|
|
|
|
.values(row)
|
|
|
|
|
|
|
|
.returningAll()
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return insertedRows[0];
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
deleteOne: publicProcedure
|
|
|
|
deleteOne: publicProcedure
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
.mutation(async ({ input: { id }, ctx: { db } }) => {
|
|
|
|
.mutation(async ({ input: { id }, ctx: { dbClient } }) => {
|
|
|
|
await db.conversations.delete(id);
|
|
|
|
await dbClient.deleteFrom("conversations").where("id", "=", id).execute();
|
|
|
|
return { ok: true };
|
|
|
|
return { ok: true };
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
updateTitle: publicProcedure
|
|
|
|
updateTitle: publicProcedure
|
|
|
@ -31,14 +48,23 @@ export const conversations = router({
|
|
|
|
title: string;
|
|
|
|
title: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.mutation(async ({ input: { id, title }, ctx: { db } }) => {
|
|
|
|
.mutation(async ({ input: { id, title }, ctx: { dbClient } }) => {
|
|
|
|
await db.conversations.update(id, { title });
|
|
|
|
await dbClient
|
|
|
|
|
|
|
|
.updateTable("conversations")
|
|
|
|
|
|
|
|
.set({ title })
|
|
|
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
return { ok: true };
|
|
|
|
return { ok: true };
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
fetchMessages: publicProcedure
|
|
|
|
fetchMessages: publicProcedure
|
|
|
|
.input((x) => x as { conversationId: string })
|
|
|
|
.input((x) => x as { conversationId: string })
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { db } }) => {
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => {
|
|
|
|
return await db.conversations.fetchMessages(conversationId);
|
|
|
|
const rows = await dbClient
|
|
|
|
|
|
|
|
.selectFrom("messages")
|
|
|
|
|
|
|
|
.selectAll()
|
|
|
|
|
|
|
|
.where("conversationId", "=", conversationId)
|
|
|
|
|
|
|
|
.execute();
|
|
|
|
|
|
|
|
return rows as Array<CommittedMessage>;
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|