- 将玩法相关的显示名称字段统一为 `display_name`,移除多语言字段。 - 在 `PlayTypePatchController` 中新增即时切换玩法开关的功能,并推送大厅更新。 - 优化多个控制器和服务中的权限检查与数据处理逻辑,提升代码可读性与维护性。
402 lines
14 KiB
PHP
402 lines
14 KiB
PHP
<?php
|
|
|
|
use App\Models\AdminUser;
|
|
use App\Models\Draw;
|
|
use App\Lottery\DrawStatus;
|
|
use App\Models\DrawResultBatch;
|
|
use App\Models\DrawResultItem;
|
|
use App\Models\JackpotPool;
|
|
use App\Models\JackpotPayoutLog;
|
|
use App\Models\Player;
|
|
use App\Models\PlayerWallet;
|
|
use App\Models\SettlementBatch;
|
|
use App\Models\TicketItem;
|
|
use App\Models\WalletTxn;
|
|
use App\Lottery\DrawResultBatchStatus;
|
|
use App\Services\Draw\DrawPrizeLayout;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use App\Events\JackpotBurstBroadcast;
|
|
use App\Events\DrawStatusChangeBroadcast;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Database\Seeders\CurrencySeeder;
|
|
use Database\Seeders\PlayTypeSeeder;
|
|
use Database\Seeders\LotterySettingsSeeder;
|
|
use Database\Seeders\OperationalConfigV1Seeder;
|
|
use App\Services\Settlement\SettlementOrchestrator;
|
|
use App\Services\Settlement\SettlementBatchWorkflowService;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function (): void {
|
|
$this->seed(CurrencySeeder::class);
|
|
$this->seed(PlayTypeSeeder::class);
|
|
$this->seed(OperationalConfigV1Seeder::class);
|
|
$this->seed(LotterySettingsSeeder::class);
|
|
});
|
|
|
|
function mintSettlementAdminToken(): string
|
|
{
|
|
$admin = AdminUser::query()->create([
|
|
'username' => 'settlement_admin',
|
|
'name' => 'Settlement QA',
|
|
'email' => null,
|
|
'password' => Hash::make('secret-strong'),
|
|
'status' => 0,
|
|
]);
|
|
grantSuperAdminRole($admin);
|
|
|
|
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
|
}
|
|
|
|
function mintRiskOperatorToken(): string
|
|
{
|
|
$now = now();
|
|
DB::table('admin_roles')->updateOrInsert(
|
|
['slug' => 'risk_operator'],
|
|
['name' => 'Risk', 'code' => 'risk_operator', 'created_at' => $now, 'updated_at' => $now],
|
|
);
|
|
$roleId = (int) DB::table('admin_roles')->where('slug', 'risk_operator')->value('id');
|
|
|
|
$admin = AdminUser::query()->create([
|
|
'username' => 'risk_jp_admin',
|
|
'name' => 'Risk JP',
|
|
'email' => null,
|
|
'password' => Hash::make('secret-strong'),
|
|
'status' => 0,
|
|
]);
|
|
|
|
$siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
|
|
DB::table('admin_user_site_roles')->insert([
|
|
'admin_user_id' => $admin->id,
|
|
'site_id' => $siteId,
|
|
'role_id' => $roleId,
|
|
'granted_at' => $now,
|
|
]);
|
|
|
|
$manageMenuActionId = (int) DB::table('admin_menu_actions')
|
|
->where('permission_code', 'config.jackpot.manage')
|
|
->value('id');
|
|
if ($manageMenuActionId > 0) {
|
|
DB::table('admin_role_menu_actions')->updateOrInsert(
|
|
['role_id' => $roleId, 'menu_action_id' => $manageMenuActionId],
|
|
[],
|
|
);
|
|
}
|
|
|
|
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
|
}
|
|
|
|
test('admin settlement batches index is authenticated', function (): void {
|
|
$this->getJson('/api/v1/admin/settlement-batches')->assertUnauthorized();
|
|
});
|
|
|
|
test('admin jackpot pools index returns rows', function (): void {
|
|
$token = mintSettlementAdminToken();
|
|
$this->withHeader('Authorization', 'Bearer '.$token)
|
|
->getJson('/api/v1/admin/jackpot/pools')
|
|
->assertOk()
|
|
->assertJsonPath('data.items.0.currency_code', 'NPR')
|
|
->assertJsonPath('data.items.0.contribution_rate', '0.0200')
|
|
->assertJsonPath('data.items.0.trigger_threshold', 100000000)
|
|
->assertJsonPath('data.items.0.payout_rate', '0.5000')
|
|
->assertJsonPath('data.items.0.force_trigger_draw_gap', 100)
|
|
->assertJsonPath('data.items.0.min_bet_amount', 100)
|
|
->assertJsonPath('data.items.0.status', 0)
|
|
->assertJsonPath('data.items.0.combo_trigger_play_codes', []);
|
|
});
|
|
|
|
test('admin can update jackpot combo trigger', function (): void {
|
|
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
|
$token = mintSettlementAdminToken();
|
|
|
|
$this->withHeader('Authorization', 'Bearer '.$token)
|
|
->putJson('/api/v1/admin/jackpot/pools/'.$pool->id, [
|
|
'combo_trigger_play_codes' => ['straight', 'ibox'],
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.combo_trigger_play_codes.0', 'straight')
|
|
->assertJsonPath('data.combo_trigger_play_codes.1', 'ibox');
|
|
});
|
|
|
|
test('risk operator cannot manually burst jackpot', function (): void {
|
|
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
|
$pool->forceFill(['current_amount' => 1000, 'status' => 1])->save();
|
|
|
|
$draw = Draw::query()->create([
|
|
'draw_no' => '20260518-099',
|
|
'business_date' => '2026-05-18',
|
|
'sequence_no' => 99,
|
|
'status' => DrawStatus::Settled->value,
|
|
'start_time' => now()->subHours(2),
|
|
'close_time' => now()->subHour(),
|
|
'draw_time' => now()->subHour(),
|
|
'cooling_end_time' => null,
|
|
'result_source' => null,
|
|
'current_result_version' => 0,
|
|
'settle_version' => 1,
|
|
'is_reopened' => false,
|
|
]);
|
|
|
|
$token = mintRiskOperatorToken();
|
|
|
|
$this->withHeader('Authorization', 'Bearer '.$token)
|
|
->postJson('/api/v1/admin/jackpot/pools/'.$pool->id.'/manual-burst', [
|
|
'draw_id' => $draw->id,
|
|
])
|
|
->assertForbidden();
|
|
});
|
|
|
|
test('super admin manual burst allocates jackpot to first prize winners after settlement', function (): void {
|
|
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
|
$pool->forceFill([
|
|
'current_amount' => 10_000,
|
|
'contribution_rate' => '0',
|
|
'trigger_threshold' => 999_999_999,
|
|
'payout_rate' => '0.5000',
|
|
'force_trigger_draw_gap' => 0,
|
|
'min_bet_amount' => 0,
|
|
'status' => 1,
|
|
'last_trigger_draw_id' => null,
|
|
])->save();
|
|
|
|
$player = Player::query()->create([
|
|
'site_code' => 'test',
|
|
'site_player_id' => 'manual-burst-p1',
|
|
'username' => 'manual_burst_p1',
|
|
'nickname' => null,
|
|
'default_currency' => 'NPR',
|
|
'status' => 0,
|
|
]);
|
|
PlayerWallet::query()->create([
|
|
'player_id' => $player->id,
|
|
'wallet_type' => 'lottery',
|
|
'currency_code' => 'NPR',
|
|
'balance' => 5_000_000,
|
|
'frozen_balance' => 0,
|
|
'status' => 0,
|
|
'version' => 0,
|
|
]);
|
|
|
|
$draw = Draw::query()->create([
|
|
'draw_no' => '20260518-010',
|
|
'business_date' => '2026-05-18',
|
|
'sequence_no' => 10,
|
|
'status' => DrawStatus::Open->value,
|
|
'start_time' => now()->subMinutes(5),
|
|
'close_time' => now()->addMinutes(5),
|
|
'draw_time' => now()->addMinutes(6),
|
|
'cooling_end_time' => null,
|
|
'result_source' => null,
|
|
'current_result_version' => 0,
|
|
'settle_version' => 0,
|
|
'is_reopened' => false,
|
|
]);
|
|
|
|
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
|
->postJson('/api/v1/ticket/place', [
|
|
'draw_id' => (string) $draw->draw_no,
|
|
'currency_code' => 'NPR',
|
|
'client_trace_id' => 'manual-burst-bet-1',
|
|
'lines' => [['number' => '1234', 'play_code' => 'straight', 'amount' => 10_000]],
|
|
])
|
|
->assertOk();
|
|
|
|
$batch = DrawResultBatch::query()->create([
|
|
'draw_id' => $draw->id,
|
|
'result_version' => 1,
|
|
'source_type' => 'rng',
|
|
'raw_seed_encrypted' => null,
|
|
'status' => DrawResultBatchStatus::Published->value,
|
|
'created_by' => null,
|
|
'confirmed_by' => null,
|
|
'confirmed_at' => now(),
|
|
]);
|
|
|
|
foreach (DrawPrizeLayout::slots() as $slot) {
|
|
$num = $slot['prize_type'] === 'first' ? '1234' : '5678';
|
|
DrawResultItem::query()->create([
|
|
'draw_id' => $draw->id,
|
|
'result_batch_id' => $batch->id,
|
|
'prize_type' => $slot['prize_type'],
|
|
'prize_index' => $slot['prize_index'],
|
|
'number_4d' => $num,
|
|
'suffix_3d' => substr($num, -3),
|
|
'suffix_2d' => substr($num, -2),
|
|
'head_digit' => (int) substr($num, 0, 1),
|
|
'tail_digit' => (int) substr($num, 3, 1),
|
|
]);
|
|
}
|
|
|
|
$draw->forceFill([
|
|
'status' => DrawStatus::Settling->value,
|
|
'current_result_version' => 1,
|
|
])->save();
|
|
|
|
expect(app(SettlementOrchestrator::class)->trySettleDraw($draw->fresh()))->toBeTrue();
|
|
|
|
$item = TicketItem::query()->where('draw_id', $draw->id)->firstOrFail();
|
|
expect((int) $item->jackpot_win_amount)->toBe(0);
|
|
|
|
$admin = AdminUser::query()->where('username', 'settlement_admin')->first();
|
|
if ($admin === null) {
|
|
mintSettlementAdminToken();
|
|
$admin = AdminUser::query()->where('username', 'settlement_admin')->firstOrFail();
|
|
} else {
|
|
grantSuperAdminRole($admin);
|
|
}
|
|
|
|
$settlementBatch = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
|
|
app(SettlementBatchWorkflowService::class)->approve($settlementBatch, $admin);
|
|
app(SettlementBatchWorkflowService::class)->payout($settlementBatch->fresh());
|
|
|
|
Event::fake([JackpotBurstBroadcast::class, DrawStatusChangeBroadcast::class]);
|
|
config([
|
|
'broadcasting.default' => 'reverb',
|
|
'broadcasting.connections.reverb.driver' => 'reverb',
|
|
]);
|
|
|
|
$token = $admin->createToken('burst', ['*'], now()->addDay())->plainTextToken;
|
|
|
|
$this->withHeader('Authorization', 'Bearer '.$token)
|
|
->postJson('/api/v1/admin/jackpot/pools/'.$pool->id.'/manual-burst', [
|
|
'draw_id' => $draw->id,
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.burst_amount', 5000)
|
|
->assertJsonPath('data.current_amount', 5000)
|
|
->assertJsonPath('data.winner_count', 1);
|
|
|
|
$item->refresh();
|
|
expect((int) $item->jackpot_win_amount)->toBe(5000);
|
|
|
|
$log = JackpotPayoutLog::query()->where('draw_id', $draw->id)->firstOrFail();
|
|
expect($log->trigger_type)->toBe('manual')
|
|
->and($log->winner_count)->toBe(1)
|
|
->and((int) $log->total_payout_amount)->toBe(5000);
|
|
|
|
expect(WalletTxn::query()->where('biz_type', 'jackpot_manual_payout')->count())->toBe(1);
|
|
|
|
Event::assertDispatched(
|
|
JackpotBurstBroadcast::class,
|
|
fn (JackpotBurstBroadcast $event): bool => $event->drawId === (int) $draw->id
|
|
&& $event->triggerType === 'manual'
|
|
&& $event->totalPayoutAmount === 5000
|
|
&& $event->winnerCount === 1
|
|
&& $event->firstPrizeNumber === '1234',
|
|
);
|
|
});
|
|
|
|
test('manual burst broadcast includes published first prize number', function (): void {
|
|
$pool = JackpotPool::query()->where('currency_code', 'NPR')->firstOrFail();
|
|
$pool->forceFill(['current_amount' => 500, 'status' => 1, 'payout_rate' => '1'])->save();
|
|
|
|
$player = Player::query()->create([
|
|
'site_code' => 'test',
|
|
'site_player_id' => 'manual-burst-p2',
|
|
'username' => 'manual_burst_p2',
|
|
'nickname' => null,
|
|
'default_currency' => 'NPR',
|
|
'status' => 0,
|
|
]);
|
|
PlayerWallet::query()->create([
|
|
'player_id' => $player->id,
|
|
'wallet_type' => 'lottery',
|
|
'currency_code' => 'NPR',
|
|
'balance' => 1_000_000,
|
|
'frozen_balance' => 0,
|
|
'status' => 0,
|
|
'version' => 0,
|
|
]);
|
|
|
|
$draw = Draw::query()->create([
|
|
'draw_no' => '20260518-002',
|
|
'business_date' => '2026-05-18',
|
|
'sequence_no' => 2,
|
|
'status' => DrawStatus::Open->value,
|
|
'start_time' => now()->subMinutes(5),
|
|
'close_time' => now()->addMinutes(5),
|
|
'draw_time' => now()->addMinutes(6),
|
|
'cooling_end_time' => null,
|
|
'result_source' => null,
|
|
'current_result_version' => 0,
|
|
'settle_version' => 0,
|
|
'is_reopened' => false,
|
|
]);
|
|
|
|
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
|
|
->postJson('/api/v1/ticket/place', [
|
|
'draw_id' => (string) $draw->draw_no,
|
|
'currency_code' => 'NPR',
|
|
'client_trace_id' => 'manual-burst-bet-2',
|
|
'lines' => [['number' => '1234', 'play_code' => 'straight', 'amount' => 10_000]],
|
|
])
|
|
->assertOk();
|
|
|
|
$resultBatch = DrawResultBatch::query()->create([
|
|
'draw_id' => $draw->id,
|
|
'result_version' => 1,
|
|
'source_type' => 'rng',
|
|
'raw_seed_encrypted' => null,
|
|
'status' => DrawResultBatchStatus::Published->value,
|
|
'created_by' => null,
|
|
'confirmed_by' => null,
|
|
'confirmed_at' => now(),
|
|
]);
|
|
|
|
foreach (DrawPrizeLayout::slots() as $slot) {
|
|
$num = $slot['prize_type'] === 'first' ? '1234' : '5678';
|
|
DrawResultItem::query()->create([
|
|
'draw_id' => $draw->id,
|
|
'result_batch_id' => $resultBatch->id,
|
|
'prize_type' => $slot['prize_type'],
|
|
'prize_index' => $slot['prize_index'],
|
|
'number_4d' => $num,
|
|
'suffix_3d' => substr($num, -3),
|
|
'suffix_2d' => substr($num, -2),
|
|
'head_digit' => (int) substr($num, 0, 1),
|
|
'tail_digit' => (int) substr($num, 3, 1),
|
|
]);
|
|
}
|
|
|
|
$draw->forceFill([
|
|
'status' => DrawStatus::Settling->value,
|
|
'current_result_version' => 1,
|
|
'result_source' => 'rng',
|
|
])->save();
|
|
|
|
expect(app(SettlementOrchestrator::class)->trySettleDraw($draw->fresh()))->toBeTrue();
|
|
|
|
$admin = AdminUser::query()->create([
|
|
'username' => 'manual_burst_admin2',
|
|
'name' => 'Burst Admin',
|
|
'email' => null,
|
|
'password' => Hash::make('secret-strong'),
|
|
'status' => 0,
|
|
]);
|
|
grantSuperAdminRole($admin);
|
|
$settlementBatch = SettlementBatch::query()->where('draw_id', $draw->id)->firstOrFail();
|
|
app(SettlementBatchWorkflowService::class)->approve($settlementBatch, $admin);
|
|
app(SettlementBatchWorkflowService::class)->payout($settlementBatch->fresh());
|
|
|
|
Event::fake([JackpotBurstBroadcast::class, DrawStatusChangeBroadcast::class]);
|
|
config([
|
|
'broadcasting.default' => 'reverb',
|
|
'broadcasting.connections.reverb.driver' => 'reverb',
|
|
]);
|
|
|
|
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
|
|
|
$this->withHeader('Authorization', 'Bearer '.$token)
|
|
->postJson('/api/v1/admin/jackpot/pools/'.$pool->id.'/manual-burst', [
|
|
'draw_id' => $draw->id,
|
|
])
|
|
->assertOk();
|
|
|
|
Event::assertDispatched(
|
|
JackpotBurstBroadcast::class,
|
|
fn (JackpotBurstBroadcast $event): bool => $event->firstPrizeNumber === '1234',
|
|
);
|
|
});
|