Skip to content

Commit 06b3e4b

Browse files
committed
update
1 parent 002ace4 commit 06b3e4b

File tree

4 files changed

+180
-83
lines changed

4 files changed

+180
-83
lines changed

app/Http/Controllers/Seller/CreateSellerController.php

Lines changed: 43 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\User;
77
use App\Models\Image;
88
use Illuminate\Http\Request;
9+
use App\Jobs\CreateSellerJob;
910
use Illuminate\Support\Facades\Log;
1011
use App\Http\Controllers\Controller;
1112
use Illuminate\Support\Facades\Hash;
@@ -19,99 +20,58 @@ class CreateSellerController extends Controller
1920
public function create(Request $request){
2021

2122

22-
try{
23-
$seller=new User;
24-
$seller->firstName=$request->firstName;
25-
$seller->lastName=$request->lastName;
26-
$seller->email=$request->email;
27-
$seller->phone_number=$request->phone_number;
28-
$seller->birthDate=$request->birthDate;
29-
$seller->isWholesaler=$request->isWholesaler;
30-
$seller->role_id=2;
31-
$seller->nationality=$request->nationality;
23+
try {
24+
Log::info('CreateSellerController started', ['email' => $request->email]);
3225

33-
$file_cni_front = $request->file('identity_card_in_front');
34-
$image_path_cni_front = $file_cni_front->store('cni/front', 'public');
35-
$seller->identity_card_in_front=$image_path_cni_front;
26+
// 🗂️ Sauvegarde temporaire des fichiers
27+
$paths = [
28+
'identity_card_in_front' => $this->storeTempFile($request, 'identity_card_in_front'),
29+
'identity_card_in_back' => $this->storeTempFile($request, 'identity_card_in_back'),
30+
'identity_card_with_the_person' => $this->storeTempFile($request, 'identity_card_with_the_person'),
31+
'shop_profile' => $this->storeTempFile($request, 'shop_profile'),
32+
];
3633

37-
$file_cni_back = $request->file('identity_card_in_back');
38-
$image_path_cni_back = $file_cni_back->store('cni/back', 'public');
39-
$seller->identity_card_in_back=$image_path_cni_back;
34+
$imagePaths = [];
35+
if ($request->hasFile('images')) {
36+
foreach ($request->file('images') as $img) {
37+
$imagePaths[] = $this->storeTempFileDirect($img, 'shop/images');
38+
}
39+
}
4040

41-
$file_cni_with_the_person = $request->file('identity_card_with_the_person');
42-
$image_path_cni_with_the_person = $file_cni_with_the_person->store('cni/person', 'public');
43-
$seller->identity_card_with_the_person=$image_path_cni_with_the_person;
41+
// 🧾 On passe des chemins, pas des fichiers
42+
$payload = array_merge($request->except(['identity_card_in_front', 'identity_card_in_back', 'identity_card_with_the_person', 'shop_profile', 'images']), [
43+
'files' => $paths,
44+
'images' => $imagePaths,
45+
]);
4446

45-
$seller->password=Hash::make($request->password);
46-
$seller->isWholesaler=$request->isWholesaler;
47-
if( $seller->save()){
48-
$shop=new Shop;
49-
$shop->shop_name=$request->shop_name;
50-
$shop->shop_description=$request->shop_description;
51-
$shop->user_id=$seller->id;
52-
$shop->town_id=intval($request->town_id);
53-
$shop->quarter_id=intval($request->quarter_id);
54-
$shop->product_type=$request->product_type;
55-
$shop->shop_gender=(string)$request->shop_gender;
56-
$shop_profile = $request->file('shop_profile');
57-
$shop->shop_profile=$shop_profile->store('shop/profile','public');
58-
59-
}
60-
if($shop->save()){
61-
Log::info('PaymentProcessingJob: Payment complete',[
62-
'cat'=>$request->categories
63-
]);
64-
GenerateUniqueShopKeyJob::dispatch($shop->id)->delay(now()->addMinutes(1));
65-
//$urlShop=$this->getUrlSyncAccount();
66-
//$accountId=(new CreateAccountSyncService())->createSyncAccount(
67-
//$request->shop_name,
68-
//$urlShop,
69-
//$request->email,
70-
//$request->phone_number,
71-
//$shop->id,
72-
//);
47+
// 🧱 Dispatch du Job
48+
CreateSellerJob::dispatch($payload);
7349

74-
//$updateShopSyncAccount=$this->updateAccountSyncWithShop($shop->id,$accountId);
50+
return response()->json([
51+
'success' => true,
52+
'message' => 'Création du vendeur en cours...',
53+
], 202);
7554

76-
77-
$shop->categories()->attach($request->categories);
78-
79-
Log::info('PaymentProcessingJob: Payment complete',[
80-
'cat'=>$request->categories
81-
]);
82-
foreach($request->images as $image){
83-
$i=new Image;
84-
$i->image_path=$image->store('shop/images','public');
85-
if($i->save()){
86-
$shop->images()->attach($i);
87-
}
88-
89-
}
90-
return response()->json(['message'=>"seller created successfully",'success'=>true],201);
55+
} catch (Exception $e) {
56+
Log::error('CreateSellerController failed', ['error' => $e->getMessage()]);
57+
return response()->json([
58+
'success' => false,
59+
'message' => 'Something went wrong',
60+
'errors' => $e->getMessage(),
61+
], 500);
9162
}
92-
93-
94-
95-
}catch(\Exception $e){
96-
Log::info('PaymentProcessingJob: Payment complete',[
97-
'errpr'=>$e->getMessage()
98-
]);
99-
return response()->json([
100-
'success' => false,
101-
'message' => 'Something went wrong',
102-
'errors' => $e->getMessage()
103-
], 500);
104-
}
105-
10663
}
10764

108-
private function updateAccountSyncWithShop($shopId,$accountId){
109-
$shop=Shop::find($shopId);
110-
$shop->accountId=$accountId;
111-
$shop->save();
65+
private function storeTempFile(Request $request, string $key)
66+
{
67+
if ($request->hasFile($key)) {
68+
return $request->file($key)->store("temp/$key", 'public');
69+
}
70+
return null;
11271
}
11372

114-
private function getUrlSyncAccount(){
115-
return "https://main.akevas/shop/$shop->id";
73+
private function storeTempFileDirect($file, string $path)
74+
{
75+
return $file->store("temp/$path", 'public');
11676
}
11777
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Shops;
4+
5+
use App\Models\Shop;
6+
use App\Models\User;
7+
use Illuminate\Http\Request;
8+
use App\Http\Controllers\Controller;
9+
10+
class CheckShopStatusController extends Controller
11+
{
12+
public function checkShopStatus(Request $request){
13+
$user=User::where("email",$request->email)->first();
14+
15+
if(!$user){
16+
return response()->json(['exists' => false]);
17+
}
18+
19+
$shop=Shop::where("user_id",$user->id)->exists();
20+
21+
return response()->json(['exists' => $shop]);
22+
}
23+
}

app/Jobs/CreateSellerJob.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace App\Jobs;
4+
5+
use App\Models\Shop;
6+
use App\Models\User;
7+
use App\Models\Image;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Support\Facades\Log;
10+
use Illuminate\Support\Facades\Hash;
11+
use App\Jobs\GenerateUniqueShopKeyJob;
12+
use Illuminate\Queue\SerializesModels;
13+
use Illuminate\Support\Facades\Storage;
14+
use Illuminate\Queue\InteractsWithQueue;
15+
use Illuminate\Contracts\Queue\ShouldQueue;
16+
use Illuminate\Foundation\Bus\Dispatchable;
17+
18+
class CreateSellerJob implements ShouldQueue
19+
{
20+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
21+
22+
/**
23+
* Create a new job instance.
24+
*/
25+
26+
protected $data;
27+
public function __construct(array $data)
28+
{
29+
$this->data = $data;
30+
}
31+
32+
/**
33+
* Execute the job.
34+
*/
35+
public function handle(): void
36+
{
37+
try {
38+
Log::info('CreateSellerJob started', ['email' => $this->data['email']]);
39+
40+
// 👤 Création du vendeur
41+
$seller = new User();
42+
$seller->firstName = $this->data['firstName'];
43+
$seller->lastName = $this->data['lastName'];
44+
$seller->email = $this->data['email'];
45+
$seller->phone_number = $this->data['phone_number'];
46+
$seller->birthDate = $this->data['birthDate'];
47+
$seller->nationality = $this->data['nationality'];
48+
$seller->role_id = 2;
49+
$seller->isWholesaler = $this->data['isWholesaler'];
50+
$seller->password = Hash::make($this->data['password']);
51+
52+
// 📄 Déplacement des fichiers vers leur dossier final
53+
$files = $this->data['files'] ?? [];
54+
$seller->identity_card_in_front = $this->moveTempFile($files['identity_card_in_front'] ?? null, 'cni/front');
55+
$seller->identity_card_in_back = $this->moveTempFile($files['identity_card_in_back'] ?? null, 'cni/back');
56+
$seller->identity_card_with_the_person = $this->moveTempFile($files['identity_card_with_the_person'] ?? null, 'cni/person');
57+
$seller->save();
58+
59+
// 🏬 Création de la boutique
60+
$shop = new Shop();
61+
$shop->shop_name = $this->data['shop_name'];
62+
$shop->shop_description = $this->data['shop_description'];
63+
$shop->user_id = $seller->id;
64+
$shop->town_id = intval($this->data['town_id']);
65+
$shop->quarter_id = intval($this->data['quarter_id']);
66+
$shop->product_type = $this->data['product_type'];
67+
$shop->shop_gender = (string) $this->data['shop_gender'];
68+
$shop->shop_profile = $this->moveTempFile($files['shop_profile'] ?? null, 'shop/profile');
69+
$shop->save();
70+
71+
// 🔗 Catégories
72+
if (!empty($this->data['categories'])) {
73+
$shop->categories()->attach($this->data['categories']);
74+
}
75+
76+
// 🖼️ Images
77+
if (!empty($this->data['images'])) {
78+
foreach ($this->data['images'] as $imgPath) {
79+
$newPath = $this->moveTempFile($imgPath, 'shop/images');
80+
$img = Image::create(['image_path' => $newPath]);
81+
$shop->images()->attach($img);
82+
}
83+
}
84+
85+
// 🗝️ Génération de clé unique différée
86+
GenerateUniqueShopKeyJob::dispatch($shop->id)->delay(now()->addMinute());
87+
88+
Log::info('CreateSellerJob finished successfully', ['shop_id' => $shop->id]);
89+
90+
} catch (\Exception $e) {
91+
Log::error('CreateSellerJob failed', [
92+
'error' => $e->getMessage(),
93+
'trace' => $e->getTraceAsString(),
94+
]);
95+
}
96+
}
97+
98+
private function moveTempFile(?string $tempPath, string $finalDir)
99+
{
100+
if (!$tempPath || !Storage::disk('public')->exists($tempPath)) {
101+
return null;
102+
}
103+
104+
$filename = basename($tempPath);
105+
$newPath = "$finalDir/$filename";
106+
Storage::disk('public')->move($tempPath, $newPath);
107+
return $newPath;
108+
}
109+
}

routes/api.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use App\Http\Controllers\Delivery\OrderHistoryController;
4949
use App\Http\Controllers\Delivery\StatOverviewController;
5050
use App\Http\Controllers\Product\DetailProductController;
51+
use App\Http\Controllers\Shops\CheckShopStatusController;
5152
use App\Http\Controllers\Category\CategoryByUrlController;
5253
use App\Http\Controllers\Payment\Stripe\PaymentController;
5354
use App\Http\Controllers\Product\SimilarProductController;
@@ -115,6 +116,10 @@
115116

116117

117118

119+
120+
121+
Route::post("check/shop/status",[CheckShopStatusController::class,'checkShopStatus']);
122+
118123
Route::get("catalogue/{shop_key}",[CatalogueController::class,'index']);
119124

120125
Route::get("resize/products",[ResizeAllProductImageController::class,"resizeAllProductImage"]);

0 commit comments

Comments
 (0)