From 8acbd08293948a8521c2f38ffaca74bd6b46623b Mon Sep 17 00:00:00 2001 From: Matteo Gheza Date: Fri, 1 Apr 2022 00:15:05 +0200 Subject: [PATCH] Service update --- backend/apiRouter.php | 10 +++++++++ backend/utils.php | 22 ++++++++++++++++++- .../modal-alert/modal-alert.component.ts | 4 ++-- .../app/_components/table/table.component.ts | 4 ++-- .../edit-service/edit-service.component.ts | 10 ++++++--- frontend/src/assets/i18n/en.json | 8 ++++--- frontend/src/assets/i18n/it.json | 8 ++++--- 7 files changed, 52 insertions(+), 14 deletions(-) diff --git a/backend/apiRouter.php b/backend/apiRouter.php index 8ee0a48..e2d321a 100644 --- a/backend/apiRouter.php +++ b/backend/apiRouter.php @@ -164,6 +164,16 @@ function apiRouter (FastRoute\RouteCollector $r) { apiResponse($services->get($vars['id'])); } ); + $r->addRoute( + ['POST'], + '/services/{id}', + function ($vars) { + global $services, $users; + requireLogin(); + $users->online_time_update(); + apiResponse($services->update($vars['id'], $_POST["start"], $_POST["end"], $_POST["code"], $_POST["chief"], $_POST["drivers"], $_POST["crew"], $_POST["place"], $_POST["notes"], $_POST["type"], $users->auth->getUserId())); + } + ); $r->addRoute( ['DELETE'], '/services/{id}', diff --git a/backend/utils.php b/backend/utils.php index 2af6c79..465ed78 100644 --- a/backend/utils.php +++ b/backend/utils.php @@ -459,6 +459,26 @@ class Services { return $serviceId; } + public function update($id, $start, $end, $code, $chief, $drivers, $crew, $place, $notes, $type, $inserted_by) + { + $service = $this->db->selectRow( + "SELECT `chief`, `drivers`, `crew` FROM `".DB_PREFIX."_services` WHERE `id` = :id", + ["id" => $id] + ); + $this->decrement_counter($service["chief"].";".$service["drivers"].";".$service["crew"]); + + $this->db->update( + DB_PREFIX."_services", + ["start" => $start, "end" => $end, "code" => $code, "chief" => $chief, "drivers" => $drivers, "crew" => $crew, "place" => $place, "place_reverse" => $this->places->save_place_reverse(explode(";", $place)[0], explode(";", $place)[1]), "notes" => $notes, "type" => $type, "inserted_by" => $inserted_by], + ["id" => $id] + ); + + $this->increment_counter($chief.",".$drivers.",".$crew); + logger("log_messages.service_updated"); + + return $id; + } + public function delete($id) { $service = $this->db->selectRow( @@ -471,7 +491,7 @@ class Services { DB_PREFIX."_services", ["id" => $id] ); - logger("log_messages.service_deleted"); + logger("log_messages.service_removed"); return true; } diff --git a/frontend/src/app/_components/modal-alert/modal-alert.component.ts b/frontend/src/app/_components/modal-alert/modal-alert.component.ts index ba3485f..5f04f39 100644 --- a/frontend/src/app/_components/modal-alert/modal-alert.component.ts +++ b/frontend/src/app/_components/modal-alert/modal-alert.component.ts @@ -86,12 +86,12 @@ export class ModalAlertComponent implements OnInit, OnDestroy { this.bsModalRef.hide(); this.api.alertsChanged.next(); /* - this.translate.get('table.service_deleted_successfully').subscribe((res: string) => { + this.translate.get('table.service_removed_successfully').subscribe((res: string) => { this.toastr.success(res); }); this.loadTableData(); }).catch((e) => { - this.translate.get('table.service_deleted_error').subscribe((res: string) => { + this.translate.get('table.service_removed_error').subscribe((res: string) => { this.toastr.error(res); */ }); diff --git a/frontend/src/app/_components/table/table.component.ts b/frontend/src/app/_components/table/table.component.ts index 391e855..198e9f1 100644 --- a/frontend/src/app/_components/table/table.component.ts +++ b/frontend/src/app/_components/table/table.component.ts @@ -106,12 +106,12 @@ export class TableComponent implements OnInit, OnDestroy { }).then((result) => { if (result.isConfirmed) { this.api.delete(`services/${id}`).then((response) => { - this.translate.get('table.service_deleted_successfully').subscribe((res: string) => { + this.translate.get('table.service_removed_successfully').subscribe((res: string) => { this.toastr.success(res); }); this.loadTableData(); }).catch((e) => { - this.translate.get('table.service_deleted_error').subscribe((res: string) => { + this.translate.get('table.service_removed_error').subscribe((res: string) => { this.toastr.error(res); }); }); diff --git a/frontend/src/app/_routes/edit-service/edit-service.component.ts b/frontend/src/app/_routes/edit-service/edit-service.component.ts index a553b89..d986253 100644 --- a/frontend/src/app/_routes/edit-service/edit-service.component.ts +++ b/frontend/src/app/_routes/edit-service/edit-service.component.ts @@ -177,15 +177,19 @@ export class EditServiceComponent implements OnInit { values.drivers = values.drivers.join(";"); values.crew = values.crew.join(";"); console.log(values); - this.api.post("services", values).then((res) => { + let submit_url = "services"; + if(!this.addingService) { + submit_url = `services/${this.serviceId}`; + } + this.api.post(submit_url, values).then((res) => { console.log(res); - this.translate.get('edit_service.service_added_successfully').subscribe((res: string) => { + this.translate.get(this.addingService ? 'edit_service.service_added_successfully' : 'edit_service.service_updated_successfully').subscribe((res: string) => { this.toastr.success(res); }); this.submittingForm = false; }).catch((err) => { console.error(err); - this.translate.get('edit_service.service_add_failed').subscribe((res: string) => { + this.translate.get(this.addingService ? 'edit_service.service_add_failed' : 'edit_service.service_update_failed').subscribe((res: string) => { this.toastr.error(res); }); this.submittingForm = false; diff --git a/frontend/src/assets/i18n/en.json b/frontend/src/assets/i18n/en.json index 097a19f..2a86ce2 100644 --- a/frontend/src/assets/i18n/en.json +++ b/frontend/src/assets/i18n/en.json @@ -12,8 +12,8 @@ "cancel": "Cancel", "remove_service_confirm": "Are you sure you want to remove this service?", "remove_service_confirm_text": "This action cannot be undone.", - "service_deleted_successfully": "Service deleted successfully", - "service_deleted_error": "Service could not be deleted. Please try again." + "service_removed_successfully": "Service deleted successfully", + "service_removed_error": "Service could not be deleted. Please try again." }, "list": { "your_availability_is": "You are:", @@ -54,7 +54,9 @@ "type_already_exists": "Type already exists", "type_added_successfully": "Type added successfully", "service_added_successfully": "Service added successfully", - "service_add_error": "Service could not be added. Please try again" + "service_add_error": "Service could not be added. Please try again", + "service_updated_successfully": "Service updated successfully", + "service_update_error": "Service could not be updated. Please try again" }, "update_availability_schedule": "Update availability schedule", "save_changes": "Save changes", diff --git a/frontend/src/assets/i18n/it.json b/frontend/src/assets/i18n/it.json index f492806..c88c979 100644 --- a/frontend/src/assets/i18n/it.json +++ b/frontend/src/assets/i18n/it.json @@ -12,8 +12,8 @@ "cancel": "Annulla", "remove_service_confirm": "Sei sicuro di voler rimuovere questo intervento?", "remove_service_confirm_text": "Questa operazione non può essere annullata.", - "service_deleted_successfully": "Intervento rimosso con successo", - "service_deleted_error": "Errore durante la rimozione dell'intervento. Riprova più tardi" + "service_removed_successfully": "Intervento rimosso con successo", + "service_removed_error": "Errore durante la rimozione dell'intervento. Riprova più tardi" }, "list": { "your_availability_is": "Attualmente sei:", @@ -54,7 +54,9 @@ "type_already_exists": "La tipologia è già presente", "type_added_successfully": "Tipologia aggiunta con successo", "service_added_successfully": "Intervento aggiunto con successo", - "service_add_error": "Errore durante l'aggiunta dell'intervento. Riprovare più tardi" + "service_add_error": "Errore durante l'aggiunta dell'intervento. Riprovare più tardi", + "service_updated_successfully": "Intervento modificato con successo", + "service_update_error": "Errore durante la modifica dell'intervento. Riprovare più tardi" }, "update_availability_schedule": "Aggiorna programmazione disponibilità", "save_changes": "Salva modifiche",