|
|
|
@ -1,33 +1,41 @@
|
|
|
|
|
import { TRPCError } from "@trpc/server";
|
|
|
|
|
import type { CommittedMessage } from "../../types";
|
|
|
|
|
import { router, publicProcedure, createCallerFactory } from "./server";
|
|
|
|
|
import { router, createCallerFactory, authProcedure } from "./server";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
|
|
export const conversations = router({
|
|
|
|
|
fetchAll: publicProcedure.query(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return [];
|
|
|
|
|
const authConversationProcedure = authProcedure
|
|
|
|
|
.input(z.object({ id: z.string() }))
|
|
|
|
|
.use(async ({ input: { id }, ctx: { dbClient, jwt }, next }) => {
|
|
|
|
|
const rows = await dbClient
|
|
|
|
|
.selectFrom("conversations")
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
.selectAll()
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
.execute();
|
|
|
|
|
return rows;
|
|
|
|
|
}),
|
|
|
|
|
fetchOne: publicProcedure
|
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
|
.query(async ({ input: { id }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return null;
|
|
|
|
|
const row = await dbClient
|
|
|
|
|
if (rows[0].userId !== jwt.id) {
|
|
|
|
|
throw new TRPCError({ code: "UNAUTHORIZED" });
|
|
|
|
|
}
|
|
|
|
|
return next({
|
|
|
|
|
ctx: {
|
|
|
|
|
conversationRow: rows[0],
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const conversations = router({
|
|
|
|
|
fetchAll: authProcedure.query(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const rows = await dbClient
|
|
|
|
|
.selectFrom("conversations")
|
|
|
|
|
.where("userId", "=", jwt.id as string)
|
|
|
|
|
.selectAll()
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
.execute();
|
|
|
|
|
return row[0];
|
|
|
|
|
return rows;
|
|
|
|
|
}),
|
|
|
|
|
start: publicProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return null;
|
|
|
|
|
fetchOne: authConversationProcedure.query(
|
|
|
|
|
async ({ ctx: { conversationRow } }) => {
|
|
|
|
|
return conversationRow;
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
start: authProcedure.mutation(async ({ ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const insertedRows = await dbClient
|
|
|
|
|
.insertInto("conversations")
|
|
|
|
|
.values({
|
|
|
|
@ -38,42 +46,34 @@ export const conversations = router({
|
|
|
|
|
.execute();
|
|
|
|
|
return insertedRows[0];
|
|
|
|
|
}),
|
|
|
|
|
deleteOne: publicProcedure
|
|
|
|
|
.input((x) => x as { id: string })
|
|
|
|
|
.mutation(async ({ input: { id }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return { ok: false };
|
|
|
|
|
deleteOne: authConversationProcedure.mutation(
|
|
|
|
|
async ({ input: { id }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
await dbClient
|
|
|
|
|
.deleteFrom("conversations")
|
|
|
|
|
.where("id", "=", id)
|
|
|
|
|
.where("userId", "=", userId)
|
|
|
|
|
.where("userId", "=", jwt.id as string)
|
|
|
|
|
.execute();
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}),
|
|
|
|
|
updateTitle: publicProcedure
|
|
|
|
|
.input(
|
|
|
|
|
(x) =>
|
|
|
|
|
x as {
|
|
|
|
|
id: string;
|
|
|
|
|
title: string;
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
updateTitle: authConversationProcedure
|
|
|
|
|
.input(
|
|
|
|
|
z.object({
|
|
|
|
|
title: z.string(),
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
.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)
|
|
|
|
|
.where("userId", "=", jwt.id as string)
|
|
|
|
|
.execute();
|
|
|
|
|
return { ok: true };
|
|
|
|
|
}),
|
|
|
|
|
fetchMessages: publicProcedure
|
|
|
|
|
.input((x) => x as { conversationId: string })
|
|
|
|
|
fetchMessages: authProcedure
|
|
|
|
|
.input(z.object({ conversationId: z.string() }))
|
|
|
|
|
.query(async ({ input: { conversationId }, ctx: { dbClient, jwt } }) => {
|
|
|
|
|
const userId = jwt?.id as string | null;
|
|
|
|
|
if (!userId) return [];
|
|
|
|
|
const rows = await dbClient
|
|
|
|
|
.selectFrom("messages")
|
|
|
|
|
.innerJoin(
|
|
|
|
@ -83,7 +83,7 @@ export const conversations = router({
|
|
|
|
|
)
|
|
|
|
|
.selectAll("messages")
|
|
|
|
|
.where("conversationId", "=", conversationId)
|
|
|
|
|
.where("conversations.userId", "=", userId)
|
|
|
|
|
.where("conversations.userId", "=", jwt.id as string)
|
|
|
|
|
.execute();
|
|
|
|
|
return rows as Array<CommittedMessage>;
|
|
|
|
|
}),
|
|
|
|
|