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

182 lines
4.3 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
2026-05-11 16:25:16 +02:00
.$pickTables<"appunti" | "appunti_groups" | "users">()
.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<AppuntiGroups[]> => {
try {
2026-05-11 16:25:16 +02:00
return await db
.$pickTables<"appunti_groups">()
.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 {
2026-05-11 16:25:16 +02:00
await db
.$pickTables<"appunti_groups">()
.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 {
2026-05-11 16:25:16 +02:00
await db
.$pickTables<"appunti_groups">()
.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 {
2026-05-11 16:25:16 +02:00
await db
.$pickTables<"appunti">()
.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 {
2026-05-11 16:25:16 +02:00
await db
.$pickTables<"appunti">()
.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 {
2026-05-11 16:25:16 +02:00
await db
.$pickTables<"appunti">()
.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
2026-05-11 16:25:16 +02:00
.$pickTables<"appunti">()
.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}`,
});
}
};