infoalloggi-monorepo/apps/infoalloggi/src/server/services/chat.service.ts
2025-08-29 16:18:32 +02:00

50 lines
1 KiB
TypeScript

import { TRPCError } from "@trpc/server";
import type { ChatsChatid } from "~/schemas/public/Chats";
import type { UsersId } from "~/schemas/public/Users";
import type { Querier } from "~/server/db";
export type ChatUserInfo = {
id: UsersId;
username: string;
email: string;
};
export const getRaw = async ({
db,
chatId,
}: {
db: Querier;
chatId: ChatsChatid;
}) => {
try {
return db
.selectFrom("chats")
.selectAll()
.where("chats.chatid", "=", chatId)
.executeTakeFirstOrThrow();
} catch (e) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error while getting chat: ${(e as Error).message}`,
});
}
};
export const add = async ({ db, userId }: { db: Querier; userId: UsersId }) => {
try {
const newChat = await db
.insertInto("chats")
.values({
created_at: new Date(),
user_ref: userId,
})
.returningAll()
.executeTakeFirstOrThrow();
return newChat;
} catch (e) {
throw new TRPCError({
code: "BAD_REQUEST",
message: `Error while creating chat: ${(e as Error).message}`,
});
}
};