Add source to WarrantyAPILog. #555

This commit is contained in:
Korina Cordero 2021-04-30 07:25:51 +00:00
parent 42c2b52690
commit 7ec5ba439c
6 changed files with 2964 additions and 1256 deletions

4117
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -33,6 +33,7 @@ use App\Ramcar\WarrantyClass;
use App\Ramcar\WarrantyStatus; use App\Ramcar\WarrantyStatus;
use App\Ramcar\FuelType; use App\Ramcar\FuelType;
use App\Ramcar\VehicleStatusCondition; use App\Ramcar\VehicleStatusCondition;
use App\Ramcar\WarrantySource;
use DateTime; use DateTime;
@ -74,14 +75,14 @@ class CustomerWarrantyController extends APIController
return $missing; return $missing;
} }
protected function checkRequiredParams(Request $req, $params, $logger, $log_data, $user_id, $action) protected function checkRequiredParams(Request $req, $params, $logger, $log_data, $user_id, $action, $source)
{ {
// check required parameters // check required parameters
$missing = $this->checkMissingParameters($req, $params); $missing = $this->checkMissingParameters($req, $params);
if (count($missing) > 0) if (count($missing) > 0)
{ {
$miss_string = implode(', ', $missing); $miss_string = implode(', ', $missing);
$logger->logWarrantyInfo($log_data, 'Missing parameter(s): ' . $miss_string, $user_id, $action); $logger->logWarrantyInfo($log_data, 'Missing parameter(s): ' . $miss_string, $user_id, $action, $source);
return new APIResponse(false, 'Missing parameter(s): ' . $miss_string); return new APIResponse(false, 'Missing parameter(s): ' . $miss_string);
} }
@ -105,9 +106,11 @@ class CustomerWarrantyController extends APIController
'serial' => $serial, 'serial' => $serial,
]; ];
$action = 'check'; $action = 'check';
$source = WarrantySource::CAPI;
// check required parameters // check required parameters
$required_params = []; $required_params = [];
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action); $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action, $source);
if (!$res) if (!$res)
return $res; return $res;
@ -316,13 +319,16 @@ class CustomerWarrantyController extends APIController
'invoice' => $req->request->get('invoice'), 'invoice' => $req->request->get('invoice'),
]; ];
$action = 'create/update'; $action = 'create/update';
$source = WarrantySource::CAPI;
error_log('SOURCE: ' . $source);
$required_params = [ $required_params = [
'first_name', 'first_name',
'last_name', 'last_name',
'plate_num' 'plate_num'
]; ];
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action); $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action, $source);
if (!$res) if (!$res)
return $res; return $res;
@ -338,7 +344,8 @@ class CustomerWarrantyController extends APIController
$wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard'); $wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard');
// do actual registering // do actual registering
$res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, $logger, $log_data, $user_id, $action); $res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename,
$logger, $log_data, $user_id, $action, $source);
// flush to db // flush to db
$em->flush(); $em->flush();
@ -380,7 +387,7 @@ class CustomerWarrantyController extends APIController
} }
protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null,
$logger, $log_data, $user_id, $action) $logger, $log_data, $user_id, $action, $source)
{ {
$plate_num = $this->cleanPlateNumber($req->request->get('plate_num')); $plate_num = $this->cleanPlateNumber($req->request->get('plate_num'));
@ -389,7 +396,7 @@ class CustomerWarrantyController extends APIController
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
if ($warr_serial == null) if ($warr_serial == null)
{ {
$logger->logWarrantyInfo($log_data, 'Invalid warranty serial code..', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Invalid warranty serial code..', $user_id, $action, $source);
return new APIResponse(false, 'Invalid warranty serial code.'); return new APIResponse(false, 'Invalid warranty serial code.');
} }
@ -405,7 +412,7 @@ class CustomerWarrantyController extends APIController
if ($plate_num != $warr_plate_num) if ($plate_num != $warr_plate_num)
{ {
$logger->logWarrantyInfo($log_data, 'Plate number does not match vehicle registered to warranty.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Plate number does not match vehicle registered to warranty.', $user_id, $action, $source);
return new APIResponse(false, 'Plate number does not match vehicle registered to warranty.'); return new APIResponse(false, 'Plate number does not match vehicle registered to warranty.');
} }
@ -431,7 +438,7 @@ class CustomerWarrantyController extends APIController
$sap_bty = $em->getRepository(SAPBattery::class)->find($sku); $sap_bty = $em->getRepository(SAPBattery::class)->find($sku);
if ($sap_bty == null) if ($sap_bty == null)
{ {
$logger->logWarrantyInfo($log_data, 'Cound not find battery entry for warranty.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Cound not find battery entry for warranty.', $user_id, $action, $source);
return new APIResponse(false, 'Could not find battery entry for warranty.'); return new APIResponse(false, 'Could not find battery entry for warranty.');
} }
} }
@ -444,7 +451,7 @@ class CustomerWarrantyController extends APIController
$vehicle = $em->getRepository(Vehicle::class)->find($vmake_id); $vehicle = $em->getRepository(Vehicle::class)->find($vmake_id);
if ($vehicle == null) if ($vehicle == null)
{ {
$logger->logWarrantyInfo($log_data, 'Could not find vehicle specified for warranty.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Could not find vehicle specified for warranty.', $user_id, $action, $source);
return new APIResponse(false, 'Could not find vehicle specified for warranty.'); return new APIResponse(false, 'Could not find vehicle specified for warranty.');
} }
} }
@ -461,7 +468,7 @@ class CustomerWarrantyController extends APIController
$date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase')); $date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase'));
if (!$date_pur_cust) if (!$date_pur_cust)
{ {
$logger->logWarrantyInfo($log_data, 'Invalid date format for date of purchase.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Invalid date format for date of purchase.', $user_id, $action, $source);
return new APIResponse(false, 'Invalid date format for date of purchase.'); return new APIResponse(false, 'Invalid date format for date of purchase.');
} }
} }
@ -538,7 +545,7 @@ class CustomerWarrantyController extends APIController
$em->persist($warr); $em->persist($warr);
$logger->logWarrantyInfo($log_data, '', $user_id, $action); $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source);
// TODO: check if we need to do anyting else // TODO: check if we need to do anyting else
$data = []; $data = [];

View file

@ -29,6 +29,7 @@ use App\Ramcar\WarrantyClass;
use App\Ramcar\WarrantyStatus; use App\Ramcar\WarrantyStatus;
use App\Ramcar\FuelType; use App\Ramcar\FuelType;
use App\Ramcar\VehicleStatusCondition; use App\Ramcar\VehicleStatusCondition;
use App\Ramcar\WarrantySource;
use DateTime; use DateTime;
@ -189,12 +190,13 @@ class WarrantyController extends APIController
'mobile_number' => $mnum, 'mobile_number' => $mnum,
]; ];
$action = 'create'; $action = 'create';
$source = WarrantySource::CAPI;
$msg = $this->checkRequiredParameters($req, $params); $msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg); error_log('msg - ' . $msg);
if ($msg) if ($msg)
{ {
$logger->logWarrantyInfo($log_data, $msg, $user_id, $action); $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source);
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
} }
@ -207,7 +209,7 @@ class WarrantyController extends APIController
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string); $date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
if ($date_expire === false) if ($date_expire === false)
{ {
$logger->logWarrantyInfo($log_data, 'Wrong date format: date_expire.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Wrong date format: date_expire.', $user_id, $action, $source);
return new APIResponse(false, 'Wrong date format: date_expire.'); return new APIResponse(false, 'Wrong date format: date_expire.');
} }
@ -215,14 +217,14 @@ class WarrantyController extends APIController
$date_pur = DateTime::createFromFormat('Ymd', $date_pur_string); $date_pur = DateTime::createFromFormat('Ymd', $date_pur_string);
if ($date_pur === false) if ($date_pur === false)
{ {
$logger->logWarrantyInfo($log_data, 'Wrong date format: date_purchase', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Wrong date format: date_purchase', $user_id, $action, $source);
return new APIResponse(false, 'Wrong date format: date_purchase.'); return new APIResponse(false, 'Wrong date format: date_purchase.');
} }
// valid warranty class // valid warranty class
if (!WarrantyClass::validate($warr_class)) if (!WarrantyClass::validate($warr_class))
{ {
$logger->logWarrantyInfo($log_data, 'Invalid warranty class.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Invalid warranty class.', $user_id, $action, $source);
return new APIResponse(false, 'Invalid warranty class.'); return new APIResponse(false, 'Invalid warranty class.');
} }
@ -230,7 +232,7 @@ class WarrantyController extends APIController
$plate = Warranty::cleanPlateNumber($plate); $plate = Warranty::cleanPlateNumber($plate);
if (!$plate) if (!$plate)
{ {
$logger->logWarrantyInfo($log_data, 'Invalid plate number.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Invalid plate number.', $user_id, $action, $source);
return new APIResponse(false, 'Invalid plate number.'); return new APIResponse(false, 'Invalid plate number.');
} }
@ -243,7 +245,7 @@ class WarrantyController extends APIController
$batt = $em->getRepository(SAPBattery::class)->find($sku); $batt = $em->getRepository(SAPBattery::class)->find($sku);
if ($batt == null) if ($batt == null)
{ {
$logger->logWarrantyInfo($log_data, 'Invalid battery SKU.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Invalid battery SKU.', $user_id, $action, $source);
return new APIResponse(false, 'Invalid battery SKU.'); return new APIResponse(false, 'Invalid battery SKU.');
} }
} }
@ -283,7 +285,7 @@ class WarrantyController extends APIController
} }
catch (UniqueConstraintViolationException $e) catch (UniqueConstraintViolationException $e)
{ {
$logger->logWarrantyInfo($log_data, 'Duplicate serial encountered.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Duplicate serial encountered.', $user_id, $action, $source);
return new APIResponse(false, 'Duplicate serial encountered.'); return new APIResponse(false, 'Duplicate serial encountered.');
} }
@ -293,7 +295,7 @@ class WarrantyController extends APIController
]; ];
// log creation of warranty data // log creation of warranty data
$logger->logWarrantyInfo($log_data, '', $user_id, $action); $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source);
return new APIResponse(true, 'Warranty registered.', $data); return new APIResponse(true, 'Warranty registered.', $data);
} }
@ -317,11 +319,12 @@ class WarrantyController extends APIController
'id' => $id, 'id' => $id,
]; ];
$action = 'claim'; $action = 'claim';
$source = WarrantySource::CAPI;
$msg = $this->checkRequiredParameters($req, $params); $msg = $this->checkRequiredParameters($req, $params);
if ($msg) if ($msg)
{ {
$logger->logWarrantyInfo($log_data, $msg, $user_id, $action); $logger->logWarrantyInfo($log_data, $msg, $user_id, $action, $source);
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
} }
@ -329,14 +332,14 @@ class WarrantyController extends APIController
$warr = $em->getRepository(Warranty::class)->find($id); $warr = $em->getRepository(Warranty::class)->find($id);
if ($warr == null) if ($warr == null)
{ {
$logger->logWarrantyInfo($log_data, 'No warranty found with that id.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'No warranty found with that id.', $user_id, $action, $source);
return new APIResponse(false, 'No warranty found with that id.', null, 404); return new APIResponse(false, 'No warranty found with that id.', null, 404);
} }
// warranty is not active // warranty is not active
if (!$warr->canClaim()) if (!$warr->canClaim())
{ {
$logger->logWarrantyInfo($log_data, 'Warranty is not active.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Warranty is not active.', $user_id, $action, $source);
return new APIResponse(false, 'Warranty is not active.'); return new APIResponse(false, 'Warranty is not active.');
} }
@ -346,7 +349,7 @@ class WarrantyController extends APIController
$check_warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]); $check_warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]);
if ($check_warr != null) if ($check_warr != null)
{ {
$logger->logWarrantyInfo($log_data, 'Serial for replacement has already been used.', $user_id, $action); $logger->logWarrantyInfo($log_data, 'Serial for replacement has already been used.', $user_id, $action, $source);
return new APIResponse(false, 'Serial for replacement has already been used.'); return new APIResponse(false, 'Serial for replacement has already been used.');
} }
@ -374,7 +377,7 @@ class WarrantyController extends APIController
$em->flush(); $em->flush();
// TODO: claim log // TODO: claim log
$logger->logWarrantyInfo($log_data, '', $user_id, $action); $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source);
return new APIResponse(true, 'Warranty claimed successfully.'); return new APIResponse(true, 'Warranty claimed successfully.');
} }

View file

@ -53,11 +53,19 @@ class WarrantyAPILog
*/ */
protected $action; protected $action;
// source
/**
* @ORM\Column(type="string", length=30)
*/
protected $source;
public function __construct() public function __construct()
{ {
$this->date_create = new DateTime(); $this->date_create = new DateTime();
$this->errors = new ArrayCollection(); $this->errors = new ArrayCollection();
$this->all_data = []; $this->all_data = [];
$this->action = '';
$this->source = '';
} }
public function getID() public function getID()
@ -142,4 +150,14 @@ class WarrantyAPILog
return $this->action; return $this->action;
} }
public function setSource($source)
{
$this->source = $source;
return $this;
}
public function getSource()
{
return $this->source;
}
} }

View file

@ -0,0 +1,20 @@
<?php
namespace App\Ramcar;
class WarrantySource extends NameValue
{
const CAPI = 'capi';
const RAPI = 'rapi';
const ADMIN_PANEL = 'admin_panel';
const BULK_UPLOAD = 'bulk_upload';
const MOBILE = 'mobile';
const COLLECTION = [
'capi' => 'Third Party API',
'rapi' => 'Rider API',
'admin_panel' => 'Admin Panel',
'bulk_upload' => 'Bulk Upload',
'mobile' => 'Mobile API',
];
}

View file

@ -15,7 +15,7 @@ class WarrantyAPILogger
$this->em = $em; $this->em = $em;
} }
public function logWarrantyInfo($log_data, $error, $user_id, $action) public function logWarrantyInfo($log_data, $error, $user_id, $action, $source)
{ {
$log_entry = new WarrantyAPILog(); $log_entry = new WarrantyAPILog();
@ -27,7 +27,8 @@ class WarrantyAPILogger
$log_entry->setApiUser($user_id) $log_entry->setApiUser($user_id)
->setErrors($err_array) ->setErrors($err_array)
->setAllData($json_data) ->setAllData($json_data)
->setAction($action); ->setAction($action)
->setSource($source);
$this->em->persist($log_entry); $this->em->persist($log_entry);
$this->em->flush(); $this->em->flush();