wip
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Auth\Verification;
|
||||
use App\Models\Ecommerce\Channel\Channel;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class EntrepreneurAuthController extends Controller
|
||||
{
|
||||
public function register(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'first_name' => ['required', 'string'],
|
||||
'phone_number' => ['required', 'integer', 'between:61000000,71999999', 'unique:users,phone_number'],
|
||||
'email' => ['required', 'email', 'unique:users,email'],
|
||||
'password' => ['required'],
|
||||
'region' => ['required', 'string', 'in:mr,ag,ah,dz,lb,bn'],
|
||||
'patent_data' => ['nullable'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->rest($validator->messages()->get('*'), 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($request) {
|
||||
$user = User::create([
|
||||
'first_name' => $request->first_name,
|
||||
'last_name' => ' ',
|
||||
'email' => $request->email,
|
||||
'password' => bcrypt($request->password),
|
||||
'phone_number' => $request->phone_number,
|
||||
]);
|
||||
|
||||
$user->documents()->create([
|
||||
'patent_data' => str_replace('public/', '', $request->file('patent_data')?->store('public/entrepreneur/patent_data') ?? 'public/'),
|
||||
]);
|
||||
|
||||
sendSMSVerification($request->phone_number);
|
||||
|
||||
// Verification::updateOrCreate(['username' => $request->email, 'code' => 12345]);
|
||||
});
|
||||
|
||||
return response()->rest();
|
||||
}
|
||||
|
||||
public function verifyPhoneNumber(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), ['phone_number' => 'required|integer|between:61000000,65999999', 'code' => 'required|string']);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->rest($validator->messages()->get('*'), 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
$verification = Verification::where('username', $request->phone_number)->where('code', $request->code)->first();
|
||||
|
||||
if (! $verification) {
|
||||
return response()->rest([], 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
User::where('phone_number', $request->phone_number)->update(['phone_number_verified_at' => now()]);
|
||||
|
||||
return response()->rest();
|
||||
}
|
||||
|
||||
public function verifyEmail(Request $request)
|
||||
{
|
||||
// email should be validated for deleted_at to
|
||||
$validator = Validator::make($request->all(), ['email' => ['required', 'string', 'email', 'exists:users,email'], 'code' => ['required', 'string']]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->rest($validator->messages()->get('*'), 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
$verfication = Verification::where('username', $request->email)->where('code', $request->code)->first();
|
||||
|
||||
if (! $verfication) {
|
||||
return response()->rest([], 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
$user = User::where('email', $request->email)->first();
|
||||
$user->email_verified_at = now();
|
||||
$user->save();
|
||||
|
||||
event(new Verified($user));
|
||||
|
||||
return response()->rest();
|
||||
}
|
||||
|
||||
public function finalize(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), [
|
||||
'email' => ['required', 'string', 'email', 'exists:users,email'],
|
||||
'password' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->rest($validator->messages()->get('*'), 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
if (! auth()->attempt(['email' => $request->email, 'password' => $request->password])) {
|
||||
return response()->rest([], 400, 'Wrong credentials');
|
||||
}
|
||||
|
||||
$user = User::where('email', $request->email)->first();
|
||||
|
||||
if (! $user || ! Hash::check($request->password, $user->password)) {
|
||||
return response()->rest([], 400, 'Failed');
|
||||
}
|
||||
|
||||
if (! $user->hasRole('vendor')) {
|
||||
$user->assignRole('vendor');
|
||||
|
||||
// User
|
||||
$name = $user->first_name;
|
||||
|
||||
$channel = Channel::create([
|
||||
'name' => $name,
|
||||
'slug' => Str::slug($name).'_'.random_int(10000, 9999999),
|
||||
'description' => '',
|
||||
'timezone' => 'asia/ashgabat',
|
||||
'url' => url('/'),
|
||||
'is_default' => true,
|
||||
'channelables_type' => 'App\Models\User',
|
||||
'channelables_id' => $user->id,
|
||||
'is_visible' => true,
|
||||
]);
|
||||
|
||||
$channel->inventories()->create([
|
||||
'name' => $name,
|
||||
'code' => Str::slug($name).'_'.random_int(10000, 9999999),
|
||||
'region' => 'ag',
|
||||
'shareable' => false,
|
||||
'is_default' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
$bearerToken = $user->createToken(bin2hex(random_bytes(20)))->plainTextToken;
|
||||
|
||||
return response()->rest($bearerToken, 201);
|
||||
}
|
||||
|
||||
public function login(Request $request)
|
||||
{
|
||||
$validator = Validator::make($request->all(), ['email' => 'required|string|email', 'password' => 'required|string']);
|
||||
|
||||
if ($validator->fails()) {
|
||||
return response()->rest($validator->messages()->get('*'), 400, 'Absolutely wrong credentials.');
|
||||
}
|
||||
|
||||
$user = User::where('email', $request->email)->first();
|
||||
|
||||
if (! $user || ! Hash::check($request->password, $user->password)) {
|
||||
return response()->rest([], 400, 'Failed');
|
||||
}
|
||||
|
||||
$bearerToken = $user->createToken(bin2hex(random_bytes(20)))->plainTextToken;
|
||||
|
||||
return response()->rest($bearerToken, 201);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\V1\Vendor\Order\VendorOrderIndexResource;
|
||||
use App\Http\Resources\Api\V1\Vendor\Order\VendorOrderShowResource;
|
||||
use App\Models\Ecommerce\Product\Order\Order;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class EntrepreneurOrderController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$order_ids = DB::table('order_items')
|
||||
->where('channel_id', auth()->user()->channel()->id)
|
||||
->distinct()
|
||||
->pluck('order_id');
|
||||
|
||||
$perPage = $request->input('perPage') ?? 20;
|
||||
$page = $request->input('page') ?? 1;
|
||||
|
||||
return response()->rest_paginate(
|
||||
VendorOrderIndexResource::collection(
|
||||
Order::query()
|
||||
->whereIntegerInRaw('orders.id', $order_ids)
|
||||
->with('paymentType')
|
||||
->latest()
|
||||
->paginate($perPage, ['*'], 'page', $page)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function show(Order $order)
|
||||
{
|
||||
$order->load(['items' => ['product' => ['media']]]);
|
||||
|
||||
return new VendorOrderShowResource($order);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class VendorProductStoreRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
// essentials...
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'description' => ['nullable', 'string', 'max:255'],
|
||||
'files' => ['nullable'],
|
||||
|
||||
// prices...
|
||||
'cost_amount' => ['required', 'numeric'],
|
||||
'old_price_amount' => ['nullable', 'numeric', 'gt:cost_amount'],
|
||||
|
||||
// relationships...
|
||||
// 'integer', 'exists:brands,id'
|
||||
'brand_id' => ['nullable'],
|
||||
'category_ids' => ['required', 'array'],
|
||||
'collection_ids' => ['nullable', 'array'],
|
||||
|
||||
// inventories...
|
||||
'stock' => ['required', 'integer', 'min:1'],
|
||||
'sku' => ['nullable', 'string', 'max:255'],
|
||||
'barcode' => ['nullable', 'string', 'max:255', 'unique:products,barcode'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class VendorProductUpdateRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
// essentials...
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'description' => ['nullable', 'string', 'max:255'],
|
||||
'new_imgs' => ['nullable'],
|
||||
'deleted_images' => ['nullable'],
|
||||
|
||||
// prices...
|
||||
'cost_amount' => ['required', 'numeric'],
|
||||
'old_price_amount' => ['nullable', 'numeric', 'gt:cost_amount'],
|
||||
|
||||
// relationships...
|
||||
'brand_id' => ['nullable', 'integer', 'exists:brands,id'],
|
||||
'category_ids' => ['required', 'array'],
|
||||
'collection_ids' => ['nullable', 'array'],
|
||||
|
||||
// inventories...
|
||||
'stock' => ['required', 'integer', 'min:1'],
|
||||
'sku' => ['nullable', 'string', 'max:255'],
|
||||
'barcode' => ['nullable', 'string', 'max:255'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class VendorMetricsController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$channel = auth()->user()->channel();
|
||||
|
||||
$products_count = $channel->products()->count();
|
||||
$order_items = DB::table('order_items')->where('channel_id', $channel->id)->count();
|
||||
$order_items_today = DB::table('order_items')->where('channel_id', $channel->id)->whereDate('created_at', Carbon::today())->count();
|
||||
|
||||
return response()->rest([
|
||||
'products_count' => $products_count,
|
||||
'order_items_count' => $order_items,
|
||||
'order_items_today_count' => $order_items_today,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur;
|
||||
|
||||
use App\Http\Controllers\Api\V1\Entrepreneur\Requests\VendorProductStoreRequest;
|
||||
use App\Http\Controllers\Api\V1\Entrepreneur\Requests\VendorProductUpdateRequest;
|
||||
use App\Http\Controllers\Api\V1\Product\Resources\Review\ProductReviewResource;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Resources\Api\V1\Vendor\Product\VendorProductIndexResource;
|
||||
use App\Models\Ecommerce\Product\Product\Product;
|
||||
use App\Models\User;
|
||||
use App\Repositories\Ecommerce\Product\ProductRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class VendorProductController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
$channel = $user->channel();
|
||||
|
||||
if (! $channel) {
|
||||
return response()->rest([], 400, 'User doest not have a channel');
|
||||
}
|
||||
|
||||
return response()->rest_paginate(
|
||||
VendorProductIndexResource::collection(
|
||||
ProductRepository::make($request)
|
||||
->queryAsFromResource($channel)
|
||||
->attachEagerLoadingRelationship([
|
||||
'brand',
|
||||
'media' => function ($query) {
|
||||
$query->orderBy('order_column', 'asc');
|
||||
},
|
||||
])
|
||||
->applyFilters()
|
||||
->applySorting()
|
||||
->simplePaginate()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(VendorProductStoreRequest $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
$channel = $user->channel();
|
||||
$tax = productTaxForCategory($request->category_ids ?? []);
|
||||
|
||||
if ($request->brand_id == 0) {
|
||||
$request->merge(['brand_id' => null]);
|
||||
}
|
||||
|
||||
$this->addProduct($request, $user, $channel, $tax);
|
||||
|
||||
return response()->rest([], 201, 'Created');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product
|
||||
*/
|
||||
public function addProduct(Request $request, User $user, mixed $channel, int $tax)
|
||||
{
|
||||
$product = Product::create([
|
||||
'name' => $request->name,
|
||||
'description' => $request->description,
|
||||
'cost_amount' => $request->cost_amount,
|
||||
'price_amount' => ProductRepository::calculatePrice($request->cost_amount, $tax),
|
||||
'old_price_amount' => $request->old_price_amount ? ProductRepository::calculatePrice($request->old_price_amount, $tax) : null,
|
||||
'sku' => $request->sku,
|
||||
'barcode' => $request->barcode,
|
||||
'brand_id' => $request->brand_id,
|
||||
'stock' => $request->stock,
|
||||
'is_visible' => false,
|
||||
'options' => json_encode(ProductRepository::shippingAttributes()),
|
||||
]);
|
||||
|
||||
$product->categories()->attach($request->category_ids);
|
||||
$product->collections()->attach($request->collection_ids);
|
||||
$product->channels()->attach($channel->id);
|
||||
|
||||
$inventory = $channel->inventories()->first() ?: tmpostDefaultInventory();
|
||||
|
||||
$product->inventories()->attach($inventory->id, ['stock' => $request->stock]);
|
||||
|
||||
if ($request->hasFile('files')) {
|
||||
$product->addMultipleMediaFromRequest(['files'])
|
||||
->each(function ($fileAdder) {
|
||||
$fileAdder->preservingOriginal()->toMediaCollection('uploads');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param App\Models\Ecommerce\Product\Product\Product $product
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(Product $product)
|
||||
{
|
||||
$product->load('media');
|
||||
|
||||
return response()->rest(new VendorProductIndexResource($product));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(VendorProductUpdateRequest $request, Product $product)
|
||||
{
|
||||
$barcodeCheck = DB::table('products')
|
||||
->where('barcode', $request->barcode)
|
||||
->whereNot('id', $product->id)
|
||||
->exists();
|
||||
|
||||
if ($barcodeCheck) {
|
||||
return response()->rest([
|
||||
'message' => 'Used barcode',
|
||||
'errors' => ['barcode' => ['already taken']],
|
||||
]);
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
$channel = $user->channel();
|
||||
$tax = productTaxForCategory($request->category_ids ?? []);
|
||||
|
||||
$product->update([
|
||||
'name' => $request->name,
|
||||
'description' => $request->description,
|
||||
'cost_amount' => $request->cost_amount,
|
||||
'price_amount' => ProductRepository::calculatePrice($request->cost_amount, $tax),
|
||||
'old_price_amount' => $request->old_price_amount
|
||||
? ProductRepository::calculatePrice($request->old_price_amount, $tax)
|
||||
: null,
|
||||
'sku' => $request->sku,
|
||||
'barcode' => $request->barcode,
|
||||
'brand_id' => $request->brand_id,
|
||||
'stock' => $request->stock,
|
||||
]);
|
||||
|
||||
$product->categories()->attach($request->category_ids);
|
||||
$product->collections()->attach($request->collection_ids);
|
||||
|
||||
$inventory = $channel->inventories()->first() ?: tmpostDefaultInventory();
|
||||
|
||||
$product->inventories()->sync($inventory->id, ['stock' => $request->stock]);
|
||||
|
||||
if ($request->hasFile('files')) {
|
||||
$product->addMultipleMediaFromRequest(['files'])
|
||||
->each(function ($fileAdder) {
|
||||
$fileAdder->preservingOriginal()->toMediaCollection('uploads');
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->filled('deleted_images')) {
|
||||
$product->getMedia('uploads')->map(function ($media) use ($request) {
|
||||
if (in_array($media->getUrl('thumb'), $request->deleted_images)) {
|
||||
$media->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ($request->hasFile('new_imgs')) {
|
||||
$product->addMultipleMediaFromRequest(['new_imgs'])
|
||||
->each(function ($fileAdder) {
|
||||
$fileAdder->toMediaCollection('uploads');
|
||||
});
|
||||
}
|
||||
|
||||
return response()->rest([], 201, 'Updated');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy(Product $product)
|
||||
{
|
||||
$product->delete();
|
||||
|
||||
return response()->rest();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product reviews
|
||||
*/
|
||||
public function reviews(Product $product)
|
||||
{
|
||||
return ProductReviewResource::collection($product->reviews()->get());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class VendorProfileController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
return response()->rest([
|
||||
'id' => $user->id,
|
||||
'first_name' => $user->first_name,
|
||||
'last_name' => $user->last_name,
|
||||
'email' => $user->email,
|
||||
'phone_number' => $user->phone_number,
|
||||
'email_verified_at' => $user->email_verified_at,
|
||||
'verified' => $user->verified,
|
||||
'options' => $user->options,
|
||||
'created_at' => $user->created_at,
|
||||
'updated_at' => $user->updated_at,
|
||||
'phone_number_verified_at' => $user->phone_number_verified_at,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur\Resources\Order;
|
||||
|
||||
use App\Models\Ecommerce\Product\Order\Shipping\OrderShipping;
|
||||
use App\Models\Ecommerce\Product\Order\Status\OrderStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderIndexResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'status' => OrderStatus::formattedStatusFor($this->status),
|
||||
'shipping_method' => OrderShipping::formattedShippingMethod($this->shipping_method),
|
||||
'notes' => $this->notes,
|
||||
'delivery_time' => $this->delivery_time,
|
||||
'delivery_at' => $this->delivery_at,
|
||||
'region' => $this->region,
|
||||
'payment_type' => $this->paymentType?->name,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1\Entrepreneur\Resources\Order;
|
||||
|
||||
use App\Models\Ecommerce\Product\Order\Shipping\OrderShipping;
|
||||
use App\Models\Ecommerce\Product\Order\Status\OrderStatus;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderShowResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'status' => OrderStatus::formattedStatusFor($this->status),
|
||||
'shipping_method' => OrderShipping::formattedShippingMethod($this->shipping_method),
|
||||
'notes' => $this->notes,
|
||||
'delivery_time' => $this->delivery_time,
|
||||
'delivery_at' => $this->delivery_at,
|
||||
'region' => $this->region,
|
||||
'payment_type' => $this->paymentType?->name,
|
||||
'products' => $this->items->map(fn ($item) => [
|
||||
'quantity' => $item->quantity,
|
||||
'product' => [
|
||||
'id' => $item->product->id,
|
||||
'name' => $item->product->name,
|
||||
'price_amount' => $item->product->price_amount,
|
||||
'thumbnail' => $item->product->thumbnail('thumb150x150'),
|
||||
],
|
||||
]),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user