Refactor helper functions and update seeders

- Simplified `tmpostChannel` and `tmpostDefaultInventory` functions by removing caching logic.
- Changed return type of `namesWithTaxes` method in `CategoryRepository` to `mixed`.
- Enabled `ProductsTableSeeder` and added user association logic for product relations.
- Updated `BrandsSeeder` to include the product ID in the slug.
- Added `old_customer_id` to `options` in `SellersTableSeeder` for user tracking.
This commit is contained in:
Mekan1206
2026-02-08 23:12:40 +05:00
parent d83bc03258
commit 683fa5e0d9
6 changed files with 47 additions and 36 deletions

View File

@@ -176,9 +176,7 @@ if (! function_exists('tmpostChannel')) {
*/
function tmpostChannel(): Channel
{
Cache::rememberForever('tmpostChannel', fn () => Channel::tmpostDefault());
return Cache::get('tmpostChannel');
return Channel::tmpostDefault();
}
}
@@ -188,9 +186,7 @@ if (! function_exists('tmpostDefaultInventory')) {
*/
function tmpostDefaultInventory(): mixed
{
Cache::rememberForever('tmpostDefaultInventory', fn () => Inventory::tmpostDefault());
return Cache::get('tmpostDefaultInventory');
return Inventory::tmpostDefault();
}
}

View File

@@ -124,7 +124,7 @@ class CategoryRepository
/**
* Names with taxes (usefull for nova)
*/
public static function namesWithTaxes(): array
public static function namesWithTaxes(): mixed
{
return CacheRepository::make(
time: 5,

View File

@@ -23,7 +23,7 @@ class DatabaseSeeder extends Seeder
// BrandsSeeder::class,
// CustomersTableSeeder::class,
// SellersTableSeeder::class,
// ProductsTableSeeder::class,
ProductsTableSeeder::class,
]);
}
}

View File

@@ -19,7 +19,7 @@ class BrandsSeeder extends Seeder
foreach ($datas as $data) {
DB::table($table)->insertOrIgnore([
'id' => $data->id,
'slug' => Str::slug(json_decode($data->name)->en ?? $data->code),
'slug' => Str::slug(json_decode($data->name)->en ?? $data->code). '_' . $data->id,
'name' => $data->name,
'is_visible' => $data->is_blocked,
'sort_order' => $data->priority,

View File

@@ -2,45 +2,59 @@
namespace Database\Seeders\New;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use JsonMachine\Items;
use JsonMachine\JsonDecoder\ExtJsonDecoder;
use Illuminate\Support\Str;
class ProductsTableSeeder extends Seeder
{
public function run()
{
$table = 'products';
DB::table($table)->truncate();
DB::transaction(function () {
$table = 'products';
$users = User::whereNotNull('options->old_customer_id')->get();
$items = Items::fromFile(
database_path('data/items.json'),
['decoder' => new ExtJsonDecoder(true)]
);
DB::table($table)->truncate();
foreach ($items as $data) {
$name = json_decode($data['name']);
$description = json_decode($data['description']);
$items = Items::fromFile(
database_path('data/items.json'),
['decoder' => new ExtJsonDecoder(true)]
);
DB::table($table)->insert([
'id' => $data['id'],
'sku' => $data['sku'],
'name' => $name->tm,
'slug' => $data['slug'],
'description' => $description->tm,
'brand_id' => $data['brand_id'],
'is_visible' => $data['is_blocked'],
'created_at' => $data['created_at'],
'updated_at' => $data['updated_at'],
]);
foreach ($items as $data) {
$name = json_decode($data['name']);
$description = json_decode($data['description']);
// $data->seller_id;
}
DB::table($table)->insert([
'id' => $data['id'],
'sku' => $data['sku'],
'name' => $name->tm ?? $name->en ?? '',
'slug' => $data['slug'],
'description' => $description->tm ?? $description->en ?? '',
'brand_id' => $data['brand_id'],
'is_visible' => $data['is_blocked'],
'created_at' => $data['created_at'],
'updated_at' => $data['updated_at'],
]);
DB::statement("
SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table}))
");
// We find user with $data->seller_id matching old_customer_id in options, then we get users channel and attach product to it
$user = $users->firstWhere(function ($user) use ($data) {
return $user->options['old_customer_id'] == $data['seller_id'];
});
$channel = $user ? $user->channel() : tmpostChannel();
DB::table('product_has_relations')->insert([
'product_id' => $data['id'],
'productable_id' => $channel->id,
'productable_type' => 'channel',
]);
}
DB::statement("
SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table}))
");
});
}
}

View File

@@ -26,6 +26,7 @@ class SellersTableSeeder extends Seeder
'email' => $data->email,
'phone_number' => Str::after($data->phone, '993'),
'password' => Hash::make('12345678'),
'options' => sprintf('{"old_customer_id":"%s"}', $data->id),
]);
$user->assignRole('vendor');