fix: add profile index & fixed orphan token & fixed duplicated tokens (#817)

This commit is contained in:
Kyrch
2025-05-13 22:09:27 -03:00
committed by GitHub
parent 69cc755077
commit c7ba086947
4 changed files with 64 additions and 4 deletions
@@ -30,11 +30,11 @@ class StoreExternalProfileTokenAction
*
* @param ExternalToken $token
* @param array $parameters
* @return ExternalProfile|null
* @return ExternalProfile
*
* @throws Exception
*/
public function firstOrCreate(ExternalToken $token, array $parameters): ?ExternalProfile
public function firstOrCreate(ExternalToken $token, array $parameters): ExternalProfile
{
try {
$site = ExternalProfileSite::fromLocalizedName(Arr::get($parameters, 'site'));
@@ -9,9 +9,11 @@ use App\Actions\Models\List\ExternalProfile\ExternalToken\Site\AnilistExternalTo
use App\Actions\Models\List\ExternalProfile\ExternalToken\Site\MalExternalTokenAction;
use App\Actions\Models\List\ExternalProfile\StoreExternalProfileTokenAction;
use App\Enums\Models\List\ExternalProfileSite;
use App\Models\List\External\ExternalToken;
use App\Models\List\ExternalProfile;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use RuntimeException;
@@ -39,13 +41,20 @@ class ExternalTokenCallbackAction
$action = $this->getActionClass($profileSite);
$externalToken = $action->store($parameters);
$externalToken = ExternalToken::query()
->whereRelation(ExternalToken::RELATION_PROFILE, ExternalProfile::ATTRIBUTE_USER, Auth::id())
->whereRelation(ExternalToken::RELATION_PROFILE, ExternalProfile::ATTRIBUTE_SITE, $profileSite->value)
->first();
if (! $externalToken instanceof ExternalToken) {
$externalToken = $action->store($parameters);
}
$profileAction = new StoreExternalProfileTokenAction();
$profile = $profileAction->firstOrCreate($externalToken, $parameters);
$externalToken->externalprofile()->associate($profile);
$profile->externaltoken()->save($externalToken);
DB::commit();
+10
View File
@@ -160,6 +160,16 @@ class ExternalProfile extends BaseModel
return $this->attributesToArray();
}
/**
* Get the index name for the model when searching.
*
* @return string
*/
public function searchableAs(): string
{
return 'profiles';
}
/**
* Determine if the model should be searchable.
*
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
use Elastic\Adapter\Indices\Mapping;
use Elastic\Migrations\Facades\Index;
use Elastic\Migrations\MigrationInterface;
/**
* Class CreateProfileIndex.
*/
final class CreateProfileIndex implements MigrationInterface
{
/**
* Run the migration.
*/
public function up(): void
{
Index::createIfNotExists('profiles', function (Mapping $mapping) {
$mapping->long('profile_id');
$mapping->date('created_at');
$mapping->text('name', [
'fields' => [
'keyword' => [
'type' => 'keyword',
],
],
]);
$mapping->long('site');
$mapping->date('updated_at');
});
}
/**
* Reverse the migration.
*/
public function down(): void
{
Index::dropIfExists('profiles');
}
}