Seconda parte della replica soccorso istruttorio speculare al BE Gepafin.
Completata: scheduler cron (expire + reminder), upload documenti istruttore
e benef, fix duplicati config.
==SCHEDULER (app/scheduler.py NUOVO)==
APScheduler BackgroundScheduler integrato nel lifespan FastAPI.
Due cron attivi (timezone Europe/Rome):
expire_amendments() - cron 01:05 ogni notte
Speculare a ApplicationAmendmentScheduler.processAmendmentExpirationScheduler.
Trova amendment AWAITING con deadline < today, passa a EXPIRED.
Rimette pratica a UNDER_REVIEW se non ha altri amendment aperti.
Ritorna dict stats per logging/test.
queue_reminders() - cron 09:00 ogni mattina
Speculare a ExpirationScheduler.processAmendmentExpiration (data-driven).
Legge remission_expiration_config (type='AMENDMENT', interval_days=N),
per ogni riga trova amendment con deadline esattamente today+N e setta
pec_retry_after (marker che il BE vede via /internal pending-reminder).
Multipli row = multipli reminder (seed: 7gg + 2gg).
Il microservizio aggiorna solo stato DB. L invio effettivo di email
reminder lo fa il BE Gepafin tramite polling, tenant-aware.
==UPLOAD DOCUMENTI==
3 nuovi endpoint nel router istruttoria:
POST /instructor/{pid}/amendment/{aid}/upload-document
- Istruttore allega PDF al soccorso (motivazione, scheda tecnica).
- Consentito in DRAFT o AWAITING. Sostituisce precedente se esiste.
- Popola amendment_document_path + amendment_document_type.
DELETE /instructor/{pid}/amendment/{aid}/upload-document
- Rimuove allegato (solo in DRAFT).
POST /instructor/{pid}/amendment/{aid}/upload-response-document
- Benef allega PDF come supporto alla risposta.
- Consentito in AWAITING/RESPONSE_RECEIVED, solo proprietario.
- Popola response_document_path + response_document_type.
Riusa save_upload() esistente con entity_type dedicati.
==FIX storage.py==
Whitelist entity_type estesa con 'amendment-instructor-doc' +
'amendment-response-doc' (prima accettava solo invoice/ula/document,
bloccando l'upload con StorageError).
==FIX migration dedup==
Scoperto in test: migration 8 faceva INSERT ON CONFLICT DO NOTHING su
remission_expiration_config ma senza UNIQUE constraint. Ogni restart
inseriva duplicati (16 righe in DB invece di 2). Fix in migration 9:
DELETE duplicati + ADD UNIQUE (type, interval_days) + re-seed pulito.
==REQUIREMENTS==
APScheduler==3.10.4
==TEST E2E==
/tmp/test_amendment_r2_fixed.py passa su tutto:
[A] upload amendment_document istruttore + response_document benef + respond
[B] amendment scaduto artificiale -> expire_amendments lo marca EXPIRED,
pratica torna UNDER_REVIEW
[C] amendment a +2gg e +7gg -> queue_reminders accoda 2 reminder,
/internal pending-reminder li espone entrambi
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
"""
|
|
rendicontazione-api — microservizio sviluppato da BFLOWS per Gepafin.
|
|
Gestisce schemi di rendicontazione per bando, pratiche di rendicontazione,
|
|
fatture, ULA, soccorso istruttorio, upload file, verbale istruttoria.
|
|
|
|
Stack: FastAPI + SQLAlchemy + PostgreSQL (schema gepafin_rendic) + weasyprint.
|
|
Auth: JWT condiviso con GEPAFIN-BE.
|
|
"""
|
|
import logging
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from sqlalchemy import text
|
|
|
|
from .config import get_settings
|
|
from .db import engine, Base
|
|
from .migrations import run_migrations
|
|
from .scheduler import start_scheduler, stop_scheduler
|
|
from .routers import health, schemas, practices, debug, instructor, files, verbale, custom_checks, assignment, internal
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
|
log = logging.getLogger("rendicontazione-api")
|
|
|
|
settings = get_settings()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
log.info("Avvio rendicontazione-api")
|
|
try:
|
|
with engine.begin() as conn:
|
|
conn.execute(text(f'CREATE SCHEMA IF NOT EXISTS {settings.db_schema}'))
|
|
Base.metadata.create_all(bind=engine)
|
|
run_migrations(engine)
|
|
log.info(f"Schema '{settings.db_schema}' + tabelle + migrations OK")
|
|
start_scheduler()
|
|
except Exception as e:
|
|
log.error(f"Errore bootstrap DB: {e}")
|
|
raise
|
|
yield
|
|
stop_scheduler()
|
|
log.info("Shutdown rendicontazione-api")
|
|
|
|
|
|
app = FastAPI(
|
|
title="rendicontazione-api",
|
|
description="Microservizio rendicontazione per Gepafin — sviluppato da BFLOWS",
|
|
version="0.4.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=settings.cors_list,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(health.router, tags=["health"])
|
|
app.include_router(schemas.router)
|
|
app.include_router(practices.router)
|
|
app.include_router(debug.router)
|
|
app.include_router(instructor.router)
|
|
app.include_router(files.router)
|
|
app.include_router(verbale.router)
|
|
app.include_router(custom_checks.router)
|
|
app.include_router(assignment.router)
|
|
app.include_router(internal.router)
|
|
|
|
|
|
@app.get("/", tags=["root"])
|
|
def root():
|
|
return {
|
|
"service": "rendicontazione-api",
|
|
"version": "0.4.0",
|
|
"docs": "/docs",
|
|
"health": "/health",
|
|
}
|