mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
236 lines
8.3 KiB
PHP
236 lines
8.3 KiB
PHP
<?php
|
|
|
|
use App\Classes\Modules\Accounts\DataTransferObjects\KeyValuePairObject;
|
|
use App\Classes\Modules\Accounts\Services\CreatesKeyValuePair;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Classes\ValueObjects\Constants\BookingAttributeNames;
|
|
use App\Http\Controllers\Questionnaires\AdminWorkflowBaseController;
|
|
use App\Http\Resources\BookingResource;
|
|
use App\Models\Booking;
|
|
use App\Models\KeyValuePair;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
// Generalized JSON response function
|
|
|
|
if (!function_exists('responseJson')) {
|
|
function responseJson($data = null, $message = null, $status = 200)
|
|
{
|
|
$response = ['message' => $message];
|
|
|
|
if ($data !== null) {
|
|
$response['data'] = $data;
|
|
}
|
|
|
|
return response()->json($response, $status);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('markedProcessing')) {
|
|
function markedProcessing($model, $key){
|
|
$kvp = $model->attributesKVP()->where('key', $key)->first();
|
|
|
|
if($kvp){
|
|
$kvp->delete();
|
|
$kvp = null;
|
|
}
|
|
|
|
if(!$kvp){
|
|
$keyValuePairObject = new KeyValuePairObject($key, true);
|
|
(App()->make(CreatesKeyValuePair::class))->execute($model, $keyValuePairObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
Route::group(['prefix' => 'admin-work-flow', 'as' => 'admin_work_flow.', 'namespace' => 'Questionnaires'], function () {
|
|
|
|
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')
|
|
->where(function ($query) {
|
|
$query->where('key', 'approve_po_admin_workflow_processed')
|
|
->orWhere(function ($query) {
|
|
$query->where('key', 'approve_po_admin_workflow_processing')
|
|
->where('updated_at', '>', Carbon::now()->subHour());
|
|
});
|
|
})
|
|
->pluck('owner_id')
|
|
->filter(function ($value) {
|
|
return is_numeric($value);
|
|
})
|
|
->toArray();
|
|
|
|
$booking = Booking::with('transactions')
|
|
->where('service_id', 4)
|
|
->where('status', ApprovalStatus::APPROVED)
|
|
->whereNotIn('id', $excludedBookingIds)
|
|
->whereHas('transactions', fn ($query) => $query->where('type', TransactionType::PURCHASE_ORDER)->where('status', '<', ApprovalStatus::APPROVED))
|
|
->first();
|
|
|
|
if(!$booking){
|
|
return responseJson(null, 'No booking found', 404);
|
|
}
|
|
|
|
markedProcessing($booking, 'approve_po_admin_workflow_processing');
|
|
|
|
$result = new BookingResource($booking);
|
|
return responseJson([
|
|
'booking' => $result,
|
|
]);
|
|
|
|
// return $booking ? responseJson(new BookingResource($booking)) : responseJson(null, 'No booking found', 404);
|
|
})->name('fetch_pending_approve_po');
|
|
|
|
|
|
Route::get('/fetch-pending-fill-po', function () {
|
|
$excludedBookingIds = KeyValuePair::where('owner_type', 'App\Models\Booking')
|
|
->where(function ($query) {
|
|
$query->where('key', 'fill_po_admin_workflow_processed')
|
|
->orWhere(function ($query) {
|
|
$query->where('key', 'fill_po_admin_workflow_processing')
|
|
->where('updated_at', '>', Carbon::now()->subHour());
|
|
});
|
|
})
|
|
->pluck('owner_id')
|
|
->filter(function ($value) {
|
|
return is_numeric($value);
|
|
})
|
|
->toArray();
|
|
|
|
$booking = Booking::with('transactions')
|
|
->where('service_id', 4)
|
|
->where('status', ApprovalStatus::APPROVED)
|
|
->whereNotIn('id', $excludedBookingIds)
|
|
->whereDoesntHave('transactions', fn ($query) => $query->where('type', TransactionType::PURCHASE_ORDER))
|
|
->first();
|
|
|
|
if(!$booking){
|
|
return responseJson(null, 'No booking found', 404);
|
|
}
|
|
|
|
markedProcessing($booking, 'fill_po_admin_workflow_processing');
|
|
|
|
$result = new BookingResource($booking);
|
|
return responseJson([
|
|
'booking' => $result,
|
|
]);
|
|
|
|
// return $booking ? responseJson(new BookingResource($booking)) : responseJson(null, 'No booking found', 404);
|
|
})->name('fetch_pending_fill_po');
|
|
|
|
|
|
|
|
|
|
// Route::post('/add-workflow-timestamp', function (Request $request) {
|
|
// $validator = Validator::make($request->all(), [
|
|
// 'current_node' => 'required|string',
|
|
// 'next_node' => 'required|string',
|
|
// 'seconds' => 'required|integer',
|
|
// 'user_id' => 'required|integer|exists:users,id',
|
|
// 'session_id' => 'required|string',
|
|
// ]);
|
|
|
|
// if ($validator->fails()) {
|
|
// throw new RequestValidationException($validator->messages()->first());
|
|
// }
|
|
|
|
// $workflowTimestamp = WorkflowTimestamp::create($validator->validated());
|
|
|
|
// return responseJson($workflowTimestamp, 'Workflow timestamp created successfully', 201);
|
|
// })->name('add_workflow_timestamp');
|
|
|
|
|
|
// Route::post('/create-1688-login-issue-remark', function (Request $request) {
|
|
// $validator = Validator::make($request->all(), [
|
|
// 'booking_id' => 'required',
|
|
// 'remark' => 'required',
|
|
// 'user_id' => 'required',
|
|
// ]);
|
|
|
|
// if ($validator->fails()) {
|
|
// throw new RequestValidationException($validator->messages()->first());
|
|
// }
|
|
|
|
// $booking = Booking::find($request->booking_id);
|
|
|
|
// if (!$booking) {
|
|
// return responseJson(null, 'Booking not found', 404);
|
|
// }
|
|
|
|
// $remark = new Remark([
|
|
// 'commenter_id' => $request->user_id,
|
|
// 'content' => $request->remark,
|
|
// 'owner_type' => get_class($booking),
|
|
// 'owner_id' => $booking->id,
|
|
// ]);
|
|
|
|
// $remark->save();
|
|
|
|
// return responseJson($remark, 'Remark created successfully', 201);
|
|
// })->name('create_1688_login_issue_remark');
|
|
|
|
// Route::post('/create_issue_remark', function (Request $request) {
|
|
// $validator = Validator::make($request->all(), [
|
|
// 'booking_id' => 'required',
|
|
// 'remark' => 'required',
|
|
// 'user_id' => 'required',
|
|
// ]);
|
|
|
|
// if ($validator->fails()) {
|
|
// throw new RequestValidationException($validator->messages()->first());
|
|
// }
|
|
|
|
// $booking = Booking::find($request->booking_id);
|
|
|
|
|
|
// if (!$booking) {
|
|
// return responseJson(null, 'Booking not found', 404);
|
|
// }
|
|
|
|
// $remark = new Remark([
|
|
// 'commenter_id' => $request->user_id,
|
|
// 'content' => $request->remark,
|
|
// 'owner_type' => get_class($booking),
|
|
// 'owner_id' => $booking->id,
|
|
// ]);
|
|
|
|
// $remark->save();
|
|
|
|
// return responseJson($remark, 'Remark created successfully', 201);
|
|
// })->name('create_issue_remark');
|
|
|
|
// Route::post('/create_po_issue_remark', function (Request $request) {
|
|
// $validator = Validator::make($request->all(), [
|
|
// 'booking_id' => 'required',
|
|
// 'remark' => 'required',
|
|
// 'user_id' => 'required',
|
|
// ]);
|
|
|
|
// if ($validator->fails()) {
|
|
// return response()->json(['message' => $validator->messages()->first()], 400); // Return validation error
|
|
// }
|
|
|
|
// $booking = Booking::find($request->booking_id);
|
|
|
|
// if (!$booking) {
|
|
// return response()->json(['message' => 'Booking not found'], 404);
|
|
// }
|
|
|
|
// $remark = new Remark([
|
|
// 'commenter_id' => $request->user_id,
|
|
// 'content' => $request->remark,
|
|
// 'owner_type' => get_class($booking),
|
|
// 'owner_id' => $booking->id,
|
|
// ]);
|
|
|
|
// $remark->save();
|
|
|
|
// return response()->json(['data' => $remark, 'message' => 'Remark created successfully'], 201);
|
|
// })->name('create_po_issue_remark');
|
|
|
|
});
|