Fix issue when creating API user that's not a rider. Fix issues when uploading a file. #704
This commit is contained in:
parent
a3e9c45ec3
commit
5b848a7f10
4 changed files with 31 additions and 22 deletions
|
|
@ -187,6 +187,6 @@ capi_dealer_list:
|
||||||
|
|
||||||
# warranty serial api
|
# warranty serial api
|
||||||
capi_warranty_serial_upload:
|
capi_warranty_serial_upload:
|
||||||
path: /capi/warranty_serial
|
path: /capi/warranty_serial/upload
|
||||||
controller: App\Controller\CAPI\WarrantySerialController::upload
|
controller: App\Controller\CAPI\WarrantySerialController::uploadWarrantySerialFile
|
||||||
methods: [POST]
|
methods: [POST]
|
||||||
|
|
|
||||||
|
|
@ -155,18 +155,20 @@ class APIUserController extends Controller
|
||||||
// metadata
|
// metadata
|
||||||
$rider_id = $req->request->get('rider_id');
|
$rider_id = $req->request->get('rider_id');
|
||||||
$rider = $em->getRepository(Rider::class)->find($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
|
$obj->setRider($rider)
|
||||||
$rider->setAPIUser($obj);
|
->setMetadata($meta);
|
||||||
|
}
|
||||||
|
|
||||||
// set and save values
|
// set and save values
|
||||||
$obj->setName($req->request->get('name'))
|
$obj->setName($req->request->get('name'))
|
||||||
->setEnabled($req->request->get('enabled') ? true : false)
|
->setEnabled($req->request->get('enabled') ? true : false)
|
||||||
->setMetadata($meta)
|
|
||||||
->setRider($rider)
|
|
||||||
->clearRoles();
|
->clearRoles();
|
||||||
|
|
||||||
// set roles
|
// set roles
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||||
use Catalyst\APIBundle\Controller\APIController;
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
use Catalyst\APIBundle\Response\APIResponse;
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
|
|
||||||
use App\Entity\WarrantySerialiQueue;
|
use App\Entity\WarrantySerialQueue;
|
||||||
|
|
||||||
use App\Service\WarrantySerialUploadLogger;
|
use App\Service\WarrantySerialUploadLogger;
|
||||||
|
|
||||||
|
|
@ -25,13 +25,15 @@ use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
||||||
class WarrantySerialController extends APIController
|
class WarrantySerialController extends APIController
|
||||||
{
|
{
|
||||||
protected $acl_gen;
|
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->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.');
|
$this->denyAccessUnlessGranted('warrantyserial.upload', null, 'No access.');
|
||||||
|
|
||||||
|
|
@ -41,9 +43,9 @@ class WarrantySerialController extends APIController
|
||||||
|
|
||||||
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
|
$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;
|
return $res;
|
||||||
|
|
||||||
// get the csv file
|
// get the csv file
|
||||||
|
|
@ -51,10 +53,10 @@ class WarrantySerialController extends APIController
|
||||||
|
|
||||||
// process file upload
|
// process file upload
|
||||||
$upload_dir = $kernel->getProjectDir() . '/public/warranty_serial_uploads';
|
$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
|
// 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
|
// flush to db
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
@ -62,7 +64,7 @@ class WarrantySerialController extends APIController
|
||||||
return $res;
|
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);
|
$orig_filename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
|
||||||
|
|
||||||
|
|
@ -82,9 +84,11 @@ class WarrantySerialController extends APIController
|
||||||
];
|
];
|
||||||
|
|
||||||
$this->upload_logger->logWarrantySerialUploadInfo($log_data);
|
$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
|
// create target dir if it doesn't exist
|
||||||
if (!file_exists($target_dir))
|
if (!file_exists($target_dir))
|
||||||
|
|
@ -114,10 +118,10 @@ class WarrantySerialController extends APIController
|
||||||
return $str_curr_date . '/' . $filename;
|
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
|
// check required parameters
|
||||||
$missing = $this->checkMissingParameters($req, $params);
|
$missing = $this->checkMissingParametersForFiles($req, $params);
|
||||||
if (count($missing) > 0)
|
if (count($missing) > 0)
|
||||||
{
|
{
|
||||||
// log the error
|
// log the error
|
||||||
|
|
@ -136,7 +140,7 @@ class WarrantySerialController extends APIController
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function checkMissingParameters(Request $req, $params = [])
|
protected function checkMissingParametersForFiles(Request $req, $params = [])
|
||||||
{
|
{
|
||||||
$missing = [];
|
$missing = [];
|
||||||
|
|
||||||
|
|
@ -151,9 +155,12 @@ class WarrantySerialController extends APIController
|
||||||
}
|
}
|
||||||
else if ($req->getMethod() == 'POST')
|
else if ($req->getMethod() == 'POST')
|
||||||
{
|
{
|
||||||
$check = $req->request->get($param);
|
// get files from request.
|
||||||
|
$check = $req->files->get($param);
|
||||||
if (empty($check))
|
if (empty($check))
|
||||||
|
{
|
||||||
$missing[] = $param;
|
$missing[] = $param;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return $params;
|
return $params;
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ class WarrantySerialUploadLogger
|
||||||
$uploaded_file_serial = $log_data['uploaded_file_serial'];
|
$uploaded_file_serial = $log_data['uploaded_file_serial'];
|
||||||
|
|
||||||
$log_entry->setApiUser($user_id)
|
$log_entry->setApiUser($user_id)
|
||||||
->setLoaded($is_uploaded)
|
->setUploaded($is_uploaded)
|
||||||
->setOrigFileSerial($orig_file_serial)
|
->setOrigFileSerial($orig_file_serial)
|
||||||
->setUploadedFileSerial($uploaded_file_serial)
|
->setUploadedFileSerial($uploaded_file_serial)
|
||||||
->setError($error);
|
->setError($error);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue