def change_parcel_destination(parcel_id): """Change the destination of a parcel delivery order""" current_user = get_jwt_identity() user_input = request.get_json(force=True) parcel = db.get_a_parcel(parcel_id) if parcel: if current_user.get('id') == parcel[2]: destination = user_input.get('destination') price = user_input.get('total_price') validated_destination = Validator.validate_str_to_change(destination) if validated_destination: return validated_destination if parcel[10] == 'cancelled' or parcel[10] == 'delivered': return jsonify({"message":f"Destination of parcel {parcel_id} can not be changed because it was cancelled or delivered"}), 400 destination_change = db.change_destination(parcel_id, destination, price) if destination_change: return jsonify({"message":f"Destination of parcel {parcel_id} changed to {destination}"}), 200 return jsonify({'message':f"Invalid request! You do not have rights to change the destination of a parcel"}), 401 return jsonify({'message':f"There is no parcel with ID {parcel_id}"}), 404
def change_present_location(parcel_id): """Change the present location of a parcel delivery order""" current_user = get_jwt_identity() user_input = request.get_json(force=True) parcel = db.get_a_parcel(parcel_id) if parcel: if current_user.get('role') == True: present_location = user_input.get('present_location') validated_location = Validator.validate_str_to_change(present_location) if validated_location: return validated_location if parcel[10] == 'cancelled' or parcel[10] == 'delivered': return jsonify({"message":f"Present location of parcel {parcel_id} can not be changed because it was cancelled or delivered"}), 400 location_change = db.change_location(parcel_id, present_location) if location_change: if present_location != parcel[7]: db.change_status(parcel_id, 'intransit') elif present_location == parcel[7]: db.change_status(parcel_id, 'delivered') return jsonify({"message":f"Present location of parcel {parcel_id} changed to {present_location}"}), 200 return jsonify({'message':'Invalid request! You do not have rights to change the present location of a parcel'}), 401 return jsonify({'message':f"There is no parcel with ID {parcel_id}"}), 404