Skip to content
This repository was archived by the owner on Feb 8, 2025. It is now read-only.

refactor: simplify log message handling #1103

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
50 changes: 46 additions & 4 deletions app/Helpers/LogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@
use App\Models\Company\TeamLog;
use App\Models\Company\AuditLog;
use App\Models\Company\EmployeeLog;
use App\Models\Company\Logger;

class LogHelper
{
private static function processLog(Logger $log, string $prefix): string
{
$key = "account.{$prefix}_{$log->action}";

/** @var \Illuminate\Translation\Translator $translator */
$translator = trans();
if (! $translator->has($key)) {
throw new \Exception('missing message');
}
$text = trans($key, [], config('app.locale'));

preg_match_all('/\:(?<replace>[^ .]+)/', $text, $matched);

$replace = [];
foreach ($matched['replace'] as $match) {
if ($match !== '') {
if (!isset($log->object->{$match})) {
throw new \Exception('missing key');
}
$replace[$match] = $log->object->{$match};
}
}

return trans($key, $replace);
}

/**
* Return an sentence explaining what the log contains.
* A log is stored in a json file and needs some kind of processing to make
Expand All @@ -19,6 +46,11 @@ class LogHelper
*/
public static function processAuditLog(AuditLog $log): string
{
try {
return self::processLog($log, 'log');
} catch (\Exception $e) {
}

switch ($log->action) {
case 'account_created':
$sentence = trans('account.log_account_created');
Expand Down Expand Up @@ -50,7 +82,7 @@ public static function processAuditLog(AuditLog $log): string

case 'team_created':
$sentence = trans('account.log_team_created', [
'name' => $log->object->{'team_name'},
'team_name' => $log->object->{'team_name'},
]);
break;

Expand Down Expand Up @@ -1308,6 +1340,11 @@ public static function processAuditLog(AuditLog $log): string
*/
public static function processEmployeeLog(EmployeeLog $log): string
{
try {
return self::processLog($log, 'employee_log');
} catch (\Exception $e) {
}

switch ($log->action) {
case 'employee_created':
$sentence = trans('account.employee_log_employee_created');
Expand Down Expand Up @@ -1848,15 +1885,15 @@ public static function processEmployeeLog(EmployeeLog $log): string
break;

case 'meeting_decision_updated':
$sentence = trans('account.employee_log_log_meeting_decision_updated', [
$sentence = trans('account.employee_log_meeting_decision_updated', [
'group_id' => $log->object->{'group_id'},
'group_name' => $log->object->{'group_name'},
'meeting_id' => $log->object->{'meeting_id'},
]);
break;

case 'add_guest_to_meeting':
$sentence = trans('account.employee_log_log_add_guest_to_meeting', [
$sentence = trans('account.employee_log_add_guest_to_meeting', [
'group_id' => $log->object->{'group_id'},
'group_name' => $log->object->{'group_name'},
'meeting_id' => $log->object->{'meeting_id'},
Expand All @@ -1880,10 +1917,15 @@ public static function processEmployeeLog(EmployeeLog $log): string
*/
public static function processTeamLog(TeamLog $log): string
{
try {
return self::processLog($log, 'team_log');
} catch (\Exception $e) {
}

switch ($log->action) {
case 'team_created':
$sentence = trans('account.team_log_team_created', [
'name' => $log->object->{'team_name'},
'team_name' => $log->object->{'team_name'},
]);
break;

Expand Down
15 changes: 1 addition & 14 deletions app/Models/Company/AuditLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace App\Models\Company;

use App\Helpers\LogHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class AuditLog extends Model
class AuditLog extends Logger
{
use HasFactory;

Expand Down Expand Up @@ -57,18 +56,6 @@ public function author()
return $this->belongsTo(Employee::class);
}

/**
* Get the JSON object.
*
* @param mixed $value

* @return mixed
*/
public function getObjectAttribute($value)
{
return json_decode($this->objects);
}

/**
* Get the content of the audit log, if defined.
*
Expand Down
15 changes: 1 addition & 14 deletions app/Models/Company/EmployeeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

use App\Helpers\LogHelper;
use App\Helpers\DateHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
* Audit log at the employee level.
*/
class EmployeeLog extends Model
class EmployeeLog extends Logger
{
use HasFactory;

Expand Down Expand Up @@ -61,18 +60,6 @@ public function author()
return $this->belongsTo(Employee::class);
}

/**
* Get the JSON object.
*
* @param mixed $value
*
* @return array
*/
public function getObjectAttribute($value)
{
return json_decode($this->objects);
}

/**
* Get the date of the employee log.
*
Expand Down
20 changes: 20 additions & 0 deletions app/Models/Company/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models\Company;

use Illuminate\Database\Eloquent\Model;

class Logger extends Model
{
/**
* Get the JSON object.
*
* @param mixed $value

* @return mixed
*/
public function getObjectAttribute($value)
{
return json_decode($this->objects);
}
}
15 changes: 1 addition & 14 deletions app/Models/Company/TeamLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

use App\Helpers\LogHelper;
use App\Helpers\DateHelper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class TeamLog extends Model
class TeamLog extends Logger
{
use HasFactory;

Expand Down Expand Up @@ -58,18 +57,6 @@ public function author()
return $this->belongsTo(Employee::class);
}

/**
* Get the JSON object.
*
* @param mixed $value
*
* @return array
*/
public function getObjectAttribute($value)
{
return json_decode($this->objects);
}

/**
* Get the date of the team log.
*
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@
'employee_log_meeting_decision_updated' => 'Updated a meeting decision about an agenda item in a meeting in the group called :group_name.',
'employee_log_add_guest_to_meeting' => 'Has been added in a meeting of the project called :project_name.',
// team logs
'team_log_team_created' => 'Created the team.',
'team_log_team_created' => 'Created the team :team_name.',
'team_log_team_updated' => 'Changed the name from :old_name to :new_name.',
'team_log_employee_added_to_team' => 'Added :employee_name to the team.',
'team_log_employee_removed_from_team' => 'Removed :employee_name from the team.',
Expand Down
13 changes: 9 additions & 4 deletions tests/Unit/Helpers/LogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function it_returns_the_string_explaining_the_audit_log(): void
]);

$this->assertIsString(LogHelper::processAuditLog($log));
$this->assertNotEquals(trans('account.log_employee_invited_to_become_user'), LogHelper::processAuditLog($log));
}

/** @test */
Expand All @@ -52,17 +53,21 @@ public function it_returns_the_string_explaining_the_employee_log(): void
/** @test */
public function it_returns_the_string_explaining_the_team_log(): void
{
$team = Team::factory()->create([]);
$team = Team::factory()->create([
'name' => 'The best'
]);

$log = TeamLog::factory()->create([
'action' => 'team_log_team_created',
'action' => 'team_created',
'objects' => json_encode([
'team_name' => $team->id,
'team_id' => $team->id,
'team_name' => $team->name,
]),
'team_id' => $team->id,
]);

$this->assertIsString(LogHelper::processTeamLog($log));
$this->assertEquals('Created the team The best.', LogHelper::processTeamLog($log));
$this->assertNotEquals(trans('account.team_log_team_created'), LogHelper::processTeamLog($log));
}

/** @test */
Expand Down