Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entries are deleted from meilisearch on campaign/user deletion #877

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Models/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function entity(): BelongsTo
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return BelongsTo
*/
public function origin()
{
Expand Down
2 changes: 2 additions & 0 deletions app/Observers/CampaignObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Models\CampaignSetting;
use App\Models\UserLog;
use App\Notifications\Header;
use App\Services\Campaign\SearchCleanupService;
use App\Services\EntityMappingService;
use App\Services\ImageService;
use App\Services\Users\CampaignService;
Expand Down Expand Up @@ -157,6 +158,7 @@ public function saved(Campaign $campaign)
*/
public function deleted(Campaign $campaign)
{
SearchCleanupService::cleanup($campaign);
ImageService::cleanup($campaign);
UserCache::clear();
}
Expand Down
21 changes: 21 additions & 0 deletions app/Services/Campaign/SearchCleanupService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Services\Campaign;

use Meilisearch\Client;
use App\Models\Campaign;

class SearchCleanupService
{
/**
* Send cleanup request
*/
public static function cleanup(Campaign $campaign)
{
//Cleanup deleted campaign entries from meilisearch
$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
$client->getKeys();

$client->index('entities')->deleteDocuments(['filter' => 'campaign_id = ' . $campaign->id]);
}
}
2 changes: 2 additions & 0 deletions app/Services/Users/CleanupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\CampaignUser;
use App\Models\CommunityEventEntry;
use App\Models\Feature;
use App\Services\Campaign\SearchCleanupService;
use App\Services\ImageService;
use App\Traits\UserAware;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -43,6 +44,7 @@ protected function removeCampaigns(): self

// Delete a campaign if no one is left in it. Since we did the "with", it's cached, hence checking on 1
if ($member->campaign->members->count() <= 1) {
SearchCleanupService::cleanup($member->campaign);
ilestis marked this conversation as resolved.
Show resolved Hide resolved
ImageService::cleanup($member->campaign);
$member->campaign->delete();
}
Expand Down