Skip to content

Commit

Permalink
Merge pull request #877 from owlchester/meilisearch-delete-deleted-ca…
Browse files Browse the repository at this point in the history
…mpaign-data

Entries are deleted from meilisearch on campaign/user deletion
  • Loading branch information
ilestis authored May 20, 2024
2 parents ee532ba + 548cbc8 commit 3728d5d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
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);
ImageService::cleanup($member->campaign);
$member->campaign->delete();
}
Expand Down

0 comments on commit 3728d5d

Please sign in to comment.