From 5b848a7f10f5e7e9d2aeb9a12f991b66352eb876 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 15 Sep 2022 05:56:43 +0000 Subject: [PATCH] Fix issue when creating API user that's not a rider. Fix issues when uploading a file. #704 --- config/routes/capi.yaml | 4 +-- src/Controller/APIUserController.php | 14 ++++---- .../CAPI/WarrantySerialController.php | 33 +++++++++++-------- src/Service/WarrantySerialUploadLogger.php | 2 +- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/config/routes/capi.yaml b/config/routes/capi.yaml index de9bf872..0ae29c7c 100644 --- a/config/routes/capi.yaml +++ b/config/routes/capi.yaml @@ -187,6 +187,6 @@ capi_dealer_list: # warranty serial api capi_warranty_serial_upload: - path: /capi/warranty_serial - controller: App\Controller\CAPI\WarrantySerialController::upload + path: /capi/warranty_serial/upload + controller: App\Controller\CAPI\WarrantySerialController::uploadWarrantySerialFile methods: [POST] diff --git a/src/Controller/APIUserController.php b/src/Controller/APIUserController.php index 06f128e9..122d3830 100644 --- a/src/Controller/APIUserController.php +++ b/src/Controller/APIUserController.php @@ -155,18 +155,20 @@ class APIUserController extends Controller // metadata $rider_id = $req->request->get('rider_id'); $rider = $em->getRepository(Rider::class)->find($rider_id); - // TODO: check for null rider + if ($rider != null) + { + $meta = ['rider_id' => $rider_id]; - $meta = ['rider_id' => $rider_id]; + // set api user in rider + $rider->setAPIUser($obj); - // set api user in rider - $rider->setAPIUser($obj); + $obj->setRider($rider) + ->setMetadata($meta); + } // set and save values $obj->setName($req->request->get('name')) ->setEnabled($req->request->get('enabled') ? true : false) - ->setMetadata($meta) - ->setRider($rider) ->clearRoles(); // set roles diff --git a/src/Controller/CAPI/WarrantySerialController.php b/src/Controller/CAPI/WarrantySerialController.php index e1226c1f..ecd706bf 100644 --- a/src/Controller/CAPI/WarrantySerialController.php +++ b/src/Controller/CAPI/WarrantySerialController.php @@ -13,7 +13,7 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException; use Catalyst\APIBundle\Controller\APIController; use Catalyst\APIBundle\Response\APIResponse; -use App\Entity\WarrantySerialiQueue; +use App\Entity\WarrantySerialQueue; use App\Service\WarrantySerialUploadLogger; @@ -25,13 +25,15 @@ use Catalyst\APIBundle\Access\Generator as ACLGenerator; class WarrantySerialController extends APIController { protected $acl_gen; + protected $upload_logger; - public function __construct(ACLGenerator $acl_gen) + public function __construct(ACLGenerator $acl_gen, WarrantySerialUploadLogger $upload_logger) { $this->acl_gen = $acl_gen; + $this->upload_logger = $upload_logger; } - public function upload(Request $req, EntityManagerInterface $em, WarrantySerialUploadLogger $upload_logger) + public function uploadWarrantySerialFile(Request $req, EntityManagerInterface $em, KernelInterface $kernel) { $this->denyAccessUnlessGranted('warrantyserial.upload', null, 'No access.'); @@ -41,9 +43,9 @@ class WarrantySerialController extends APIController $user_id = $_SERVER['HTTP_X_CATA_API_KEY']; - $res = $this->checkRequiredParams($req, $required_params, $upload_logger, $user_id); + $res = $this->checkRequiredParamsForFiles($req, $required_params, $user_id); - if (!$res) + if ($res !== true) return $res; // get the csv file @@ -51,10 +53,10 @@ class WarrantySerialController extends APIController // process file upload $upload_dir = $kernel->getProjectDir() . '/public/warranty_serial_uploads'; - $serial_filename = $this->handleSerialFileUpload($csv_file, $upload_dir, $upload_logger); + $serial_filename = $this->handleSerialFileUpload($csv_file, $upload_dir); // insert to warranty serial queue - $res = $this->registerWarrantySerialFile($em, $csv_file, $serial_filename, $upload_logger, $user_id); + $res = $this->registerWarrantySerialFile($em, $csv_file, $serial_filename, $user_id); // flush to db $em->flush(); @@ -62,7 +64,7 @@ class WarrantySerialController extends APIController return $res; } - protected function registerWarrantySerialFile($em, $file, $serial_filename, $upload_logger, $user_id) + protected function registerWarrantySerialFile($em, $file, $serial_filename, $user_id) { $orig_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); @@ -82,9 +84,11 @@ class WarrantySerialController extends APIController ]; $this->upload_logger->logWarrantySerialUploadInfo($log_data); + + return new APIResponse(true, 'Warranty serial file uploaded.'); } - protected function handleSerialFileUpload($file, $upload_dir, $upload_logger) + protected function handleSerialFileUpload($file, $target_dir) { // create target dir if it doesn't exist if (!file_exists($target_dir)) @@ -114,10 +118,10 @@ class WarrantySerialController extends APIController return $str_curr_date . '/' . $filename; } - protected function checkRequiredParams(Request $req, $params, $upload_logger, $user_id) + protected function checkRequiredParamsForFiles(Request $req, $params, $user_id) { // check required parameters - $missing = $this->checkMissingParameters($req, $params); + $missing = $this->checkMissingParametersForFiles($req, $params); if (count($missing) > 0) { // log the error @@ -136,7 +140,7 @@ class WarrantySerialController extends APIController return true; } - protected function checkMissingParameters(Request $req, $params = []) + protected function checkMissingParametersForFiles(Request $req, $params = []) { $missing = []; @@ -151,9 +155,12 @@ class WarrantySerialController extends APIController } else if ($req->getMethod() == 'POST') { - $check = $req->request->get($param); + // get files from request. + $check = $req->files->get($param); if (empty($check)) + { $missing[] = $param; + } } else return $params; diff --git a/src/Service/WarrantySerialUploadLogger.php b/src/Service/WarrantySerialUploadLogger.php index d4c24852..d708792c 100644 --- a/src/Service/WarrantySerialUploadLogger.php +++ b/src/Service/WarrantySerialUploadLogger.php @@ -34,7 +34,7 @@ class WarrantySerialUploadLogger $uploaded_file_serial = $log_data['uploaded_file_serial']; $log_entry->setApiUser($user_id) - ->setLoaded($is_uploaded) + ->setUploaded($is_uploaded) ->setOrigFileSerial($orig_file_serial) ->setUploadedFileSerial($uploaded_file_serial) ->setError($error);