mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
Merge branch 'lark-push-booking-transactions' of https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0 into vapor/staging
# Conflicts: # database/migrations/2025_10_25_000000_create_booking_transactions_view.php
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
||||
use App\Classes\ValueObjects\Constants\TransactionType;
|
||||
use App\Models\Booking;
|
||||
use App\Models\BookingTransactions;
|
||||
use App\Models\LarkPushed;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class LarkPushBookingTransactionsModuleCommand extends Command
|
||||
{
|
||||
protected $signature = 'lark:push-booking-transactions-module {--limit=10}';
|
||||
protected $description = 'Send booking transactions records to a Lark webhook, one by one.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$module = 'Booking Transactions';
|
||||
$webhookId = config('services.lark.booking_transactions_webhook_id', env('LARK_BOOKING_TRANSACTIONS_WEBHOOK_ID'));
|
||||
if (!$webhookId) {
|
||||
$this->error('[Lark ' . $module . '] Missing LARK_BOOKING1_WEBHOOK_ID');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
print_r(env('APP_ENV'));
|
||||
|
||||
$limit = (int) $this->option('limit');
|
||||
$url = "https://hfbbr1an7s0.sg.larksuite.com/base/automation/webhook/event/{$webhookId}";
|
||||
$statusM = ApprovalStatus::APPROVAL_STATUS_ID;
|
||||
$transactionTypeM = TransactionType::ID_TO_NAME;
|
||||
|
||||
$bookingTransactions = BookingTransactions::query()
|
||||
->whereNotExists(function ($q) {
|
||||
$b = new BookingTransactions;
|
||||
$q->selectRaw(1)
|
||||
->from('lark_pushed')
|
||||
->whereColumn('lark_pushed.owner_id', 'booking_transactions_view.transaction_id')
|
||||
->where('lark_pushed.owner_type', $b->getMorphClass())
|
||||
->where('lark_pushed.is_success', 1);
|
||||
})
|
||||
->limit($limit)
|
||||
->get();
|
||||
|
||||
if ($bookingTransactions->isEmpty()) {
|
||||
$this->warn('[Lark ' . $module . '] Nothing to push.');
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
$this->info("[Lark " . $module . "] Pushing {$bookingTransactions->count()} record(s)...");
|
||||
Log::info('[Lark ' . $module . '] Start', ['limit' => $limit, 'env' => app()->environment()]);
|
||||
|
||||
foreach ($bookingTransactions as $b) {
|
||||
$payload = [
|
||||
'transaction_id' => $b->transaction_id,
|
||||
'transaction_type' => $transactionTypeM[$b->transaction_type],
|
||||
'transaction_status' => $statusM[$b->transaction_status],
|
||||
'booking_id' => $b->booking_id,
|
||||
'booking_marking' => $b->booking_marking,
|
||||
'company_name' => $b->company_name,
|
||||
'company_reference' => $b->company_reference,
|
||||
'service_name' => $b->service_name,
|
||||
'currency_code' => $b->currency_code,
|
||||
'booking_amount' => $b->booking_amount,
|
||||
'transaction_created_at' => $b->transaction_created_at,
|
||||
'transaction_updated_at' => $b->transaction_updated_at,
|
||||
'need_e_invoice' => $b->need_e_invoice,
|
||||
];
|
||||
|
||||
try {
|
||||
$res = Http::asJson()->post($url, $payload);
|
||||
$ok = $res->successful() && (int) data_get($res->json(), 'code', -1) === 0;
|
||||
|
||||
LarkPushed::create([
|
||||
'owner_id' => $b->transaction_id,
|
||||
'owner_type' => $b->getMorphClass(),
|
||||
'is_success' => $ok ? 1 : 0,
|
||||
]);
|
||||
|
||||
$trransactionTypeString = $transactionTypeM[$b->transaction_type];
|
||||
if ($ok) {
|
||||
$this->info("[Lark $module]: ✅ Sent: {$b->transaction_id} | {$trransactionTypeString}");
|
||||
} else {
|
||||
$this->error("[Lark $module] Failed {$b->transaction_id} | {$trransactionTypeString}: {$res->status()} {$res->body()}");
|
||||
Log::error('[Lark ' . $module . '] Push failed', [
|
||||
'transaction_id' => $b->transaction_id,
|
||||
'status' => $res->status(),
|
||||
'body' => $res->body(),
|
||||
]);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
LarkPushed::create([
|
||||
'owner_id' => $b->transaction_id,
|
||||
'owner_type' => $b->getMorphClass(),
|
||||
'is_success' => 0,
|
||||
]);
|
||||
$this->error("[Lark $module]: ❌ Exception {$b->id} | {$b->marking}: {$e->getMessage()}");
|
||||
Log::error('[Lark ' . $module . '] Exception', [
|
||||
'transaction_id' => $b->transaction_id,
|
||||
'exception' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
$this->info("[Lark " . $module . "] Finished {$bookingTransactions->count()} record(s).");
|
||||
Log::info('[Lark ' . $module . '] Finished', ['count' => $bookingTransactions->count()]);
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -72,13 +72,13 @@ class Kernel extends ConsoleKernel
|
||||
// ->withoutOverlapping();
|
||||
|
||||
// Push company module to Lark
|
||||
$schedule->command('lark:push-company-module')
|
||||
->hourly()
|
||||
->withoutOverlapping();
|
||||
// Push booking module to Lark
|
||||
$schedule->command('lark:push-booking-module')
|
||||
->hourly()
|
||||
->withoutOverlapping();
|
||||
// $schedule->command('lark:push-company-module')
|
||||
// ->hourly()
|
||||
// ->withoutOverlapping();
|
||||
// // Push booking module to Lark
|
||||
// $schedule->command('lark:push-booking-module')
|
||||
// ->hourly()
|
||||
// ->withoutOverlapping();
|
||||
}
|
||||
}
|
||||
//Commands Version 1: Before Laravel Vapor/AWS
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
/**
|
||||
* Class Booking
|
||||
*/
|
||||
class BookingTransactions extends AbstractModel
|
||||
{
|
||||
|
||||
protected $table = 'booking_transactions_view';
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ return new class extends Migration
|
||||
SELECT
|
||||
t.id AS transaction_id,
|
||||
t.type AS transaction_type,
|
||||
t.status AS transaction_status,
|
||||
b.id AS booking_id,
|
||||
b.marking AS booking_marking,
|
||||
c.name AS company_name,
|
||||
@@ -19,6 +20,8 @@ return new class extends Migration
|
||||
st.name AS service_name,
|
||||
fc.short_code AS currency_code,
|
||||
b.fix_amount AS booking_amount,
|
||||
t.created_at AS transaction_created_at,
|
||||
t.updated_at AS transaction_updated_at,
|
||||
CASE c.e_invoice
|
||||
WHEN 1 THEN 'Yes'
|
||||
WHEN 0 THEN 'No'
|
||||
|
||||
Reference in New Issue
Block a user