Ali2206 commited on
Commit
5069021
·
1 Parent(s): d7e0738

Add dynamic checklist template system - Added checklist template storage in database - Created endpoint to fetch default template - Updated frontend to load template from database - Added script to populate default template - Removed hardcoded checklist structure

Browse files
Files changed (2) hide show
  1. insert_default_checklist.py +159 -0
  2. main.py +145 -0
insert_default_checklist.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Script to insert the default checklist template into the database
4
+ This script creates a template checklist that can be used as a base for new users
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ import asyncio
10
+ from datetime import datetime
11
+ from dotenv import load_dotenv
12
+ import motor.motor_asyncio
13
+
14
+ # Load environment variables
15
+ load_dotenv('mongodb.env')
16
+
17
+ # MongoDB connection
18
+ MONGODB_URI = os.getenv('MONGODB_URI', 'mongodb://localhost:27017/audit_checklist')
19
+
20
+ async def insert_default_checklist():
21
+ """Insert the default checklist template into the database"""
22
+
23
+ # Connect to MongoDB
24
+ client = motor.motor_asyncio.AsyncIOMotorClient(MONGODB_URI)
25
+ db = client.audit_checklist
26
+
27
+ # Default checklist template
28
+ default_checklist_template = {
29
+ "templateId": "default-audit-checklist",
30
+ "title": "Checklist di Audit Operativo (38 Controlli)",
31
+ "description": "Template per audit operativo in ambiente di produzione alimentare",
32
+ "version": "1.0",
33
+ "isTemplate": True,
34
+ "createdAt": datetime.utcnow(),
35
+ "updatedAt": datetime.utcnow(),
36
+ "sections": [
37
+ {
38
+ "id": "S1",
39
+ "title": "1. PERSONALE E IGIENE",
40
+ "icon": "Users",
41
+ "items": [
42
+ {"id": "I1.1", "requirement": "Indumenti da lavoro puliti (divisa, grembiule).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
43
+ {"id": "I1.2", "requirement": "Scarpe antinfortunistiche / Calzature pulite.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
44
+ {"id": "I1.3", "requirement": "Cuffie e/o Retine per capelli indossate correttamente.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
45
+ {"id": "I1.4", "requirement": "Assenza di gioielli, piercing visibili, unghie lunghe.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
46
+ {"id": "I1.5", "requirement": "Lavaggio mani documentato all'ingresso/reingresso.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
47
+ {"id": "I1.6", "requirement": "Assenza di cibo/bevande non autorizzate nelle aree produttive.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
48
+ ]
49
+ },
50
+ {
51
+ "id": "S2",
52
+ "title": "2. STRUTTURE E IMPIANTI",
53
+ "icon": "Building",
54
+ "items": [
55
+ {"id": "I2.1", "requirement": "Illuminazione adeguata e funzionante in tutte le aree.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
56
+ {"id": "I2.2", "requirement": "Porte esterne/interne in buone condizioni e chiuse correttamente (sigillatura).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
57
+ {"id": "I2.3", "requirement": "Integrità di pavimenti, pareti e soffitti (assenza di crepe/danni).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
58
+ {"id": "I2.4", "requirement": "Controllo vetri / lampade protette o anti-frantumazione.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
59
+ {"id": "I2.5", "requirement": "Condizioni igieniche dei servizi igienici e spogliatoi (pulizia e dotazioni).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
60
+ {"id": "I2.6", "requirement": "Ventilazione e aspirazione funzionanti, pulite e non ostruite.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
61
+ ]
62
+ },
63
+ {
64
+ "id": "S3",
65
+ "title": "3. GESTIONE E IGIENE AMBIENTALE",
66
+ "icon": "Package",
67
+ "items": [
68
+ {"id": "I3.1", "requirement": "Contenitori dei rifiuti puliti, chiusi e identificati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
69
+ {"id": "I3.2", "requirement": "Separazione corretta dei rifiuti (es. umido, secco, plastica).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
70
+ {"id": "I3.3", "requirement": "Area di stoccaggio rifiuti (interna ed esterna) ordinata e sanificata.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
71
+ {"id": "I3.4", "requirement": "Frequenza di rimozione dei rifiuti adeguata a prevenire accumuli.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
72
+ ]
73
+ },
74
+ {
75
+ "id": "S4",
76
+ "title": "4. CONTROLLO PROCESSO E QUALITÀ",
77
+ "icon": "Settings",
78
+ "items": [
79
+ {"id": "I4.1", "requirement": "Monitoraggio e registrazione dei Punti Critici di Controllo (CCP).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
80
+ {"id": "I4.2", "requirement": "Procedure di Buona Fabbricazione (GMP) rispettate (es. pulizia prima dell'avvio).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
81
+ {"id": "I4.3", "requirement": "Verifica e calibrazione del Metal Detector eseguita e registrata.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
82
+ {"id": "I4.4", "requirement": "Corretta identificazione dei lotti di materie prime e semilavorati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
83
+ {"id": "I4.5", "requirement": "Rispettate le temperature di stoccaggio dei prodotti finiti e intermedi.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
84
+ {"id": "I4.6", "requirement": "Corretta gestione, etichettatura e isolamento dei prodotti non conformi.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
85
+ ]
86
+ },
87
+ {
88
+ "id": "S5",
89
+ "title": "5. CONTROLLO INFESTANTI",
90
+ "icon": "Shield",
91
+ "items": [
92
+ {"id": "I5.1", "requirement": "Assenza di tracce visibili di roditori, insetti o uccelli.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
93
+ {"id": "I5.2", "requirement": "Stazioni di monitoraggio/trappole numerate, integre e registrate.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
94
+ {"id": "I5.3", "requirement": "Mappe e registri delle trappole aggiornati e disponibili.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
95
+ {"id": "I5.4", "requirement": "Barriere fisiche (es. reti, spazzole) anti-intrusione in buone condizioni.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
96
+ {"id": "I5.5", "requirement": "Prodotti chimici di controllo infestanti stoccati in modo sicuro e separato.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
97
+ ]
98
+ },
99
+ {
100
+ "id": "S6",
101
+ "title": "6. MANUTENZIONE E VETRI",
102
+ "icon": "Settings",
103
+ "items": [
104
+ {"id": "I6.1", "requirement": "Piano di manutenzione preventiva e correttiva rispettato e registrato.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
105
+ {"id": "I6.2", "requirement": "Lubrificanti e oli utilizzati autorizzati per uso alimentare (se richiesto).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
106
+ {"id": "I6.3", "requirement": "Registri di calibrazione/verifica degli strumenti di misura critici aggiornati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
107
+ {"id": "I6.4", "requirement": "Gestione dei vetri rotti (registri rotture) aggiornata e conforme alla procedura.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
108
+ {"id": "I6.5", "requirement": "Assenza di perdite, gocciolamenti o cavi scoperti da impianti/macchinari.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
109
+ {"id": "I6.6", "requirement": "Area di deposito utensili e pezzi di ricambio ordinata e pulita.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
110
+ ]
111
+ },
112
+ {
113
+ "id": "S7",
114
+ "title": "7. DOCUMENTAZIONE E FORMAZIONE",
115
+ "icon": "FileText",
116
+ "items": [
117
+ {"id": "I7.1", "requirement": "Documentazione di processo (procedure, istruzioni) aggiornata e disponibile.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
118
+ {"id": "I7.2", "requirement": "Registri di formazione del personale aggiornati e verificabili.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
119
+ {"id": "I7.3", "requirement": "Certificazioni e attestati del personale validi e aggiornati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
120
+ {"id": "I7.4", "requirement": "Controllo documenti (versioni, distribuzione, obsolescenza) conforme.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
121
+ {"id": "I7.5", "requirement": "Archiviazione e conservazione documenti secondo i requisiti normativi.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
122
+ ]
123
+ }
124
+ ],
125
+ "totalItems": 38,
126
+ "completedItems": 0,
127
+ "nonCompliantItems": 0,
128
+ "complianceScore": 0.0
129
+ }
130
+
131
+ try:
132
+ # Check if template already exists
133
+ existing_template = await db.checklist_templates.find_one({"templateId": "default-audit-checklist"})
134
+
135
+ if existing_template:
136
+ print("✅ Default checklist template already exists in database")
137
+ print(f"Template ID: {existing_template['templateId']}")
138
+ print(f"Created: {existing_template['createdAt']}")
139
+ return
140
+
141
+ # Insert the template
142
+ result = await db.checklist_templates.insert_one(default_checklist_template)
143
+
144
+ if result.inserted_id:
145
+ print("✅ Successfully inserted default checklist template into database")
146
+ print(f"Template ID: default-audit-checklist")
147
+ print(f"Total sections: {len(default_checklist_template['sections'])}")
148
+ print(f"Total items: {default_checklist_template['totalItems']}")
149
+ else:
150
+ print("❌ Failed to insert template")
151
+
152
+ except Exception as e:
153
+ print(f"❌ Error inserting template: {e}")
154
+ finally:
155
+ client.close()
156
+
157
+ if __name__ == "__main__":
158
+ print("🚀 Inserting default checklist template into database...")
159
+ asyncio.run(insert_default_checklist())
main.py CHANGED
@@ -738,6 +738,151 @@ 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/checklists", response_model=Dict[str, Any])
742
  async def get_all_checklists():
743
  """
 
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
+ """
744
+ Get the default checklist template
745
+
746
+ Returns:
747
+ Dictionary containing the default checklist template structure
748
+
749
+ Raises:
750
+ HTTPException: If template not found or database operation fails
751
+ """
752
+ try:
753
+ logger.info("Retrieving default checklist template")
754
+
755
+ # Find the default template
756
+ template_doc = await db.checklist_templates.find_one({"templateId": "default-audit-checklist"})
757
+
758
+ if not template_doc:
759
+ # If template doesn't exist, create it from the hardcoded structure
760
+ logger.info("Default template not found, creating from hardcoded structure")
761
+
762
+ # Use the same structure as in get_checklist but mark as template
763
+ default_template = {
764
+ "templateId": "default-audit-checklist",
765
+ "title": "Checklist di Audit Operativo (38 Controlli)",
766
+ "description": "Template per audit operativo in ambiente di produzione alimentare",
767
+ "version": "1.0",
768
+ "isTemplate": True,
769
+ "createdAt": datetime.utcnow(),
770
+ "updatedAt": datetime.utcnow(),
771
+ "sections": [
772
+ {
773
+ "id": "S1",
774
+ "title": "1. PERSONALE E IGIENE",
775
+ "icon": "Users",
776
+ "items": [
777
+ {"id": "I1.1", "requirement": "Indumenti da lavoro puliti (divisa, grembiule).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
778
+ {"id": "I1.2", "requirement": "Scarpe antinfortunistiche / Calzature pulite.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
779
+ {"id": "I1.3", "requirement": "Cuffie e/o Retine per capelli indossate correttamente.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
780
+ {"id": "I1.4", "requirement": "Assenza di gioielli, piercing visibili, unghie lunghe.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
781
+ {"id": "I1.5", "requirement": "Lavaggio mani documentato all'ingresso/reingresso.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
782
+ {"id": "I1.6", "requirement": "Assenza di cibo/bevande non autorizzate nelle aree produttive.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
783
+ ]
784
+ },
785
+ {
786
+ "id": "S2",
787
+ "title": "2. STRUTTURE E IMPIANTI",
788
+ "icon": "Building",
789
+ "items": [
790
+ {"id": "I2.1", "requirement": "Illuminazione adeguata e funzionante in tutte le aree.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
791
+ {"id": "I2.2", "requirement": "Porte esterne/interne in buone condizioni e chiuse correttamente (sigillatura).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
792
+ {"id": "I2.3", "requirement": "Integrità di pavimenti, pareti e soffitti (assenza di crepe/danni).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
793
+ {"id": "I2.4", "requirement": "Controllo vetri / lampade protette o anti-frantumazione.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
794
+ {"id": "I2.5", "requirement": "Condizioni igieniche dei servizi igienici e spogliatoi (pulizia e dotazioni).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
795
+ {"id": "I2.6", "requirement": "Ventilazione e aspirazione funzionanti, pulite e non ostruite.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
796
+ ]
797
+ },
798
+ {
799
+ "id": "S3",
800
+ "title": "3. GESTIONE E IGIENE AMBIENTALE",
801
+ "icon": "Package",
802
+ "items": [
803
+ {"id": "I3.1", "requirement": "Contenitori dei rifiuti puliti, chiusi e identificati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
804
+ {"id": "I3.2", "requirement": "Separazione corretta dei rifiuti (es. umido, secco, plastica).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
805
+ {"id": "I3.3", "requirement": "Area di stoccaggio rifiuti (interna ed esterna) ordinata e sanificata.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
806
+ {"id": "I3.4", "requirement": "Frequenza di rimozione dei rifiuti adeguata a prevenire accumuli.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
807
+ ]
808
+ },
809
+ {
810
+ "id": "S4",
811
+ "title": "4. CONTROLLO PROCESSO E QUALITÀ",
812
+ "icon": "Settings",
813
+ "items": [
814
+ {"id": "I4.1", "requirement": "Monitoraggio e registrazione dei Punti Critici di Controllo (CCP).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
815
+ {"id": "I4.2", "requirement": "Procedure di Buona Fabbricazione (GMP) rispettate (es. pulizia prima dell'avvio).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
816
+ {"id": "I4.3", "requirement": "Verifica e calibrazione del Metal Detector eseguita e registrata.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
817
+ {"id": "I4.4", "requirement": "Corretta identificazione dei lotti di materie prime e semilavorati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
818
+ {"id": "I4.5", "requirement": "Rispettate le temperature di stoccaggio dei prodotti finiti e intermedi.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
819
+ {"id": "I4.6", "requirement": "Corretta gestione, etichettatura e isolamento dei prodotti non conformi.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
820
+ ]
821
+ },
822
+ {
823
+ "id": "S5",
824
+ "title": "5. CONTROLLO INFESTANTI",
825
+ "icon": "Shield",
826
+ "items": [
827
+ {"id": "I5.1", "requirement": "Assenza di tracce visibili di roditori, insetti o uccelli.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
828
+ {"id": "I5.2", "requirement": "Stazioni di monitoraggio/trappole numerate, integre e registrate.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
829
+ {"id": "I5.3", "requirement": "Mappe e registri delle trappole aggiornati e disponibili.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
830
+ {"id": "I5.4", "requirement": "Barriere fisiche (es. reti, spazzole) anti-intrusione in buone condizioni.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
831
+ {"id": "I5.5", "requirement": "Prodotti chimici di controllo infestanti stoccati in modo sicuro e separato.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
832
+ ]
833
+ },
834
+ {
835
+ "id": "S6",
836
+ "title": "6. MANUTENZIONE E VETRI",
837
+ "icon": "Settings",
838
+ "items": [
839
+ {"id": "I6.1", "requirement": "Piano di manutenzione preventiva e correttiva rispettato e registrato.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
840
+ {"id": "I6.2", "requirement": "Lubrificanti e oli utilizzati autorizzati per uso alimentare (se richiesto).", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
841
+ {"id": "I6.3", "requirement": "Registri di calibrazione/verifica degli strumenti di misura critici aggiornati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
842
+ {"id": "I6.4", "requirement": "Gestione dei vetri rotti (registri rotture) aggiornata e conforme alla procedura.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
843
+ {"id": "I6.5", "requirement": "Assenza di perdite, gocciolamenti o cavi scoperti da impianti/macchinari.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
844
+ {"id": "I6.6", "requirement": "Area di deposito utensili e pezzi di ricambio ordinata e pulita.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
845
+ ]
846
+ },
847
+ {
848
+ "id": "S7",
849
+ "title": "7. DOCUMENTAZIONE E FORMAZIONE",
850
+ "icon": "FileText",
851
+ "items": [
852
+ {"id": "I7.1", "requirement": "Documentazione di processo (procedure, istruzioni) aggiornata e disponibile.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
853
+ {"id": "I7.2", "requirement": "Registri di formazione del personale aggiornati e verificabili.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
854
+ {"id": "I7.3", "requirement": "Certificazioni e attestati del personale validi e aggiornati.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
855
+ {"id": "I7.4", "requirement": "Controllo documenti (versioni, distribuzione, obsolescenza) conforme.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None},
856
+ {"id": "I7.5", "requirement": "Archiviazione e conservazione documenti secondo i requisiti normativi.", "compliance": "N/A", "deviation": "", "action": "", "imageData": None}
857
+ ]
858
+ }
859
+ ],
860
+ "totalItems": 38,
861
+ "completedItems": 0,
862
+ "nonCompliantItems": 0,
863
+ "complianceScore": 0.0
864
+ }
865
+
866
+ # Insert the template
867
+ await db.checklist_templates.insert_one(default_template)
868
+ template_doc = default_template
869
+
870
+ # Serialize the document
871
+ serialized_template = serialize_checklist(template_doc)
872
+
873
+ return {
874
+ "success": True,
875
+ "data": serialized_template,
876
+ "message": "Default checklist template retrieved successfully"
877
+ }
878
+
879
+ except Exception as e:
880
+ logger.error(f"Error retrieving default checklist template: {e}")
881
+ raise HTTPException(
882
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
883
+ detail=f"Failed to retrieve checklist template: {str(e)}"
884
+ )
885
+
886
  @app.get("/api/checklists", response_model=Dict[str, Any])
887
  async def get_all_checklists():
888
  """