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

163 lines
4.1 KiB
TypeScript
Raw Normal View History

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<Appunti, "appunti_group_id"> & {
column: string;
} & Nullable<Pick<Users, "username">>;
export const getAppunti = async (): Promise<AppuntiData[]> => {
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",
])
.innerJoin(
"appunti_groups",
"appunti.appunti_group_id",
"appunti_groups.id",
)
.leftJoin("users", "appunti.userid", "users.id")
.select("users.username")
.orderBy("appunti_groups.name", "asc")
.orderBy("appunti.created_at", "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<AppuntiGroups[]> => {
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 db_mappings = await db
.selectFrom("appunti")
.select(["id", "appunti_group_id"])
.execute();
const updates = data
.map((item) => {
const db_item = db_mappings.find((db_item) => db_item.id === item.id);
if (db_item && db_item.appunti_group_id !== item.column) {
const { column, username: _username, ...rest } = item;
return {
...rest,
appunti_group_id: column as AppuntiGroupsId,
};
}
return null;
})
.filter((item) => item !== null);
for (const item of updates) {
await db
.updateTable("appunti")
.set(item)
.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}`,
});
}
};