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

Resolve Duplicate Activity Logs for Imports #14172

Merged
merged 6 commits into from
Feb 15, 2024
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
5 changes: 2 additions & 3 deletions app/Importer/AccessoryImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public function createAccessoryIfNotExists($row)
$this->item['min_amt'] = $this->findCsvMatch($row, "min_amt");
$accessory->fill($this->sanitizeItemForStoring($accessory));

//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
// $accessory->unsetEventDispatcher();
// This sets an attribute on the Loggable trait for the action log
$accessory->setImported(true);
if ($accessory->save()) {
$accessory->logCreate('Imported using CSV Importer');
$this->log('Accessory '.$this->item['name'].' was created');

return;
Expand Down
4 changes: 2 additions & 2 deletions app/Importer/AssetImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ public function createAssetIfNotExists(array $row)
$asset->{$custom_field} = $val;
}
}

// This sets an attribute on the Loggable trait for the action log
$asset->setImported(true);
if ($asset->save()) {

$asset->logCreate(trans('general.importer.import_note'));
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');

// If we have a target to checkout to, lets do so.
Expand Down
6 changes: 3 additions & 3 deletions app/Importer/ComponentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function createComponentIfNotExists()
$this->log('No matching component, creating one');
$component = new Component;
$component->fill($this->sanitizeItemForStoring($component));
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
$component->unsetEventDispatcher();

// This sets an attribute on the Loggable trait for the action log
$component->setImported(true);
if ($component->save()) {
$component->logCreate('Imported using CSV Importer');
$this->log('Component '.$this->item['name'].' was created');

// If we have an asset tag, checkout to that asset.
Expand Down
6 changes: 3 additions & 3 deletions app/Importer/ConsumableImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public function createConsumableIfNotExists($row)
$this->item['item_no'] = trim($this->findCsvMatch($row, 'item_number'));
$this->item['min_amt'] = trim($this->findCsvMatch($row, "min_amt"));
$consumable->fill($this->sanitizeItemForStoring($consumable));
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
$consumable->unsetEventDispatcher();

// This sets an attribute on the Loggable trait for the action log
$consumable->setImported(true);
if ($consumable->save()) {
$consumable->logCreate('Imported using CSV Importer');
$this->log('Consumable '.$this->item['name'].' was created');

return;
Expand Down
6 changes: 3 additions & 3 deletions app/Importer/LicenseImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public function createLicenseIfNotExists(array $row)
} else {
$license->fill($this->sanitizeItemForStoring($license));
}
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
// $license->unsetEventDispatcher();

// This sets an attribute on the Loggable trait for the action log
$license->setImported(true);
if ($license->save()) {
$license->logCreate('Imported using csv importer');
$this->log('License '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');

// Lets try to checkout seats if the fields exist and we have seats.
Expand Down
16 changes: 15 additions & 1 deletion app/Models/Actionlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Actionlog extends SnipeModel
{
use HasFactory;

// This is to manually set the source (via setActionSource()) for determineActionSource()
protected ?string $source = null;

protected $presenter = \App\Presenters\ActionlogPresenter::class;
use SoftDeletes;
use Presentable;
Expand Down Expand Up @@ -341,7 +344,12 @@ public function getListingOfActionLogsChronologicalOrder()
* @since v6.3.0
* @return string
*/
public function determineActionSource() {
public function determineActionSource(): string
{
// This is a manually set source
if($this->source) {
return $this->source;
}

// This is an API call
if (((request()->header('content-type') && (request()->header('accept'))=='application/json'))
Expand All @@ -358,4 +366,10 @@ public function determineActionSource() {
return 'cli/unknown';

}

// Manually sets $this->source for determineActionSource()
public function setActionSource($source = null): void
{
$this->source = $source;
}
}
8 changes: 8 additions & 0 deletions app/Models/Loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

trait Loggable
{
// an attribute for setting whether or not the item was imported
public ?bool $imported = false;

/**
* @author Daniel Meltzer <[email protected]>
* @since [v3.4]
Expand All @@ -18,6 +21,11 @@ public function log()
return $this->morphMany(Actionlog::class, 'item');
}

public function setImported(bool $bool): void
{
$this->imported = $bool;
}

/**
* @author Daniel Meltzer <[email protected]>
* @since [v3.4]
Expand Down
3 changes: 3 additions & 0 deletions app/Observers/AccessoryObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function created(Accessory $accessory)
$logAction->item_id = $accessory->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->user_id = Auth::id();
if($accessory->imported) {
$logAction->setActionSource('importer');
}
$logAction->logaction('create');
}

Expand Down
3 changes: 3 additions & 0 deletions app/Observers/AssetObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ public function created(Asset $asset)
$logAction->item_id = $asset->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->user_id = Auth::id();
if($asset->imported) {
$logAction->setActionSource('importer');
}
$logAction->logaction('create');
}

Expand Down
3 changes: 3 additions & 0 deletions app/Observers/ComponentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function created(Component $component)
$logAction->item_id = $component->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->user_id = Auth::id();
if($component->imported) {
$logAction->setActionSource('importer');
}
$logAction->logaction('create');
}

Expand Down
3 changes: 3 additions & 0 deletions app/Observers/ConsumableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function created(Consumable $consumable)
$logAction->item_id = $consumable->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->user_id = Auth::id();
if($consumable->imported) {
$logAction->setActionSource('importer');
}
$logAction->logaction('create');
}

Expand Down
3 changes: 3 additions & 0 deletions app/Observers/LicenseObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function created(License $license)
$logAction->item_id = $license->id;
$logAction->created_at = date('Y-m-d H:i:s');
$logAction->user_id = Auth::id();
if($license->imported) {
$logAction->setActionSource('importer');
}
$logAction->logaction('create');
}

Expand Down
2 changes: 2 additions & 0 deletions sample_csvs/components-sample.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Item Name,Purchase Date,Purchase Cost,Location,Company,Order Number,Serial number,Category,Quantity
RTX 4080,2024-01-01,5000.00,Austin,Grokability,2790,123456789,GPU,10
Loading