51 lines
1.1 KiB
TypeScript
51 lines
1.1 KiB
TypeScript
|
|
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,
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|