mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Merge pull request #16418 from marcusmoore/chore/custom-report-template-activity-log-removal
Stop reporting report template events to activity log
This commit is contained in:
commit
fae79a91f6
|
@ -44,44 +44,6 @@ class ReportTemplate extends Model
|
|||
$builder->where('created_by', auth()->id());
|
||||
}
|
||||
});
|
||||
|
||||
static::created(function (ReportTemplate $reportTemplate) {
|
||||
$logAction = new Actionlog([
|
||||
'item_type' => ReportTemplate::class,
|
||||
'item_id' => $reportTemplate->id,
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
$logAction->logaction('create');
|
||||
});
|
||||
|
||||
static::updated(function (ReportTemplate $reportTemplate) {
|
||||
$changed = [];
|
||||
|
||||
foreach ($reportTemplate->getDirty() as $key => $value) {
|
||||
$changed[$key] = [
|
||||
'old' => $reportTemplate->getOriginal($key),
|
||||
'new' => $reportTemplate->getAttribute($key),
|
||||
];
|
||||
}
|
||||
|
||||
$logAction = new Actionlog();
|
||||
$logAction->item_type = ReportTemplate::class;
|
||||
$logAction->item_id = $reportTemplate->id;
|
||||
$logAction->created_by = auth()->id();
|
||||
$logAction->log_meta = json_encode($changed);
|
||||
$logAction->logaction('update');
|
||||
});
|
||||
|
||||
static::deleted(function (ReportTemplate $reportTemplate) {
|
||||
$logAction = new Actionlog([
|
||||
'item_type' => ReportTemplate::class,
|
||||
'item_id' => $reportTemplate->id,
|
||||
'created_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
$logAction->logaction('delete');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
use App\Models\ReportTemplate;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
DB::table("action_logs")
|
||||
->where("item_type", ReportTemplate::class)
|
||||
->whereIn("action_type", ["create", "update", "delete"])
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
// nothing to do here...
|
||||
}
|
||||
};
|
|
@ -1,71 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\ActionLogTransformer;
|
||||
|
||||
use App\Http\Transformers\ActionlogsTransformer;
|
||||
use App\Models\Actionlog;
|
||||
use App\Models\ReportTemplate;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('activity-logging')]
|
||||
class ReportTemplateActionLogTransformerTest extends TestCase
|
||||
{
|
||||
public function testLogEntryForCreatingReportTemplateCanBeTransformed()
|
||||
{
|
||||
ReportTemplate::factory()->create();
|
||||
|
||||
$actionLogs = Actionlog::query()
|
||||
->whereMorphedTo('item', ReportTemplate::class)
|
||||
->where('action_type', 'create')
|
||||
->get();
|
||||
|
||||
// should be created when ActionLog is created
|
||||
$this->assertCount(1, $actionLogs);
|
||||
|
||||
$results = (new ActionlogsTransformer())->transformActionlogs($actionLogs, 10);
|
||||
|
||||
$this->assertArrayHasKey('rows', $results);
|
||||
$this->assertCount(1, $results['rows']);
|
||||
}
|
||||
|
||||
public function testLogEntryForUpdatingReportTemplateCanBeDisplayedTransformed()
|
||||
{
|
||||
$reportTemplate = ReportTemplate::factory()->create();
|
||||
|
||||
$reportTemplate->update([
|
||||
'options' => ['new' => 'value']
|
||||
]);
|
||||
|
||||
$actionLogs = Actionlog::query()
|
||||
->whereMorphedTo('item', ReportTemplate::class)
|
||||
->where('action_type', 'update')
|
||||
->get();
|
||||
|
||||
$this->assertCount(1, $actionLogs);
|
||||
|
||||
$results = (new ActionlogsTransformer())->transformActionlogs($actionLogs, 10);
|
||||
|
||||
$this->assertArrayHasKey('rows', $results);
|
||||
$this->assertCount(1, $results['rows']);
|
||||
}
|
||||
|
||||
public function testLogEntryForDeletingReportTemplateCanBeDisplayedTransformed()
|
||||
{
|
||||
$reportTemplate = ReportTemplate::factory()->create();
|
||||
|
||||
$reportTemplate->delete();
|
||||
|
||||
$actionLogs = Actionlog::query()
|
||||
->whereMorphedTo('item', ReportTemplate::class)
|
||||
->where('action_type', 'delete')
|
||||
->get();
|
||||
|
||||
$this->assertCount(1, $actionLogs);
|
||||
|
||||
$results = (new ActionlogsTransformer())->transformActionlogs($actionLogs, 10);
|
||||
|
||||
$this->assertArrayHasKey('rows', $results);
|
||||
$this->assertCount(1, $results['rows']);
|
||||
}
|
||||
}
|
|
@ -1,100 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Models\ReportTemplates;
|
||||
|
||||
use App\Models\ReportTemplate;
|
||||
use App\Models\User;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use Tests\TestCase;
|
||||
|
||||
#[Group('custom-reporting')]
|
||||
#[Group('activity-logging')]
|
||||
class ReportTemplateActivityLoggingTest extends TestCase
|
||||
{
|
||||
public function testCreatingReportTemplateIsLogged()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$reportTemplate = ReportTemplate::factory()->create();
|
||||
|
||||
$this->assertDatabaseHas('action_logs', [
|
||||
'created_by' => $user->id,
|
||||
'action_type' => 'create',
|
||||
'target_id' => null,
|
||||
'target_type' => null,
|
||||
'item_type' => ReportTemplate::class,
|
||||
'item_id' => $reportTemplate->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testUpdatingReportTemplateIsLogged()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$reportTemplate = ReportTemplate::factory()->create([
|
||||
'name' => 'Name A',
|
||||
'options' => [
|
||||
'company' => '1',
|
||||
'location' => '1',
|
||||
'by_company_id' => ['1'],
|
||||
'by_location_id' => ['17'],
|
||||
],
|
||||
]);
|
||||
|
||||
$reportTemplate->update([
|
||||
'name' => 'Another Name',
|
||||
'options' => [
|
||||
'company' => '1',
|
||||
'by_company_id' => ['1'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertDatabaseHas('action_logs', [
|
||||
'created_by' => $user->id,
|
||||
'action_type' => 'update',
|
||||
'target_id' => null,
|
||||
'target_type' => null,
|
||||
'item_type' => ReportTemplate::class,
|
||||
'item_id' => $reportTemplate->id,
|
||||
'log_meta' => json_encode([
|
||||
'name' => [
|
||||
'old' => 'Name A',
|
||||
'new' => 'Another Name'
|
||||
],
|
||||
'options' => [
|
||||
'old' => [
|
||||
'company' => '1',
|
||||
'location' => '1',
|
||||
'by_company_id' => ['1'],
|
||||
'by_location_id' => ['17'],
|
||||
],
|
||||
'new' => [
|
||||
'company' => '1',
|
||||
'by_company_id' => ['1'],
|
||||
],
|
||||
],
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testDeletingReportTemplateIsLogged()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$reportTemplate = ReportTemplate::factory()->create();
|
||||
|
||||
$reportTemplate->delete();
|
||||
|
||||
$this->assertDatabaseHas('action_logs', [
|
||||
'created_by' => $user->id,
|
||||
'action_type' => 'delete',
|
||||
'target_id' => null,
|
||||
'target_type' => null,
|
||||
'item_type' => ReportTemplate::class,
|
||||
'item_id' => $reportTemplate->id,
|
||||
]);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue