mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-11 22:07:29 -08:00
Merge pull request #14172 from spencerrlongg/bug/sc-23514
Resolve Duplicate Activity Logs for Imports
This commit is contained in:
commit
b871813cfd
|
@ -46,10 +46,9 @@ class AccessoryImporter extends ItemImporter
|
||||||
$this->item['min_amt'] = $this->findCsvMatch($row, "min_amt");
|
$this->item['min_amt'] = $this->findCsvMatch($row, "min_amt");
|
||||||
$accessory->fill($this->sanitizeItemForStoring($accessory));
|
$accessory->fill($this->sanitizeItemForStoring($accessory));
|
||||||
|
|
||||||
//FIXME: this disables model validation. Need to find a way to avoid double-logs without breaking everything.
|
// This sets an attribute on the Loggable trait for the action log
|
||||||
// $accessory->unsetEventDispatcher();
|
$accessory->setImported(true);
|
||||||
if ($accessory->save()) {
|
if ($accessory->save()) {
|
||||||
$accessory->logCreate('Imported using CSV Importer');
|
|
||||||
$this->log('Accessory '.$this->item['name'].' was created');
|
$this->log('Accessory '.$this->item['name'].' was created');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -135,10 +135,10 @@ class AssetImporter extends ItemImporter
|
||||||
$asset->{$custom_field} = $val;
|
$asset->{$custom_field} = $val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// This sets an attribute on the Loggable trait for the action log
|
||||||
|
$asset->setImported(true);
|
||||||
if ($asset->save()) {
|
if ($asset->save()) {
|
||||||
|
|
||||||
$asset->logCreate(trans('general.importer.import_note'));
|
|
||||||
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
|
$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.
|
// If we have a target to checkout to, lets do so.
|
||||||
|
|
|
@ -48,10 +48,10 @@ class ComponentImporter extends ItemImporter
|
||||||
$this->log('No matching component, creating one');
|
$this->log('No matching component, creating one');
|
||||||
$component = new Component;
|
$component = new Component;
|
||||||
$component->fill($this->sanitizeItemForStoring($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()) {
|
if ($component->save()) {
|
||||||
$component->logCreate('Imported using CSV Importer');
|
|
||||||
$this->log('Component '.$this->item['name'].' was created');
|
$this->log('Component '.$this->item['name'].' was created');
|
||||||
|
|
||||||
// If we have an asset tag, checkout to that asset.
|
// If we have an asset tag, checkout to that asset.
|
||||||
|
|
|
@ -45,10 +45,10 @@ class ConsumableImporter extends ItemImporter
|
||||||
$this->item['item_no'] = trim($this->findCsvMatch($row, 'item_number'));
|
$this->item['item_no'] = trim($this->findCsvMatch($row, 'item_number'));
|
||||||
$this->item['min_amt'] = trim($this->findCsvMatch($row, "min_amt"));
|
$this->item['min_amt'] = trim($this->findCsvMatch($row, "min_amt"));
|
||||||
$consumable->fill($this->sanitizeItemForStoring($consumable));
|
$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()) {
|
if ($consumable->save()) {
|
||||||
$consumable->logCreate('Imported using CSV Importer');
|
|
||||||
$this->log('Consumable '.$this->item['name'].' was created');
|
$this->log('Consumable '.$this->item['name'].' was created');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -85,10 +85,10 @@ class LicenseImporter extends ItemImporter
|
||||||
} else {
|
} else {
|
||||||
$license->fill($this->sanitizeItemForStoring($license));
|
$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()) {
|
if ($license->save()) {
|
||||||
$license->logCreate('Imported using csv importer');
|
|
||||||
$this->log('License '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
|
$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.
|
// Lets try to checkout seats if the fields exist and we have seats.
|
||||||
|
|
|
@ -19,6 +19,9 @@ class Actionlog extends SnipeModel
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
|
|
||||||
|
// This is to manually set the source (via setActionSource()) for determineActionSource()
|
||||||
|
protected ?string $source = null;
|
||||||
|
|
||||||
protected $presenter = \App\Presenters\ActionlogPresenter::class;
|
protected $presenter = \App\Presenters\ActionlogPresenter::class;
|
||||||
use SoftDeletes;
|
use SoftDeletes;
|
||||||
use Presentable;
|
use Presentable;
|
||||||
|
@ -341,7 +344,12 @@ class Actionlog extends SnipeModel
|
||||||
* @since v6.3.0
|
* @since v6.3.0
|
||||||
* @return string
|
* @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
|
// This is an API call
|
||||||
if (((request()->header('content-type') && (request()->header('accept'))=='application/json'))
|
if (((request()->header('content-type') && (request()->header('accept'))=='application/json'))
|
||||||
|
@ -358,4 +366,10 @@ class Actionlog extends SnipeModel
|
||||||
return 'cli/unknown';
|
return 'cli/unknown';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Manually sets $this->source for determineActionSource()
|
||||||
|
public function setActionSource($source = null): void
|
||||||
|
{
|
||||||
|
$this->source = $source;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,9 @@ use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
trait Loggable
|
trait Loggable
|
||||||
{
|
{
|
||||||
|
// an attribute for setting whether or not the item was imported
|
||||||
|
public ?bool $imported = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
||||||
* @since [v3.4]
|
* @since [v3.4]
|
||||||
|
@ -18,6 +21,11 @@ trait Loggable
|
||||||
return $this->morphMany(Actionlog::class, 'item');
|
return $this->morphMany(Actionlog::class, 'item');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setImported(bool $bool): void
|
||||||
|
{
|
||||||
|
$this->imported = $bool;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
* @author Daniel Meltzer <dmeltzer.devel@gmail.com>
|
||||||
* @since [v3.4]
|
* @since [v3.4]
|
||||||
|
|
|
@ -38,6 +38,9 @@ class AccessoryObserver
|
||||||
$logAction->item_id = $accessory->id;
|
$logAction->item_id = $accessory->id;
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
$logAction->user_id = Auth::id();
|
$logAction->user_id = Auth::id();
|
||||||
|
if($accessory->imported) {
|
||||||
|
$logAction->setActionSource('importer');
|
||||||
|
}
|
||||||
$logAction->logaction('create');
|
$logAction->logaction('create');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -109,6 +109,9 @@ class AssetObserver
|
||||||
$logAction->item_id = $asset->id;
|
$logAction->item_id = $asset->id;
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
$logAction->user_id = Auth::id();
|
$logAction->user_id = Auth::id();
|
||||||
|
if($asset->imported) {
|
||||||
|
$logAction->setActionSource('importer');
|
||||||
|
}
|
||||||
$logAction->logaction('create');
|
$logAction->logaction('create');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,9 @@ class ComponentObserver
|
||||||
$logAction->item_id = $component->id;
|
$logAction->item_id = $component->id;
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
$logAction->user_id = Auth::id();
|
$logAction->user_id = Auth::id();
|
||||||
|
if($component->imported) {
|
||||||
|
$logAction->setActionSource('importer');
|
||||||
|
}
|
||||||
$logAction->logaction('create');
|
$logAction->logaction('create');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,9 @@ class ConsumableObserver
|
||||||
$logAction->item_id = $consumable->id;
|
$logAction->item_id = $consumable->id;
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
$logAction->user_id = Auth::id();
|
$logAction->user_id = Auth::id();
|
||||||
|
if($consumable->imported) {
|
||||||
|
$logAction->setActionSource('importer');
|
||||||
|
}
|
||||||
$logAction->logaction('create');
|
$logAction->logaction('create');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,9 @@ class LicenseObserver
|
||||||
$logAction->item_id = $license->id;
|
$logAction->item_id = $license->id;
|
||||||
$logAction->created_at = date('Y-m-d H:i:s');
|
$logAction->created_at = date('Y-m-d H:i:s');
|
||||||
$logAction->user_id = Auth::id();
|
$logAction->user_id = Auth::id();
|
||||||
|
if($license->imported) {
|
||||||
|
$logAction->setActionSource('importer');
|
||||||
|
}
|
||||||
$logAction->logaction('create');
|
$logAction->logaction('create');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
sample_csvs/components-sample.csv
Normal file
2
sample_csvs/components-sample.csv
Normal file
|
@ -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…
Reference in a new issue