infoalloggi-monorepo/apps/infoalloggi/src/server/services/chat.service.ts

51 lines
1.1 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import type { ChatsChatid } from "~/schemas/public/Chats";
import type { Querier } from "~/server/db";
import { TRPCError } from "@trpc/server";
import type { UsersId } from "~/schemas/public/Users";
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({
user_ref: userId,
created_at: new Date(),
})
.returningAll()
.executeTakeFirstOrThrow();
return newChat;
} catch (e) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Error while creating chat: " + (e as Error).message,
});
}
};