Admin Workflow - API code refactor to fetch booking information

This commit is contained in:
Dillon Ngo
2025-01-03 19:40:00 +08:00
parent 44b7c7294b
commit abe1f3df2b
7 changed files with 228 additions and 79 deletions
@@ -19,7 +19,7 @@ class FetchBookingLogic extends AbstractControllerLogic
protected function notification():array {
return [
'title' => 'Retrieved Booking',
'message' => 'You have successfully retrieved a Address'
'message' => 'You have successfully retrieved a Booking'
];
}
@@ -58,4 +58,4 @@ class FetchBookingLogic extends AbstractControllerLogic
}
}
}
@@ -0,0 +1,100 @@
<?php
namespace App\Classes\Modules\Questionnaires\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Questionnaires\Standards\Rules\CanFetchQuestion;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Http\Resources\BookingBaseResource;
use App\Models\Booking;
use App\Models\KeyValuePair;
use Carbon\Carbon;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class FetchAdminWFBookingLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Retrieved Booking for Admin Workflow ',
'message' => 'You have successfully retrieved a Booking for Admin Workflow'
];
}
/** @var CanFetchQuestion */
private $canFetchQuestion;
/**
* FetchAdminWFBookingLogic constructor.
* @param CanFetchQuestion $canFetchQuestion
*/
public function __construct(CanFetchQuestion $canFetchQuestion)
{
$this->canFetchQuestion = $canFetchQuestion;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
$this->canFetchQuestion->passes();
$excludedBookingIds = KeyValuePair::where('owner_type', 'App\Models\Booking')
->where(function ($query) {
$query->where('key', '1688_admin_workflow_processed')
->orWhere(function ($query) {
$query->where('key', '1688_admin_workflow_processing')
->where('updated_at', '>', Carbon::now()->subHour());
});
})
->pluck('owner_id')
->filter(function ($value) {
return is_numeric($value);
})
->toArray();
$timeAgo = Carbon::now()->subMonths(6);
$serviceId = 4;
$booking = Booking::where('service_id', $serviceId)
->where('status', ApprovalStatus::APPROVED)
->whereNotIn('id', $excludedBookingIds)
->whereHas('bills', function ($query) {
$query->whereHas('groupTransaction', function ($query) {
$query->whereHas('group', function ($query) {
$query->whereDoesntHave('billGroup')->whereIn('issuer', [2]);
});
});
})
->where('created_at', '>=', $timeAgo)
->latest()
->first();
if(!$booking){
return responseJson(null, 'No booking found', 404);
}
markedProcessing($booking, '1688_admin_workflow_processing');
// return responseJson([
// 'passwords' => $booking->bank->holder_name ?? null,
// 'account_no' => $booking->bank->account_no ?? null,
// 'pin' => $booking->bank->bank_branch ?? null,
// 'holder_name' => $booking->bank->holder_name ?? null,
// 'booking' => $booking
// ]);
return $this->resourceResponse(new BookingBaseResource($booking));
}
}
@@ -0,0 +1,75 @@
<?php
namespace App\Classes\Modules\Questionnaires\ControllersLogic;
use App\Classes\General\Abstracts\AbstractControllerLogic;
use App\Classes\Modules\Questionnaires\Standards\Rules\CanFetchQuestion;
use App\Classes\ValueObjects\Constants\BookingAttributeNames;
use App\Http\Resources\BookingBaseResource;
use App\Models\Booking;
use ErrorException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class FetchAdminWFModelAttributesLogic extends AbstractControllerLogic
{
/**
* @return array
*/
protected function notification():array {
return [
'title' => 'Retrieved Model Attributes for Admin Workflow ',
'message' => 'You have successfully retrieved Model Attributes for Admin Workflow'
];
}
/** @var CanFetchQuestion */
private $canFetchQuestion;
/**
* FetchAdminWFModelAttributesLogic constructor.
* @param CanFetchQuestion $canFetchQuestion
*/
public function __construct(CanFetchQuestion $canFetchQuestion)
{
$this->canFetchQuestion = $canFetchQuestion;
}
/**
* @param Request $request
* @return JsonResponse
* @throws ErrorException
*/
public function logic(Request $request) : JsonResponse
{
$this->canFetchQuestion->passes();
$booking = Booking::find($request->route('booking_id'));
if (!$booking) {
return responseJson(null, 'No booking found', 404);
}
$attributes = $booking->modelAttributes()
->where('name', BookingAttributeNames::ORDER_REFERENCE_NO)
->get(['id', 'value'])
->map(fn ($attr) => $attr->only(['id', 'value']));
$transaction = $booking->bills()->first(); //cief todo: 74 - more than 1 record?
return responseJson([
'booking' => $booking,
'booking_attributes' => $attributes,
'reference' =>$transaction->owner->owner->marking,
'marking' => $transaction->owner->owner->company->reference,
'currency_rate' => $transaction->currency_rate,
'total_amount' =>$transaction->currency->short_code . ' ' . number_format((float)$transaction->amount, 2, '.', '')
]);
return $this->resourceResponse(new BookingBaseResource($booking));
}
}
@@ -1,61 +1,30 @@
<?php
namespace App\Http\Controllers\Questionnaires;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Booking;
use App\Models\KeyValuePair;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use App\Classes\Modules\Questionnaires\ControllersLogic\FetchAdminWFBookingLogic;
use App\Classes\Modules\Questionnaires\ControllersLogic\FetchAdminWFModelAttributesLogic;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class AdminWorkflowBaseController
{
public function fetchOldestOrder()
{
$excludedBookingIds = KeyValuePair::where('owner_type', 'App\Models\Booking')
->where(function ($query) {
$query->where('key', '1688_admin_workflow_processed')
->orWhere(function ($query) {
$query->where('key', '1688_admin_workflow_processing')
->where('updated_at', '>', Carbon::now()->subHour());
});
})
->pluck('owner_id')
->filter(function ($value) {
return is_numeric($value);
})
->toArray();
/**
* @param Request $request
* @param FetchQuestionV1AdminWFLogic $logic
* @return JsonResponse
*/
public function fetchBooking(Request $request, FetchAdminWFBookingLogic $logic): JsonResponse {
return $logic->execute($request);
}
$timeAgo = Carbon::now()->subMonths(6);
$serviceId = 4;
$booking = Booking::where('service_id', $serviceId)
->where('status', ApprovalStatus::APPROVED)
->whereNotIn('id', $excludedBookingIds)
->whereHas('bills', function ($query) {
$query->whereHas('groupTransaction', function ($query) {
$query->whereHas('group', function ($query) {
$query->whereDoesntHave('billGroup')->whereIn('issuer', [2]);
});
});
})
->where('created_at', '>=', $timeAgo)
->latest()
->first();
if(!$booking){
return responseJson(null, 'No booking found', 404);
}
markedProcessing($booking, '1688_admin_workflow_processing');
return responseJson([
'passwords' => $booking->bank->holder_name ?? null,
'account_no' => $booking->bank->account_no ?? null,
'pin' => $booking->bank->bank_branch ?? null,
'holder_name' => $booking->bank->holder_name ?? null,
'booking' => $booking
]);
/**
* @param Request $request
* @param FetchAdminWFModelAttributesLogic $logic
* @return JsonResponse
*/
public function fetchModelAttributes(Request $request, FetchAdminWFModelAttributesLogic $logic): JsonResponse {
return $logic->execute($request);
}
}
@@ -0,0 +1,26 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class BookingBaseResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function toArray($request)
{
return [
'id' => $this->id,
'company' => new CompanyResource($this->company),
'bank' => new BankResource($this->bank),
'marking' => $this->marking,
];
}
}
@@ -39,7 +39,7 @@
</span></h3>
</td>
<td>
<h3>{{ externalApiResponse.data.account_no }}</h3>
<h3>{{ externalApiResponse.data.booking.bank.account_no }}</h3>
</td>
</tr>
<tr>
@@ -48,7 +48,7 @@
</span></h3>
</td>
<td>
<h3>{{ externalApiResponse.data.holder_name }}</h3>
<h3>{{ externalApiResponse.data.booking.bank.holder_name }}</h3>
</td>
</tr>
<tr>
@@ -57,7 +57,7 @@
</span></h3>
</td>
<td>
<h3>{{ externalApiResponse.data.pin }}</h3>
<h3>{{ externalApiResponse.data.booking.bank.bank_branch }}</h3>
</td>
</tr>
</table>
+2 -23
View File
@@ -46,7 +46,8 @@ if (!function_exists('markedProcessing')) {
Route::group(['prefix' => 'admin-work-flow', 'as' => 'admin_work_flow.', 'namespace' => 'Questionnaires'], function () {
Route::get('/fetch-oldest-order', [AdminWorkflowBaseController::class, 'fetchOldestOrder'])->name('fetch_oldest_order');
Route::get('/fetch-booking', [AdminWorkflowBaseController::class, 'fetchBooking'])->name('fetch_oldest_order');
Route::get('{booking_id}/fetch-model-attributes', [AdminWorkflowBaseController::class, 'fetchModelAttributes'])->name('fetch_model_attributes');
Route::get('/fetch-pending-approved-po', function () {
$excludedBookingIds = KeyValuePair::where('owner_type', 'App\Models\Booking')
@@ -122,28 +123,6 @@ Route::group(['prefix' => 'admin-work-flow', 'as' => 'admin_work_flow.', 'namesp
})->name('fetch_pending_fill_po');
Route::get('{booking_id}/fetch-model-attributes', function ($bookingId) {
$booking = Booking::find($bookingId);
if (!$booking) {
return responseJson(null, 'No booking found', 404);
}
$attributes = $booking->modelAttributes()
->where('name', BookingAttributeNames::ORDER_REFERENCE_NO)
->get(['id', 'value'])
->map(fn ($attr) => $attr->only(['id', 'value']));
$transaction = $booking->bills()->first(); //cief todo: 74 - more than 1 record?
return responseJson([
'booking' => $booking,
'booking_attributes' => $attributes,
'reference' =>$transaction->owner->owner->marking,
'marking' => $transaction->owner->owner->company->reference,
'currency_rate' => $transaction->currency_rate,
'total_amount' =>$transaction->currency->short_code . ' ' . number_format((float)$transaction->amount, 2, '.', '')
]);
})->name('fetch_model_attributes');
// Route::post('/add-workflow-timestamp', function (Request $request) {