Test run a long running query with pdf upload to S3 bucket

This commit is contained in:
Dillon Ngo
2024-06-22 15:58:29 +08:00
parent 5f443aeadc
commit fdfe3f98d8
2 changed files with 197 additions and 0 deletions
@@ -0,0 +1,153 @@
<?php
namespace App\Classes\Jobs\Commands\V2;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\PaymentMethodType;
use App\Classes\ValueObjects\Constants\TransactionType;
use App\Models\Booking;
use App\Models\Company;
use App\Models\Transaction;
use App\Models\Wallet;
use Illuminate\Support\Facades\DB;
use Mccarlosen\LaravelMpdf\Facades\LaravelMpdf;
use App\Classes\General\AWSS3Helper;
use Illuminate\Support\Facades\Storage;
class TestRunningALongQueryV2CommandJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle()
{
Log::info(Carbon::now() . ': Start job - This is a test to run a long query that generates a pdf file to be uploaded to S3 bucket.');
$start = new Carbon();
////https://dev.exchange.izyim.com/company/2294/false/transaction/export
$companyId = 2294; //$request->route('id');
$isPrecise = false; //$request->route('is_precise');
$company = Company::find($companyId);
$transactions = Transaction::where(function ($query) use ($companyId) {
$query
->where('type', TransactionType::PAYMENT)
->where('owner_type', Booking::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->where('payment_method', '!=', PaymentMethodType::WALLET)
->whereHas('booking', function ($q) use ($companyId) {
$q->where('company_id', $companyId);
});
})
->orWhere(function ($query) use ($companyId) {
$query->whereHas('owner', function ($q) use ($companyId) {
$q->where('owner_id', $companyId);
$q->where('owner_type', Company::class);
})
->where('owner_type', Wallet::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
})
->orWhere(function ($query) use ($companyId) {
$query
->where('type', TransactionType::INVOICE)
->where('owner_type', Booking::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->where('payment_method', '!=', PaymentMethodType::WALLET)
->whereHas('booking', function ($q) use ($companyId) {
$q->where('company_id', $companyId);
});
})
->groupBy(
DB::raw(
'if (transactions.type = 2,transactions.owner_id,transactions.id)'
),
DB::raw(
'if (transactions.type = 2,transactions.type,transactions.id)'
)
)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->orderBy('created_at', 'asc')
->get();
$total_incoming = Transaction::where(function ($query) use ($companyId) {
$query->whereHas('owner', function ($q) use ($companyId) {
$q->where('owner_id', $companyId);
$q->where('owner_type', Company::class);
})
->where('owner_type', Wallet::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->where('type', '!=', TransactionType::PAYMENT);
})
->orWhere(function ($query) use ($companyId, $transactions) {
$query
->where('type', TransactionType::INVOICE)
->where('owner_type', Booking::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->where('payment_method', '!=', PaymentMethodType::WALLET)
->whereHas('booking', function ($q) use ($companyId) {
$q->where('company_id', $companyId);
});
})
->groupBy(
DB::raw(
'if (transactions.type = 2,transactions.owner_id,transactions.id)'
),
DB::raw(
'if (transactions.type = 2,transactions.type,transactions.id)'
)
)->get();
$total_incoming = $total_incoming->sum(function ($transaction) {
if ($transaction->type === TransactionType::INVOICE) {
return $transaction->amount / $transaction->currency_rate + $transaction->service_charge;
}
return $transaction->amount;
});
$total_outgoing = Transaction::where(function ($query) use ($companyId) {
$query
->where('type', TransactionType::PAYMENT)
->where('payment_method', '!=', PaymentMethodType::WALLET)
->where('owner_type', Booking::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->whereHas('booking', function ($q) use ($companyId) {
$q->where('company_id', $companyId);
});
})
->orWhere(function ($query) use ($companyId) {
$query->whereHas('owner', function ($q) use ($companyId) {
$q->where('owner_id', $companyId);
$q->where('owner_type', Company::class);
})
->where('owner_type', Wallet::class)
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
->where('type', TransactionType::PAYMENT);
})
->sum('transactions.amount');
$currentBalance = $total_incoming - $total_outgoing;
$pdf = LaravelMpdf::loadView('pages.pdfs.company_account_statement_transaction', ['transactions' => $transactions, 'runningBalance' => $currentBalance, 'company' => $company, 'isPrecise' => $isPrecise == 'true']);
$exportFileName = "{$company->reference}_account_statement_transactions.pdf";
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
$pdfContent = $pdf->output();
$temporaryUrl = AWSS3Helper::S3PDF($exportFileName, $pdfContent);
Log::info('Temporary S3 Url: ' . $temporaryUrl );
}
$end = new Carbon();
$elapsedTime = $start->diff($end)->format('%H:%I:%S');
Log::info(Carbon::now() . ': End job - This is a test to run a long query that generates a pdf file to be uploaded to S3 bucket. ElapsedTime: ' . $elapsedTime . '.');
}
}
@@ -0,0 +1,44 @@
<?php
namespace App\Console\Commands\V2;
use App\Classes\Jobs\Commands\V2\TestRunningALongQueryV2CommandJob;
use Illuminate\Console\Command;
class TestRunningALongQueryV2Command extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test-running-long-query-s3-pdf-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This is a test to run a long query that generates a pdf file to be uploaded to S3 bucket';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
TestRunningALongQueryV2CommandJob::dispatch();
}
}