77from compliance_api .auth import auth
88from compliance_api .exceptions import ResourceNotFoundError
99from compliance_api .schemas import (
10- InspectionRecordCreateSchema , InspectionRecordSchema , ResetInspectionRecordFieldSchema ,
10+ CreateInspectionRecordApprovalSchema , InspectionRecordApprovalSchema , InspectionRecordCreateSchema ,
11+ InspectionRecordSchema , ResetInspectionRecordFieldSchema , UpdateInspectionRecordApprovalSchema ,
1112 UpdateInspectionRecordSchema )
12- from compliance_api .services import InspectionRecordService
13+ from compliance_api .services import InspectionRecordApprovalService , InspectionRecordService
1314from compliance_api .utils .enum import PermissionEnum
1415from compliance_api .utils .util import cors_preflight
1516
1617from .apihelper import Api as ApiHelper
1718
1819
19- API = Namespace ("inspection-records" , description = "Endpoints for Inspection Record" )
20+ API = Namespace ("inspection-records" ,
21+ description = "Endpoints for Inspection Record" )
2022
2123ir_create_request_model = ApiHelper .convert_ma_schema_to_restx_model (
2224 API , InspectionRecordCreateSchema (), "IRCreateRequest"
2729ir_update_request_model = ApiHelper .convert_ma_schema_to_restx_model (
2830 API , UpdateInspectionRecordSchema (), "UpdateInspection"
2931)
32+ ir_approval_create_request = ApiHelper .convert_ma_schema_to_restx_model (
33+ API , CreateInspectionRecordApprovalSchema (), "IRApproval"
34+ )
35+ ir_approval_schema = ApiHelper .convert_ma_schema_to_restx_model (
36+ API , InspectionRecordApprovalSchema (), "IRApproval"
37+ )
38+ ir_approval_update_request = ApiHelper .convert_ma_schema_to_restx_model (
39+ API , UpdateInspectionRecordApprovalSchema (), "IRApprovalUpdate"
40+ )
3041ir_reset_field_model = ApiHelper .convert_ma_schema_to_restx_model (
3142 API , ResetInspectionRecordFieldSchema (), "ResetInspectionField"
3243)
3344
3445
35- @cors_preflight ("GET, OPTIONS, POST" )
46+ @cors_preflight ("GET, OPTIONS, POST, PATCH " )
3647@API .route ("" , methods = ["POST" , "GET" , "OPTIONS" ])
3748class InspectionRecords (Resource ):
3849 """Resource for managing inspection records."""
@@ -61,7 +72,8 @@ def get(inspection_id):
6172 def post (inspection_id ):
6273 """Create a agency."""
6374 ir_create_request = InspectionRecordCreateSchema ().load (API .payload )
64- created_ir = InspectionRecordService .create (ir_create_request , inspection_id )
75+ created_ir = InspectionRecordService .create (
76+ ir_create_request , inspection_id )
6577 return InspectionRecordSchema ().dump (created_ir ), HTTPStatus .CREATED
6678
6779
@@ -79,7 +91,8 @@ class InspectionRecord(Resource):
7991 @auth .require
8092 def get (inspection_record_id ):
8193 """Fetch inspection record by id."""
82- inspection_record = InspectionRecordService .get_by_id (inspection_record_id )
94+ inspection_record = InspectionRecordService .get_by_id (
95+ inspection_record_id )
8396 if not inspection_record :
8497 raise ResourceNotFoundError (
8598 f"No inspection found for the given ID : { inspection_record_id } "
@@ -99,12 +112,95 @@ def patch(inspection_id, inspection_record_id):
99112 updated_ir = InspectionRecordService .update (
100113 inspection_id , inspection_record_id , ir_update_data
101114 )
102- if not updated_ir :
103- raise ResourceNotFoundError ("Inspection record not found" )
104115 return InspectionRecordSchema ().dump (updated_ir ), HTTPStatus .OK
105116
106117
107118@cors_preflight ("PATCH, OPTIONS" )
119+ @API .route ("/<int:inspection_record_id>/switch-to-final" , methods = ["PATCH" , "OPTIONS" ])
120+ class InspectionRecordFinal (Resource ):
121+ """Resource to handle InspectionRecord."""
122+
123+ @staticmethod
124+ @API .response (code = 200 , description = "Sucess" , model = ir_list_model )
125+ @API .expect (ir_update_request_model )
126+ @ApiHelper .swagger_decorators (API , endpoint_description = "Switch to FINAL IR" )
127+ @API .response (404 , "Not Found" )
128+ @auth .require
129+ def patch (inspection_id , inspection_record_id ):
130+ """Swith IR to FINAL."""
131+ final_ir = InspectionRecordService .switch_to_final (
132+ inspection_id , inspection_record_id
133+ )
134+ return InspectionRecordSchema ().dump (final_ir ), HTTPStatus .OK
135+
136+
137+ @cors_preflight ("GET, OPTIONS, POST, PATCH" )
138+ @API .route ("/<int:inspection_record_id>/approvals" , methods = ["POST" , "GET" , "OPTIONS" ])
139+ class InspectionRecordApprovals (Resource ):
140+ """Resource for managing inspection records."""
141+
142+ @staticmethod
143+ @API .response (code = 200 , description = "Success" , model = [ir_approval_schema ])
144+ @ApiHelper .swagger_decorators (
145+ API , endpoint_description = "Fetch all inspection record approvals"
146+ )
147+ @auth .require
148+ def get (inspection_id , inspection_record_id ): # pylint: disable=no-self-use, unused-argument
149+ """Fetch all inspection record approvals."""
150+ approvals = InspectionRecordApprovalService .get_all_approvals (
151+ inspection_record_id
152+ )
153+ approval_schema = InspectionRecordApprovalSchema (many = True )
154+ return approval_schema .dump (approvals ), HTTPStatus .OK
155+
156+ @staticmethod
157+ @auth .require
158+ @ApiHelper .swagger_decorators (
159+ API , endpoint_description = "Create an inspection record approval"
160+ )
161+ @API .expect (ir_approval_create_request )
162+ @API .response (
163+ code = 201 , model = ir_approval_schema , description = "IRApprovalRequestCreated"
164+ )
165+ @API .response (400 , "Bad Request" )
166+ def post (inspection_id , inspection_record_id ):
167+ """Create a agency."""
168+ ir_approval_request = CreateInspectionRecordApprovalSchema ().load (API .payload )
169+ created_aproval = InspectionRecordApprovalService .create_approval (
170+ ir_approval_request , inspection_id , inspection_record_id
171+ )
172+ return (
173+ InspectionRecordApprovalSchema ().dump (created_aproval ),
174+ HTTPStatus .CREATED ,
175+ )
176+
177+
178+ @cors_preflight ("OPTIONS, PATCH, GET" )
179+ @API .route (
180+ "/<int:inspection_record_id>/approvals/<int:approval_id>" ,
181+ methods = ["PATCH" , "GET" , "OPTIONS" ],
182+ )
183+ class InspectionRecordApproval (Resource ):
184+ """Resource for managing inspection record approval."""
185+
186+ @staticmethod
187+ @API .response (code = 200 , description = "Sucess" , model = ir_approval_schema )
188+ @API .expect (ir_approval_update_request )
189+ @ApiHelper .swagger_decorators (
190+ API , endpoint_description = "Update inspection record approval"
191+ )
192+ @API .response (404 , "Not Found" )
193+ @API .response (400 , "Bad Request" )
194+ @auth .require
195+ def patch (inspection_id , inspection_record_id , approval_id ):
196+ """Update inspection record approval."""
197+ approval_update_data = UpdateInspectionRecordApprovalSchema ().load (API .payload )
198+ updated_approval = InspectionRecordApprovalService .update_approval (
199+ inspection_id , inspection_record_id , approval_id , approval_update_data
200+ )
201+ return InspectionRecordApprovalSchema ().dump (updated_approval ), HTTPStatus .OK
202+
203+
108204@API .route ("/<int:inspection_record_id>/reset" , methods = ["PATCH" , "OPTIONS" ])
109205class InspectionRecordReset (Resource ):
110206 """Resource for resetting inspection record fields."""
0 commit comments