Auto-create default template on server startup - Add ensure_default_template_exists function - Template automatically created if not found - No manual script execution needed
Browse files
main.py
CHANGED
|
@@ -738,6 +738,128 @@ async def upload_checklist_image(user_id: str, image: UploadFile = File(...)):
|
|
| 738 |
detail=f"Failed to upload image: {str(e)}"
|
| 739 |
)
|
| 740 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 741 |
@app.get("/api/checklist-template/default", response_model=Dict[str, Any])
|
| 742 |
async def get_default_checklist_template():
|
| 743 |
"""
|
|
@@ -752,8 +874,12 @@ async def get_default_checklist_template():
|
|
| 752 |
try:
|
| 753 |
logger.info("Retrieving default checklist template")
|
| 754 |
|
| 755 |
-
#
|
| 756 |
-
template_doc = await
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
|
| 758 |
if not template_doc:
|
| 759 |
# If template doesn't exist, create it from the hardcoded structure
|
|
|
|
| 738 |
detail=f"Failed to upload image: {str(e)}"
|
| 739 |
)
|
| 740 |
|
| 741 |
+
async def ensure_default_template_exists():
|
| 742 |
+
"""
|
| 743 |
+
Ensure the default template exists in the database.
|
| 744 |
+
Creates it if it doesn't exist.
|
| 745 |
+
"""
|
| 746 |
+
try:
|
| 747 |
+
# Check if template exists
|
| 748 |
+
template = await db.checklist_templates.find_one({"templateId": "default"})
|
| 749 |
+
|
| 750 |
+
if not template:
|
| 751 |
+
logger.info("Default template not found, creating it...")
|
| 752 |
+
|
| 753 |
+
# Create the default template
|
| 754 |
+
default_template = {
|
| 755 |
+
"templateId": "default",
|
| 756 |
+
"title": "Checklist di Audit Operativo",
|
| 757 |
+
"sections": [
|
| 758 |
+
{
|
| 759 |
+
"id": "S1",
|
| 760 |
+
"title": "1. PERSONALE E IGIENE",
|
| 761 |
+
"icon": "Users",
|
| 762 |
+
"items": [
|
| 763 |
+
{"id": "I1.1", "requirement": "Indumenti da lavoro puliti (divisa, grembiule)."},
|
| 764 |
+
{"id": "I1.2", "requirement": "Scarpe antinfortunistiche / Calzature pulite."},
|
| 765 |
+
{"id": "I1.3", "requirement": "Cuffie e/o Retine per capelli indossate correttamente."},
|
| 766 |
+
{"id": "I1.4", "requirement": "Assenza di gioielli, piercing visibili, unghie lunghe."},
|
| 767 |
+
{"id": "I1.5", "requirement": "Lavaggio mani documentato all'ingresso/reingresso."},
|
| 768 |
+
{"id": "I1.6", "requirement": "Assenza di cibo/bevande non autorizzate nelle aree produttive."},
|
| 769 |
+
],
|
| 770 |
+
},
|
| 771 |
+
{
|
| 772 |
+
"id": "S2",
|
| 773 |
+
"title": "2. STRUTTURE E IMPIANTI",
|
| 774 |
+
"icon": "Building",
|
| 775 |
+
"items": [
|
| 776 |
+
{"id": "I2.1", "requirement": "Illuminazione adeguata e funzionante in tutte le aree."},
|
| 777 |
+
{"id": "I2.2", "requirement": "Porte esterne/interne in buone condizioni e chiuse correttamente (sigillatura)."},
|
| 778 |
+
{"id": "I2.3", "requirement": "Integrità di pavimenti, pareti e soffitti (assenza di crepe/danni)."},
|
| 779 |
+
{"id": "I2.4", "requirement": "Controllo vetri / lampade protette o anti-frantumazione."},
|
| 780 |
+
{"id": "I2.5", "requirement": "Condizioni igieniche dei servizi igienici e spogliatoi (pulizia e dotazioni)."},
|
| 781 |
+
{"id": "I2.6", "requirement": "Ventilazione e aspirazione funzionanti, pulite e non ostruite."},
|
| 782 |
+
],
|
| 783 |
+
},
|
| 784 |
+
{
|
| 785 |
+
"id": "S3",
|
| 786 |
+
"title": "3. GESTIONE E IGIENE AMBIENTALE",
|
| 787 |
+
"icon": "Package",
|
| 788 |
+
"items": [
|
| 789 |
+
{"id": "I3.1", "requirement": "Contenitori dei rifiuti puliti, chiusi e identificati."},
|
| 790 |
+
{"id": "I3.2", "requirement": "Separazione corretta dei rifiuti (es. umido, secco, plastica)."},
|
| 791 |
+
{"id": "I3.3", "requirement": "Area di stoccaggio rifiuti (interna ed esterna) ordinata e sanificata."},
|
| 792 |
+
{"id": "I3.4", "requirement": "Frequenza di rimozione dei rifiuti adeguata a prevenire accumuli."},
|
| 793 |
+
],
|
| 794 |
+
},
|
| 795 |
+
{
|
| 796 |
+
"id": "S4",
|
| 797 |
+
"title": "4. CONTROLLO PROCESSO E QUALITÀ",
|
| 798 |
+
"icon": "Settings",
|
| 799 |
+
"items": [
|
| 800 |
+
{"id": "I4.1", "requirement": "Monitoraggio e registrazione dei Punti Critici di Controllo (CCP)."},
|
| 801 |
+
{"id": "I4.2", "requirement": "Procedure di Buona Fabbricazione (GMP) rispettate (es. pulizia prima dell'avvio)."},
|
| 802 |
+
{"id": "I4.3", "requirement": "Verifica e calibrazione del Metal Detector eseguita e registrata."},
|
| 803 |
+
{"id": "I4.4", "requirement": "Corretta identificazione dei lotti di materie prime e semilavorati."},
|
| 804 |
+
{"id": "I4.5", "requirement": "Rispettate le temperature di stoccaggio dei prodotti finiti e intermedi."},
|
| 805 |
+
{"id": "I4.6", "requirement": "Corretta gestione, etichettatura e isolamento dei prodotti non conformi."},
|
| 806 |
+
],
|
| 807 |
+
},
|
| 808 |
+
{
|
| 809 |
+
"id": "S5",
|
| 810 |
+
"title": "5. CONTROLLO INFESTANTI",
|
| 811 |
+
"icon": "Shield",
|
| 812 |
+
"items": [
|
| 813 |
+
{"id": "I5.1", "requirement": "Assenza di tracce visibili di roditori, insetti o uccelli."},
|
| 814 |
+
{"id": "I5.2", "requirement": "Stazioni di monitoraggio/trappole numerate, integre e registrate."},
|
| 815 |
+
{"id": "I5.3", "requirement": "Mappe e registri delle trappole aggiornati e disponibili."},
|
| 816 |
+
{"id": "I5.4", "requirement": "Barriere fisiche (es. reti, spazzole) anti-intrusione in buone condizioni."},
|
| 817 |
+
{"id": "I5.5", "requirement": "Prodotti chimici di controllo infestanti stoccati in modo sicuro e separato."},
|
| 818 |
+
],
|
| 819 |
+
},
|
| 820 |
+
{
|
| 821 |
+
"id": "S6",
|
| 822 |
+
"title": "6. MANUTENZIONE E VETRI",
|
| 823 |
+
"icon": "Settings",
|
| 824 |
+
"items": [
|
| 825 |
+
{"id": "I6.1", "requirement": "Piano di manutenzione preventiva e correttiva rispettato e registrato."},
|
| 826 |
+
{"id": "I6.2", "requirement": "Lubrificanti e oli utilizzati autorizzati per uso alimentare (se richiesto)."},
|
| 827 |
+
{"id": "I6.3", "requirement": "Registri di calibrazione/verifica degli strumenti di misura critici aggiornati."},
|
| 828 |
+
{"id": "I6.4", "requirement": "Gestione dei vetri rotti (registri rotture) aggiornata e conforme alla procedura."},
|
| 829 |
+
{"id": "I6.5", "requirement": "Assenza di perdite, gocciolamenti o cavi scoperti da impianti/macchinari."},
|
| 830 |
+
{"id": "I6.6", "requirement": "Area di deposito utensili e pezzi di ricambio ordinata e pulita."},
|
| 831 |
+
],
|
| 832 |
+
},
|
| 833 |
+
{
|
| 834 |
+
"id": "S7",
|
| 835 |
+
"title": "7. DOCUMENTAZIONE E FORMAZIONE",
|
| 836 |
+
"icon": "FileText",
|
| 837 |
+
"items": [
|
| 838 |
+
{"id": "I7.1", "requirement": "Documentazione di processo (procedure, istruzioni) aggiornata e disponibile."},
|
| 839 |
+
{"id": "I7.2", "requirement": "Registri di formazione del personale aggiornati e verificabili."},
|
| 840 |
+
{"id": "I7.3", "requirement": "Certificazioni e attestati del personale validi e aggiornati."},
|
| 841 |
+
{"id": "I7.4", "requirement": "Controllo documenti (versioni, distribuzione, obsolescenza) conforme."},
|
| 842 |
+
{"id": "I7.5", "requirement": "Archiviazione e conservazione documenti secondo i requisiti normativi."},
|
| 843 |
+
],
|
| 844 |
+
},
|
| 845 |
+
],
|
| 846 |
+
"metadata": {
|
| 847 |
+
"createdAt": datetime.utcnow().isoformat(),
|
| 848 |
+
"updatedAt": datetime.utcnow().isoformat(),
|
| 849 |
+
}
|
| 850 |
+
}
|
| 851 |
+
|
| 852 |
+
result = await db.checklist_templates.insert_one(default_template)
|
| 853 |
+
logger.info(f"Created default template with ID: {result.inserted_id}")
|
| 854 |
+
return default_template
|
| 855 |
+
else:
|
| 856 |
+
logger.info("Default template already exists")
|
| 857 |
+
return template
|
| 858 |
+
|
| 859 |
+
except Exception as e:
|
| 860 |
+
logger.error(f"Error ensuring default template exists: {e}")
|
| 861 |
+
return None
|
| 862 |
+
|
| 863 |
@app.get("/api/checklist-template/default", response_model=Dict[str, Any])
|
| 864 |
async def get_default_checklist_template():
|
| 865 |
"""
|
|
|
|
| 874 |
try:
|
| 875 |
logger.info("Retrieving default checklist template")
|
| 876 |
|
| 877 |
+
# Ensure template exists (creates if not found)
|
| 878 |
+
template_doc = await ensure_default_template_exists()
|
| 879 |
+
|
| 880 |
+
if not template_doc:
|
| 881 |
+
# Fallback to looking for old template ID
|
| 882 |
+
template_doc = await db.checklist_templates.find_one({"templateId": "default-audit-checklist"})
|
| 883 |
|
| 884 |
if not template_doc:
|
| 885 |
# If template doesn't exist, create it from the hardcoded structure
|