From bece495ac8a4064b640574bf0af450eb89deba4d Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Fri, 26 Jan 2024 16:37:39 +0100 Subject: [PATCH] fixes edge cases realted to creating new profil update requests --- application/controllers/Cis/Profil.php | 30 +++------- application/controllers/Cis/ProfilUpdate.php | 58 +++++++++++++------- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/application/controllers/Cis/Profil.php b/application/controllers/Cis/Profil.php index 3b3ccf182..5b0cacfbf 100644 --- a/application/controllers/Cis/Profil.php +++ b/application/controllers/Cis/Profil.php @@ -96,7 +96,7 @@ class Profil extends Auth_Controller $json = json_decode($this->input->raw_input_stream); $payload = $json->payload; - $type = property_exists($json->payload,"kontakt_id")? "kontakt_id" : "adresse_id"; + $identifier = property_exists($json->payload,"kontakt_id")? "kontakt_id" : "adresse_id"; $name = $this->PersonModel->getFullName($this->uid); if(isError($name)){ @@ -111,36 +111,28 @@ class Profil extends Auth_Controller $res = hasData($res) ? getData($res) : null; if($res){ - foreach($res as $update_request){ + $pending_changes = array_filter($res, function($element) { + return $element->status == 'pending'; + }); + foreach($pending_changes as $update_request){ $existing_change = json_decode($update_request->requested_change); - - if(!isset($existing_change->add) && property_exists($existing_change,$type) && property_exists($payload,$type) && $existing_change->$type == $payload->$type){ + //? the user can add as many new kontakt/adresse as he likes + if( !isset($payload->add) && property_exists($existing_change,$identifier) && property_exists($payload,$identifier) && $existing_change->$identifier == $payload->$identifier){ //? the kontakt_id / adresse_id of a change has to be unique - echo json_encode(error("cannot change the same resource twice")); return; } - - if(isset($payload->add)){ - //TODO: add functionality for adding a new kontakt or address - - }elseif($update_request->topic == $json->topic ){ - + elseif(!isset($payload->add) && !isset($payload->delete) && $update_request->topic == $json->topic ){ //? if it is not a delete or add request than the topic in combination with the uid of the user have to be unique echo json_encode(error("A request to change " . $json->topic . " is already open")); return; } }} - - - - $insert_res = $this->ProfilChangeModel->insert($data); - - + if(isError($insert_res)){ //catch error }else{ @@ -149,10 +141,6 @@ class Profil extends Auth_Controller $insert_res->retval = date_create($editTimestamp)->format('d.m.Y'); echo json_encode($insert_res); } - - - - } public function updateProfilRequest() diff --git a/application/controllers/Cis/ProfilUpdate.php b/application/controllers/Cis/ProfilUpdate.php index 66a38cd17..24db74ddd 100644 --- a/application/controllers/Cis/ProfilUpdate.php +++ b/application/controllers/Cis/ProfilUpdate.php @@ -58,14 +58,29 @@ class ProfilUpdate extends Auth_Controller } if(is_array($requested_change) && array_key_exists("adresse_id",$requested_change)){ - $resID = $this->handleAdresse($requested_change, $personID); - $resID = hasData($resID) ? getData($resID) : null; - $requested_change['adresse_id'] = $resID; - + $insertID = $this->handleAdresse($requested_change, $personID); + $insertID = hasData($insertID) ? getData($insertID) : null; + if(isset($insertID)) { + $requested_change['kontakt_id'] = $insertID; + $update_res = $this->updateRequestedChange($id,$requested_change); + if(isError($update_res)){ + echo json_encode(error("was not able to add addresse_id " . $insertID . " to profilRequest " . $id )); + return; + } + } + }else if (is_array($requested_change) && array_key_exists("kontakt_id", $requested_change)){ - $resID = $this->handleKontakt($requested_change, $personID); - $resID = hasData($resID) ? getData($resID)[0] : null; - $requested_change['kontakt_id'] = $resID; + $insertID = $this->handleKontakt($requested_change, $personID); + $insertID = hasData($insertID) ? getData($insertID) : null; + if(isset($insertID)) { + $requested_change['kontakt_id'] = $insertID; + $update_res = $this->updateRequestedChange($id,$requested_change); + if(isError($update_res)){ + echo json_encode(error("was not able to add kontakt_id " . $insertID . " to profilRequest " . $id )); + return; + } + } + }else{ switch($topic){ @@ -74,7 +89,7 @@ class ProfilUpdate extends Auth_Controller } $result = $this->PersonModel->update($personID,[$topic=>$requested_change]); if(isError($result)){ - echo json_encode($result); + echo json_encode(error("was not able to update Person Information: " . $topic . " with value : " . $requested_change)); return; } } @@ -91,10 +106,12 @@ class ProfilUpdate extends Auth_Controller echo json_encode($this->setStatusOnUpdateRequest($id, "rejected", $status_message)); } - private function setStatusOnUpdateRequest($id, $status, $status_message, $requested_change=NULL){ - $update = ["status"=>$status,"status_timestamp"=>"NOW()","status_message"=>$status_message]; - if(isset($requested_change)) { $update['requested_change'] = $requested_change; } - return $this->ProfilChangeModel->update([$id], $update); + private function updateRequestedChange($id, $requested_change){ + return $this->ProfilChangeModel->update([$id], ['requested_change'=>json_encode($requested_change)]); + } + + private function setStatusOnUpdateRequest($id, $status, $status_message ){ + return $this->ProfilChangeModel->update([$id], ["status"=>$status,"status_timestamp"=>"NOW()","status_message"=>$status_message]); } @@ -110,17 +127,18 @@ class ProfilUpdate extends Auth_Controller unset($requested_change['add']); //? fields like insertvon are not filled when inserting new row $requested_change['person_id'] = $personID; - $res = $this->KontaktModel->insert($requested_change); + $insertID = $this->KontaktModel->insert($requested_change); + } //! DELETE elseif(array_key_exists('delete',$requested_change) && $requested_change['delete']){ - $res = $this->KontaktModel->delete($kontakt_id); + $this->KontaktModel->delete($kontakt_id); } //! UPDATE else{ - $res = $this->KontaktModel->update($kontakt_id,$requested_change); + $this->KontaktModel->update($kontakt_id,$requested_change); } - return $res; + return isset($insertID) ? $insertID : null; } private function handleAdresse($requested_change, $personID){ @@ -143,16 +161,16 @@ class ProfilUpdate extends Auth_Controller //? fields like insertvon are not filled when inserting new row $requested_change['person_id'] = $personID; //TODO: zustelladresse, heimatadresse, rechnungsadresse und nation werden nicht beachtet - $res = $this->AdresseModel->insert($requested_change); + $insertID = $this->AdresseModel->insert($requested_change); } //! DELETE elseif(array_key_exists('delete',$requested_change) && $requested_change['delete']){ - $res = $this->AdresseModel->delete($adresse_id); + $this->AdresseModel->delete($adresse_id); } //! UPDATE else{ - $res = $this->AdresseModel->update($adresse_id,$requested_change); + $this->AdresseModel->update($adresse_id,$requested_change); } - return $res; + return isset($insertID)? $insertID : null; } } \ No newline at end of file