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

51 lines
1 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import { TRPCError } from "@trpc/server";
2025-08-28 18:27:07 +02:00
import type { ChatsChatid } from "~/schemas/public/Chats";
2025-08-04 17:45:44 +02:00
import type { UsersId } from "~/schemas/public/Users";
2025-08-28 18:27:07 +02:00
import type { Querier } from "~/server/db";
2025-08-04 17:45:44 +02:00
export type ChatUserInfo = {
2025-08-28 18:27:07 +02:00
id: UsersId;
username: string;
email: string;
2025-08-04 17:45:44 +02:00
};
export const getRaw = async ({
2025-08-28 18:27:07 +02:00
db,
chatId,
2025-08-04 17:45:44 +02:00
}: {
2025-08-28 18:27:07 +02:00
db: Querier;
chatId: ChatsChatid;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
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}`,
});
}
2025-08-04 17:45:44 +02:00
};
export const add = async ({ db, userId }: { db: Querier; userId: UsersId }) => {
2025-08-28 18:27:07 +02:00
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}`,
});
}
2025-08-04 17:45:44 +02:00
};