You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import type { CommittedMessage } from "../../types";
|
|
import { router, publicProcedure, createCallerFactory } from "./server";
|
|
|
|
export const conversations = router({
|
|
fetchAll: publicProcedure.query(async ({ ctx: { dbClient, jwt } }) => {
|
|
const userId = jwt?.id as string | null;
|
|
if (!userId) return [];
|
|
const rows = await dbClient
|
|
.selectFrom("conversations")
|
|
.where("userId", "=", userId)
|
|
.selectAll()
|
|
.execute();
|
|
return rows;
|
|
}),
|
|
fetchOne: publicProcedure
|
|
.input((x) => x as { id: string })
|
|
.query(async ({ input: { id }, ctx: { dbClient } }) => {
|
|
const row = await dbClient
|
|
.selectFrom("conversations")
|
|
.selectAll()
|
|
.where("id", "=", id)
|
|
.execute();
|
|
return row[0];
|
|
}),
|
|
start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
|
|
const row = {
|
|
title: "New Conversation",
|
|
userId: jwt?.id as string,
|
|
};
|
|
const insertedRows = await dbClient
|
|
.insertInto("conversations")
|
|
.values(row)
|
|
.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();
|
|
return { ok: true };
|
|
}),
|
|
updateTitle: publicProcedure
|
|
.input(
|
|
(x) =>
|
|
x as {
|
|
id: string;
|
|
title: string;
|
|
}
|
|
)
|
|
.mutation(async ({ input: { id, title }, ctx: { dbClient } }) => {
|
|
await dbClient
|
|
.updateTable("conversations")
|
|
.set({ title })
|
|
.where("id", "=", id)
|
|
.execute();
|
|
return { ok: true };
|
|
}),
|
|
fetchMessages: publicProcedure
|
|
.input((x) => x as { conversationId: string })
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient } }) => {
|
|
const rows = await dbClient
|
|
.selectFrom("messages")
|
|
.selectAll()
|
|
.where("conversationId", "=", conversationId)
|
|
.execute();
|
|
return rows as Array<CommittedMessage>;
|
|
}),
|
|
});
|
|
|
|
export const createCaller = createCallerFactory(conversations);
|