resq/src/Controller/CustomerAppAPI/ApiController.php
2024-10-29 17:22:29 +08:00

188 lines
6.2 KiB
PHP

<?php
namespace App\Controller\CustomerAppAPI;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\HttpKernel\KernelInterface;
use Catalyst\ApiBundle\Controller\ApiController as BaseApiController;
use Catalyst\ApiBundle\Component\Response as ApiResponse;
use App\Ramcar\JOStatus;
use App\Entity\Warranty;
use App\Entity\JobOrder;
use App\Entity\CustomerSession;
use App\Service\PayMongoConnector;
use DateTime;
class ApiController extends BaseApiController
{
protected $em;
protected $session;
public function __construct(EntityManagerInterface $em, KernelInterface $kernel)
{
$this->session = new CustomerSession; // NOTE: original was null
$this->em = $em;
// load env file
$dotenv = new Dotenv();
$dotenv->loadEnv($kernel->getProjectDir() . '/.env');
}
protected function debugRequest(Request $req)
{
$all = $req->request->all();
error_log(print_r($all, true));
}
protected function hasMissingParams(Request $req, $params = [])
{
return $this->checkRequiredParameters($req, $params);
}
protected function validateSession($session_key)
{
// check if the session exists
$session = $this->em->getRepository(CustomerSession::class)->find($session_key);
if ($session === null) {
return false;
}
$this->session = $session;
return true;
}
protected function validateRequest(Request $req, $params = [])
{
$error = $this->hasMissingParams($req, $params);
$session_key = $req->query->get('session_key');
if (!$error) {
if (empty($session_key) || !$this->validateSession($session_key)) {
$error = 'Invalid session key.';
}
}
// update session timestamp
$this->updateSessionTimestamp();
return [
'is_valid' => !$error,
'error' => $error,
];
}
protected function findWarranty($plate_number)
{
// NOTE: Modify the search for the latest warranty. This seems hacky.
// get latest warranty using plate number
$warranty_results = $this->em->getRepository(Warranty::class)->findBy(
['plate_number' => $plate_number],
['date_create' => 'desc']
);
$warr = [];
// check if warranty_results is empty
if (empty($warranty_results)) {
/*
$res->setError(true)
->setErrorMessage('No warranty found for plate number');
return $res->getReturnResponse();
*/
return $warr;
}
// get first entry
$warranty = current($warranty_results);
// check for null values for battery and date claim and date expire
$batt_model = '';
$batt_size = '';
$sap_batt = '';
$claim_date = '';
$expiry_date = '';
if (!(is_null($warranty->getBatteryModel()))) {
$batt_model = $warranty->getBatteryModel()->getName();
}
if (!(is_null($warranty->getBatterySize()))) {
$batt_size = $warranty->getBatterySize()->getName();
}
if (!(is_null($warranty->getSAPBattery()))) {
$sap_batt = $warranty->getSAPBattery()->getID();
}
if (!(is_null($warranty->getDateClaim()))) {
$claim_date = $warranty->getDateClaim()->format("d M Y");
}
if (!(is_null($warranty->getDateExpire()))) {
$expiry_date = $warranty->getDateExpire()->format("d M Y");
}
$warr[] = [
'id' => $warranty->getID(),
'serial' => $warranty->getSerial(),
'warranty_class' => $warranty->getWarrantyClass(),
'plate_number' => $warranty->getPlateNumber(),
'first_name' => $warranty->getFirstName(),
'last_name' => $warranty->getLastName(),
'mobile_number' => $warranty->getMobileNumber(),
'battery_model' => $batt_model,
'battery_size' => $batt_size,
'sap_battery' => $sap_batt,
'status' => $warranty->getStatus(),
'date_create' => $warranty->getDateCreate()->format("d M Y g:i A"),
'date_purchase' => $warranty->getDatePurchase()->format("d M Y"),
'date_expire' => $expiry_date,
'date_claim' => $claim_date,
'claim_from' => $warranty->getClaimedFrom(),
'is_activated' => $warranty->isActivated() ? 1 : 0,
];
return $warr;
}
protected function getBatteryImageURL($req, $batt)
{
// TODO: workaround for now, we get static image of battery based on model name
$filename = trim(strtolower($batt->getModel()->getName())) . '_mobile.jpg';
$filename = str_replace(" ", "_", $filename);
$file_path = $req->getSchemeAndHttpHost() . $this->generateUrl('static_battery_image') . '/' . $filename;
return $file_path;
}
protected function getOngoingJobOrders($cust)
{
$ongoing_jos = $this->em->getRepository(JobOrder::class)->findBy([
'customer' => $cust,
'status' => [JOStatus::PENDING, JOStatus::RIDER_ASSIGN, JOStatus::IN_TRANSIT, JOStatus::ASSIGNED, JOStatus::IN_PROGRESS],
], ['date_schedule' => 'desc']);
return $ongoing_jos;
}
protected function getGeoErrorMessage()
{
return 'Our services are currently limited to some areas in Metro Manila, Baguio, Batangas, Laguna, Cavite, Pampanga, and Palawan. We will update you as soon as we are available in your area. Thank you for understanding. Keep safe!';
}
protected function initializeSubscriptionPayMongoConnector(PayMongoConnector &$pm)
{
$pm->initialize(
$this->getParameter('subscription_paymongo_public_key'),
$this->getParameter('subscription_paymongo_secret_key'),
);
}
protected function updateSessionTimestamp()
{
error_log('Updating session timestamp');
error_log('Session ID: '. $this->session->getID());
$this->session->setDateLatestActivity(new DateTime());
$this->em->flush();
}
}