import { TRPCError } from "@trpc/server"; import type { Nullable } from "kysely"; import type { Appunti, AppuntiId, AppuntiUpdate, NewAppunti, } from "~/schemas/public/Appunti"; import type { AppuntiGroups, AppuntiGroupsId, NewAppuntiGroups, } from "~/schemas/public/AppuntiGroups"; import type { Users } from "~/schemas/public/Users"; import { db } from "../db"; export type AppuntiData = Omit & { column: string; } & Nullable>; export const getAppunti = async (): Promise => { try { const appunti = await db .selectFrom("appunti") .select([ "appunti.id", "appunti.name", "appunti.created_at", "appunti.descrizione", "appunti.userid", "appunti.appunti_group_id as column", "appunti.posizione", ]) .innerJoin( "appunti_groups", "appunti.appunti_group_id", "appunti_groups.id", ) .leftJoin("users", "appunti.userid", "users.id") .select("users.username") //.orderBy("appunti.created_at", "asc") .orderBy("appunti.posizione", (ob) => ob.asc().nullsLast()) //.orderBy("appunti_groups.name", "asc") .execute(); return appunti.map((data) => ({ ...data, created_at: new Date(data.created_at), })); } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to fetch Appunti, error: ${(e as Error).message}`, }); } }; export const getAppuntiGroups = async (): Promise => { try { return await db.selectFrom("appunti_groups").selectAll().execute(); } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to fetch Appunti groups, error: ${(e as Error).message}`, }); } }; export const addAppuntiGroup = async (data: NewAppuntiGroups) => { try { await db.insertInto("appunti_groups").values(data).execute(); return { status: "success" }; } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to add Appunti group, error: ${(e as Error).message}`, }); } }; export const deleteAppuntiGroup = async (id: AppuntiGroupsId) => { try { await db.deleteFrom("appunti_groups").where("id", "=", id).execute(); return { status: "success" }; } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to delete Appunti group, error: ${(e as Error).message}`, }); } }; export const addAppunti = async (data: NewAppunti) => { try { await db.insertInto("appunti").values(data).execute(); return { status: "success" }; } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to add Appunti, error: ${(e as Error).message}`, }); } }; export const deleteAppunti = async (id: AppuntiId) => { try { await db.deleteFrom("appunti").where("id", "=", id).execute(); return { status: "success" }; } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to delete Appunti, error: ${(e as Error).message}`, }); } }; export const updateAppunto = async (id: AppuntiId, data: AppuntiUpdate) => { try { await db.updateTable("appunti").set(data).where("id", "=", id).execute(); return { status: "success" }; } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to update Appunti, error: ${(e as Error).message}`, }); } }; export const updateAppunti = async (data: AppuntiData[]) => { try { const dataWithPositions = data.map((item, index) => ({ ...item, posizione: index, })); for (const item of dataWithPositions) { const { column, username: _username, ...rest } = item; await db .updateTable("appunti") .set({ ...rest, appunti_group_id: column as AppuntiGroupsId, }) .where("id", "=", item.id) .execute(); } return { status: "success" }; } catch (e) { throw new TRPCError({ code: "INTERNAL_SERVER_ERROR", message: `Failed to update appunti, error: ${(e as Error).message}`, }); } };