chore: last consulted at field update is now in a job (#3586)

This commit is contained in:
Regis Freyd
2020-02-14 19:34:50 -05:00
committed by GitHub
parent 4863b923fc
commit fb67cf9f79
4 changed files with 81 additions and 2 deletions
@@ -7,6 +7,7 @@ use App\Helpers\SearchHelper;
use App\Models\Contact\Contact;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use App\Jobs\UpdateLastConsultedDate;
use Illuminate\Database\QueryException;
use App\Services\Contact\Contact\SetMeContact;
use Illuminate\Validation\ValidationException;
@@ -96,7 +97,7 @@ class ApiContactController extends ApiController
return $this->respondNotFound();
}
$contact->updateConsulted();
UpdateLastConsultedDate::dispatch($contact);
if ($this->getWithParameter() == 'contactfields') {
return new ContactWithContactFieldsResource($contact);
+2 -1
View File
@@ -18,6 +18,7 @@ use App\Models\Contact\Contact;
use App\Services\VCard\ExportVCard;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use App\Jobs\UpdateLastConsultedDate;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\View\Factory;
use App\Models\Relationship\Relationship;
@@ -244,7 +245,7 @@ class ContactsController extends Controller
$query->orderBy('updated_at', 'desc');
}]);
$contact->updateConsulted();
UpdateLastConsultedDate::dispatch($contact);
$relationships = $contact->relationships;
// get love relationship type
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use App\Models\Contact\Contact;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class UpdateLastConsultedDate implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $contact;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Contact $contact)
{
$this->contact = $contact;
}
/**
* Update the Last Consulted At field for the given contact.
*
* @return void
*/
public function handle(): void
{
$timestamps = $this->contact->timestamps;
$this->contact->timestamps = false;
$this->contact->last_consulted_at = now();
$this->contact->number_of_views = $this->contact->number_of_views + 1;
$this->contact->save();
$this->contact->timestamps = $timestamps;
}
}
@@ -0,0 +1,32 @@
<?php
namespace Tests\Unit\Jobs;
use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Contact\Contact;
use App\Jobs\UpdateLastConsultedDate;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class UpdateLastConsultedDateTest extends TestCase
{
use DatabaseTransactions;
/** @test */
public function it_updates_the_last_consulted_at_field_for_the_given_contact()
{
Carbon::setTestNow(Carbon::create(2017, 1, 1, 7, 0, 0, 'America/New_York'));
$contact = factory(Contact::class)->create([
'number_of_views' => 1,
]);
dispatch(new UpdateLastConsultedDate($contact));
$this->assertDatabaseHas('contacts', [
'id' => $contact->id,
'last_consulted_at' => '2017-01-01 07:00:00',
'number_of_views' => 2,
]);
}
}