Handle file uploads properly #540

This commit is contained in:
Kendrick Chan 2021-03-14 01:34:06 +08:00
parent d79f3b8854
commit 6a6b93e087
3 changed files with 63 additions and 11 deletions

View file

@ -5,7 +5,7 @@ customer_list:
customer_rows: customer_rows:
path: /customers/rows path: /customers/rows
controller: App\Controller\CustomerController::rows controller: App\Controller\CustomerController::rows
methods: [POST] methods: [GET,POST]
customer_vehicle_search: customer_vehicle_search:
path: /customers/vehicles path: /customers/vehicles

View file

@ -2805,9 +2805,8 @@ class APIController extends Controller implements LoggedController
$is_registered = true; $is_registered = true;
$is_customer_warranty = false; $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(); $cust = $this->session->getCustomer();
$is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust); $is_customer_warranty = $this->checkCustomerPlateNumber($warr_plate, $cust);
// null mobile number should be blank string instead // null mobile number should be blank string instead
@ -2880,6 +2879,7 @@ class APIController extends Controller implements LoggedController
'battery' => $battery, 'battery' => $battery,
'message' => [ 'message' => [
'register_error' => 'Warranty serial code has already been registered.', '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) protected function handlePictureUpload($file, $target_dir, $serial, $name)
{ {
error_log("handling $name upload");
// no file sent // no file sent
if ($file == null) if ($file == null)
{
error_log('no file');
return null; return null;
}
// create target dir if it doesn't exist // create target dir if it doesn't exist
if (!file_exists($target_dir)) if (!file_exists($target_dir))
@ -2904,10 +2908,15 @@ class APIController extends Controller implements LoggedController
} }
} }
error_log('HERE');
// move file // move file
$filename = $name . $file->getClientOriginalExtension(); $filename = $name . '.' . $file->getClientOriginalExtension();
$file->move($target_dir . '/' . $serial, $filename); $file->move($target_dir . '/' . $serial, $filename);
error_log("filename - $filename");
error_log($target_dir . '/' . $serial . '/' . $filename);
return $serial . '/' . $filename; return $serial . '/' . $filename;
} }
@ -2927,8 +2936,8 @@ class APIController extends Controller implements LoggedController
// process picture uploads // process picture uploads
$upload_dir = $kernel->getProjectDir() . '/public/warranty_uploads'; $upload_dir = $kernel->getProjectDir() . '/public/warranty_uploads';
$inv_filename = $this->warrantyRegister($invoice, $upload_dir, $serial, 'invoice'); $inv_filename = $this->handlePictureUpload($invoice, $upload_dir, $serial, 'invoice');
$wcard_filename = $this->warrantyRegister($warr_card, $upload_dir, $serial, 'wcard'); $wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard');
$res = $this->checkParamsAndKey($req, $em, $required_params); $res = $this->checkParamsAndKey($req, $em, $required_params);
if ($res->isError()) if ($res->isError())
@ -2962,9 +2971,29 @@ class APIController extends Controller implements LoggedController
// skip warranty if it already exists // skip warranty if it already exists
if ($warr != null) if ($warr != null)
{ {
/*
// NOTE: we could not update in the old version
$res->setError(true) $res->setError(true)
->setErrorMessage('Warranty registration entry already exists.'); ->setErrorMessage('Warranty registration entry already exists.');
return $res; 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 // get sap battery
@ -2980,8 +3009,7 @@ class APIController extends Controller implements LoggedController
$date_pur = new DateTime(); $date_pur = new DateTime();
// create new warranty entry // create or update warranty entry
$warr = new Warranty();
$warr->setSerial($serial) $warr->setSerial($serial)
->setFirstName($req->request->get('first_name')) ->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name')) ->setLastName($req->request->get('last_name'))
@ -2996,7 +3024,9 @@ class APIController extends Controller implements LoggedController
// ->setBatteryModel() // ->setBatteryModel()
->setSAPBattery($sap_bty) ->setSAPBattery($sap_bty)
->setMobileNumber(substr($this->session->getPhoneNumber(), 2)) ->setMobileNumber(substr($this->session->getPhoneNumber(), 2))
->setActivated(true); ->setActivated(true)
->setFileInvoice($inv_filename)
->setFileWarrantyCard($wcard_filename);
// TODO: check for date purchase and date expire // TODO: check for date purchase and date expire

View file

@ -144,13 +144,13 @@ class Warranty
// invoice picture // invoice picture
/** /**
* @ORM\Column(type="string", length=80) * @ORM\Column(type="string", length=80, nullable=true)
*/ */
protected $file_invoice; protected $file_invoice;
// warranty card picture // warranty card picture
/** /**
* @ORM\Column(type="string", length=80) * @ORM\Column(type="string", length=80, nullable=true)
*/ */
protected $file_warr_card; protected $file_warr_card;
@ -425,4 +425,26 @@ class Warranty
return $this->privacy_policy; 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;
}
} }