Refactor activities

This commit is contained in:
David Bomba 2023-06-24 23:02:38 +10:00
parent 4065427da0
commit 455cc41d77
8 changed files with 49 additions and 40 deletions

View File

@ -103,9 +103,8 @@ class ActivityController extends BaseController
$system = ctrans('texts.system');
$data = $activities->cursor()->map(function ($activity) use ($system) {
$data = $activities->cursor()->map(function ($activity) {
return $activity->activity_string();
$arr =
[
'client' => $activity->client ? $activity->client : '',
@ -126,21 +125,31 @@ class ActivityController extends BaseController
];
$activity_array = $activity->toArray();
$activity_meta = [];
$activity_meta['hashed_id'] = $activity->hashed_id;
$activity_meta['notes'] = $activity->notes;
$activity_meta['ip'] = $activity->ip;
$activity_meta['activity_type_id'] = $activity->activity_type_id;
$activity_meta['created_at'] = $activity->created_at;
$activity_meta['updated_at'] = $activity->updated_at;
$activity_meta['is_system'] = $activity->is_system;
// $activity_meta['currency_id'] = $activity->currency_id;
return array_merge($arr, $activity_array);
});
return response()->json(['data' => $data->toArray()], 200);
}
elseif($request->has('reactv2')) {
/** @var \App\Models\User auth()->user() */
$user = auth()->user();
if (!$user->isAdmin()) {
$activities->where('user_id', auth()->user()->id);
}
$system = ctrans('texts.system');
$data = $activities->cursor()->map(function ($activity) use ($system) {
return $activity->activity_string();
});
return response()->json(['data' => $data->toArray()], 200);
}
return $this->listResponse($activities);
}

View File

@ -58,7 +58,6 @@ class InvoiceEmailFailedActivity implements ShouldQueue
$fields->client_contact_id = $event->invitation->client_contact_id;
$fields->company_id = $event->invitation->invoice->company_id;
$fields->activity_type_id = Activity::EMAIL_INVOICE_FAILED;
$fields->notes = $event->message;
$this->activity_repo->save($fields, $event->invitation->invoice, $event->event_vars);
}

View File

@ -46,7 +46,6 @@ class ArchivedUserActivity implements ShouldQueue
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->creating_user->id;
$fields->user_id = $user_id;
$fields->notes = $event->creating_user->present()->name.' Archived User '.$event->user->present()->name();
$fields->company_id = $event->company->id;
$fields->activity_type_id = Activity::ARCHIVE_USER;

View File

@ -46,7 +46,6 @@ class CreatedUserActivity implements ShouldQueue
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->creating_user->id;
$fields->user_id = $user_id;
$fields->notes = $event->creating_user->present()->name().' Created the user '.$event->user->present()->name();
$fields->company_id = $event->company->id;
$fields->activity_type_id = Activity::CREATE_USER;

View File

@ -51,8 +51,6 @@ class DeletedUserActivity implements ShouldQueue
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->creating_user->id;
$fields->user_id = $user_id;
$fields->notes = $event->creating_user->present()->name().' Deleted the user '.$event->user->present()->name();
$fields->company_id = $event->company->id;
$fields->activity_type_id = Activity::DELETE_USER;

View File

@ -45,7 +45,6 @@ class RestoredUserActivity implements ShouldQueue
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->creating_user->id;
$fields->user_id = $user_id;
$fields->notes = $event->creating_user->present()->name().' Restored user '.$event->user->present()->name();
$fields->company_id = $event->company->id;
$fields->activity_type_id = Activity::RESTORE_USER;

View File

@ -45,7 +45,6 @@ class UpdatedUserActivity implements ShouldQueue
$user_id = array_key_exists('user_id', $event->event_vars) ? $event->event_vars['user_id'] : $event->creating_user->id;
$fields->user_id = $user_id;
$fields->notes = $event->creating_user->present()->name().' Updated user '.$event->user->present()->name();
$fields->company_id = $event->company->id;
$fields->activity_type_id = Activity::UPDATE_USER;

View File

@ -484,40 +484,41 @@ class Activity extends StaticModel
':number',
':payment_amount',
':gateway',
':adjustment',
':ip',
':adjustment'
];
$found_variables = array_intersect(explode(" ",trans("texts.activity_{$this->activity_type_id}")), $intersect);
$replacements = [];
foreach($found_variables as $var){
$replacements[] = $this->matchVar($var);
}
foreach($found_variables as $var)
$replacements = array_merge($replacements, $this->matchVar($var));
foreach(['id', 'hashed_id','notes','created_at','activity_type_id'] as $var)
$replacements[] = $this->matchVar($var);
// return ctrans("texts.activity_{$this->activity_type_id}",$replacements);
if($this->client)
$replacements['client'] = ['label' => $this?->client?->present()->name() ?? '', 'hashed_id' => $this->client->hashed_id ?? ''];
if($this->vendor)
$replacements['vendor'] = ['label' => $this?->vendor?->present()->name() ?? '', 'hashed_id' => $this->vendor->hashed_id ?? ''];
$replacements['activity_type_id'] = $this->activity_type_id;
$replacements['id'] = $this->id;
$replacements['hashed_id'] = $this->hashed_id;
$replacements['notes'] = $this->notes ?? '';
$replacements['created_at'] = $this->created_at ?? '';
$replacements['ip'] = $this->ip ?? '';
return $replacements;
}
private function matchVar(string $variable)
{
$system = ctrans('texts.system');
return match($variable) {
'activity_type_id' => $translation = ['activity_type_id' => $this->activity_type_id],
'id' => $translation = ['id' => $this->id],
'hashed_id' => $translation = ['hashed_id' => $this->hashed_id],
'notes' => $translation = ['notes' => $this->notes ?? ''],
'created_at' => $translation = ['created_at' => $this->created_at ?? ''],
':invoice' => $translation = [substr($variable, 1) => [ 'label' => $this?->invoice?->number ?? '', 'hashed_id' => $this->invoice?->hashed_id ?? '']],
':client' => $translation = [substr($variable, 1) => [ 'label' => $this?->client?->present()->name() ?? '', 'hashed_id' => $this->client->hashed_id ?? '']],
':contact' => $translation = [substr($variable, 1) => [ 'label' => $this?->contact?->present()->name() ?? '', 'hashed_id' => $this->contact->client->hashed_id ?? '']],
':user' => $translation = [substr($variable, 1) => [ 'label' => $this?->user?->present()->name() ??'', 'hashed_id' => $this->user->hashed_id ?? '']],
':vendor' => $translation = [substr($variable, 1) => [ 'label' => $this?->vendor?->present()->name() ?? '', 'hashed_id' => $this->vendor->hashed_id ?? '']],
':contact' => $translation = $this->resolveContact(),
':user' => $translation = [substr($variable, 1) => [ 'label' => $this?->user?->present()->name() ?? $system, 'hashed_id' => $this->user->hashed_id ?? '']],
':quote' => $translation = [substr($variable, 1) => [ 'label' => $this?->quote?->number ?? '', 'hashed_id' => $this->quote->hashed_id ?? '']],
':credit' => $translation = [substr($variable, 1) => [ 'label' => $this?->credit?->number ?? '', 'hashed_id' => $this->credit->hashed_id ?? '']],
':payment' => $translation = [substr($variable, 1) => [ 'label' => $this?->payment?->number ?? '', 'hashed_id' => $this->payment->hashed_id ?? '']],
@ -527,17 +528,23 @@ class Activity extends StaticModel
':subscription' => $translation = [substr($variable, 1) => [ 'label' => $this?->subscription?->number ?? '', 'hashed_id' => $this->subscription->hashed_id ?? '' ]],
':recurring_invoice' => $translation = [substr($variable, 1) =>[ 'label' => $this?->recurring_invoice?->number ??'', 'hashed_id' => $this->recurring_invoice->hashed_id ?? '']],
':recurring_expense' => $translation = [substr($variable, 1) => [ 'label' => $this?->recurring_expense?->number ??'', 'hashed_id' => $this->recurring_expense->hashed_id ?? '']],
':payment_amount' => $translation = [substr($variable, 1) =>[ 'label' => Number::formatMoney($this?->payment?->amount, $this?->payment?->client) ?? '']],
':adjustment' => $translation = [substr($variable, 1) =>[ 'label' => Number::formatMoney($this?->payment?->refunded, $this?->payment?->client) ?? '']],
':payment_amount' => $translation = [substr($variable, 1) =>[ 'label' => Number::formatMoney($this?->payment?->amount, $this?->payment?->client) ?? '', 'hashed_id' => '']],
':adjustment' => $translation = [substr($variable, 1) =>[ 'label' => Number::formatMoney($this?->payment?->refunded, $this?->payment?->client) ?? '', 'hashed_id' => '']],
':ip' => $translation = [ 'ip' => $this->ip ?? ''],
default => $translation = [substr($variable, 1) => ''],
default => $translation = [],
};
return $translation;
}
private function returnEntityValue(string $variable)
private function resolveContact() : array
{
$contact = $this->contact ? $this->contact : $this->vendor_contact;
$entity = $this->contact ? $this->client : $this->vendor;
$contact_entity = $this->contact ? 'clients' : 'vendors';
return ['contact' => [ 'label' => $contact?->present()->name() ?? '', 'hashed_id' => $entity->hashed_id ?? '', 'contact_entity' => $contact_entity]];
}
}