Add last_login timestamps for vendorcontacts and vendors

This commit is contained in:
David Bomba 2023-08-10 12:40:52 +10:00
parent 45c5e2195b
commit 59ed13122c
8 changed files with 144 additions and 12 deletions

View File

@ -0,0 +1,45 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Events\Vendor;
use App\Models\Company;
use App\Models\VendorContact;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
/**
* Class VendorContactLoggedIn.
*/
class VendorContactLoggedIn
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
*/
public function __construct(public VendorContact $contact, public Company $company, public array $event_vars)
{
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return [];
}
}

View File

@ -11,14 +11,16 @@
namespace App\Http\Middleware;
use App\Libraries\MultiDB;
use App\Models\Vendor;
use App\Models\VendorContact;
use Auth;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Utils\Ninja;
use App\Models\Vendor;
use App\Libraries\MultiDB;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Models\VendorContact;
use Illuminate\Support\Facades\Cache;
use App\Events\Vednor\VendorContactLoggedIn;
class VendorContactKeyLogin
{
@ -56,6 +58,8 @@ class VendorContactKeyLogin
$vendor_contact->save();
auth()->guard('vendor')->loginUsingId($vendor_contact->id, true);
event(new VendorContactLoggedIn($vendor_contact, $vendor_contact->company, Ninja::eventVars()));
if ($request->query('redirect') && ! empty($request->query('redirect'))) {
return redirect()->to($request->query('redirect'));
@ -72,7 +76,7 @@ class VendorContactKeyLogin
$vendor_contact->save();
auth()->guard('vendor')->loginUsingId($vendor_contact->id, true);
event(new VendorContactLoggedIn($vendor_contact, $vendor_contact->company, Ninja::eventVars()));
if ($request->query('next')) {
return redirect()->to($request->query('next'));
}

View File

@ -0,0 +1,45 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Listeners\Vendor;
use App\Libraries\MultiDB;
use Illuminate\Contracts\Queue\ShouldQueue;
class UpdateVendorContactLastLogin
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
MultiDB::setDb($event->company->db);
$contact = $event->contact;
$contact->last_login = now();
$contact->vendor->last_login = now();
$contact->push();
}
}

View File

@ -152,6 +152,7 @@ class Vendor extends BaseModel
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'last_login' => 'timestamp',
];
protected $touches = [];

View File

@ -106,6 +106,7 @@ class VendorContact extends Authenticatable implements HasLocalePreference
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
'last_login' => 'timestamp',
];
protected $fillable = [

View File

@ -70,13 +70,13 @@ use App\Events\Quote\QuoteWasRestored;
use App\Events\Client\ClientWasCreated;
use App\Events\Client\ClientWasDeleted;
use App\Events\Client\ClientWasUpdated;
use App\Events\Design\DesignWasDeleted;
use App\Events\Design\DesignWasUpdated;
use App\Events\Contact\ContactLoggedIn;
use App\Events\Credit\CreditWasCreated;
use App\Events\Credit\CreditWasDeleted;
use App\Events\Credit\CreditWasEmailed;
use App\Events\Credit\CreditWasUpdated;
use App\Events\Design\DesignWasDeleted;
use App\Events\Design\DesignWasUpdated;
use App\Events\Vendor\VendorWasCreated;
use App\Events\Vendor\VendorWasDeleted;
use App\Events\Vendor\VendorWasUpdated;
@ -85,10 +85,10 @@ use App\Observers\SubscriptionObserver;
use Illuminate\Mail\Events\MessageSent;
use App\Events\Client\ClientWasArchived;
use App\Events\Client\ClientWasRestored;
use App\Events\Design\DesignWasRestored;
use App\Events\Credit\CreditWasArchived;
use App\Events\Credit\CreditWasRestored;
use App\Events\Design\DesignWasArchived;
use App\Events\Design\DesignWasRestored;
use App\Events\Invoice\InvoiceWasViewed;
use App\Events\Misc\InvitationWasViewed;
use App\Events\Payment\PaymentWasVoided;
@ -133,6 +133,7 @@ use App\Listeners\User\UpdateUserLastLogin;
use App\Events\Document\DocumentWasArchived;
use App\Events\Document\DocumentWasRestored;
use App\Events\Invoice\InvoiceWasMarkedSent;
use App\Events\Vendor\VendorContactLoggedIn;
use App\Listeners\Quote\QuoteViewedActivity;
use App\Listeners\User\ArchivedUserActivity;
use App\Listeners\User\RestoredUserActivity;
@ -220,6 +221,7 @@ use App\Listeners\Invoice\InvoiceEmailFailedActivity;
use App\Events\PurchaseOrder\PurchaseOrderWasAccepted;
use App\Events\PurchaseOrder\PurchaseOrderWasArchived;
use App\Events\PurchaseOrder\PurchaseOrderWasRestored;
use App\Listeners\Vendor\UpdateVendorContactLastLogin;
use App\Events\RecurringQuote\RecurringQuoteWasCreated;
use App\Events\RecurringQuote\RecurringQuoteWasDeleted;
use App\Events\RecurringQuote\RecurringQuoteWasUpdated;
@ -615,6 +617,9 @@ class EventServiceProvider extends ServiceProvider
VendorWasUpdated::class => [
VendorUpdatedActivity::class,
],
VendorContactLoggedIn::class => [
UpdateVendorContactLastLogin::class,
],
\SocialiteProviders\Manager\SocialiteWasCalled::class => [
// ... Manager won't register drivers that are not added to this listener.
\SocialiteProviders\Apple\AppleExtendSocialite::class.'@handle',

View File

@ -44,6 +44,7 @@ class VendorContactTransformer extends EntityTransformer
'custom_value3' => $vendor->custom_value3 ?: '',
'custom_value4' => $vendor->custom_value4 ?: '',
'link' => $vendor->getLoginLink(),
'last_login' => 'timestamp',
];
}
}

View File

@ -11,13 +11,15 @@
namespace Tests\Feature;
use Tests\TestCase;
use App\Utils\Ninja;
use Tests\MockAccountData;
use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Session;
use App\Events\Vendor\VendorContactLoggedIn;
use Illuminate\Validation\ValidationException;
use Tests\MockAccountData;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;
/**
* @test
@ -44,6 +46,34 @@ class VendorApiTest extends TestCase
Model::reguard();
}
public function testVendorLoggedInEvents()
{
$v = \App\Models\Vendor::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id
]);
$vc = \App\Models\VendorContact::factory()->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'vendor_id' => $v->id
]);
$this->assertNull($v->last_login);
$this->assertNull($vc->last_login);
event(new VendorContactLoggedIn($vc, $this->company, Ninja::eventVars()));
$this->expectsEvents([VendorContactLoggedIn::class]);
// $vc->fresh();
// $v->fresh();
// $this->assertNotNull($vc->fresh()->last_login);
// $this->assertNotNull($v->fresh()->last_login);
}
public function testVendorLocale()
{
$v = \App\Models\Vendor::factory()->create([