feat: update GetAnnunciFromMiogest with improved error handling and validation; add curl installation in Dockerfile; implement checkAnnunci procedure for active announcements notification

This commit is contained in:
Marco Pedone 2026-01-12 10:05:42 +01:00
parent d56d8f8ec8
commit 3de192636b
3 changed files with 46 additions and 6 deletions

View file

@ -47,21 +47,29 @@ func (m *MiogestHandler) GetAnnunci() typesdefs.AnnunciXML {
func (m *MiogestHandler) GetAnnunciFromMiogest() {
maxRetries := 3
baseDelay := 30 * time.Second
baseDelay := 10 * time.Second
for attempt := 0; attempt < maxRetries; attempt++ {
for attempt := range maxRetries {
var err error
m.AnnunciXML, err = utils.GetDataXML[typesdefs.AnnunciXML]("http://partner.miogest.com/agenzie/infoalloggi.xml")
if err == nil {
// Success
m.annunci_cache_expires = time.Now().Add(1 * time.Hour)
return
// Success - validate we got data
if len(m.AnnunciXML.Annuncio) > 0 {
m.annunci_cache_expires = time.Now().Add(1 * time.Hour)
return
}
log.Printf("Warning: Successfully fetched XML but got 0 annunci")
}
// Log the specific error type
if err != nil {
log.Printf("Failed to get annunci (attempt %d/%d): %v", attempt+1, maxRetries, err)
}
// Failed - check if we should retry
if attempt < maxRetries-1 {
delay := baseDelay * time.Duration(1<<attempt) // Exponential backoff: 2s, 4s, 8s
delay := baseDelay * time.Duration(1<<attempt) // Exponential backoff
log.Printf("Failed to get annunci (attempt %d/%d): %v. Retrying in %v...",
attempt+1, maxRetries, err, delay)
time.Sleep(delay)

View file

@ -134,6 +134,7 @@ ENV STORAGE_URL=$STORAGE_URL
ARG STORAGE_TOKEN
ENV STORAGE_TOKEN=$STORAGE_TOKEN
RUN apk add --no-cache curl
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001

View file

@ -2,7 +2,9 @@ import { z } from "zod/v4";
import type { AnnunciUpdate } from "~/schemas/public/Annunci";
import {
adminProcedure,
apiProcedure,
createTRPCRouter,
protectedApiProcedure,
protectedProcedure,
publicProcedure,
} from "~/server/api/trpc";
@ -18,6 +20,8 @@ import {
getOptions_AnnunciHandler,
getProprietarioDataHandler,
} from "~/server/controllers/annunci.controller";
import { db } from "~/server/db";
import { NewMail } from "~/server/services/mailer";
import { zAnnuncioId, zServizioId } from "~/server/utils/zod_types";
export const annunciRouter = createTRPCRouter({
@ -108,4 +112,31 @@ export const annunciRouter = createTRPCRouter({
...input,
});
}),
checkAnnunci: apiProcedure.query(async () => {
const hasAnnunci = await db
.selectFrom("annunci")
.select("id")
.where("stato", "=", "Attivo")
.where("web", "is", true)
.execute();
if (hasAnnunci.length === 0) {
await NewMail({
template: {
mailType: "generic",
props: {
title: "Attenzione: Nessun annuncio attivo!",
testo:
"Nel sistema non è presente nessun annuncio con stato Attivo e visibile sul web.",
},
},
mail: {
subject: "Attenzione: Nessun annuncio attivo!",
to: "m.pedone98@gmail.com",
},
});
return { status: "no_annunci", timestamp: Date.now() };
}
return { status: "ok", timestamp: Date.now() };
}),
});