Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add audit logs for company creation #3592

Merged
merged 5 commits into from
Feb 17, 2020
Merged
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
20 changes: 11 additions & 9 deletions app/Helpers/AuditLogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ public static function getCollectionOfAuditForSettings($logs): Collection
// in that case, we will display a link to point to this contact
$contact = Contact::findOrFail($log->object->{'contact_id'});
$description = trans(
'app.settings_log_'.$log->action.'_with_name_with_link',
'logs.settings_log_'.$log->action.'_with_name_with_link',
[
'link' => '/people/'.$contact->hashId(),
'name' => $contact->name,
]
);
} catch (ModelNotFoundException $e) {
// the contact doesn't exist anymore, we don't need a link
$description = trans('app.settings_log_'.$log->action.'_with_name', ['name' => $log->object->{'contact_name'}]);
// the contact doesn't exist anymore, we don't need a link, we'll only display a name
$description = trans('logs.settings_log_'.$log->action.'_with_name', ['name' => $log->object->{'contact_name'}]);
}

$logsCollection->push([
'author_name' => ($log->author) ? $log->author->name : $log->author_name,
'description' => $description,
'audited_at' => DateHelper::getShortDateWithTime($log->audited_at),
]);
} else {
$description = trans('logs.settings_log_'.$log->action, ['name' => $log->object->{'name'}]);
}

$logsCollection->push([
'author_name' => ($log->author) ? $log->author->name : $log->author_name,
'description' => $description,
'audited_at' => DateHelper::getShortDateWithTime($log->audited_at),
]);
}

return $logsCollection;
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Api/Account/ApiCompanyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function store(Request $request)
+
[
'account_id' => auth()->user()->account->id,
'author_id' => auth()->user()->id,
]
);
} catch (ModelNotFoundException $e) {
Expand Down
32 changes: 32 additions & 0 deletions app/Services/Account/Company/CreateCompany.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App\Services\Account\Company;

use App\Models\User\User;
use App\Services\BaseService;
use function Safe\json_encode;
use App\Models\Account\Company;
use Safe\Exceptions\JsonException;
use App\Jobs\AuditLog\LogAccountAudit;

class CreateCompany extends BaseService
{
Expand All @@ -16,6 +20,7 @@ public function rules()
{
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'name' => 'required|string|max:255',
'website' => 'nullable|string|max:255',
'number_of_employees' => 'nullable|integer',
Expand All @@ -32,11 +37,38 @@ public function execute(array $data): Company
{
$this->validate($data);

$this->log($data);

return Company::create([
'account_id' => $data['account_id'],
'name' => $data['name'],
'website' => $this->nullOrValue($data, 'website'),
'number_of_employees' => $this->nullOrValue($data, 'number_of_employees'),
]);
}

/**
* Add an audit log.
*
* @param array $data
* @return void
* @throws JsonException
*/
private function log(array $data): void
{
$author = User::find($data['author_id']);

LogAccountAudit::dispatch([
'action' => 'company_created',
'account_id' => $data['account_id'],
'about_contact_id' => null,
'author_id' => $author->id,
'author_name' => $author->name,
'audited_at' => now(),
'should_appear_on_dashboard' => true,
'objects' => json_encode([
'name' => $data['name'],
]),
]);
}
}
2 changes: 1 addition & 1 deletion app/Services/Instance/AuditLog/LogAccountAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function rules(): array
return [
'account_id' => 'required|integer|exists:accounts,id',
'author_id' => 'required|integer|exists:users,id',
'about_contact_id' => 'sometimes|integer|exists:contacts,id',
'about_contact_id' => 'nullable|integer|exists:contacts,id',
'author_name' => 'required|string|max:255',
'audited_at' => 'required|date',
'action' => 'required|string|max:255',
Expand Down
2 changes: 1 addition & 1 deletion public/js/langs/en.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/vendor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"/js/manifest.js": "/js/manifest.js?id=7db827d654313dce4250",
"/js/vendor.js": "/js/vendor.js?id=4aad3b6b61d82b195c8c",
"/js/vendor.js": "/js/vendor.js?id=58f8c918d131f9533b33",
"/js/app.js": "/js/app.js?id=af51dc6a63b48b956ea9",
"/css/app-ltr.css": "/css/app-ltr.css?id=39bbf162ef135d3dc3b8",
"/css/app-rtl.css": "/css/app-rtl.css?id=e3525984bd5a135cde0f",
Expand Down
14 changes: 0 additions & 14 deletions resources/lang/en/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,4 @@
'dav_birthdays_description' => ':name’s contact’s birthdays',
'dav_tasks' => 'Tasks',
'dav_tasks_description' => ':name’s tasks',

// audit log
'contact_log_contact_created' => 'Created the contact.',
'settings_log_contact_created_with_name' => 'Added :name as a contact.',
'settings_log_contact_created_with_name_with_link' => 'Added <a href=":link">:name</a> as a contact.',
'contact_log_contact_description_updated' => 'Updated the description.',
'settings_log_contact_description_updated_with_name' => 'Updated the description of :name.',
'settings_log_contact_description_updated_with_name_with_link' => 'Updated the description of <a href=":link">:name</a>.',
'contact_log_contact_description_cleared' => 'Cleared the description.',
'settings_log_contact_description_cleared_with_name' => 'Cleared the description of :name.',
'settings_log_contact_description_cleared_with_name_with_link' => 'Cleared the description of <a href=":link">:name</a>.',
'contact_log_contact_work_updated' => 'Updated work information.',
'settings_log_contact_work_updated_with_name' => 'Updated work information of :name.',
'settings_log_contact_work_updated_with_name_with_link' => 'Updated work information of <a href=":link">:name</a>.',
];
29 changes: 29 additions & 0 deletions resources/lang/en/logs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

// audit log

return [

// contact creation
'contact_log_contact_created' => 'Created the contact.',
'settings_log_contact_created_with_name' => 'Added :name as a contact.',
'settings_log_contact_created_with_name_with_link' => 'Added <a href=":link">:name</a> as a contact.',

// contat description update
'contact_log_contact_description_updated' => 'Updated the description.',
'settings_log_contact_description_updated_with_name' => 'Updated the description of :name.',
'settings_log_contact_description_updated_with_name_with_link' => 'Updated the description of <a href=":link">:name</a>.',

// contact description clear
'contact_log_contact_description_cleared' => 'Cleared the description.',
'settings_log_contact_description_cleared_with_name' => 'Cleared the description of :name.',
'settings_log_contact_description_cleared_with_name_with_link' => 'Cleared the description of <a href=":link">:name</a>.',

// contact work information update
'contact_log_contact_work_updated' => 'Updated work information.',
'settings_log_contact_work_updated_with_name' => 'Updated work information of :name.',
'settings_log_contact_work_updated_with_name_with_link' => 'Updated work information of <a href=":link">:name</a>.',

// company created
'settings_log_company_created' => 'Created a company called :name.',
];
2 changes: 1 addition & 1 deletion tests/Unit/Helpers/AuditLogHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function it_prepares_a_collection_of_audit_logs_without_likns_for_the_set
);

$this->assertEquals(
'app.settings_log_account_created_with_name',
'logs.settings_log_account_created_with_name',
$collection[0]['description']
);
}
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Services/Account/Company/CreateCompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace Tests\Unit\Services\Account\Place;

use Tests\TestCase;
use App\Models\User\User;
use App\Models\Account\Account;
use App\Models\Account\Company;
use Illuminate\Support\Facades\Queue;
use App\Jobs\AuditLog\LogAccountAudit;
use Illuminate\Validation\ValidationException;
use App\Services\Account\Company\CreateCompany;
use Illuminate\Foundation\Testing\DatabaseTransactions;
Expand All @@ -16,10 +19,16 @@ class CreateCompanyTest extends TestCase
/** @test */
public function it_stores_a_company()
{
Queue::fake();

$account = factory(Account::class)->create([]);
$user = factory(User::class)->create([
'account_id' => $account->id,
]);

$request = [
'account_id' => $account->id,
'author_id' => $user->id,
'name' => 'central perk',
'website' => 'https://centralperk.com',
'number_of_employees' => 3,
Expand All @@ -39,6 +48,16 @@ public function it_stores_a_company()
Company::class,
$company
);

Queue::assertPushed(LogAccountAudit::class, function ($job) use ($user) {
return $job->auditLog['action'] === 'company_created' &&
$job->auditLog['author_id'] === $user->id &&
$job->auditLog['about_contact_id'] === null &&
$job->auditLog['should_appear_on_dashboard'] === true &&
$job->auditLog['objects'] === json_encode([
'name' => 'central perk',
]);
});
}

/** @test */
Expand Down