Deploy Ladill Link / deploy (push) Successful in 29s
Sync QR and storefront short codes from sibling apps via a platform catalog API so My Links shows every ladl.link URL in one place. Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
2.2 KiB
PHP
65 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Link;
|
|
|
|
use App\Models\ShortLink;
|
|
use App\Models\User;
|
|
use App\Support\LadillLink;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Mirrors platform-wide ladl.link slugs into the local catalog for unified listing.
|
|
*/
|
|
class LinkCatalogSyncService
|
|
{
|
|
public function __construct(
|
|
private PlatformLinkCatalogClient $catalog,
|
|
) {}
|
|
|
|
public function syncForUser(User $user): void
|
|
{
|
|
$remote = $this->catalog->linksForUser((string) $user->public_id);
|
|
$remoteSlugs = collect($remote)->pluck('slug')->filter()->all();
|
|
|
|
DB::transaction(function () use ($user, $remote, $remoteSlugs): void {
|
|
foreach ($remote as $row) {
|
|
$slug = (string) ($row['slug'] ?? '');
|
|
if ($slug === '') {
|
|
continue;
|
|
}
|
|
|
|
if (ShortLink::query()
|
|
->where('slug', $slug)
|
|
->where('is_managed_here', true)
|
|
->exists()) {
|
|
continue;
|
|
}
|
|
|
|
ShortLink::query()->updateOrCreate(
|
|
['slug' => $slug],
|
|
[
|
|
'user_id' => $user->id,
|
|
'label' => $row['label'] ?? null,
|
|
'destination_url' => (string) ($row['destination_url'] ?? LadillLink::url($slug)),
|
|
'source_app' => (string) ($row['source_app'] ?? 'platform'),
|
|
'source_kind' => (string) ($row['source_kind'] ?? 'qr'),
|
|
'source_ref' => isset($row['source_ref']) ? (string) $row['source_ref'] : null,
|
|
'manage_url' => $row['manage_url'] ?? null,
|
|
'is_managed_here' => false,
|
|
'is_active' => (bool) ($row['is_active'] ?? true),
|
|
'clicks_total' => (int) ($row['clicks_total'] ?? 0),
|
|
],
|
|
);
|
|
}
|
|
|
|
if ($remoteSlugs !== []) {
|
|
ShortLink::query()
|
|
->where('user_id', $user->id)
|
|
->where('is_managed_here', false)
|
|
->whereNotIn('slug', $remoteSlugs)
|
|
->delete();
|
|
}
|
|
});
|
|
}
|
|
}
|