feat: refactor getForRestock and getForTransaction methods to improve media handling and streamline data retrieval
This commit is contained in:
parent
0d1e6ba1fe
commit
41a32b08f2
@ -14,6 +14,7 @@
|
|||||||
use App\Services\StockMutationService;
|
use App\Services\StockMutationService;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||||
|
|
||||||
class ProductVariantService
|
class ProductVariantService
|
||||||
{
|
{
|
||||||
@ -26,7 +27,7 @@ public function __construct(
|
|||||||
|
|
||||||
public function getForRestock(): array
|
public function getForRestock(): array
|
||||||
{
|
{
|
||||||
return Product::query()
|
$products = Product::query()
|
||||||
->select(['id', 'name', 'status'])
|
->select(['id', 'name', 'status'])
|
||||||
->with([
|
->with([
|
||||||
'productVariants:id,product_id,name,stock,reject_stock',
|
'productVariants:id,product_id,name,stock,reject_stock',
|
||||||
@ -34,10 +35,24 @@ public function getForRestock(): array
|
|||||||
])
|
])
|
||||||
->active()
|
->active()
|
||||||
->orderBy('name')
|
->orderBy('name')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$allVariantIds = $products->pluck('productVariants.*.id')->flatten()->filter()->all();
|
||||||
|
|
||||||
|
if ($allVariantIds !== []) {
|
||||||
|
$mediaByVariant = Media::query()
|
||||||
|
->whereIn('mediable_id', $allVariantIds)
|
||||||
|
->where('mediable_type', ProductVariant::class)
|
||||||
|
->where('collection_name', 'images')
|
||||||
->get()
|
->get()
|
||||||
->each(function (Product $product) {
|
->groupBy('mediable_id');
|
||||||
$product->productVariants->each(function (ProductVariant $variant) {
|
} else {
|
||||||
$media = $variant->getFirstMedia('images');
|
$mediaByVariant = collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $products->each(function (Product $product) use ($mediaByVariant) {
|
||||||
|
$product->productVariants->each(function (ProductVariant $variant) use ($mediaByVariant) {
|
||||||
|
$media = $mediaByVariant->get($variant->id, collect())->first();
|
||||||
$variant->photo_url = $media
|
$variant->photo_url = $media
|
||||||
? $this->s3Service->getTemporaryUrl($media->getPath())
|
? $this->s3Service->getTemporaryUrl($media->getPath())
|
||||||
: null;
|
: null;
|
||||||
@ -55,7 +70,7 @@ public function getForRestock(): array
|
|||||||
|
|
||||||
public function getForTransaction(): array
|
public function getForTransaction(): array
|
||||||
{
|
{
|
||||||
return Product::query()
|
$products = Product::query()
|
||||||
->select(['id', 'name', 'status'])
|
->select(['id', 'name', 'status'])
|
||||||
->with([
|
->with([
|
||||||
'productVariants:id,product_id,name,stock,reject_stock',
|
'productVariants:id,product_id,name,stock,reject_stock',
|
||||||
@ -63,10 +78,24 @@ public function getForTransaction(): array
|
|||||||
])
|
])
|
||||||
->active()
|
->active()
|
||||||
->orderBy('name')
|
->orderBy('name')
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$allVariantIds = $products->pluck('productVariants.*.id')->flatten()->filter()->all();
|
||||||
|
|
||||||
|
if ($allVariantIds !== []) {
|
||||||
|
$mediaByVariant = Media::query()
|
||||||
|
->whereIn('mediable_id', $allVariantIds)
|
||||||
|
->where('mediable_type', ProductVariant::class)
|
||||||
|
->where('collection_name', 'images')
|
||||||
->get()
|
->get()
|
||||||
->each(function (Product $product) {
|
->groupBy('mediable_id');
|
||||||
$product->productVariants->each(function (ProductVariant $variant) {
|
} else {
|
||||||
$media = $variant->getFirstMedia('images');
|
$mediaByVariant = collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $products->each(function (Product $product) use ($mediaByVariant) {
|
||||||
|
$product->productVariants->each(function (ProductVariant $variant) use ($mediaByVariant) {
|
||||||
|
$media = $mediaByVariant->get($variant->id, collect())->first();
|
||||||
$variant->photo_url = $media
|
$variant->photo_url = $media
|
||||||
? $this->s3Service->getTemporaryUrl($media->getPath())
|
? $this->s3Service->getTemporaryUrl($media->getPath())
|
||||||
: null;
|
: null;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user