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

View File

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

View File

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

View File

@@ -19,7 +19,7 @@ class BrandsSeeder extends Seeder
foreach ($datas as $data) { foreach ($datas as $data) {
DB::table($table)->insertOrIgnore([ DB::table($table)->insertOrIgnore([
'id' => $data->id, '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, 'name' => $data->name,
'is_visible' => $data->is_blocked, 'is_visible' => $data->is_blocked,
'sort_order' => $data->priority, 'sort_order' => $data->priority,

View File

@@ -2,17 +2,20 @@
namespace Database\Seeders\New; namespace Database\Seeders\New;
use App\Models\User;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use JsonMachine\Items; use JsonMachine\Items;
use JsonMachine\JsonDecoder\ExtJsonDecoder; use JsonMachine\JsonDecoder\ExtJsonDecoder;
use Illuminate\Support\Str;
class ProductsTableSeeder extends Seeder class ProductsTableSeeder extends Seeder
{ {
public function run() public function run()
{ {
DB::transaction(function () {
$table = 'products'; $table = 'products';
$users = User::whereNotNull('options->old_customer_id')->get();
DB::table($table)->truncate(); DB::table($table)->truncate();
$items = Items::fromFile( $items = Items::fromFile(
@@ -27,20 +30,31 @@ class ProductsTableSeeder extends Seeder
DB::table($table)->insert([ DB::table($table)->insert([
'id' => $data['id'], 'id' => $data['id'],
'sku' => $data['sku'], 'sku' => $data['sku'],
'name' => $name->tm, 'name' => $name->tm ?? $name->en ?? '',
'slug' => $data['slug'], 'slug' => $data['slug'],
'description' => $description->tm, 'description' => $description->tm ?? $description->en ?? '',
'brand_id' => $data['brand_id'], 'brand_id' => $data['brand_id'],
'is_visible' => $data['is_blocked'], 'is_visible' => $data['is_blocked'],
'created_at' => $data['created_at'], 'created_at' => $data['created_at'],
'updated_at' => $data['updated_at'], 'updated_at' => $data['updated_at'],
]); ]);
// $data->seller_id; // 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(" DB::statement("
SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table})) SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table}))
"); ");
});
} }
} }

View File

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