Add logging for creation of warranty. #555
This commit is contained in:
parent
24806d8b42
commit
18b0417403
4 changed files with 233 additions and 5 deletions
|
|
@ -284,3 +284,8 @@ services:
|
|||
App\Service\HubFilterLogger:
|
||||
arguments:
|
||||
$em: "@doctrine.orm.entity_manager"
|
||||
|
||||
# warranty api logger
|
||||
App\Service\WarrantyAPILogger:
|
||||
arguments:
|
||||
$em: "@doctrine.orm.entity_manager"
|
||||
|
|
|
|||
|
|
@ -20,6 +20,9 @@ use App\Entity\PrivacyPolicy;
|
|||
use App\Entity\Customer;
|
||||
use App\Entity\CustomerVehicle;
|
||||
use App\Entity\Vehicle;
|
||||
use App\Entity\WarrantyAPILog;
|
||||
|
||||
use App\Service\WarrantyAPILogger;
|
||||
|
||||
use App\Ramcar\NameValue;
|
||||
use App\Ramcar\WarrantyClass;
|
||||
|
|
@ -142,7 +145,7 @@ class WarrantyController extends APIController
|
|||
return new APIResponse(true, 'Warranties found.', $data);
|
||||
}
|
||||
|
||||
public function register(Request $req, EntityManagerInterface $em)
|
||||
public function register(Request $req, EntityManagerInterface $em, WarrantyAPILogger $logger)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('warranty.register.battery', null, 'No access.');
|
||||
|
||||
|
|
@ -159,10 +162,6 @@ class WarrantyController extends APIController
|
|||
'battery_size_id',
|
||||
*/
|
||||
];
|
||||
$msg = $this->checkRequiredParameters($req, $params);
|
||||
error_log('msg - ' . $msg);
|
||||
if ($msg)
|
||||
return new APIResponse(false, $msg);
|
||||
|
||||
$serial = $req->request->get('serial');
|
||||
$date_expire_string = $req->request->get('date_expire');
|
||||
|
|
@ -175,6 +174,29 @@ class WarrantyController extends APIController
|
|||
$lname = $req->request->get('last_name', null);
|
||||
$mnum = $req->request->get('mobile_number', null);
|
||||
|
||||
// set up information for logging
|
||||
// get user from header
|
||||
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
|
||||
$log_data = [
|
||||
'serial' => $serial,
|
||||
'date_expire' => $date_expire_string,
|
||||
'date_pur_string' => $date_pur_string,
|
||||
'warranty_class' => $warr_class,
|
||||
'plate_number' => $plate,
|
||||
'sku' => $sku,
|
||||
'first_name' => $fname,
|
||||
'last_name' => $lname,
|
||||
'moblie_number' => $mnum,
|
||||
];
|
||||
|
||||
$msg = $this->checkRequiredParameters($req, $params);
|
||||
error_log('msg - ' . $msg);
|
||||
if ($msg)
|
||||
{
|
||||
$logger->logWarrantyInfo($log_data, $msg, $user_id);
|
||||
return new APIResponse(false, $msg);
|
||||
}
|
||||
|
||||
/*
|
||||
$bmodel_id = $req->request->get('battery_model_id');
|
||||
$bsize_id = $req->request->get('battery_size_id');
|
||||
|
|
@ -183,21 +205,33 @@ class WarrantyController extends APIController
|
|||
// wrong date expire format
|
||||
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
|
||||
if ($date_expire === false)
|
||||
{
|
||||
$logger->logWarrantyInfo($log_data, 'Wrong date format: date_expire.', $user_id);
|
||||
return new APIResponse(false, 'Wrong date format: date_expire.');
|
||||
}
|
||||
|
||||
// wrong date purchase format
|
||||
$date_pur = DateTime::createFromFormat('Ymd', $date_pur_string);
|
||||
if ($date_pur === false)
|
||||
{
|
||||
$logger->logWarrantyInfo($log_data, 'Wrong date format: date_purchase', $user_id);
|
||||
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);
|
||||
return new APIResponse(false, 'Invalid warranty class.');
|
||||
}
|
||||
|
||||
// plate number
|
||||
$plate = Warranty::cleanPlateNumber($plate);
|
||||
if (!$plate)
|
||||
{
|
||||
$logger->logWarrantyInfo($log_data, 'Invalid plate number.', $user_id);
|
||||
return new APIResponse(false, 'Invalid plate number.');
|
||||
}
|
||||
|
||||
// check if sku is blank
|
||||
if ((empty($sku)) || ($sku == null))
|
||||
|
|
@ -207,7 +241,10 @@ class WarrantyController extends APIController
|
|||
// battery
|
||||
$batt = $em->getRepository(SAPBattery::class)->find($sku);
|
||||
if ($batt == null)
|
||||
{
|
||||
$logger->logWarrantyInfo($log_data, 'Invalid battery SKU.', $user_id);
|
||||
return new APIResponse(false, 'Invalid battery SKU.');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -253,6 +290,9 @@ class WarrantyController extends APIController
|
|||
'warranty' => $this->generateWarrantyData($warr),
|
||||
];
|
||||
|
||||
// log creation of warranty data
|
||||
$logger->logWarrantyInfo($log_data, '', $user_id);
|
||||
|
||||
return new APIResponse(true, 'Warranty registered.', $data);
|
||||
}
|
||||
|
||||
|
|
@ -705,4 +745,25 @@ class WarrantyController extends APIController
|
|||
return $customers;
|
||||
}
|
||||
|
||||
protected function logWarrantyInfo(EntityManagerInterface $em, $log_data, $error)
|
||||
{
|
||||
// get user from header
|
||||
$user_id = $_SERVER['HTTP_X_CATA_API_KEY'];
|
||||
|
||||
$log_entry = new WarrantyAPILog();
|
||||
|
||||
$err_aray = [];
|
||||
$err_array[] = $error;
|
||||
|
||||
$json_data = json_encode($log_data);
|
||||
|
||||
$log_entry->setApiUser($user_id)
|
||||
->setErrors($err_array)
|
||||
->setAllData($json_data);
|
||||
|
||||
$em->persist($log_entry);
|
||||
$em->flush();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
127
src/Entity/WarrantyAPILog.php
Normal file
127
src/Entity/WarrantyAPILog.php
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
use DateTime;
|
||||
|
||||
/**
|
||||
* @ORM\Entity
|
||||
* @ORM\Table(name="warranty_api_log")
|
||||
*/
|
||||
class WarrantyAPILog
|
||||
{
|
||||
// unique id
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(type="integer")
|
||||
* @ORM\GeneratedValue(strategy="AUTO")
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
// date created
|
||||
/**
|
||||
* @ORM\Column(type="datetime")
|
||||
*/
|
||||
protected $date_create;
|
||||
|
||||
// user that created warranty
|
||||
/**
|
||||
* @ORM\Column(type="string", length=32)
|
||||
*/
|
||||
protected $api_user;
|
||||
|
||||
// data sent
|
||||
/**
|
||||
* @ORM\Column(type="json")
|
||||
*/
|
||||
protected $all_data;
|
||||
|
||||
// errors
|
||||
/**
|
||||
* @ORM\Column(type="array", nullable=true)
|
||||
*/
|
||||
protected $errors;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->date_create = new DateTime();
|
||||
$this->errors = new ArrayCollection();
|
||||
$this->all_data = [];
|
||||
}
|
||||
|
||||
public function getID()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setDateCreate(DateTime $date_create)
|
||||
{
|
||||
$this->date_create = $date_create;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDateCreate()
|
||||
{
|
||||
return $this->date_create;
|
||||
}
|
||||
|
||||
public function setApiUser($api_user)
|
||||
{
|
||||
$this->api_user = $api_user;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getApiUser()
|
||||
{
|
||||
return $this->api_user;
|
||||
}
|
||||
|
||||
public function addAllData($id, $value)
|
||||
{
|
||||
$this->all_data[$id] = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAllData($all_data)
|
||||
{
|
||||
$this->all_data = $all_data;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAllData($id)
|
||||
{
|
||||
// return null if we don't have it
|
||||
if (!isset($this->all_data[$id]))
|
||||
return null;
|
||||
|
||||
return $this->all_data[$id];
|
||||
}
|
||||
|
||||
public function getErrors()
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
public function setErrors(array $errors)
|
||||
{
|
||||
$this->errors = new ArrayCollection();
|
||||
|
||||
foreach ($errors as $error)
|
||||
{
|
||||
$this->errors->add($error);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function clearErrors()
|
||||
{
|
||||
$this->errors = new ArrayCollection();
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
35
src/Service/WarrantyAPILogger.php
Normal file
35
src/Service/WarrantyAPILogger.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
use App\Entity\WarrantyAPILog;
|
||||
|
||||
class WarrantyAPILogger
|
||||
{
|
||||
protected $em;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
public function logWarrantyInfo($log_data, $error, $user_id)
|
||||
{
|
||||
$log_entry = new WarrantyAPILog();
|
||||
|
||||
$err_aray = [];
|
||||
$err_array[] = $error;
|
||||
|
||||
$json_data = json_encode($log_data);
|
||||
|
||||
$log_entry->setApiUser($user_id)
|
||||
->setErrors($err_array)
|
||||
->setAllData($json_data);
|
||||
|
||||
$this->em->persist($log_entry);
|
||||
$this->em->flush();
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue