Add controller and routes for capi customer warranty #540

This commit is contained in:
Kendrick Chan 2021-03-17 00:09:51 +08:00
parent 7e7c93ce55
commit 9f00d8ded7
2 changed files with 214 additions and 0 deletions

View file

@ -149,3 +149,9 @@ capi_customer_register:
path: /capi/quick_registration
controller: App\Controller\CAPI\CustomerController::register
methods: [POST]
# customer warranty api
capi_cwarr_check:
path: /capi/customer_warranty/{serial}
controller: App\Controller\CAPI\CustomerWarrantyController::check
methods: [GET]

View file

@ -0,0 +1,208 @@
<?php
namespace App\Controller\CAPI;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Query;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\WarrantySerial;
use App\Entity\Warranty;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
use App\Entity\SAPBattery;
use App\Entity\SAPBatterySize;
use App\Entity\SAPBatteryBrand;
use App\Entity\PrivacyPolicy;
use App\Entity\Customer;
use App\Entity\CustomerVehicle;
use App\Entity\Vehicle;
use App\Ramcar\NameValue;
use App\Ramcar\WarrantyClass;
use App\Ramcar\WarrantyStatus;
use App\Ramcar\FuelType;
use App\Ramcar\VehicleStatusCondition;
use DateTime;
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
// third party API
class CustomerWarrantyController extends APIController
{
protected $acl_gen;
public function __construct(ACLGenerator $acl_gen)
{
$this->acl_gen = $acl_gen;
}
protected function checkMissingParameters(Request $req, $params = [])
{
$missing = [];
// check if parameters are there
foreach ($params as $param)
{
if ($req->getMethod() == 'GET')
{
$check = $req->query->get($param);
if (empty($check))
$missing[] = $param;
}
else if ($req->getMethod() == 'POST')
{
$check = $req->request->get($param);
if (empty($check))
$missing[] = $param;
}
else
return $params;
}
return $missing;
}
protected function checkRequiredParams(Request $req, $params)
{
// check required parameters
$missing = $this->checkMissingParameters($req, $params);
if (count($missing) > 0)
{
$miss_string = implode(', ', $missing);
return new APIResponse(false, 'Missing parameter(s): ' . $miss_string);
}
return true;
}
protected function cleanSerial($serial)
{
return trim(strtoupper($serial));
}
public function check($serial, EntityManagerInterface $em, Request $req)
{
// check required parameters
$required_params = [];
$res = $this->checkRequiredParams($req, $required_params);
if (!$res)
return $res;
// check if warranty serial is there
$warr_serial = $em->getRepository(WarrantySerial::class)->find($serial);
$warr = $em->getRepository(Warranty::class)->findOneBy(['serial' => $serial]);
$batt = null;
$is_registered = false;
if ($warr_serial == null)
{
return new APIResponse(false, 'Invalid warranty serial code.');
}
$today = new DateTime();
// if we have a warranty entry for the serial already
if ($warr != null)
{
$warr_plate = $warr->getPlateNumber();
$is_registered = true;
// null mobile number should be blank string instead
if ($warr->getMobileNumber() == null)
$mobile_num = '';
else
$mobile_num = $warr->getMobileNumber();
// purchase date of customer
if ($warr->getDatePurchaseCustomer() != null)
$date_purchase_cust = $warr->getDatePurchaseCustomer()->format('Y-m-d');
else
$date_purchase_cust = $today->format('Y-m-d');
// invoice
if ($warr->getFileInvoice() != null)
$invoice_url = $req->getSchemeAndHttpHost() . '/warranty_uploads/' . $warr->getFileInvoice();
else
$invoice_url = '';
// warranty card
if ($warr->getFileWarrantyCard() != null)
$warr_card_url = $req->getSchemeAndHttpHost() . '/warranty_uploads/' . $warr->getFileWarrantyCard();
else
$warr_card_url = '';
$customer = [
'first_name' => $warr->getFirstName(),
'last_name' => $warr->getLastName(),
'mobile_number' => $mobile_num,
'plate_number' => $warr_plate,
'email' => $warr->getEmail(),
];
$other_data = [
'odometer' => $warr->getOdometer(),
'date_purchase' => $date_purchase_cust,
'invoice' => $invoice_url,
'warr_card' => $warr_card_url,
];
}
else
{
$customer = [
'first_name' => '',
'last_name' => '',
'mobile_number' => '',
'plate_number' => '',
'email' => '',
];
$other_data = [
'odometer' => 0,
'date_purchase' => $today->format('Y-m-d'),
'invoice' => '',
'warr_card' => '',
];
}
$sku = $warr_serial->getSKU();
$batt = $em->getRepository(SAPBattery::class)->find($sku);
// TODO: put this in a config file
$image_url = $req->getSchemeAndHttpHost() . '/battery/generic.png';
if ($batt != null)
{
$battery = [
'brand' => $batt->getBrand()->getName(),
'size' => $batt->getSize()->getName(),
'image_url' => $image_url,
];
}
else
{
$battery = [
'brand' => '',
'size' => '',
'image_url' => '',
];
}
// populate data
$data = [
'is_valid' => true,
'is_registered' => $is_registered,
'customer' => $customer,
'battery' => $battery,
'odometer' => $other_data['odometer'],
'invoice' => $other_data['invoice'],
'warr_card' => $other_data['warr_card'],
'date_purchase' => $other_data['date_purchase'],
];
return new APIResponse(true, 'Warranty found.', $data);
}
}