Fix issue with subscription not being marked as active due to listener conflict, add endpoint for unfulfilled sub count #799

This commit is contained in:
Ramon Gutierrez 2024-10-24 06:07:36 +08:00
parent 8c83393b0c
commit 874c35bfff
4 changed files with 57 additions and 18 deletions

View file

@ -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]

View file

@ -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)
{

View file

@ -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(),

View file

@ -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,