Major Refactor

This commit is contained in:
2025-09-01 16:42:59 -04:00
parent 9a2f9a6564
commit d250e136c3
5 changed files with 310 additions and 431 deletions

View File

@@ -547,71 +547,57 @@ def customer_automatic_assignment(customer_id):
"ok": True,
'status': status
}), 200
@customer.route("/edit/tank/<int:customer_id>", methods=["PUT"])
@login_required
def edit_customer_tank(customer_id):
"""
Safely edits or creates tank and description details for a customer.
"""
get_customer = (db.session
.query(Customer_Customer)
.filter(Customer_Customer.id == customer_id)
.first())
get_customer_description = (db.session
.query(Customer_Description)
.filter(Customer_Description.customer_id == customer_id)
.first())
get_customer_tank = (db.session
.query(Customer_Tank_Inspection)
.filter(Customer_Tank_Inspection.customer_id == customer_id)
.first())
get_customer = db.session.query(Customer_Customer).filter(Customer_Customer.id == customer_id).one_or_none()
if not get_customer:
return jsonify({"ok": False, "error": "Customer not found"}), 404
response_tank_status = request.json["tank_status"]
get_customer_description = db.session.query(Customer_Description).filter(Customer_Description.customer_id == customer_id).first()
if not get_customer_description:
get_customer_description = Customer_Description(customer_id=customer_id)
db.session.add(get_customer_description)
if response_tank_status == 'true':
get_customer_tank.tank_status = True
get_customer_tank = db.session.query(Customer_Tank_Inspection).filter(Customer_Tank_Inspection.customer_id == customer_id).first()
if not get_customer_tank:
get_customer_tank = Customer_Tank_Inspection(customer_id=customer_id)
db.session.add(get_customer_tank)
elif response_tank_status == 'false':
get_customer_tank.outside_or_inside = False
else:
pass
data = request.get_json()
if 'tank_status' in data:
get_customer_tank.tank_status = data["tank_status"]
if 'outside_or_inside' in data:
get_customer_tank.outside_or_inside = data["outside_or_inside"]
response_outside_or_inside = request.json["outside_or_inside"]
if response_outside_or_inside == 'true':
get_customer_tank.outside_or_inside = True
elif response_outside_or_inside == 'false':
get_customer_tank.outside_or_inside = False
else:
pass
response_last_tank_inspection = request.json["last_tank_inspection"]
response_tank_size = request.json["tank_size"]
response_customer_fill_location = request.json["fill_location"]
response_last_tank_inspection = data.get("last_tank_inspection", None)
response_tank_size = data.get("tank_size", 0)
# --- FIX APPLIED HERE ---
# 1. Get the value from the request. Default to 0 if it's missing.
response_customer_fill_location = data.get("fill_location", 0)
# 2. Add a safety check: if the frontend sent an empty string, convert it to 0.
if response_customer_fill_location == "":
response_customer_fill_location = 0
get_customer_tank.last_tank_inspection = response_last_tank_inspection
get_customer_tank.tank_size = response_tank_size
get_customer_description.fill_location = response_customer_fill_location
if get_customer.customer_automatic == 1:
get_auto_info = (db.session
.query(Auto_Delivery)
.filter(Auto_Delivery.customer_id == customer_id)
.first())
get_auto_info.tank_size = response_tank_size
db.session.add(get_auto_info)
get_auto_info = db.session.query(Auto_Delivery).filter(Auto_Delivery.customer_id == customer_id).first()
if get_auto_info:
get_auto_info.tank_size = response_tank_size
db.session.add(get_auto_info)
db.session.add(get_customer_description)
db.session.add(get_customer)
db.session.commit()
return jsonify({
"ok": True,
}), 200
return jsonify({"ok": True}), 200