diff --git a/application/controllers/Cis/Profil.php b/application/controllers/Cis/Profil.php index 06c829763..c20fb9907 100755 --- a/application/controllers/Cis/Profil.php +++ b/application/controllers/Cis/Profil.php @@ -20,13 +20,7 @@ class Profil extends Auth_Controller 'foto_sperre_function' => ['student/anrechnung_beantragen:r', 'user:r'], 'getView' => ['student/anrechnung_beantragen:r', 'user:r'], 'View' => ['student/anrechnung_beantragen:r', 'user:r'], - 'insertProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], - 'updateProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], - 'deleteProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], - 'selectProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], - 'insertFile' => ['student/anrechnung_beantragen:r', 'user:r'], - 'getProfilRequestFiles' => ['student/anrechnung_beantragen:r', 'user:r'], - 'deleteOldVersionFiles' => ['student/anrechnung_beantragen:r', 'user:r'], + ]); @@ -44,11 +38,9 @@ class Profil extends Auth_Controller $this->load->model('person/Kontakt_model', 'KontaktModel'); $this->load->model('person/Profil_update_model', 'ProfilUpdateModel'); $this->load->model('content/DmsVersion_model', 'DmsVersionModel'); - $this->load->model('DmsVersion_model','DmsVersionModel'); - $this->load->library('DmsLib'); - //? put the uid and pid inside the controller to reuse in controller + //? put the uid and pid inside the controller for reusability $this->uid = getAuthUID(); $this->pid = getAuthPersonID(); @@ -79,178 +71,6 @@ class Profil extends Auth_Controller } - public function insertFile(){ - - //? Version des Dokuments - /*$this->DmsVersionModel->addSelect(["version"]); - $fileVersion = $this->DmsVersionModel->loadWhere(["name"=>$_FILES['files']['name'], "mimetype"=>$_FILES['files']['type']]); - $fileVersion = hasData($fileVersion) ? getData($fileVersion)[0]->version : 0; - if($fileVersion) $fileVersion++; */ - if(!count($_FILES)){ - echo json_encode([]); - return; - } - - - - - $files = $_FILES['files']; - $file_count = count($files['name']); - - $res=[]; - - for ($i = 0; $i < $file_count; $i++) { - $_FILES['files']['name'] = $files['name'][$i]; - $_FILES['files']['type'] = $files['type'][$i]; - $_FILES['files']['tmp_name'] = $files['tmp_name'][$i]; - $_FILES['files']['error'] = $files['error'][$i]; - $_FILES['files']['size'] = $files['size'][$i]; - - $dms = [ - "kategorie_kurzbz"=>"profil_aenderung", - "version"=>0, - "name"=>$_FILES['files']['name'], - "mimetype"=>$_FILES['files']['type'], - "beschreibung"=>$this->uid . " Profil Änderung", - "insertvon"=>$this->uid, - "insertamum"=>"NOW()", - ]; - $tmp_res=$this->dmslib->upload($dms , 'files'); - $tmp_res = hasData($tmp_res)? getData($tmp_res) : null; - array_push($res,$tmp_res); - } - - echo json_encode($res); - } - - - public function deleteOldVersionFiles(){ - $file_array = json_decode($this->input->raw_input_stream); - if(!isset($file_array) || !sizeof($file_array)){ - return; - } - - $res =[]; - foreach($file_array as $id){ - //? delete all the different versions of the dms_file - - $zwischen_res = $this->DmsVersionModel->loadWhere(["dms_id"=>$id]); - $zwischen_res = hasData($zwischen_res) ? getData($zwischen_res) : null; - if(isset($zwischen_res)){ - $zwischen_res = array_map(function($item){ return $item->version;},$zwischen_res); - foreach($zwischen_res as $version){ - array_push($res, $this->DmsVersionModel->delete([$id,$version])); - } - }else{ - echo json_encode(error("No version of the file has been found")); - } - } - echo json_encode($res); - } - - - - - - public function selectProfilRequest(){ - $_GET = json_decode($this->input->raw_input_stream, true); - $uid = $this->input->get('uid'); - $id = $this->input->get('id'); - $whereClause=['uid'=> $this->uid]; - - if(isset($uid)) $whereClause['uid'] = $uid; - if(isset($id)) $whereClause['id'] = $id; - - $res= $this->ProfilUpdateModel->getProfilUpdatesWhere($whereClause); - - echo json_encode($res); - - } - - - public function getProfilRequestFiles(){ - $id = json_decode($this->input->raw_input_stream); - echo json_encode($this->ProfilUpdateModel->getFilesFromChangeRequest($id)); - } - - public function insertProfilRequest() - { - - $json = json_decode($this->input->raw_input_stream); - $payload = $json->payload; - - $identifier = property_exists($json->payload,"kontakt_id")? "kontakt_id" : (property_exists($json->payload,"adresse_id")? "adresse_id" : null); - - $name = $this->PersonModel->getFullName($this->uid); - if(isError($name)){ - // error handling - var_dump($name); - return; - } - $data = ["topic"=>$json->topic,"uid" => $this->uid, "name"=>getData($name), "requested_change" => json_encode($payload), "insertamum" => "NOW()", "insertvon"=>$this->uid,"status"=>"pending" ]; - - //? loops over all updateRequests from a user to validate if the new request is valid - $res = $this->ProfilUpdateModel->getProfilUpdatesWhere(["uid"=>$this->uid]); - $res = hasData($res) ? getData($res) : null; - - if($res){ - $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); - - //? 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; - } - - elseif(!$identifier && $update_request->topic == $json->topic ){ - //? if it is not a delete or add request than the topic has to be unique - echo json_encode(error("A request to change " . $json->topic . " is already open")); - return; - } - }} - - $insertID = $this->ProfilUpdateModel->insert($data); - - if(isError($insertID)){ - //catch error - }else{ - $insertID = hasData($insertID)? getData($insertID): null; - $editTimestamp = $this->ProfilUpdateModel->getTimestamp($insertID); - - $date = success(date_create($editTimestamp)->format('d.m.Y')); - echo json_encode($date); - } - } - - public function updateProfilRequest() - { - - $json = json_decode($this->input->raw_input_stream); - - - $updateID =$this->ProfilUpdateModel->update([$json->ID],["requested_change" => json_encode($json->payload), "updateamum" => "NOW()", "updatevon" => $this->uid]); - if(isError($updateID)){ - //catch error - }else{ - $updateID = hasData($updateID)? getData($updateID)[0]: null; - $editTimestamp = $this->ProfilUpdateModel->getTimestamp($updateID,true); - - $date = success(date_create($editTimestamp)->format('d.m.Y')); - echo json_encode($date); - } - } - - public function deleteProfilRequest(){ - - $json = json_decode($this->input->raw_input_stream); - $delete_res = $this->ProfilUpdateModel->delete([$json]); - echo json_encode($delete_res); - } @@ -758,7 +578,7 @@ class Profil extends Auth_Controller if ( - isSuccess($adresse_res = $this->AdresseModel->addSelect(["adresse_id","strasse", "tbl_adressentyp.bezeichnung as adr_typ", "plz", "ort","zustelladresse"])) && + isSuccess($adresse_res = $this->AdresseModel->addSelect(["adresse_id","strasse", "tbl_adressentyp.bezeichnung as typ", "plz", "ort","zustelladresse"])) && isSuccess($adresse_res = $this->AdresseModel->addOrder("zustelladresse", "DESC")) && isSuccess($adresse_res = $this->AdresseModel->addJoin("tbl_adressentyp", "typ=adressentyp_kurzbz")) ) { diff --git a/application/controllers/Cis/ProfilUpdate.php b/application/controllers/Cis/ProfilUpdate.php index 7e39d6310..9672892fa 100755 --- a/application/controllers/Cis/ProfilUpdate.php +++ b/application/controllers/Cis/ProfilUpdate.php @@ -16,8 +16,15 @@ class ProfilUpdate extends Auth_Controller 'acceptProfilRequest'=>['student/stammdaten:rw','mitarbeiter/stammdaten:rw'], 'denyProfilRequest'=>['student/stammdaten:rw','mitarbeiter/stammdaten:rw'], 'show'=>['student/stammdaten:r','mitarbeiter/stammdaten:r'], + + 'insertProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], + 'updateProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], + 'deleteProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], + 'selectProfilRequest' => ['student/anrechnung_beantragen:r', 'user:r'], + 'insertFile' => ['student/anrechnung_beantragen:r', 'user:r'], + 'getProfilRequestFiles' => ['student/anrechnung_beantragen:r', 'user:r'], + - ]); @@ -26,6 +33,12 @@ class ProfilUpdate extends Auth_Controller $this->load->model('person/Adresse_model','AdresseModel'); $this->load->model('person/Adressentyp_model', 'AdressenTypModel'); $this->load->model('person/Person_model','PersonModel'); + + $this->load->library('DmsLib'); + + //? put the uid and pid inside the controller for reusability + $this->uid = getAuthUID(); + $this->pid = getAuthPersonID(); } @@ -45,6 +58,221 @@ class ProfilUpdate extends Auth_Controller echo json_encode($res); } + + + + public function insertFile($replace){ + $replace = json_decode($replace); + + if(!count($_FILES)){ + echo json_encode([]); + return; + } + + //? if replace is set it contains the profil_update_id in which the attachment_id has to be replaced + if(isset($replace)){ + $this->ProfilUpdateModel->addSelect(["attachment_id"]); + $attachmentID = $this->ProfilUpdateModel->load([$replace]); + if(isError($attachmentID)){ + return json_encode(error("Error loading ProfilUpdate resource")); + } + //? get the attachmentID + $dms_id = hasData($attachmentID) ? getData($attachmentID)[0]->attachment_id : null; + + //? delete old dms_file of Profil Update + $this->deleteOldVersionFile($dms_id); + } + + + $files = $_FILES['files']; + $file_count = count($files['name']); + + $res=[]; + + for ($i = 0; $i < $file_count; $i++) { + $_FILES['files']['name'] = $files['name'][$i]; + $_FILES['files']['type'] = $files['type'][$i]; + $_FILES['files']['tmp_name'] = $files['tmp_name'][$i]; + $_FILES['files']['error'] = $files['error'][$i]; + $_FILES['files']['size'] = $files['size'][$i]; + + $dms = [ + "kategorie_kurzbz"=>"profil_aenderung", + "version"=>0, + "name"=>$_FILES['files']['name'], + "mimetype"=>$_FILES['files']['type'], + "beschreibung"=>$this->uid . " Profil Änderung", + "insertvon"=>$this->uid, + "insertamum"=>"NOW()", + ]; + + $tmp_res=$this->dmslib->upload($dms , 'files'); + $tmp_res = hasData($tmp_res)? getData($tmp_res) : null; + array_push($res,$tmp_res); + } + + echo json_encode($res); + } + + + + private function deleteOldVersionFile($dms_id){ + if(!isset($dms_id)){ + return; + } + + //? collect all the results of the deleted versions in an array + $res = array(); + + //? delete all the different versions of the dms_file + $dmsVersions = $this->DmsVersionModel->loadWhere(["dms_id"=>$dms_id]); + $dmsVersions = hasData($dmsVersions) ? getData($dmsVersions) : null; + if(isset($dmsVersions)){ + $zwischen_res = array_map(function($item){ return $item->version;},$dmsVersions); + foreach($zwischen_res as $version){ + array_push($res, $this->DmsVersionModel->delete([$dms_id,$version])); + } + }else{ + echo json_encode(error("No version of the file has been found")); + } + + //? returns a result for each deleted dms_file + return $res; + } + + + + + + public function selectProfilRequest(){ + $_GET = json_decode($this->input->raw_input_stream, true); + $uid = $this->input->get('uid'); + $id = $this->input->get('id'); + $whereClause=['uid'=> $this->uid]; + + if(isset($uid)) $whereClause['uid'] = $uid; + if(isset($id)) $whereClause['id'] = $id; + + $res= $this->ProfilUpdateModel->getProfilUpdatesWhere($whereClause); + + echo json_encode($res); + + } + + + public function getProfilRequestFiles(){ + $id = json_decode($this->input->raw_input_stream); + + $this->ProfilUpdateModel->addSelect(["attachment_id"]); + $attachmentID = $this->ProfilUpdateModel->load([$id]); + if(isError($attachmentID)){ + return json_encode(error("Error loading ProfilUpdate resource")); + } + //? get the attachmentID + $dms_id = hasData($attachmentID) ? getData($attachmentID)[0]->attachment_id : null; + + //? get the name to the file + $this->DmsVersionModel->addSelect(["name", "dms_id"]); + $attachment = $this->DmsVersionModel->load([$dms_id,0]); + if(isError($attachment)){ + return json_encode(error("Error loading DmsVersion resource")); + } + $attachment = hasData($attachment) ? getData($attachment) : null; + + //? returns {name:..., dms_id:...} + echo json_encode($attachment); + } + + public function insertProfilRequest() + { + + $json = json_decode($this->input->raw_input_stream); + $payload = $json->payload; + + $identifier = property_exists($json->payload,"kontakt_id")? "kontakt_id" : (property_exists($json->payload,"adresse_id")? "adresse_id" : null); + + $name = $this->PersonModel->getFullName($this->uid); + if(isError($name)){ + // error handling + var_dump($name); + return; + } + $data = ["topic"=>$json->topic,"uid" => $this->uid, "name"=>getData($name), "requested_change" => json_encode($payload), "insertamum" => "NOW()", "insertvon"=>$this->uid,"status"=>"pending" ]; + //? insert fileID in the dataset if sent with post request + if(isset($json->fileID)){ + $data['attachment_id'] = $json->fileID; + + } + + + //? loops over all updateRequests from a user to validate if the new request is valid + $res = $this->ProfilUpdateModel->getProfilUpdatesWhere(["uid"=>$this->uid]); + $res = hasData($res) ? getData($res) : null; + + if($res){ + $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); + + //? 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; + } + + elseif(!$identifier && $update_request->topic == $json->topic ){ + //? if it is not a delete or add request than the topic has to be unique + echo json_encode(error("A request to change " . $json->topic . " is already open")); + return; + } + }} + + $insertID = $this->ProfilUpdateModel->insert($data); + + if(isError($insertID)){ + //catch error + }else{ + $insertID = hasData($insertID)? getData($insertID): null; + $editTimestamp = $this->ProfilUpdateModel->getTimestamp($insertID); + + $date = success(date_create($editTimestamp)->format('d.m.Y')); + echo json_encode($date); + } + } + + public function updateProfilRequest() + { + $json = json_decode($this->input->raw_input_stream); + + $updateData = ["requested_change" => json_encode($json->payload), "updateamum" => "NOW()", "updatevon" => $this->uid]; + if(isset($json->fileID)){ + $updateData['attachment_id'] = json_decode($json->fileID); + } + $updateID =$this->ProfilUpdateModel->update([$json->ID],$updateData); + //? insert fileID in the dataset if sent with post request + + if(isError($updateID)){ + //catch error + }else{ + $updateID = hasData($updateID)? getData($updateID)[0]: null; + $editTimestamp = $this->ProfilUpdateModel->getTimestamp($updateID,true); + + $date = success(date_create($editTimestamp)->format('d.m.Y')); + echo json_encode($date); + } + } + + public function deleteProfilRequest(){ + + $json = json_decode($this->input->raw_input_stream); + $delete_res = $this->ProfilUpdateModel->delete([$json]); + echo json_encode($delete_res); + } + + public function getProfilUpdateWithPermission($status=null){ @@ -125,11 +353,11 @@ class ProfilUpdate extends Auth_Controller } private function updateRequestedChange($id, $requested_change){ - return $this->ProfilUpdateModel->update([$id], ['requested_change'=>json_encode($requested_change)]); + return $this->update([$id], ['requested_change'=>json_encode($requested_change)]); } private function setStatusOnUpdateRequest($id, $status, $status_message ){ - return $this->ProfilUpdateModel->update([$id], ["status"=>$status,"status_timestamp"=>"NOW()","status_message"=>$status_message]); + return $this->update([$id], ["status"=>$status,"status_timestamp"=>"NOW()","status_message"=>$status_message]); } diff --git a/application/models/person/Profil_update_model.php b/application/models/person/Profil_update_model.php index d316feab0..5bdbc3c32 100755 --- a/application/models/person/Profil_update_model.php +++ b/application/models/person/Profil_update_model.php @@ -49,6 +49,7 @@ class Profil_update_model extends DB_Model } + //? queries the tbl_profil_updates without permissions of the user public function getProfilUpdatesWhere($whereClause){ $res = $this->loadWhere($whereClause); if(isError($res)){ diff --git a/public/js/apps/api/profilUpdate.js b/public/js/apps/api/profilUpdate.js index 7f0bdc63e..93311fa00 100755 --- a/public/js/apps/api/profilUpdate.js +++ b/public/js/apps/api/profilUpdate.js @@ -15,6 +15,82 @@ export default { denyProfilRequest: function(payload){ const url = FHC_JS_DATA_STORAGE_OBJECT.app_root + FHC_JS_DATA_STORAGE_OBJECT.ci_router + '/Cis/ProfilUpdate/denyProfilRequest'; return axios.post(url,payload); - } + }, + + replaceProfilUpdateAttachment: function (dms) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/replaceProfilUpdateAttachment`; + + return axios.post(url, dms, { + headers: { "Content-Type": "multipart/form-data" }, + }); + }, + + + //? new reuquests + insertFile: function (dms,replace=null) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/insertFile/${replace}`; + + return axios.post(url, dms, { + headers: { "Content-Type": "multipart/form-data" }, + }); + }, + + + + getProfilRequestFiles: function (requestID) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/getProfilRequestFiles`; + + return axios.post(url, requestID); + }, + + selectProfilRequest: function (uid = null, id = null) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/selectProfilRequest`; + + return axios.get(url, { uid: uid, id: id }); + }, + + + + + insertProfilRequest: function (topic, payload, fileID=null) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/insertProfilRequest`; + + return axios.post(url, { topic, payload, ...(fileID?{fileID:fileID}:{}) }); + }, + + updateProfilRequest: function (topic, payload, ID, fileID=null) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/updateProfilRequest`; + + return axios.post(url, { topic, payload, ID, ...(fileID?{fileID:fileID}:{}) }); + }, + + deleteProfilRequest: function (requestID) { + const url = + FHC_JS_DATA_STORAGE_OBJECT.app_root + + FHC_JS_DATA_STORAGE_OBJECT.ci_router + + `/Cis/ProfilUpdate/deleteProfilRequest`; + + return axios.post(url, requestID); + }, + + } \ No newline at end of file diff --git a/public/js/apps/api/userdata.js b/public/js/apps/api/userdata.js index cc8fb5e58..9e75b6f3a 100755 --- a/public/js/apps/api/userdata.js +++ b/public/js/apps/api/userdata.js @@ -1,78 +1,7 @@ export default { //! API Calls for Profil Views - insertFile: function (dms) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/insertFile`; - return axios.post(url, dms, { - headers: { "Content-Type": "multipart/form-data" }, - }); - }, - - deleteOldVersionFiles: function (files) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/deleteOldVersionFiles`; - - return axios.post(url, files); - }, - - getProfilRequestFiles: function (requestID) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/getProfilRequestFiles`; - - return axios.post(url, requestID); - }, - - selectProfilRequest: function (uid = null, id = null) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/selectProfilRequest`; - - return axios.get(url, { uid: uid, id: id }); - }, - - insertProfilRequest: function (topic, payload) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/insertProfilRequest`; - - return axios.post(url, { topic, payload }); - }, - - updateProfilRequest: function (topic, payload, ID) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/updateProfilRequest`; - - return axios.post(url, { topic, payload, ID }); - }, - - deleteProfilRequest: function (requestID) { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/deleteProfilRequest`; - - return axios.post(url, requestID); - }, - - getEditProfil: function () { - const url = - FHC_JS_DATA_STORAGE_OBJECT.app_root + - FHC_JS_DATA_STORAGE_OBJECT.ci_router + - `/Cis/Profil/getEditProfil`; - return axios.get(url); - }, isMitarbeiterOrStudent: function (uid) { const url = diff --git a/public/js/components/Cis/Profil/EditProfil.js b/public/js/components/Cis/Profil/EditProfil.js index 6ca410214..8a83b32ba 100755 --- a/public/js/components/Cis/Profil/EditProfil.js +++ b/public/js/components/Cis/Profil/EditProfil.js @@ -24,16 +24,13 @@ export default { onShowBsModal: Function, onShownBsModal: Function, }, -/* provide(){ - return { - topic: computed(()=> "test"), - } - }, */ + data() { return { topic: null, profilUpdate: null, editData: this.value, + fileID:null, breadcrumb: null, result: false, @@ -41,17 +38,29 @@ export default { }; }, + provide(){ + return { + updateFileID: this.updateFileIDFunction, + } + }, + methods: { + + updateFileIDFunction: function(newFileID){ + this.fileID = newFileID; + }, + async submitProfilChange() { //? check if data is valid before making a request if (this.topic && this.profilUpdate) { - if (this.profilUpdate.files) { - const fileIDs = await this.uploadFiles(this.profilUpdate.files); - - if (fileIDs) { - this.profilUpdate.files = fileIDs; - } + + //? if profil update contains any attachment + if (this.fileID) { + const fileData = await this.uploadFiles(this.fileID); + + this.fileID = fileData? fileData : null; } + //? inserts new row in public.tbl_cis_profil_update //* calls the update api call if an update field is present in the data that was passed to the modal const handleApiResponse = (res) => { @@ -71,16 +80,19 @@ export default { }; this.editData.updateID - ? Vue.$fhcapi.UserData.updateProfilRequest( + ? Vue.$fhcapi.ProfilUpdate.updateProfilRequest( this.topic, this.profilUpdate, - this.editData.updateID + this.editData.updateID, + this.fileID[0] ).then((res) => { handleApiResponse(res); }) - : Vue.$fhcapi.UserData.insertProfilRequest( + : Vue.$fhcapi.ProfilUpdate.insertProfilRequest( this.topic, - this.profilUpdate + this.profilUpdate, + this.fileID[0] + ).then((res) => { handleApiResponse(res); }); @@ -91,55 +103,31 @@ export default { let updatedFiles = []; if (this.editData.updateID) { - //? if we are updating an already existing profilRequest - const existingFiles = await Vue.$fhcapi.UserData.getProfilRequestFiles( - this.editData.updateID - ).then((res) => { - return res.data; - }); - - let filesToKeep = []; - let filesToDelete = []; - existingFiles.forEach((file) => { - Array.from(files).some((f) => f.name === file.name) - ? filesToKeep.push(file) - : filesToDelete.push(file.dms_id); - }); - //? only keeps the newest version of the documents and deletes the old versions in the database - if(filesToDelete.length > 0) { - Vue.$fhcapi.UserData.deleteOldVersionFiles( - filesToDelete - ); - } - - - updatedFiles = [...filesToKeep]; } - let formData = new FormData(); - for (let i = 0; i < files.length; i++) { - if (files[i].type !== "application/x.fhc-dms+json") - formData.append("files[]", files[i]); - } - if(Array.from(formData).length > 0){ - await Vue.$fhcapi.UserData.insertFile(formData) - .then((res) => { - /* returns file information as - [{"name":"example.png", "dms_id":282531}] */ - - updatedFiles = updatedFiles.concat( - res.data?.map((file) => { - return { dms_id: file.dms_id, name: file.client_name}; - }) - ); + + + if (files[0].type !== "application/x.fhc-dms+json"){ + let formData = new FormData(); + formData.append("files[]", files[0]); + const result = this.editData.updateID ? + //? updating old attachment by replacing + //* second parameter of api request insertFile checks if the file has to be replaced or not + await Vue.$fhcapi.ProfilUpdate.insertFile(formData,this.editData.updateID).then(res => { + return res.data?.map((file) => file.dms_id); }) - .catch((err) => { - console.log(err); - }); + : + //? fresh insert of new attachment + await Vue.$fhcapi.ProfilUpdate.insertFile(formData).then(res => { + return res.data?.map((file) => file.dms_id); + }) + return result; + }else{ + //? attachment hasn't been replaced + return false; } - return updatedFiles; }, }, computed: {}, @@ -163,7 +151,6 @@ export default {