Feature: Customer Account Services
This commit is contained in:
@@ -2,25 +2,31 @@
|
||||
|
||||
namespace Modules\Core\Logging;
|
||||
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Thin wrapper around Spatie Activity Log that standardises the log channel,
|
||||
* actor (authenticated staff member), and property shape for all domain events.
|
||||
* actor, and property shape for all domain events.
|
||||
*
|
||||
* All logs are written to the 'lunar' channel. The subject is always an
|
||||
* Eloquent model, and the actor is resolved from the 'staff' guard at call time.
|
||||
* Eloquent model. $causer defaults to the 'staff' guard's current user —
|
||||
* every existing caller of this class is admin-side — but can be passed
|
||||
* explicitly for a non-staff actor (e.g. a customer editing their own
|
||||
* address on the `web` guard — see Modules\Core\Customer\Services\
|
||||
* CustomerAccountService, which passes the acting User rather than
|
||||
* relying on this default resolving to null for a web-guard session).
|
||||
*/
|
||||
class ActivityLogService
|
||||
{
|
||||
/**
|
||||
* Log a creation event. $attributes describes the initial state.
|
||||
*/
|
||||
public function created(Model $subject, array $attributes): void
|
||||
public function created(Model $subject, array $attributes, ?Authenticatable $causer = null): void
|
||||
{
|
||||
activity('lunar')
|
||||
->performedOn($subject)
|
||||
->causedBy(auth('staff')->user())
|
||||
->causedBy($causer ?? auth('staff')->user())
|
||||
->withProperties(['attributes' => $attributes])
|
||||
->log('created');
|
||||
}
|
||||
@@ -28,11 +34,11 @@ class ActivityLogService
|
||||
/**
|
||||
* Log an update event. $old holds the previous values, $attributes the new ones.
|
||||
*/
|
||||
public function updated(Model $subject, array $old, array $attributes): void
|
||||
public function updated(Model $subject, array $old, array $attributes, ?Authenticatable $causer = null): void
|
||||
{
|
||||
activity('lunar')
|
||||
->performedOn($subject)
|
||||
->causedBy(auth('staff')->user())
|
||||
->causedBy($causer ?? auth('staff')->user())
|
||||
->withProperties(['old' => $old, 'attributes' => $attributes])
|
||||
->log('updated');
|
||||
}
|
||||
@@ -40,11 +46,11 @@ class ActivityLogService
|
||||
/**
|
||||
* Log a failed operation. $attributes provides context (e.g. error message, service).
|
||||
*/
|
||||
public function failed(Model $subject, array $attributes): void
|
||||
public function failed(Model $subject, array $attributes, ?Authenticatable $causer = null): void
|
||||
{
|
||||
activity('lunar')
|
||||
->performedOn($subject)
|
||||
->causedBy(auth('staff')->user())
|
||||
->causedBy($causer ?? auth('staff')->user())
|
||||
->withProperties(['attributes' => $attributes])
|
||||
->log('failed');
|
||||
}
|
||||
@@ -52,11 +58,11 @@ class ActivityLogService
|
||||
/**
|
||||
* Log a deletion event. $attributes provides context (e.g. reason, name).
|
||||
*/
|
||||
public function deleted(Model $subject, array $attributes): void
|
||||
public function deleted(Model $subject, array $attributes, ?Authenticatable $causer = null): void
|
||||
{
|
||||
activity('lunar')
|
||||
->performedOn($subject)
|
||||
->causedBy(auth('staff')->user())
|
||||
->causedBy($causer ?? auth('staff')->user())
|
||||
->withProperties(['attributes' => $attributes])
|
||||
->log('deleted');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user