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:
@@ -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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class DatabaseSeeder extends Seeder
|
|||||||
// BrandsSeeder::class,
|
// BrandsSeeder::class,
|
||||||
// CustomersTableSeeder::class,
|
// CustomersTableSeeder::class,
|
||||||
// SellersTableSeeder::class,
|
// SellersTableSeeder::class,
|
||||||
// ProductsTableSeeder::class,
|
ProductsTableSeeder::class,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
@@ -2,45 +2,59 @@
|
|||||||
|
|
||||||
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()
|
||||||
{
|
{
|
||||||
$table = 'products';
|
DB::transaction(function () {
|
||||||
DB::table($table)->truncate();
|
$table = 'products';
|
||||||
|
$users = User::whereNotNull('options->old_customer_id')->get();
|
||||||
|
|
||||||
$items = Items::fromFile(
|
DB::table($table)->truncate();
|
||||||
database_path('data/items.json'),
|
|
||||||
['decoder' => new ExtJsonDecoder(true)]
|
|
||||||
);
|
|
||||||
|
|
||||||
foreach ($items as $data) {
|
$items = Items::fromFile(
|
||||||
$name = json_decode($data['name']);
|
database_path('data/items.json'),
|
||||||
$description = json_decode($data['description']);
|
['decoder' => new ExtJsonDecoder(true)]
|
||||||
|
);
|
||||||
|
|
||||||
DB::table($table)->insert([
|
foreach ($items as $data) {
|
||||||
'id' => $data['id'],
|
$name = json_decode($data['name']);
|
||||||
'sku' => $data['sku'],
|
$description = json_decode($data['description']);
|
||||||
'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'],
|
|
||||||
]);
|
|
||||||
|
|
||||||
// $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("
|
// We find user with $data->seller_id matching old_customer_id in options, then we get users channel and attach product to it
|
||||||
SELECT setval('{$table}_id_seq', (SELECT MAX(id) from {$table}))
|
$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,
|
'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');
|
||||||
|
|||||||
Reference in New Issue
Block a user