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:
@@ -23,7 +23,7 @@ class DatabaseSeeder extends Seeder
|
||||
// BrandsSeeder::class,
|
||||
// CustomersTableSeeder::class,
|
||||
// SellersTableSeeder::class,
|
||||
// ProductsTableSeeder::class,
|
||||
ProductsTableSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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}))
|
||||
");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user