Do not accidentally overwrite shipcall fields when fields are not passed on PUT request

This commit is contained in:
Daniel Schick 2025-11-16 19:26:15 +01:00
parent 44dd6010d7
commit ae2ce859ad
2 changed files with 22 additions and 16 deletions

View File

@ -89,7 +89,7 @@ def PutShipcalls():
# validate the PUT shipcall data and the user's authority # validate the PUT shipcall data and the user's authority
InputValidationShipcall.evaluate_put_data(user_data, loadedModel, content) InputValidationShipcall.evaluate_put_data(user_data, loadedModel, content)
return impl.shipcalls.PutShipcalls(loadedModel) return impl.shipcalls.PutShipcalls(loadedModel, content)
except ValidationError as ex: except ValidationError as ex:
return create_validation_error_response(ex=ex, status_code=400) return create_validation_error_response(ex=ex, status_code=400)

View File

@ -185,7 +185,7 @@ def PostShipcalls(schemaModel):
pooledConnection.close() pooledConnection.close()
def PutShipcalls(schemaModel): def PutShipcalls(schemaModel, original_payload=None):
""" """
:param schemaModel: The deserialized dict of the request :param schemaModel: The deserialized dict of the request
@ -209,10 +209,12 @@ def PutShipcalls(schemaModel):
was_canceled = theshipcall["canceled"] was_canceled = theshipcall["canceled"]
# query = SQLQuery.get_shipcall_put(schemaModel) provided_keys = set(original_payload.keys()) if isinstance(original_payload, dict) else None
query = "UPDATE shipcall SET "
isNotFirst = False update_clauses = []
for key in schemaModel.keys(): for key in schemaModel.keys():
if provided_keys is not None and key not in provided_keys:
continue
param_key = key param_key = key
if key == "id": if key == "id":
continue continue
@ -234,19 +236,19 @@ def PutShipcalls(schemaModel):
param_key = "evaluation_value" param_key = "evaluation_value"
if key == "evaluation_value": if key == "evaluation_value":
continue continue
if isNotFirst: update_clauses.append(f"{key} = ?{param_key}?")
query += ", "
isNotFirst = True
query += key + " = ?" + param_key + "? "
query += "WHERE id = ?id?" if update_clauses:
query = "UPDATE shipcall SET " + ", ".join(update_clauses) + " WHERE id = ?id?"
commands.execute(query, param=schemaModel)
affected_rows = commands.execute(query, param=schemaModel) ship_id_value = schemaModel.get("ship_id") if (provided_keys is None or "ship_id" in provided_keys) else theshipcall["ship_id"]
shipdata = get_ship_data_for_id(schemaModel["ship_id"]) shipdata = get_ship_data_for_id(ship_id_value)
message = shipdata['name'] message = shipdata['name']
if "type_value" in schemaModel: type_value = schemaModel.get("type_value") if (provided_keys is None or "type" in provided_keys) else theshipcall["type"]
match schemaModel["type_value"]: if type_value is not None:
match type_value:
case 1: case 1:
message += " [ARRIVAL]" message += " [ARRIVAL]"
case 2: case 2:
@ -258,6 +260,9 @@ def PutShipcalls(schemaModel):
pquery = "SELECT id, participant_id, type FROM shipcall_participant_map where shipcall_id = ?id?" pquery = "SELECT id, participant_id, type FROM shipcall_participant_map where shipcall_id = ?id?"
pdata = commands.query(pquery,param={"id" : schemaModel["id"]}) # existing list of assignments pdata = commands.query(pquery,param={"id" : schemaModel["id"]}) # existing list of assignments
if schemaModel.get("participants") is None:
schemaModel["participants"] = []
# loop across passed participant ids, creating entries for those not present in pdata # loop across passed participant ids, creating entries for those not present in pdata
existing_notifications = get_notification_for_shipcall_and_type(schemaModel["id"], 1) # type = 1 is assignment existing_notifications = get_notification_for_shipcall_and_type(schemaModel["id"], 1) # type = 1 is assignment
@ -306,8 +311,9 @@ def PutShipcalls(schemaModel):
commands.execute(nquery, param={"shipcall_id" : schemaModel["id"], "participant_id" : elem["participant_id"], "message" : message}) commands.execute(nquery, param={"shipcall_id" : schemaModel["id"], "participant_id" : elem["participant_id"], "message" : message})
break break
if schemaModel["canceled"] is not None: canceled_value = schemaModel.get("canceled")
if schemaModel["canceled"] and not was_canceled: if canceled_value is not None:
if canceled_value and not was_canceled:
# create a canceled notification for all currently assigned participants # create a canceled notification for all currently assigned participants
stornoNotificationQuery = "INSERT INTO notification (shipcall_id, participant_id, level, type, message) VALUES (?shipcall_id?, ?participant_id?, 0, 7, ?message?)" stornoNotificationQuery = "INSERT INTO notification (shipcall_id, participant_id, level, type, message) VALUES (?shipcall_id?, ?participant_id?, 0, 7, ?message?)"
for participant_assignment in schemaModel["participants"]: for participant_assignment in schemaModel["participants"]: