From 874c35bfff1f2cee0186569a3f1b7ac87bc2b280 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Thu, 24 Oct 2024 06:07:36 +0800 Subject: [PATCH] Fix issue with subscription not being marked as active due to listener conflict, add endpoint for unfulfilled sub count #799 --- config/routes/apiv2.yaml | 7 ++- .../CustomerAppAPI/SubscriptionController.php | 53 ++++++++++++++----- .../CustomerAppAPI/VehicleController.php | 10 ++-- .../GatewayTransactionListener.php | 5 ++ 4 files changed, 57 insertions(+), 18 deletions(-) diff --git a/config/routes/apiv2.yaml b/config/routes/apiv2.yaml index 1aa86e16..2278c173 100644 --- a/config/routes/apiv2.yaml +++ b/config/routes/apiv2.yaml @@ -341,6 +341,11 @@ apiv2_subscription_create: controller: App\Controller\CustomerAppAPI\SubscriptionController::createSubscription methods: [POST] +apiv2_subscription_unfulfilled_list: + path: /apiv2/subscription/unfulfilled + controller: App\Controller\CustomerAppAPI\SubscriptionController::getUnfulfilledSubs + methods: [GET] + apiv2_subscription_finalize: path: /apiv2/subscription/{id}/finalize controller: App\Controller\CustomerAppAPI\SubscriptionController::finalizeSubscription @@ -354,4 +359,4 @@ apiv2_subscription_finalize: #apiv2_subscription_activate: # path: /apiv2/subscription/{id}/activate # controller: App\Controller\CustomerAppAPI\SubscriptionController::activateSubscription -# methods: [POST] +# methods: [POST] \ No newline at end of file diff --git a/src/Controller/CustomerAppAPI/SubscriptionController.php b/src/Controller/CustomerAppAPI/SubscriptionController.php index d6efb6f8..b8396649 100644 --- a/src/Controller/CustomerAppAPI/SubscriptionController.php +++ b/src/Controller/CustomerAppAPI/SubscriptionController.php @@ -249,22 +249,11 @@ class SubscriptionController extends ApiController return new ApiResponse(false, 'Error retrieving payment intent. Please try again later.'); } - // if the paymongo sub is active, and the payment was successful, update the internal subscription status + // if the paymongo sub is active, and the payment was successful, update the gateway transaction record, which will also activate the sub via listener if ( $sub_obj->getStatus() === SubscriptionStatus::PENDING && $pi['response']['data']['attributes']['status'] === 'succeeded' ) { - $now = new DateTime(); - - $sub_start_date = $now; - $sub_end_date = clone $sub_start_date; - $sub_end_date->modify('+' . $this->getParameter('subscription_months') . ' month'); - - $sub_obj->setStatus(SubscriptionStatus::ACTIVE) - ->setDateStart($sub_start_date) - ->setDateEnd($sub_end_date); - - // update the gateway transaction record as well $gt = $this->em->getRepository(GatewayTransaction::class)->findOneBy([ 'status' => TransactionStatus::PENDING, 'ext_transaction_id' => $pm_sub['response']['data']['attributes']['latest_invoice']['id'], @@ -274,7 +263,7 @@ class SubscriptionController extends ApiController } $gt->setStatus(TransactionStatus::PAID) - ->setDatePay($now); + ->setDatePay(new DateTime()); $this->em->flush(); } @@ -285,6 +274,44 @@ class SubscriptionController extends ApiController ]); } + public function getUnfulfilledSubs(Request $req) + { + // check requirements + $validity = $this->validateRequest($req); + + if (!$validity['is_valid']) { + return new ApiResponse(false, $validity['error']); + } + + // get customer + $cust = $this->session->getCustomer(); + if ($cust == null) { + return new ApiResponse(false, 'No customer information found.'); + } + + // NOTE: this functions like an outer join as far as DQL is concerned + // get all customer vehicles for the current customer that do not have a JO + $sql = 'SELECT COUNT(cv.id) AS count + FROM App\Entity\CustomerVehicle cv + JOIN App\Entity\Subscription s WITH s.customer_vehicle = cv AND s.status = :status + LEFT JOIN App\Entity\JobOrder jo WITH jo.subscription = s + WHERE jo.id IS NULL + AND cv.customer = :customer'; + + $query = $this->em->createQuery($sql) + ->setParameters([ + 'status' => SubscriptionStatus::ACTIVE, + 'customer' => $cust, + ]); + + $count = $query->getSingleScalarResult(); + + // response + return new ApiResponse(true, '', [ + 'count' => $count, + ]); + } + /* public function getPaymentIntent(Request $req, $pid, PayMongoConnector $pm) { diff --git a/src/Controller/CustomerAppAPI/VehicleController.php b/src/Controller/CustomerAppAPI/VehicleController.php index 3c62ebec..dfde6985 100644 --- a/src/Controller/CustomerAppAPI/VehicleController.php +++ b/src/Controller/CustomerAppAPI/VehicleController.php @@ -435,12 +435,14 @@ class VehicleController extends ApiController if ($cv->getName() != null) $cv_name = $cv->getName(); + $vehicle = $cv->getVehicle(); + $row = [ 'cv_id' => $cv->getID(), - 'mfg_id' => $cv->getVehicle()->getManufacturer()->getID(), - 'make_id' => $cv->getVehicle()->getID(), - 'mfg_name' => $cv->getVehicle()->getManufacturer()->getName(), - 'make_name' => $cv->getVehicle()->getMake(), + 'mfg_id' => $vehicle->getManufacturer()->getID(), + 'make_id' => $vehicle->getID(), + 'mfg_name' => $vehicle->getManufacturer()->getName(), + 'make_name' => $vehicle->getMake(), 'name' => $cv_name, 'plate_num' => $cv->getPlateNumber(), 'model_year' => $cv->getModelYear(), diff --git a/src/EntityListener/GatewayTransactionListener.php b/src/EntityListener/GatewayTransactionListener.php index 1034dcd0..1aefd564 100644 --- a/src/EntityListener/GatewayTransactionListener.php +++ b/src/EntityListener/GatewayTransactionListener.php @@ -101,6 +101,7 @@ class GatewayTransactionListener // TODO: put subscription management into a service $sub = $this->em->getRepository(Subscription::class)->findOneBy([ 'ext_api_id' => $sub_id, + 'status' => SubscriptionStatus::PENDING, ]); if (empty($sub)) { @@ -119,6 +120,10 @@ class GatewayTransactionListener $this->em->flush(); + error_log("Subscription has been set to active via listener"); + error_log("SUB ID: " . $sub->getID()); + error_log($sub->getStatus()); + // send notification about subscription $this->fcmclient->sendSubscriptionEvent( $sub,