Skip to content

Commit

Permalink
Merge pull request #1362 from bakaphp/hotfix-product-files
Browse files Browse the repository at this point in the history
Merge pull request #1346 from bakaphp/development
  • Loading branch information
kaioken authored May 21, 2024
2 parents e8aca2f + f7cafe4 commit 29d9210
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ public function execute(): bool
}

if (! empty($this->importedProduct->files)) {
$this->product->deleteFiles();
foreach ($this->importedProduct->files as $file) {
$this->product->addFileFromUrl($file['url'], $file['name']);
}
$this->product->overWriteFiles($this->importedProduct->files);
}

$this->categories();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function execute(): Variants
$search = [
'products_id' => $this->variantDto->product->getId(),
'slug' => $this->variantDto->slug ?? Str::slug($this->variantDto->name),
'sku' => $this->variantDto->sku,
'companies_id' => $this->variantDto->product->companies_id,
'apps_id' => $this->variantDto->product->apps_id,
];
Expand All @@ -46,7 +47,6 @@ public function execute(): Variants
'description' => $this->variantDto->description,
'short_description' => $this->variantDto->short_description,
'html_description' => $this->variantDto->html_description,
'sku' => $this->variantDto->sku,
'status_id' => $this->variantDto->status_id,
'ean' => $this->variantDto->ean,
'barcode' => $this->variantDto->barcode,
Expand Down
4 changes: 1 addition & 3 deletions src/Domains/Inventory/Variants/Services/VariantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public static function createVariantsFromArray(Products $product, array $variant
}

if (! empty($variantDto->files)) {
foreach ($variantDto->files as $file) {
$variantModel->addFileFromUrl($file['url'], $file['name']);
}
$variantModel->overWriteFiles($variantDto->files);
}

$warehouse = WarehouseRepository::getById($variantDto->warehouse_id, $company, $variantDto->product->app);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ public static function getFilesByEntity(Model $entity): Collection
->where('filesystem_entities.system_modules_id', '=', $systemModule->getKey())
->where('filesystem_entities.is_deleted', '=', StateEnums::NO->getValue())
->where('filesystem.is_deleted', '=', StateEnums::NO->getValue())
->select(
'filesystem_entities.*',
'filesystem.url',
'filesystem.path',
'filesystem.name',
'filesystem.apps_id',
'filesystem.users_id',
'filesystem.size',
'filesystem.file_type'
)
->get();
}

Expand All @@ -71,6 +81,16 @@ public static function getFileFromEntityByName(Model $entity, string $name): ?Fi
->where('filesystem_entities.is_deleted', '=', StateEnums::NO->getValue())
->where('filesystem_entities.field_name', '=', $name)
->where('filesystem.is_deleted', '=', StateEnums::NO->getValue())
->select(
'filesystem_entities.*',
'filesystem.url',
'filesystem.path',
'filesystem.name',
'filesystem.apps_id',
'filesystem.users_id',
'filesystem.size',
'filesystem.file_type'
)
->first();
}

Expand Down
23 changes: 23 additions & 0 deletions src/Kanvas/Filesystem/Traits/HasFilesystemTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ public function addMultipleFilesFromUrl(array $files): bool
return true;
}

public function overWriteFiles(array $files): bool
{
$existingFiles = $this->getFiles();
$newFiles = collect($files);

// Find files to delete
$filesToDelete = $existingFiles->filter(function ($file) use ($newFiles) {
return ! $newFiles->contains('url', $file['url']);
});

// Soft delete the files (or handle deletion as per your logic)
foreach ($filesToDelete as $fileDelete) {
$fileDelete->delete();
}

// Add or update new files
foreach ($newFiles as $file) {
$this->addFileFromUrl($file['url'], $file['name']);
}

return true;
}

/**
* Attach multiple files.
*
Expand Down

0 comments on commit 29d9210

Please sign in to comment.