Fix events import payload decoding and programme link remapping.
Deploy Ladill Events / deploy (push) Successful in 25s
Deploy Ladill Events / deploy (push) Successful in 25s
Decode JSON-string payloads from the platform export so event content, covers, and programme references import correctly. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -61,6 +61,9 @@ class ImportEventsCommand extends Command
|
||||
foreach ($payload['qr_codes'] ?? [] as $row) {
|
||||
$this->importCode($row, $commit);
|
||||
}
|
||||
if ($commit) {
|
||||
$this->remapAllProgrammeLinks();
|
||||
}
|
||||
foreach ($payload['qr_event_registrations'] ?? [] as $row) {
|
||||
$this->importRegistration($row, $commit);
|
||||
}
|
||||
@@ -127,17 +130,24 @@ class ImportEventsCommand extends Command
|
||||
'user_id',
|
||||
])->all();
|
||||
$attrs['user_id'] = $user->id;
|
||||
$attrs = $this->normalizeJsonAttributes($attrs, ['payload']);
|
||||
|
||||
if ($commit) {
|
||||
$code = QrCode::updateOrCreate(['short_code' => $shortCode], $attrs);
|
||||
$this->codeMap[$platformId] = $code->id;
|
||||
$this->remapProgrammeLinks($code);
|
||||
$this->copyCodeAssets($code);
|
||||
} else {
|
||||
$this->codeMap[$platformId] = $platformId;
|
||||
}
|
||||
}
|
||||
|
||||
private function remapAllProgrammeLinks(): void
|
||||
{
|
||||
QrCode::query()
|
||||
->where('type', QrCode::TYPE_EVENT)
|
||||
->each(fn (QrCode $code) => $this->remapProgrammeLinks($code));
|
||||
}
|
||||
|
||||
private function remapProgrammeLinks(QrCode $code): void
|
||||
{
|
||||
if ($code->type !== QrCode::TYPE_EVENT) {
|
||||
@@ -151,7 +161,30 @@ class ImportEventsCommand extends Command
|
||||
}
|
||||
|
||||
$content['programme_qr_id'] = $this->codeMap[$programmeId];
|
||||
$code->update(['content' => $content]);
|
||||
$payload = $code->payload ?? [];
|
||||
$payload['content'] = $content;
|
||||
$code->update(['payload' => $payload]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string,mixed> $attrs
|
||||
* @param list<string> $keys
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private function normalizeJsonAttributes(array $attrs, array $keys): array
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
if (! isset($attrs[$key]) || ! is_string($attrs[$key])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$decoded = json_decode($attrs[$key], true);
|
||||
if (is_array($decoded)) {
|
||||
$attrs[$key] = $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
return $attrs;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $row */
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\QrCode;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ImportEventsCommandTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_import_decodes_json_payload_and_remaps_programme_links(): void
|
||||
{
|
||||
Storage::fake('qr');
|
||||
|
||||
$owner = User::factory()->create(['public_id' => 'usr_events_owner']);
|
||||
|
||||
$payload = [
|
||||
'qr_wallets' => [],
|
||||
'qr_codes' => [
|
||||
[
|
||||
'platform_id' => 10,
|
||||
'owner_public_id' => $owner->public_id,
|
||||
'owner_email' => $owner->email,
|
||||
'short_code' => 'demo-programme',
|
||||
'type' => QrCode::TYPE_ITINERARY,
|
||||
'label' => 'Demo Programme',
|
||||
'payload' => json_encode(['content' => ['title' => 'Demo Programme'], 'style' => []]),
|
||||
'is_active' => true,
|
||||
'scans_total' => 0,
|
||||
'created_at' => now()->toDateTimeString(),
|
||||
'updated_at' => now()->toDateTimeString(),
|
||||
'destination_updated_at' => now()->toDateTimeString(),
|
||||
],
|
||||
[
|
||||
'platform_id' => 11,
|
||||
'owner_public_id' => $owner->public_id,
|
||||
'owner_email' => $owner->email,
|
||||
'short_code' => 'demo-event',
|
||||
'type' => QrCode::TYPE_EVENT,
|
||||
'label' => 'Demo Event',
|
||||
'payload' => json_encode([
|
||||
'content' => ['name' => 'Demo Event', 'programme_qr_id' => 10],
|
||||
'style' => [],
|
||||
]),
|
||||
'is_active' => true,
|
||||
'scans_total' => 0,
|
||||
'created_at' => now()->toDateTimeString(),
|
||||
'updated_at' => now()->toDateTimeString(),
|
||||
'destination_updated_at' => now()->toDateTimeString(),
|
||||
],
|
||||
],
|
||||
'qr_event_registrations' => [],
|
||||
'qr_scan_events' => [],
|
||||
'qr_transactions' => [],
|
||||
];
|
||||
|
||||
$exportPath = storage_path('framework/testing/events-import.json');
|
||||
if (! is_dir(dirname($exportPath))) {
|
||||
mkdir(dirname($exportPath), 0777, true);
|
||||
}
|
||||
file_put_contents($exportPath, json_encode($payload));
|
||||
|
||||
Artisan::call('events:import', [
|
||||
'file' => $exportPath,
|
||||
'--commit' => true,
|
||||
]);
|
||||
|
||||
$event = QrCode::where('short_code', 'demo-event')->first();
|
||||
$programme = QrCode::where('short_code', 'demo-programme')->first();
|
||||
|
||||
$this->assertNotNull($event);
|
||||
$this->assertNotNull($programme);
|
||||
$this->assertIsArray($event->payload);
|
||||
$this->assertSame('Demo Event', $event->content()['name'] ?? null);
|
||||
$this->assertSame($programme->id, $event->content()['programme_qr_id'] ?? null);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user