@@ -237,4 +237,37 @@ def get_latest_weather_data(user_id, shipment_id):
237237# return jsonify({'error': 'Shipment not found'}), 404
238238# return jsonify(shipment.to_dict()), 200
239239# except Exception as e:
240- # return jsonify({'error': str(e)}), 500
240+ # return jsonify({'error': str(e)}), 500
241+
242+ @token_required
243+ def update_shipment_status (user_id ):
244+ user = User .query .get (user_id )
245+ if not user :
246+ return jsonify ({'error' : 'User not found' }), 401
247+
248+ data = request .get_json ()
249+ shipment_id = data .get ("shipment_id" )
250+
251+ if not data or 'status' not in data :
252+ return jsonify ({'error' : 'Missing required field: status' }), 400
253+
254+ status = data ['status' ].lower ()
255+ if status not in ['active' , 'completed' , 'cancelled' ]:
256+ return jsonify ({'error' : 'Invalid status' }), 400
257+
258+ print (status , shipment_id )
259+
260+ if user .role == 'transporter_manager' :
261+ shipment = Shipment .query .filter_by (id = shipment_id , organization_id = user .organization_id ).first ()
262+ else :
263+ shipment = Shipment .query .filter_by (id = shipment_id , user_id = user_id ).first ()
264+
265+ if not shipment :
266+ return jsonify ({'error' : 'Shipment not found' }), 404
267+
268+ shipment .status = status
269+ shipment .updated_at = datetime .now (timezone .utc )
270+
271+ db .session .commit ()
272+
273+ return jsonify (shipment .to_dict ()), 200
0 commit comments