feat: add admin override switch for ignoring service preferences in AnnunciCompatibili dialog

This commit is contained in:
Marco Pedone 2025-11-11 15:41:07 +01:00
parent db8d1da5ab
commit 4408295f56
3 changed files with 40 additions and 14 deletions

View file

@ -1,5 +1,6 @@
import { ExternalLink, UnfoldVertical } from "lucide-react";
import Link from "next/link";
import { useState } from "react";
import { replaceWithBr } from "~/lib/newlineToBr";
import { cn, formatCurrency } from "~/lib/utils";
import { useTranslation } from "~/providers/I18nProvider";
@ -22,11 +23,15 @@ import {
DialogTitle,
DialogTrigger,
} from "../ui/dialog";
import { Label } from "../ui/label";
import { Switch } from "../ui/switch";
export const AnnunciCompatibili = () => {
const { servizioId } = useServizio();
const { servizioId, isAdmin } = useServizio();
const [adminOverride, setAdminOverride] = useState(false);
const { data, isLoading } = api.servizio.getCompatibileAnnunci.useQuery({
servizioId,
adminOverride,
});
return (
<Dialog>
@ -35,11 +40,25 @@ export const AnnunciCompatibili = () => {
Esplora Annunci Compatibili
</Button>
</DialogTrigger>
<DialogContent className="max-w-xl p-2 sm:max-w-5xl sm:p-6 md:max-w-7xl">
<DialogHeader>
<DialogTitle className="pt-6">
<DialogContent className="max-w-xl p-2 sm:max-w-5xl sm:p-6 md:max-w-7xl 2xl:max-w-[100rem]">
<DialogHeader className="flex-row flex-wrap items-center gap-5 pt-6">
<DialogTitle>
Annunci compatibili con le preferenze del servizio
</DialogTitle>
{isAdmin && (
<div className="flex flex-wrap items-center gap-x-2">
<Label htmlFor="adminOverride">
Ignora limitazioni preferenze
</Label>
<Switch
checked={adminOverride}
className="data-[state=checked]:bg-neutral-700"
id="adminOverride"
onCheckedChange={setAdminOverride}
/>
</div>
)}
<DialogDescription className="sr-only"></DialogDescription>
</DialogHeader>
<div className="flex max-h-[80vh] flex-col gap-2 overflow-y-auto p-0.5 sm:p-2">
@ -55,7 +74,7 @@ export const AnnunciCompatibili = () => {
</span>
</div>
) : (
<div className="relative z-0 mx-auto grid max-w-7xl grid-flow-row grid-cols-1 gap-4 sm:grid-cols-2 sm:gap-6 sm:gap-y-8 lg:grid-cols-3">
<div className="relative z-0 mx-auto grid max-w-full grid-flow-row grid-cols-1 gap-4 sm:grid-cols-2 sm:gap-6 sm:gap-y-10 lg:grid-cols-3 2xl:grid-cols-4">
{data.map((annuncio) => (
<div
className="flex flex-col justify-center gap-1"

View file

@ -142,10 +142,11 @@ export const servizioRouter = createTRPCRouter({
.input(
z.object({
servizioId: zServizioId,
adminOverride: z.boolean().optional(),
}),
)
.query(async ({ input }) => {
return await getCompatibileAnnunci(input.servizioId);
return await getCompatibileAnnunci({ ...input });
}),
getOrdineById: protectedProcedure
.input(

View file

@ -1577,9 +1577,13 @@ export const SendContactEmail = async ({
}
};
export const getCompatibileAnnunci = async (
servizioId: ServizioServizioId,
): Promise<AnnuncioRicerca[]> => {
export const getCompatibileAnnunci = async ({
servizioId,
adminOverride = false,
}: {
servizioId: ServizioServizioId;
adminOverride?: boolean;
}): Promise<AnnuncioRicerca[]> => {
try {
const servizio = await getServizioById(servizioId);
if (!servizio) {
@ -1628,8 +1632,12 @@ export const getCompatibileAnnunci = async (
.selectFrom("servizio_annunci")
.select("annunci_id")
.where("servizio_id", "=", servizioId),
)
.where("prezzo", "<=", servizio.budget * 1e2 * 1.2); // Allow 20% margin
);
if (adminOverride) {
return await qry.execute();
}
qry = qry.where("prezzo", "<=", servizio.budget * 1e2 * 1.2); // Allow 20% margin
const month = new Date().getMonth() + 1; // Months are 0-indexed in JS
if (servizio.entromese === month) {
@ -1647,9 +1655,7 @@ export const getCompatibileAnnunci = async (
qry = qry.where("consegna", "=", servizio.entromese);
}
const annunci = await qry.execute();
return annunci;
return await qry.execute();
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",