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

98 lines
2.2 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import type { AnnunciId } from "~/schemas/public/Annunci";
import type { UsersId } from "~/schemas/public/Users";
import { db } from "~/server/db";
import { TRPCError } from "@trpc/server";
export const AddIntrest = async ({
annuncioId,
userId,
}: {
annuncioId: AnnunciId;
userId: UsersId;
}) => {
try {
await db
.insertInto("servizio_interessi")
2025-08-04 17:45:44 +02:00
.values({
annuncioId,
userId,
})
.onConflict((oc) => oc.columns(["annuncioId", "userId"]).doNothing())
.execute();
return { success: true };
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"Errore durante l'aggiunta dell'interesse: " + (e as Error).message,
});
}
};
export const RemoveInterest = async ({
annuncioId,
userId,
}: {
annuncioId: AnnunciId;
userId: UsersId;
}) => {
try {
await db
.deleteFrom("servizio_interessi")
2025-08-04 17:45:44 +02:00
.where("annuncioId", "=", annuncioId)
.where("userId", "=", userId)
.execute();
return { success: true };
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"Errore durante la rimozione dell'interesse: " + (e as Error).message,
});
}
};
export const HasUserInterest = async ({
annuncioId,
userId,
}: {
annuncioId: AnnunciId;
userId: UsersId;
}) => {
try {
const interest = await db
.selectFrom("servizio_interessi")
2025-08-04 17:45:44 +02:00
.where("annuncioId", "=", annuncioId)
.where("userId", "=", userId)
.selectAll()
.executeTakeFirst();
return interest !== undefined;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"Errore durante il controllo dell'interesse: " + (e as Error).message,
});
}
};
export const GetUserInterests = async (userId: UsersId) => {
try {
return (
await db
.selectFrom("servizio_interessi")
2025-08-04 17:45:44 +02:00
.where("userId", "=", userId)
.select("annuncioId")
.execute()
).map((interest) => interest.annuncioId);
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message:
"Errore durante il recupero degli interessi: " + (e as Error).message,
});
}
};