Skip to content
Open
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
13 changes: 12 additions & 1 deletion ProcessMaker/Jobs/GenerateUserRecommendations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ProcessMaker\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
Expand All @@ -12,17 +13,27 @@
use ProcessMaker\Models\User;
use ProcessMaker\RecommendationEngine;

class GenerateUserRecommendations implements ShouldQueue
class GenerateUserRecommendations implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Seconds the unique lock is held so duplicate dispatches for the same user are dropped.
*/
public int $uniqueFor = 60;

/**
* Create a new job instance.
*/
public function __construct(public int $user_id)
{
}

public function uniqueId(): string
{
return (string) $this->user_id;
}

public function middleware(): array
{
return [(new WithoutOverlapping($this->user_id))->dontRelease()];
Expand Down
22 changes: 22 additions & 0 deletions ProcessMaker/Models/ProcessRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,28 @@ public function shouldBeSearchable()
}
}

/**
* Determine if the model existed in the search index prior to an update.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeUpdate()
{
return $this->shouldBeSearchable();
}

/**
* Determine if the model existed in the search index prior to deletion.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeDelete()
{
return $this->shouldBeSearchable();
}

/**
* Get the indexable data array for the model.
*
Expand Down
22 changes: 22 additions & 0 deletions ProcessMaker/Models/ProcessRequestToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ public function shouldBeSearchable()
}
}

/**
* Determine if the model existed in the search index prior to an update.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeUpdate()
{
return $this->shouldBeSearchable();
}

/**
* Determine if the model existed in the search index prior to deletion.
* Prevents unnecessary RemoveFromSearch jobs when indexed search is disabled.
*
* @return bool
*/
public function wasSearchableBeforeDelete()
{
return $this->shouldBeSearchable();
}

/**
* Boot application as a process instance.
*
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/Jobs/GenerateUserRecommendationsUniqueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Jobs;

use Illuminate\Support\Facades\Queue;
use ProcessMaker\Events\ActivityCompleted;
use ProcessMaker\Jobs\GenerateUserRecommendations;
use ProcessMaker\Jobs\SmartInbox;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use Tests\TestCase;

class GenerateUserRecommendationsUniqueTest extends TestCase
{
public function test_duplicate_dispatches_for_the_same_user_are_dropped(): void
{
Queue::fake();

$user = User::factory()->create(['status' => 'ACTIVE']);

GenerateUserRecommendations::dispatch($user->id);
GenerateUserRecommendations::dispatch($user->id)->onQueue('low');

Queue::assertPushed(GenerateUserRecommendations::class, 1);
}

public function test_smart_inbox_and_activity_completed_do_not_enqueue_twice_for_the_same_user(): void
{
Queue::fake();

$user = User::factory()->create(['status' => 'ACTIVE']);
$token = ProcessRequestToken::factory()->create([
'user_id' => $user->id,
'element_type' => 'task',
'status' => 'CLOSED',
]);

(new SmartInbox($token->id))->handle();
event(new ActivityCompleted($token));

Queue::assertPushed(GenerateUserRecommendations::class, 1);
Queue::assertPushed(GenerateUserRecommendations::class, function (GenerateUserRecommendations $job) use ($user) {
return $job->user_id === $user->id;
});
}
}
124 changes: 124 additions & 0 deletions tests/Feature/Models/IndexedSearchScoutSyncTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Models;

use Illuminate\Support\Facades\Queue;
use Laravel\Scout\Jobs\MakeSearchable;
use Laravel\Scout\Jobs\RemoveFromSearch;
use ProcessMaker\Cache\Settings\SettingCacheFactory;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\Setting;
use Tests\TestCase;

class IndexedSearchScoutSyncTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

config(['scout.queue' => true]);
}

private function setIndexedSearch(bool $enabled): void
{
$setting = Setting::updateOrCreate(
['key' => 'indexed-search'],
['config' => ['enabled' => $enabled]]
);

$settingCache = SettingCacheFactory::getSettingsCache();
$settingKey = $settingCache->createKey(['key' => 'indexed-search']);
$settingCache->set($settingKey, $setting->fresh());
}

public function test_was_searchable_hooks_follow_indexed_search_setting(): void
{
$this->setIndexedSearch(false);
$request = ProcessRequest::factory()->create();
$token = ProcessRequestToken::factory()->create([
'process_request_id' => $request->id,
'element_type' => 'task',
]);

$this->assertFalse($request->shouldBeSearchable());
$this->assertFalse($request->wasSearchableBeforeUpdate());
$this->assertFalse($request->wasSearchableBeforeDelete());
$this->assertFalse($token->shouldBeSearchable());
$this->assertFalse($token->wasSearchableBeforeUpdate());
$this->assertFalse($token->wasSearchableBeforeDelete());

$this->setIndexedSearch(true);

$this->assertTrue($request->shouldBeSearchable());
$this->assertTrue($request->wasSearchableBeforeUpdate());
$this->assertTrue($request->wasSearchableBeforeDelete());
$this->assertTrue($token->fresh()->shouldBeSearchable());
$this->assertTrue($token->fresh()->wasSearchableBeforeUpdate());
$this->assertTrue($token->fresh()->wasSearchableBeforeDelete());
}

public function test_save_does_not_dispatch_remove_from_search_when_indexed_search_is_disabled(): void
{
$this->setIndexedSearch(false);
$request = ProcessRequest::factory()->create();
$token = ProcessRequestToken::factory()->create([
'process_request_id' => $request->id,
'element_type' => 'task',
]);

Queue::fake();

$request->name = 'Updated request';
$request->save();
$token->element_name = 'Updated token';
$token->save();

Queue::assertNotPushed(RemoveFromSearch::class);
Queue::assertNotPushed(MakeSearchable::class);
}

public function test_save_indexes_when_indexed_search_is_enabled(): void
{
$this->setIndexedSearch(true);
$request = ProcessRequest::factory()->create();
$token = ProcessRequestToken::factory()->create([
'process_request_id' => $request->id,
'element_type' => 'task',
]);

Queue::fake();

$request->name = 'Searchable request';
$request->save();
$token->element_name = 'Searchable token';
$token->save();

Queue::assertPushed(MakeSearchable::class);
Queue::assertNotPushed(RemoveFromSearch::class);
}

public function test_delete_still_removes_from_search_when_indexed_search_is_enabled(): void
{
$this->setIndexedSearch(true);
$request = ProcessRequest::factory()->create();

Queue::fake();
$request->delete();

Queue::assertPushed(RemoveFromSearch::class);
}

public function test_delete_does_not_dispatch_remove_from_search_when_indexed_search_is_disabled(): void
{
$this->setIndexedSearch(false);
$request = ProcessRequest::factory()->create();

Queue::fake();
$request->delete();

Queue::assertNotPushed(RemoveFromSearch::class);
}
}
Loading