Add action field to WarrantyAPILog. #555

This commit is contained in:
Korina Cordero 2021-04-30 06:07:37 +00:00
parent 22047911e5
commit c6a0b96c90
4 changed files with 59 additions and 29 deletions

View file

@ -74,14 +74,14 @@ class CustomerWarrantyController extends APIController
return $missing; return $missing;
} }
protected function checkRequiredParams(Request $req, $params, $logger, $log_data, $user_id) protected function checkRequiredParams(Request $req, $params, $logger, $log_data, $user_id, $action)
{ {
// 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); $logger->logWarrantyInfo($log_data, 'Missing parameter(s): ' . $miss_string, $user_id, $action);
return new APIResponse(false, 'Missing parameter(s): ' . $miss_string); return new APIResponse(false, 'Missing parameter(s): ' . $miss_string);
} }
@ -98,13 +98,18 @@ class CustomerWarrantyController extends APIController
return preg_replace('/\s+/', '', strtoupper($plate_num)); return preg_replace('/\s+/', '', strtoupper($plate_num));
} }
public function check($serial, EntityManagerInterface $em, Request $req) public function check($serial, EntityManagerInterface $em, Request $req, WarrantyAPILogger $logger)
{ {
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
$log_data = [
'serial' => $serial,
];
$action = 'check';
// check required parameters // check required parameters
$required_params = []; $required_params = [];
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id); $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action);
if (!$res) if (!$res)
return $res; return $res;
error_log('check warranty serial'); error_log('check warranty serial');
@ -116,6 +121,7 @@ class CustomerWarrantyController extends APIController
//error_log($_SERVER['HTTP_X_CATA_API_KEY']); //error_log($_SERVER['HTTP_X_CATA_API_KEY']);
// TODO: add logging for the other scenarios
// check if warranty serial is there // check if warranty serial is there
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial); $warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]); $warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
@ -309,13 +315,14 @@ class CustomerWarrantyController extends APIController
'email' => $req->request->get('email'), 'email' => $req->request->get('email'),
'invoice' => $req->request->get('invoice'), 'invoice' => $req->request->get('invoice'),
]; ];
$action = 'create/update';
$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); $res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action);
if (!$res) if (!$res)
return $res; return $res;
@ -331,7 +338,7 @@ 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); $res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, $logger, $log_data, $user_id, $action);
// flush to db // flush to db
$em->flush(); $em->flush();
@ -372,7 +379,8 @@ class CustomerWarrantyController extends APIController
return $serial . '/' . $filename; return $serial . '/' . $filename;
} }
protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, $logger, $log_data, $user_id) protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null,
$logger, $log_data, $user_id, $action)
{ {
$plate_num = $this->cleanPlateNumber($req->request->get('plate_num')); $plate_num = $this->cleanPlateNumber($req->request->get('plate_num'));
@ -381,7 +389,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); $logger->logWarrantyInfo($log_data, 'Invalid warranty serial code..', $user_id, $action);
return new APIResponse(false, 'Invalid warranty serial code.'); return new APIResponse(false, 'Invalid warranty serial code.');
} }
@ -397,7 +405,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); $logger->logWarrantyInfo($log_data, 'Plate number does not match vehicle registered to warranty.', $user_id, $action);
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.');
} }
@ -423,7 +431,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); $logger->logWarrantyInfo($log_data, 'Cound not find battery entry for warranty.', $user_id, $action);
return new APIResponse(false, 'Could not find battery entry for warranty.'); return new APIResponse(false, 'Could not find battery entry for warranty.');
} }
} }
@ -436,7 +444,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); $logger->logWarrantyInfo($log_data, 'Could not find vehicle specified for warranty.', $user_id, $action);
return new APIResponse(false, 'Could not find vehicle specified for warranty.'); return new APIResponse(false, 'Could not find vehicle specified for warranty.');
} }
} }
@ -453,7 +461,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); $logger->logWarrantyInfo($log_data, 'Invalid date format for date of purchase.', $user_id, $action);
return new APIResponse(false, 'Invalid date format for date of purchase.'); return new APIResponse(false, 'Invalid date format for date of purchase.');
} }
} }
@ -530,7 +538,7 @@ class CustomerWarrantyController extends APIController
$em->persist($warr); $em->persist($warr);
$logger->logWarrantyInfo($log_data, '', $user_id); $logger->logWarrantyInfo($log_data, '', $user_id, $action);
// TODO: check if we need to do anyting else // TODO: check if we need to do anyting else
$data = []; $data = [];

View file

@ -188,12 +188,13 @@ class WarrantyController extends APIController
'last_name' => $lname, 'last_name' => $lname,
'mobile_number' => $mnum, 'mobile_number' => $mnum,
]; ];
$action = 'create';
$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); $logger->logWarrantyInfo($log_data, $msg, $user_id, $action);
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
} }
@ -206,7 +207,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); $logger->logWarrantyInfo($log_data, 'Wrong date format: date_expire.', $user_id, $action);
return new APIResponse(false, 'Wrong date format: date_expire.'); return new APIResponse(false, 'Wrong date format: date_expire.');
} }
@ -214,14 +215,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); $logger->logWarrantyInfo($log_data, 'Wrong date format: date_purchase', $user_id, $action);
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); $logger->logWarrantyInfo($log_data, 'Invalid warranty class.', $user_id, $action);
return new APIResponse(false, 'Invalid warranty class.'); return new APIResponse(false, 'Invalid warranty class.');
} }
@ -229,7 +230,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); $logger->logWarrantyInfo($log_data, 'Invalid plate number.', $user_id, $action);
return new APIResponse(false, 'Invalid plate number.'); return new APIResponse(false, 'Invalid plate number.');
} }
@ -242,7 +243,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); $logger->logWarrantyInfo($log_data, 'Invalid battery SKU.', $user_id, $action);
return new APIResponse(false, 'Invalid battery SKU.'); return new APIResponse(false, 'Invalid battery SKU.');
} }
} }
@ -282,6 +283,7 @@ class WarrantyController extends APIController
} }
catch (UniqueConstraintViolationException $e) catch (UniqueConstraintViolationException $e)
{ {
$logger->logWarrantyInfo($log_data, 'Duplicate serial encountered.', $user_id, $action);
return new APIResponse(false, 'Duplicate serial encountered.'); return new APIResponse(false, 'Duplicate serial encountered.');
} }
@ -291,7 +293,7 @@ class WarrantyController extends APIController
]; ];
// log creation of warranty data // log creation of warranty data
$logger->logWarrantyInfo($log_data, '', $user_id); $logger->logWarrantyInfo($log_data, '', $user_id, $action);
return new APIResponse(true, 'Warranty registered.', $data); return new APIResponse(true, 'Warranty registered.', $data);
} }
@ -314,11 +316,12 @@ class WarrantyController extends APIController
'serial' => $serial, 'serial' => $serial,
'id' => $id, 'id' => $id,
]; ];
$action = 'claim';
$msg = $this->checkRequiredParameters($req, $params); $msg = $this->checkRequiredParameters($req, $params);
if ($msg) if ($msg)
{ {
$logger->logWarrantyInfo($log_data, $msg, $user_id); $logger->logWarrantyInfo($log_data, $msg, $user_id, $action);
return new APIResponse(false, $msg); return new APIResponse(false, $msg);
} }
@ -326,14 +329,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); $logger->logWarrantyInfo($log_data, 'No warranty found with that id.', $user_id, $action);
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); $logger->logWarrantyInfo($log_data, 'Warranty is not active.', $user_id, $action);
return new APIResponse(false, 'Warranty is not active.'); return new APIResponse(false, 'Warranty is not active.');
} }
@ -343,7 +346,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); $logger->logWarrantyInfo($log_data, 'Serial for replacement has already been used.', $user_id, $action);
return new APIResponse(false, 'Serial for replacement has already been used.'); return new APIResponse(false, 'Serial for replacement has already been used.');
} }
@ -371,7 +374,7 @@ class WarrantyController extends APIController
$em->flush(); $em->flush();
// TODO: claim log // TODO: claim log
$logger->logWarrantyInfo($log_data, '', $user_id); $logger->logWarrantyInfo($log_data, '', $user_id, $action);
return new APIResponse(true, 'Warranty claimed successfully.'); return new APIResponse(true, 'Warranty claimed successfully.');
} }

View file

@ -47,6 +47,12 @@ class WarrantyAPILog
*/ */
protected $errors; protected $errors;
// action
/**
* @ORM\Column(type="string", length=20)
*/
protected $action;
public function __construct() public function __construct()
{ {
$this->date_create = new DateTime(); $this->date_create = new DateTime();
@ -124,4 +130,16 @@ class WarrantyAPILog
$this->errors = new ArrayCollection(); $this->errors = new ArrayCollection();
return $this; return $this;
} }
public function setAction($action)
{
$this->action = $action;
return $this;
}
public function getAction()
{
return $this->action;
}
} }

View file

@ -15,7 +15,7 @@ class WarrantyAPILogger
$this->em = $em; $this->em = $em;
} }
public function logWarrantyInfo($log_data, $error, $user_id) public function logWarrantyInfo($log_data, $error, $user_id, $action)
{ {
$log_entry = new WarrantyAPILog(); $log_entry = new WarrantyAPILog();
@ -26,7 +26,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);
$this->em->persist($log_entry); $this->em->persist($log_entry);
$this->em->flush(); $this->em->flush();