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;
}
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
$missing = $this->checkMissingParameters($req, $params);
if (count($missing) > 0)
{
$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);
}
@ -98,13 +98,18 @@ class CustomerWarrantyController extends APIController
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
$required_params = [];
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id);
if (!$res)
return $res;
$res = $this->checkRequiredParams($req, $required_params, $logger, $log_data, $user_id, $action);
if (!$res)
return $res;
error_log('check warranty serial');
@ -116,6 +121,7 @@ class CustomerWarrantyController extends APIController
//error_log($_SERVER['HTTP_X_CATA_API_KEY']);
// TODO: add logging for the other scenarios
// check if warranty serial is there
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
@ -309,13 +315,14 @@ class CustomerWarrantyController extends APIController
'email' => $req->request->get('email'),
'invoice' => $req->request->get('invoice'),
];
$action = 'create/update';
$required_params = [
'first_name',
'last_name',
'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)
return $res;
@ -331,7 +338,7 @@ class CustomerWarrantyController extends APIController
$wcard_filename = $this->handlePictureUpload($warr_card, $upload_dir, $serial, 'wcard');
// 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
$em->flush();
@ -372,7 +379,8 @@ class CustomerWarrantyController extends APIController
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'));
@ -381,7 +389,7 @@ class CustomerWarrantyController extends APIController
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
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.');
}
@ -397,7 +405,7 @@ class CustomerWarrantyController extends APIController
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.');
}
@ -423,7 +431,7 @@ class CustomerWarrantyController extends APIController
$sap_bty = $em->getRepository(SAPBattery::class)->find($sku);
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.');
}
}
@ -436,7 +444,7 @@ class CustomerWarrantyController extends APIController
$vehicle = $em->getRepository(Vehicle::class)->find($vmake_id);
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.');
}
}
@ -453,7 +461,7 @@ class CustomerWarrantyController extends APIController
$date_pur_cust = DateTime::createFromFormat('Y-m-d', $req->request->get('date_purchase'));
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.');
}
}
@ -530,7 +538,7 @@ class CustomerWarrantyController extends APIController
$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
$data = [];

View file

@ -188,12 +188,13 @@ class WarrantyController extends APIController
'last_name' => $lname,
'mobile_number' => $mnum,
];
$action = 'create';
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
if ($msg)
{
$logger->logWarrantyInfo($log_data, $msg, $user_id);
$logger->logWarrantyInfo($log_data, $msg, $user_id, $action);
return new APIResponse(false, $msg);
}
@ -206,7 +207,7 @@ class WarrantyController extends APIController
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
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.');
}
@ -214,14 +215,14 @@ class WarrantyController extends APIController
$date_pur = DateTime::createFromFormat('Ymd', $date_pur_string);
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.');
}
// valid warranty 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.');
}
@ -229,7 +230,7 @@ class WarrantyController extends APIController
$plate = Warranty::cleanPlateNumber($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.');
}
@ -242,7 +243,7 @@ class WarrantyController extends APIController
$batt = $em->getRepository(SAPBattery::class)->find($sku);
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.');
}
}
@ -282,6 +283,7 @@ class WarrantyController extends APIController
}
catch (UniqueConstraintViolationException $e)
{
$logger->logWarrantyInfo($log_data, 'Duplicate serial encountered.', $user_id, $action);
return new APIResponse(false, 'Duplicate serial encountered.');
}
@ -291,7 +293,7 @@ class WarrantyController extends APIController
];
// 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);
}
@ -314,11 +316,12 @@ class WarrantyController extends APIController
'serial' => $serial,
'id' => $id,
];
$action = 'claim';
$msg = $this->checkRequiredParameters($req, $params);
if ($msg)
{
$logger->logWarrantyInfo($log_data, $msg, $user_id);
$logger->logWarrantyInfo($log_data, $msg, $user_id, $action);
return new APIResponse(false, $msg);
}
@ -326,14 +329,14 @@ class WarrantyController extends APIController
$warr = $em->getRepository(Warranty::class)->find($id);
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);
}
// warranty is not active
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.');
}
@ -343,7 +346,7 @@ class WarrantyController extends APIController
$check_warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $clean_serial]);
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.');
}
@ -371,7 +374,7 @@ class WarrantyController extends APIController
$em->flush();
// TODO: claim log
$logger->logWarrantyInfo($log_data, '', $user_id);
$logger->logWarrantyInfo($log_data, '', $user_id, $action);
return new APIResponse(true, 'Warranty claimed successfully.');
}

View file

@ -47,6 +47,12 @@ class WarrantyAPILog
*/
protected $errors;
// action
/**
* @ORM\Column(type="string", length=20)
*/
protected $action;
public function __construct()
{
$this->date_create = new DateTime();
@ -124,4 +130,16 @@ class WarrantyAPILog
$this->errors = new ArrayCollection();
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;
}
public function logWarrantyInfo($log_data, $error, $user_id)
public function logWarrantyInfo($log_data, $error, $user_id, $action)
{
$log_entry = new WarrantyAPILog();
@ -26,7 +26,8 @@ class WarrantyAPILogger
$log_entry->setApiUser($user_id)
->setErrors($err_array)
->setAllData($json_data);
->setAllData($json_data)
->setAction($action);
$this->em->persist($log_entry);
$this->em->flush();