Refactor editAnnuncio functionality to use AnnunciUpdate schema

- Updated the editAnnuncio procedure to accept a new input structure containing annuncioId and data.
- Removed the old EdiAnnuncioSchema and replaced it with a direct use of AnnunciUpdate.
- Simplified the editAnnuncioHandler to directly spread the data object into the database update call.
- Renamed getAnnuncioById to getAnnuncioByIdForEdit for clarity.
This commit is contained in:
Marco Pedone 2025-08-28 12:37:32 +02:00
parent 26f1664696
commit 9d80dad9bd
3 changed files with 1396 additions and 102 deletions

View file

@ -1,4 +1,5 @@
import { z } from "zod/v4";
import { type AnnunciUpdate } from "~/schemas/public/Annunci";
import {
adminProcedure,
createTRPCRouter,
@ -6,7 +7,6 @@ import {
publicProcedure,
} from "~/server/api/trpc";
import {
EdiAnnuncioSchema,
editAnnuncioHandler,
getProprietarioDataHandler,
getCodici_AnnunciHandler,
@ -45,7 +45,7 @@ export const annunciRouter = createTRPCRouter({
.query(async ({ input }) => {
return await getAnnunciMetaByCod({ ...input });
}),
getAnnuncioById: publicProcedure
getAnnuncioByIdForEdit: publicProcedure
.input(
z.object({
id: zAnnuncioId,
@ -88,7 +88,9 @@ export const annunciRouter = createTRPCRouter({
}),
editAnnuncio: adminProcedure
.input(EdiAnnuncioSchema)
.input(
z.object({ annuncioId: zAnnuncioId, data: z.custom<AnnunciUpdate>() }),
)
.mutation(async ({ input }) => {
return await editAnnuncioHandler({
...input,

View file

@ -1,9 +1,11 @@
import type { Annunci, AnnunciId } from "~/schemas/public/Annunci";
import type {
Annunci,
AnnunciId,
AnnunciUpdate,
} from "~/schemas/public/Annunci";
import { TRPCError } from "@trpc/server";
import { createSrcset } from "~/server/services/imageServer";
import { AnnuncioObjectWithImages } from "~/server/services/annunci.service";
import { zAnnuncioId } from "~/server/utils/zod_types";
import { z } from "zod/v4";
import { db } from "~/server/db";
import type { ServizioServizioId } from "~/schemas/public/Servizio";
import { env } from "~/env.mjs";
@ -132,7 +134,7 @@ export const getAnnunciById = async ({
});
}
return AnnuncioObjectWithImages<typeof annuncio>(annuncio);
return annuncio;
} catch (e) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
@ -350,34 +352,18 @@ export const getOptions_AnnunciHandler = async () => {
}
};
export const EdiAnnuncioSchema = z.object({
annuncioId: zAnnuncioId,
titolo_it: z.string().nonempty("Inserisci un titolo valido"),
desc_it: z.string().nonempty("Inserisci una descrizione valida"),
titolo_en: z.string().nonempty("Inserisci un titolo valido"),
desc_en: z.string().nonempty("Inserisci una descrizione valida"),
status: z.string().nonempty("Seleziona uno stato"),
});
type EditAnnuncioFormValues = z.infer<typeof EdiAnnuncioSchema>;
export const editAnnuncioHandler = async ({
annuncioId,
titolo_it,
desc_it,
titolo_en,
desc_en,
status,
}: EditAnnuncioFormValues) => {
data,
}: {
annuncioId: AnnunciId;
data: AnnunciUpdate;
}) => {
try {
await db
.updateTable("annunci")
.set({
titolo_it: titolo_it,
desc_it: desc_it,
titolo_en: titolo_en,
desc_en: desc_en,
stato: status,
...data,
})
.where("id", "=", annuncioId)
.execute();