From 6a6b93e0876f3d2d054b72b7969dc574a143b974 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 14 Mar 2021 01:34:06 +0800 Subject: [PATCH] Handle file uploads properly #540 --- config/routes/customer.yaml | 2 +- src/Controller/APIController.php | 46 ++++++++++++++++++++++++++------ src/Entity/Warranty.php | 26 ++++++++++++++++-- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/config/routes/customer.yaml b/config/routes/customer.yaml index 90ac5591..9f952543 100644 --- a/config/routes/customer.yaml +++ b/config/routes/customer.yaml @@ -5,7 +5,7 @@ customer_list: customer_rows: path: /customers/rows controller: App\Controller\CustomerController::rows - methods: [POST] + methods: [GET,POST] customer_vehicle_search: path: /customers/vehicles diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 039dcc06..e6d8b7a4 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -2805,9 +2805,8 @@ class APIController extends Controller implements LoggedController $is_registered = true; $is_customer_warranty = false; - // TODO: check if the warranty is registered to a car owned by the customer + // check if the warranty is registered to a car owned by the customer $cust = $this->session->getCustomer(); - $is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust); // null mobile number should be blank string instead @@ -2880,6 +2879,7 @@ class APIController extends Controller implements LoggedController 'battery' => $battery, 'message' => [ 'register_error' => 'Warranty serial code has already been registered.', + 'edit_error' => 'Sorry, warranty is registered under another vehicle not in your list of vehicles.', ], ]; @@ -2890,9 +2890,13 @@ class APIController extends Controller implements LoggedController protected function handlePictureUpload($file, $target_dir, $serial, $name) { + error_log("handling $name upload"); // no file sent if ($file == null) + { + error_log('no file'); return null; + } // create target dir if it doesn't exist if (!file_exists($target_dir)) @@ -2904,10 +2908,15 @@ class APIController extends Controller implements LoggedController } } + error_log('HERE'); + // move file - $filename = $name . $file->getClientOriginalExtension(); + $filename = $name . '.' . $file->getClientOriginalExtension(); $file->move($target_dir . '/' . $serial, $filename); + error_log("filename - $filename"); + error_log($target_dir . '/' . $serial . '/' . $filename); + return $serial . '/' . $filename; } @@ -2927,8 +2936,8 @@ class APIController extends Controller implements LoggedController // process picture uploads $upload_dir = $kernel->getProjectDir() . '/public/warranty_uploads'; - $inv_filename = $this->warrantyRegister($invoice, $upload_dir, $serial, 'invoice'); - $wcard_filename = $this->warrantyRegister($warr_card, $upload_dir, $serial, 'wcard'); + $inv_filename = $this->handlePictureUpload($invoice, $upload_dir, $serial, 'invoice'); + $wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard'); $res = $this->checkParamsAndKey($req, $em, $required_params); if ($res->isError()) @@ -2962,9 +2971,29 @@ class APIController extends Controller implements LoggedController // skip warranty if it already exists if ($warr != null) { + /* + // NOTE: we could not update in the old version $res->setError(true) ->setErrorMessage('Warranty registration entry already exists.'); return $res; + */ + + // check if warranty is registered to a serial owned by customer + $warr_plate = $warr->getPlateNumber(); + $cust = $this->session->getCustomer(); + $is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust); + + if (!$is_customer_warranty) + { + $res->setError(true) + ->setErrorMessage('Warranty registred to a vehicle not in your list of vehicles.'); + return $res; + } + + } + else + { + $warr = new Warranty(); } // get sap battery @@ -2980,8 +3009,7 @@ class APIController extends Controller implements LoggedController $date_pur = new DateTime(); - // create new warranty entry - $warr = new Warranty(); + // create or update warranty entry $warr->setSerial($serial) ->setFirstName($req->request->get('first_name')) ->setLastName($req->request->get('last_name')) @@ -2996,7 +3024,9 @@ class APIController extends Controller implements LoggedController // ->setBatteryModel() ->setSAPBattery($sap_bty) ->setMobileNumber(substr($this->session->getPhoneNumber(), 2)) - ->setActivated(true); + ->setActivated(true) + ->setFileInvoice($inv_filename) + ->setFileWarrantyCard($wcard_filename); // TODO: check for date purchase and date expire diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index 26373b73..25eafc6a 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -144,13 +144,13 @@ class Warranty // invoice picture /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="string", length=80, nullable=true) */ protected $file_invoice; // warranty card picture /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="string", length=80, nullable=true) */ protected $file_warr_card; @@ -425,4 +425,26 @@ class Warranty return $this->privacy_policy; } + public function setFileInvoice($file = null) + { + $this->file_invoice = $file; + return $this; + } + + public function getFileInvoice() + { + return $this->file_invoice; + } + + public function setFileWarrantyCard($file = null) + { + $this->file_warr_card = $file; + return $this; + } + + public function getFileWarrantyCard() + { + return $this->file_warr_card; + } + }