infoalloggi-monorepo/apps/infoalloggi/src/server/api/routers/revalidation.ts
Marco Pedone 9c18ca37ab Refactor API structure and update flag handling
- Replaced references to `api.settings` with `api.etichette`, `api.flags`, and `api.strings` in various admin pages to align with new API structure.
- Removed the deprecated `settingsRouter` and introduced `flagsRouter`, `etichetteRouter`, and `stringsRouter` to handle respective functionalities.
- Updated the `Flags` schema to change the `value` field from string to boolean.
- Modified flag handling logic in the `SendMessage` and `NewMail` functions to check for boolean flags instead of string values.
- Added migration script to convert the `value` column in the `flags` table from string to boolean.
- Implemented new routers for banners, etichette, flags, revalidation, and strings to encapsulate related functionalities.
2025-11-20 12:38:42 +01:00

133 lines
3.6 KiB
TypeScript

import { TRPCError } from "@trpc/server";
import { z } from "zod/v4";
import { env } from "~/env";
import { adminProcedure, createTRPCRouter } from "~/server/api/trpc";
import { getCodici_AnnunciHandler } from "~/server/controllers/annunci.controller";
import {
revalidate,
revalidateMultiple,
} from "~/server/utils/revalidationHelper";
export const revalidationRouter = createTRPCRouter({
revalidateAllAnnunci: adminProcedure.mutation(async () => {
try {
const annunciOnline = await getCodici_AnnunciHandler();
const { success, failed } = await revalidateMultiple(
annunciOnline.map((cod) => `/annuncio/${cod}`),
);
//console.log(`Successfully revalidated pages: ${success.join(", ")}`);
if (success.length === 0) {
console.log("No annunci were revalidated.");
}
if (failed.length > 0) {
failed.forEach(({ path, error }) => {
console.error(`Failed to revalidate ${path}: ${error}`);
});
}
return {
status: "success",
};
} catch (err) {
console.error("Revalidation error:", err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: err instanceof Error ? err.message : "Revalidation failed",
cause: err,
});
}
}),
revalidateAnnuncio: adminProcedure
.input(
z.object({
cod: z.string(),
}),
)
.mutation(async ({ input }) => {
try {
await revalidate(`/annuncio/${input.cod}`);
return {
status: "success",
};
} catch (err) {
console.error("Revalidation error:", err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: err instanceof Error ? err.message : "Revalidation failed",
cause: err,
});
}
}),
updateAllAnnunci: adminProcedure.mutation(async () => {
try {
const response = await fetch(`${env.BACKENDSERVER_URL}/update`);
if (!response.ok) {
const errorData = await response.text().catch(() => "Unknown error");
throw new Error(
`Update failed with status ${response.status}: ${errorData}`,
);
}
const data = await response.json().catch(() => ({}));
const annunciOnline = await getCodici_AnnunciHandler();
const { success, failed } = await revalidateMultiple(
annunciOnline.map((cod) => `/annuncio/${cod}`),
);
if (success.length === 0) {
console.log("No annunci were revalidated.");
}
//console.log(`Successfully revalidated pages: ${success.join(", ")}`);
if (failed.length > 0) {
failed.forEach(({ path, error }) => {
console.error(`Failed to revalidate ${path}: ${error}`);
});
}
return {
status: "success",
details: data,
};
} catch (err) {
console.error("Update error:", err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: err instanceof Error ? err.message : "Update failed",
cause: err,
});
}
}),
updateAnnuncio: adminProcedure
.input(
z.object({
cod: z.string(),
}),
)
.mutation(async ({ input }) => {
try {
const response = await fetch(
`${env.BACKENDSERVER_URL}/update-cod/${input.cod}`,
);
if (!response.ok) {
const errorData = await response.text().catch(() => "Unknown error");
throw new Error(
`Update failed with status ${response.status}: ${errorData}`,
);
}
const data = await response.json().catch(() => ({}));
await revalidate(`/annuncio/${input.cod}`);
return {
status: "success",
details: data,
};
} catch (err) {
console.error("Update error:", err);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: err instanceof Error ? err.message : "Update failed",
cause: err,
});
}
}),
});