mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 04:23:55 +00:00
re-enable lark push
This commit is contained in:
@@ -57,6 +57,8 @@ final class TransactionType {
|
||||
self::CASH_BACK => "CASH_BACK",
|
||||
self::SUPPLIER_PAYMENT => "SUPPLIER_PAYMENT",
|
||||
self::SUPPLIER_REFUND => "SUPPLIER_REFUND",
|
||||
self::BILL_REFUND => "BILL_REFUND",
|
||||
self::RECEIPT_VOUCHER => "RECEIPT_VOUCHER",
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
<?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 LarkPushBookingSeptember2025TransactionsModuleCommand extends Command
|
||||
{
|
||||
protected $signature = 'lark:push-booking-september-2025-transactions-module {--limit=100}';
|
||||
protected $description = 'Send booking september 2025 transactions records to a Lark webhook, one by one.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$module = 'Booking September 2025 Transactions';
|
||||
$webhookId = env('LARK_BOOKING_SEPT_2025_TRANSACTIONS_WEBHOOK_ID');
|
||||
if (!$webhookId) {
|
||||
$this->error('[Lark ' . $module . '] Missing LARK_BOOKING_SEPT_2025_TRANSACTIONS_WEBHOOK_ID');
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
$this->info('[Lark ' . $module . '] Start (env: ' . app()->environment() . ')');
|
||||
|
||||
$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;
|
||||
|
||||
// $transaction->owner,
|
||||
$transactionOwnerGroup = [
|
||||
TransactionType::PAYMENT,
|
||||
TransactionType::INVOICE,
|
||||
TransactionType::PROFORMA,
|
||||
TransactionType::REFUND,
|
||||
TransactionType::PURCHASE_ORDER,
|
||||
TransactionType::SUPPLIER_DELIVER,
|
||||
TransactionType::RECEIPT_VOUCHER
|
||||
];
|
||||
|
||||
// $transaction->owner?->owner,
|
||||
$transactionOwnerOwnerGroup = [
|
||||
TransactionType::BILL,
|
||||
TransactionType::SUPPLIER_REFUND,
|
||||
TransactionType::BILL_REFUND
|
||||
];
|
||||
|
||||
// $transaction->owner?->owner?->owner,
|
||||
$transactionOwnerOwnerOwnerGroup = [
|
||||
TransactionType::TRANSFER_FEE
|
||||
];
|
||||
|
||||
$transactionstypes = array_merge($transactionOwnerGroup, $transactionOwnerOwnerGroup, $transactionOwnerOwnerOwnerGroup);
|
||||
|
||||
$bookingTransactions = BookingTransactions::query()
|
||||
->whereDate('transaction_created_at', '>=', '2025-09-01')
|
||||
->whereIn('transaction_type', $transactionstypes)
|
||||
->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],
|
||||
'transaction_bill_no' => $b->transaction_bill_no,
|
||||
'transaction_currency_rate' => $b->transaction_currency_rate,
|
||||
'transaction_service_charge' => $b->transaction_service_charge,
|
||||
'transaction_currency_code' => $b->transaction_currency_code,
|
||||
'transaction_amount' => $b->transaction_amount,
|
||||
'transaction_original_currency_code' => $b->transaction_original_currency_code,
|
||||
'transaction_original_amount' => $b->transaction_original_amount,
|
||||
'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);
|
||||
usleep(100000); //0.1 second
|
||||
}
|
||||
|
||||
$this->info("[Lark " . $module . "] Finished {$bookingTransactions->count()} record(s).");
|
||||
Log::info('[Lark ' . $module . '] Finished', ['count' => $bookingTransactions->count()]);
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -111,6 +111,8 @@ class Kernel extends ConsoleKernel
|
||||
// ->everyTenMinutes();
|
||||
// $schedule->command('lark:push-booking-2025-transactions-module')
|
||||
// ->everyTenMinutes();
|
||||
$schedule->command('lark:push-booking-september-2025-transactions-module')
|
||||
->everyTenMinutes();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -53,13 +53,13 @@ return new class extends Migration
|
||||
FROM transactions t
|
||||
LEFT JOIN bookings b
|
||||
ON (
|
||||
(t.type IN (1,2,4,6,7,8,17) AND b.id = t.owner_id)
|
||||
OR (t.type IN (3,15,16) AND b.id = (
|
||||
(t.type IN (1,2,4,6,7,8) AND b.id = t.owner_id)
|
||||
OR (t.type IN (3,15,17) AND b.id = (
|
||||
SELECT bb.id FROM transactions tt
|
||||
JOIN bookings bb ON bb.id = tt.owner_id
|
||||
WHERE tt.id = t.owner_id LIMIT 1
|
||||
))
|
||||
OR (t.type = 12 AND b.id = (
|
||||
OR (t.type IN (12, 16) AND b.id = (
|
||||
SELECT bbb.id FROM transactions ttt
|
||||
JOIN bookings bbb ON bbb.id = ttt.owner_id
|
||||
WHERE ttt.id = (
|
||||
|
||||
Reference in New Issue
Block a user