migrate to gtea from bistbucket

This commit is contained in:
2026-03-15 17:08:23 +07:00
commit 129ca2260c
3716 changed files with 566316 additions and 0 deletions

View File

@@ -0,0 +1,308 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\RestaurantDelivery\Enums\DeliveryStatus;
use Modules\RestaurantDelivery\Http\Requests\CreateDeliveryRequest;
use Modules\RestaurantDelivery\Http\Requests\UpdateDeliveryStatusRequest;
use Modules\RestaurantDelivery\Http\Resources\DeliveryResource;
use Modules\RestaurantDelivery\Http\Resources\DeliveryTrackingResource;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\DeliveryStatusHistory;
use Modules\RestaurantDelivery\Models\Rider;
use Modules\RestaurantDelivery\Services\Tracking\LiveTrackingService;
class DeliveryController extends Controller
{
public function __construct(
protected LiveTrackingService $trackingService
) {}
/**
* List deliveries
*/
public function index(Request $request): JsonResponse
{
$query = Delivery::query()
->with(['rider', 'zone', 'rating']);
// Filter by status
if ($request->has('status')) {
$query->where('status', $request->status);
}
// Filter by restaurant
if ($request->has('restaurant_id')) {
$query->where('restaurant_id', $request->restaurant_id);
}
// Filter by rider
if ($request->has('rider_id')) {
$query->where('rider_id', $request->rider_id);
}
// Filter by date range
if ($request->has('from_date')) {
$query->whereDate('created_at', '>=', $request->from_date);
}
if ($request->has('to_date')) {
$query->whereDate('created_at', '<=', $request->to_date);
}
$deliveries = $query->orderBy('created_at', 'desc')
->paginate($request->get('per_page', 20));
return response()->json([
'success' => true,
'data' => DeliveryResource::collection($deliveries),
'meta' => [
'current_page' => $deliveries->currentPage(),
'last_page' => $deliveries->lastPage(),
'per_page' => $deliveries->perPage(),
'total' => $deliveries->total(),
],
]);
}
/**
* Create a new delivery
*/
public function store(CreateDeliveryRequest $request): JsonResponse
{
$delivery = Delivery::create(array_merge($request->validated(), [
'status' => DeliveryStatus::PENDING->value,
]));
// Initialize tracking
$this->trackingService->initializeDeliveryTracking($delivery);
return response()->json([
'success' => true,
'message' => 'Delivery created successfully',
'data' => new DeliveryResource($delivery->load(['rider', 'zone'])),
], 201);
}
/**
* Get delivery details
*/
public function show(Delivery $delivery): JsonResponse
{
return response()->json([
'success' => true,
'data' => new DeliveryResource(
$delivery->load(['rider', 'zone', 'rating', 'tip', 'statusHistory'])
),
]);
}
/**
* Update delivery status
*/
public function updateStatus(UpdateDeliveryStatusRequest $request, $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$metadata = [
'changed_by_type' => $request->input('changed_by', 'api'),
'notes' => $request->input('notes'),
'latitude' => $request->input('latitude'),
'longitude' => $request->input('longitude'),
];
$success = $delivery->updateStatus($request->status, $metadata);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Invalid status transition',
], 422);
}
// Update Firebase tracking
$this->trackingService->updateDeliveryStatus($delivery, $metadata);
return response()->json([
'success' => true,
'message' => 'Status updated successfully',
'data' => new DeliveryResource($delivery->fresh(['rider', 'zone'])),
]);
}
/**
* Assign rider to delivery
*/
public function assignRider(Request $request, $uuid): JsonResponse
{
$request->validate([
'rider_id' => 'required|exists:restaurant_riders,id',
]);
$delivery = Delivery::where('uuid', $uuid)->first();
$rider = Rider::findOrFail($request->rider_id);
if (! $rider->canAcceptOrders()) {
return response()->json([
'success' => false,
'message' => 'Rider is not available',
], 422);
}
$delivery->assignRider($rider);
// Update Firebase tracking
// Check if Firebase tracking module is enabled
if (config('restaurant-delivery.firebase.enabled')) {
// Run Firebase tracking
$this->trackingService->initializeDeliveryTracking($delivery->fresh());
}
return response()->json([
'success' => true,
'message' => 'Rider assigned successfully',
'data' => new DeliveryResource($delivery->fresh(['rider'])),
]);
}
/**
* Cancel delivery
*/
public function cancel(Request $request, Delivery $delivery): JsonResponse
{
$request->validate([
'reason' => 'required|string|max:255',
'cancelled_by' => 'required|string|in:customer,restaurant,rider,admin',
'notes' => 'nullable|string',
]);
$success = $delivery->cancel(
$request->reason,
$request->cancelled_by,
$request->notes
);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Cannot cancel this delivery',
], 422);
}
// End tracking
$this->trackingService->endDeliveryTracking($delivery);
return response()->json([
'success' => true,
'message' => 'Delivery cancelled successfully',
]);
}
/**
* Mark delivery as delivered
*/
public function markDelivered(Request $request, Delivery $delivery): JsonResponse
{
$request->validate([
'photo' => 'nullable|string',
'signature' => 'nullable|string',
'recipient_name' => 'nullable|string|max:100',
]);
$success = $delivery->markDelivered([
'photo' => $request->photo,
'signature' => $request->signature,
'recipient_name' => $request->recipient_name,
]);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Cannot mark this delivery as delivered',
], 422);
}
// End tracking
$this->trackingService->endDeliveryTracking($delivery);
return response()->json([
'success' => true,
'message' => 'Delivery completed successfully',
'data' => new DeliveryResource($delivery->fresh()),
]);
}
/**
* Get delivery tracking info
*/
public function tracking(string $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
if (! $delivery->isTrackable()) {
return response()->json([
'success' => false,
'message' => 'Tracking not available for this delivery',
], 404);
}
$trackingData = $this->trackingService->getDeliveryTracking($delivery);
return response()->json([
'success' => true,
'data' => new DeliveryTrackingResource($delivery, $trackingData),
]);
}
/**
* Public tracking by tracking code
*/
public function publicTracking(string $trackingCode): JsonResponse
{
$delivery = Delivery::where('tracking_code', $trackingCode)->first();
if (! $delivery) {
return response()->json([
'success' => false,
'message' => 'Delivery not found',
], 404);
}
$trackingData = null;
if ($delivery->isTrackable()) {
$trackingData = $this->trackingService->getDeliveryTracking($delivery);
}
return response()->json([
'success' => true,
'data' => [
'tracking_code' => $delivery->tracking_code,
'status' => $delivery->status->value,
'status_label' => $delivery->status->label(),
'status_description' => $delivery->status->description(),
'status_color' => $delivery->status->color(),
'estimated_delivery' => $delivery->estimated_delivery_time?->format('H:i'),
'tracking' => $trackingData,
'timeline' => DeliveryStatusHistory::getTimeline($delivery->id),
],
]);
}
/**
* Get delivery status history
*/
public function statusHistory($uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
return response()->json([
'success' => true,
'data' => DeliveryStatusHistory::getTimeline($delivery->id),
]);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\JsonResponse;
use Modules\RestaurantDelivery\Http\Requests\DeliveryZone\DeliveryZoneStoreRequest;
use Modules\RestaurantDelivery\Http\Requests\DeliveryZone\DeliveryZoneUpdateRequest;
use Modules\RestaurantDelivery\Repositories\DeliveryZoneRepository;
class DeliveryZoneController extends Controller
{
public function __construct(private DeliveryZoneRepository $repo) {}
public function index(): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getAll(request()->all()), 'DeliveryZone has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function store(DeliveryZoneStoreRequest $request): JsonResponse
{
try {
return $this->responseSuccess($this->repo->create($request->all()), 'DeliveryZone has been created successfully.');
} catch (\Illuminate\Database\QueryException $exception) {
return $this->responseError([], 'Database error: '.$exception->getMessage());
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function show($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getById($id), 'DeliveryZone has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function update(DeliveryZoneUpdateRequest $request, $id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->update($id, $request->all()), 'DeliveryZone has been updated successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function destroy($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->delete($id), 'DeliveryZone has been deleted successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
}

View File

@@ -0,0 +1,271 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\DeliveryRating;
use Modules\RestaurantDelivery\Services\Rating\RatingService;
class RatingController extends Controller
{
public function __construct(
protected RatingService $ratingService
) {}
/**
* Create a rating for a delivery
*/
public function store(Request $request, $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$validated = $request->validate([
'overall_rating' => 'required|integer|min:1|max:5',
'speed_rating' => 'nullable|integer|min:1|max:5',
'communication_rating' => 'nullable|integer|min:1|max:5',
'food_condition_rating' => 'nullable|integer|min:1|max:5',
'professionalism_rating' => 'nullable|integer|min:1|max:5',
'review' => 'nullable|string|max:500',
'tags' => 'nullable|array',
'tags.*' => 'string',
'is_anonymous' => 'nullable|boolean',
]);
$rating = $this->ratingService->createRating(
delivery: $delivery,
overallRating: $validated['overall_rating'],
categoryRatings: [
'speed' => $validated['speed_rating'] ?? null,
'communication' => $validated['communication_rating'] ?? null,
'food_condition' => $validated['food_condition_rating'] ?? null,
'professionalism' => $validated['professionalism_rating'] ?? null,
],
review: $validated['review'] ?? null,
tags: $validated['tags'] ?? [],
isAnonymous: $validated['is_anonymous'] ?? false,
customerId: auth()->id()
);
if (! $rating) {
return response()->json([
'success' => false,
'message' => 'Unable to submit rating. The delivery may have already been rated or the rating window has expired.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Thank you for your feedback!',
'data' => [
'id' => $rating->uuid,
'overall_rating' => $rating->overall_rating,
'star_display' => $rating->star_display,
],
], 201);
}
/**
* Create a restaurant rating for a delivery
*/
public function storeRestaurantRating(Request $request, $uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$validated = $request->validate([
'overall_rating' => 'required|integer|min:1|max:5',
'review' => 'nullable|string|max:500',
'tags' => 'nullable|array',
]);
$rating = $this->ratingService->createRating(
delivery: $delivery,
overallRating: $validated['overall_rating'],
review: $validated['review'] ?? null,
tags: $validated['tags'] ?? [],
isRestaurantRating: true
);
if (! $rating) {
return response()->json([
'success' => false,
'message' => 'Unable to submit rating.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Rating submitted successfully',
], 201);
}
/**
* Check if delivery can be rated
*/
public function canRate($uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
return response()->json([
'success' => true,
'data' => [
'can_rate' => $this->ratingService->canRate($delivery),
'categories' => $this->ratingService->getCategories(),
'tags' => $this->ratingService->getTags(),
],
]);
}
/**
* Add rider response to a rating
*/
public function addRiderResponse(Request $request, $uuid): JsonResponse
{
$request->validate([
'response' => 'required|string|max:500',
]);
$rating = DeliveryRating::where('uuid', $uuid)->first();
// Verify the rider owns this rating
// if ($rating->rider_id !== auth()->user()->rider?->id) {
// return response()->json([
// 'success' => false,
// 'message' => 'Unauthorized',
// ], 403);
// }
$success = $this->ratingService->addRiderResponse($rating, $request->response);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'You have already responded to this rating',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Response added successfully',
]);
}
/**
* Mark rating as helpful
*/
public function markHelpful($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$rating->markHelpful();
return response()->json([
'success' => true,
'data' => [
'helpful_count' => $rating->helpful_count,
],
]);
}
/**
* Mark rating as not helpful
*/
public function markNotHelpful($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$rating->markNotHelpful();
return response()->json([
'success' => true,
'data' => [
'not_helpful_count' => $rating->not_helpful_count,
],
]);
}
/**
* List ratings (for admin)
*/
public function index(Request $request): JsonResponse
{
$query = DeliveryRating::with(['delivery', 'rider', 'customer']);
if ($request->has('rider_id')) {
$query->where('rider_id', $request->rider_id);
}
if ($request->has('moderation_status')) {
$query->where('moderation_status', $request->moderation_status);
}
if ($request->has('min_rating')) {
$query->where('overall_rating', '>=', $request->min_rating);
}
if ($request->has('max_rating')) {
$query->where('overall_rating', '<=', $request->max_rating);
}
$ratings = $query->orderBy('created_at', 'desc')
->paginate($request->get('per_page', 20));
return response()->json([
'success' => true,
'data' => $ratings,
]);
}
/**
* Approve a rating (admin)
*/
public function approve($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$this->ratingService->approveRating($rating);
return response()->json([
'success' => true,
'message' => 'Rating approved',
]);
}
/**
* Reject a rating (admin)
*/
public function reject(Request $request, $uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$request->validate([
'reason' => 'required|string|max:255',
]);
$this->ratingService->rejectRating($rating, $request->reason);
return response()->json([
'success' => true,
'message' => 'Rating rejected',
]);
}
/**
* Feature a rating (admin)
*/
public function feature($uuid): JsonResponse
{
$rating = DeliveryRating::where('uuid', $uuid)->first();
$this->ratingService->featureRating($rating);
return response()->json([
'success' => true,
'message' => 'Rating featured',
]);
}
}

View File

@@ -0,0 +1,413 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Modules\Authentication\Models\User;
use Modules\RestaurantDelivery\Enums\RiderStatus;
use Modules\RestaurantDelivery\Http\Resources\DeliveryResource;
use Modules\RestaurantDelivery\Http\Resources\RiderResource;
use Modules\RestaurantDelivery\Models\Rider;
use Modules\RestaurantDelivery\Services\Earnings\EarningsService;
use Modules\RestaurantDelivery\Services\Firebase\FirebaseService;
use Modules\RestaurantDelivery\Services\Rating\RatingService;
use Modules\RestaurantDelivery\Services\Tracking\LiveTrackingService;
use Spatie\Permission\Models\Role;
class RiderController extends Controller
{
public function __construct(
protected LiveTrackingService $trackingService,
protected FirebaseService $firebase,
protected EarningsService $earningsService,
protected RatingService $ratingService
) {}
/**
* List riders
*/
public function index(Request $request): JsonResponse
{
$query = Rider::query();
// Filter by status
if ($request->has('status')) {
$query->where('status', $request->status);
}
// Filter by type
if ($request->has('type')) {
$query->where('type', $request->type);
}
// Filter by online status
if ($request->has('is_online')) {
$query->where('is_online', $request->boolean('is_online'));
}
// Filter by verified
if ($request->has('is_verified')) {
$query->where('is_verified', $request->boolean('is_verified'));
}
// Filter by vehicle type
if ($request->has('vehicle_type')) {
$query->where('vehicle_type', $request->vehicle_type);
}
$riders = $query->with('user')->orderBy('created_at', 'desc')
->paginate($request->get('per_page', 20));
return response()->json([
'success' => true,
'message' => 'Rider has been fetch!',
'data' => RiderResource::collection($riders),
'meta' => [
'current_page' => $riders->currentPage(),
'last_page' => $riders->lastPage(),
'per_page' => $riders->perPage(),
'total' => $riders->total(),
],
], 201);
}
/**
* Get nearby riders
*/
public function nearby(Request $request): JsonResponse
{
$request->validate([
'latitude' => 'required|numeric',
'longitude' => 'required|numeric',
'radius' => 'nullable|numeric|min:0.1|max:50',
]);
$radius = $request->get('radius', config('restaurant-delivery.assignment.assignment_radius'));
$riders = Rider::available()
->nearby($request->latitude, $request->longitude, $radius)
->limit(20)
->get();
return $this->responseSuccess(RiderResource::collection($riders), 'NearBy Rider has been fetched successfully.');
}
/**
* Create a new rider
*/
public function store(Request $request): JsonResponse
{
$validated = $request->validate([
'first_name' => 'required|string|max:100',
'last_name' => 'required|string|max:100',
'phone' => 'required|string|unique:restaurant_riders,phone',
'email' => 'nullable|email|unique:restaurant_riders,email',
'type' => 'required|in:internal,freelance,third_party',
'vehicle_type' => 'required|in:bicycle,motorcycle,car,van',
'vehicle_number' => 'nullable|string|max:20',
'commission_type' => 'nullable|in:fixed,percentage,per_km,hybrid',
'commission_rate' => 'nullable|numeric|min:0',
]);
$rider = Rider::create(array_merge($validated, [
'status' => RiderStatus::PENDING->value,
]));
// Delivery Man -> User Create
$restaurantDeliveryManRole = Role::updateOrCreate(['name' => 'Delivery Man', 'restaurant_id' => null, 'guard_name' => 'web']);
$user = $this->createUser($restaurantDeliveryManRole, $rider->first_name, $rider->email, $rider->phone, '123456', getUserRestaurantId());
$rider->update([
'user_id' => $user->id,
]);
return $this->responseSuccess(new RiderResource($rider), 'Rider registered successfully.');
}
/**
* Get rider details
*/
public function show($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
return $this->responseSuccess(new RiderResource($rider->load(['activeDeliveries'])), 'Rider active deliveries fetch successfully.');
}
/**
* Update rider
*/
public function update(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$validated = $request->validate([
'first_name' => 'sometimes|string|max:100',
'last_name' => 'sometimes|string|max:100',
'email' => 'sometimes|nullable|email',
'vehicle_type' => 'sometimes|in:bicycle,motorcycle,car,van',
'vehicle_number' => 'sometimes|nullable|string|max:20',
'vehicle_model' => 'sometimes|nullable|string|max:100',
'vehicle_color' => 'sometimes|nullable|string|max:50',
]);
$rider->update($validated);
return $this->responseSuccess(new RiderResource($rider->fresh()), 'Rider updated successfully.');
}
/**
* Update rider location
*/
public function updateLocation(Request $request, Rider $rider): JsonResponse
{
$validated = $request->validate([
'latitude' => 'required|numeric|between:-90,90',
'longitude' => 'required|numeric|between:-180,180',
'speed' => 'nullable|numeric|min:0',
'bearing' => 'nullable|numeric|between:0,360',
'accuracy' => 'nullable|numeric|min:0',
]);
$result = $this->trackingService->updateRiderLocation(
$rider,
$validated['latitude'],
$validated['longitude'],
$validated['speed'] ?? null,
$validated['bearing'] ?? null,
$validated['accuracy'] ?? null
);
return $this->responseSuccess($result, 'Update location successfully.');
}
/**
* Toggle rider online status
*/
public function toggleOnline(Request $request, Rider $rider): JsonResponse
{
$request->validate([
'is_online' => 'required|boolean',
]);
if ($request->is_online) {
if (! $rider->canAcceptOrders()) {
return response()->json([
'success' => false,
'message' => 'Cannot go online. Please ensure your account is active and verified.',
], 422);
}
$rider->goOnline();
// Update Firebase status
$this->firebase->updateRiderStatus($rider->id, 'online');
} else {
// Check for active deliveries
if ($rider->activeDeliveries()->exists()) {
return response()->json([
'success' => false,
'message' => 'Cannot go offline while having active deliveries.',
], 422);
}
$rider->goOffline();
// Update Firebase status
$this->firebase->updateRiderStatus($rider->id, 'offline');
}
return $this->responseSuccess([
'is_online' => $rider->is_online,
], $rider->is_online ? 'You are now online' : 'You are now offline');
}
/**
* Get rider's active deliveries
*/
public function activeDeliveries($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$deliveries = $rider->activeDeliveries()
->with(['zone'])
->orderBy('created_at', 'desc')
->get();
return $this->responseSuccess(DeliveryResource::collection($deliveries), 'Active Deliveries Fetch successfully.');
}
/**
* Get rider's delivery history
*/
public function deliveryHistory(Request $request, Rider $rider): JsonResponse
{
$query = $rider->deliveries()
->completed()
->with(['rating']);
if ($request->has('from_date')) {
$query->whereDate('delivered_at', '>=', $request->from_date);
}
if ($request->has('to_date')) {
$query->whereDate('delivered_at', '<=', $request->to_date);
}
$deliveries = $query->orderBy('delivered_at', 'desc')
->paginate($request->get('per_page', 20));
return $this->responseSuccess([
'data' => DeliveryResource::collection($deliveries),
'meta' => [
'current_page' => $deliveries->currentPage(),
'last_page' => $deliveries->lastPage(),
'per_page' => $deliveries->perPage(),
'total' => $deliveries->total(),
],
], 'Delivery History Fetch successfully.');
}
/**
* Get rider earnings
*/
public function earnings(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$period = $request->get('period', 'today');
return $this->responseSuccess($this->earningsService->getRiderEarningsSummary($rider, $period), 'Rider Earning Fetch successfully.');
}
/**
* Get rider earnings history
*/
public function earningsHistory(Request $request, Rider $rider): JsonResponse
{
$type = $request->get('type');
$limit = $request->get('limit', 50);
return $this->responseSuccess($this->earningsService->getEarningsHistory($rider, $limit, $type), 'Rider Earning History Fetch successfully.');
}
/**
* Get rider payout history
*/
public function payouts(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
return $this->responseSuccess($this->earningsService->getPayoutHistory($rider), 'Rider Payout History Fetch successfully.');
}
/**
* Get rider ratings and stats
*/
public function ratings($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
return $this->responseSuccess($this->ratingService->getRiderStats($rider), 'Rider Ratings Fetch successfully.');
}
/**
* Update FCM token
*/
public function updateFcmToken(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$request->validate([
'fcm_token' => 'required|string',
'device_id' => 'nullable|string',
]);
$rider->update([
'fcm_token' => $request->fcm_token,
'device_id' => $request->device_id,
]);
return $this->responseSuccess($rider, 'FCM token updated.');
}
/**
* Verify rider
*/
public function verify(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$request->validate([
'verified_by' => 'required|string',
]);
$rider->update([
'is_verified' => true,
'verified_at' => now(),
'verified_by' => $request->verified_by,
'status' => 'active',
]);
return $this->responseSuccess(new RiderResource($rider->fresh()), 'Rider verified successfully.');
}
/**
* Suspend rider
*/
public function suspend(Request $request, $uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$request->validate([
'reason' => 'required|string|max:255',
]);
$rider->update(['status' => 'suspended']);
$rider->goOffline();
return $this->responseSuccess(new RiderResource($rider), 'Rider suspended.');
}
/**
* Reactivate rider
*/
public function reactivate($uuid): JsonResponse
{
$rider = Rider::where('uuid', $uuid)->first();
$rider->update(['status' => 'active']);
return $this->responseSuccess(new RiderResource($rider->fresh()), 'Rider reactivated.');
}
public function createUser(Role $role, string $name, string $email, string $phone, string $password, ?int $restaurantId = null)
{
$user = User::factory([
'first_name' => $name,
'email' => $email,
'email_verified_at' => now(),
'password' => Hash::make($password),
'phone' => $phone,
'user_type' => $role->name,
'role_id' => $role->id,
'remember_token' => Str::random(10),
'platform' => 'WEB',
'address' => 'Dhaka, Bangladesh',
'status' => 1,
'created_by' => 1,
'restaurant_id' => $restaurantId,
'created_at' => now(),
'updated_at' => now(),
])->create();
/* Get all the permission and assign admin role */
$user->assignRole($role);
return $user;
}
}

View File

@@ -0,0 +1,205 @@
<?php
declare(strict_types=1);
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\RestaurantDelivery\Models\Delivery;
use Modules\RestaurantDelivery\Models\DeliveryTip;
use Modules\RestaurantDelivery\Services\Tip\TipService;
class TipController extends Controller
{
public function __construct(
protected TipService $tipService
) {}
/**
* Get tip options for a delivery
*/
public function options($uuid): JsonResponse
{
$delivery = Delivery::where('uuid', $uuid)->first();
$canTipPre = $this->tipService->canTipPreDelivery($delivery);
$canTipPost = $this->tipService->canTipPostDelivery($delivery);
if (! $canTipPre && ! $canTipPost) {
return response()->json([
'success' => false,
'message' => 'Tipping is not available for this delivery',
], 422);
}
return response()->json([
'success' => true,
'data' => array_merge(
$this->tipService->getTipOptions($delivery),
[
'can_tip_pre_delivery' => $canTipPre,
'can_tip_post_delivery' => $canTipPost,
]
),
]);
}
/**
* Create a pre-delivery tip
*/
public function createPreDeliveryTip(Request $request, Delivery $delivery): JsonResponse
{
$validated = $request->validate([
'amount' => 'required|numeric|min:1',
'calculation_type' => 'required|in:fixed,percentage',
'percentage_value' => 'required_if:calculation_type,percentage|nullable|numeric|min:1|max:50',
'message' => 'nullable|string|max:200',
]);
$tip = $this->tipService->createPreDeliveryTip(
delivery: $delivery,
amount: $validated['amount'],
customerId: auth()->id(),
calculationType: $validated['calculation_type'],
percentageValue: $validated['percentage_value'] ?? null,
message: $validated['message'] ?? null
);
if (! $tip) {
return response()->json([
'success' => false,
'message' => 'Unable to add tip. This delivery may not be eligible for tipping.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Tip added successfully',
'data' => [
'id' => $tip->uuid,
'amount' => $tip->amount,
'rider_amount' => $tip->rider_amount,
'type' => $tip->type,
],
], 201);
}
/**
* Create a post-delivery tip
*/
public function createPostDeliveryTip(Request $request, Delivery $delivery): JsonResponse
{
$validated = $request->validate([
'amount' => 'required|numeric|min:1',
'calculation_type' => 'required|in:fixed,percentage',
'percentage_value' => 'required_if:calculation_type,percentage|nullable|numeric|min:1|max:50',
'message' => 'nullable|string|max:200',
]);
$tip = $this->tipService->createPostDeliveryTip(
delivery: $delivery,
amount: $validated['amount'],
customerId: auth()->id(),
calculationType: $validated['calculation_type'],
percentageValue: $validated['percentage_value'] ?? null,
message: $validated['message'] ?? null
);
if (! $tip) {
return response()->json([
'success' => false,
'message' => 'Unable to add tip. The tipping window may have expired.',
], 422);
}
return response()->json([
'success' => true,
'message' => 'Thank you for the tip!',
'data' => [
'id' => $tip->uuid,
'amount' => $tip->amount,
'rider_amount' => $tip->rider_amount,
'type' => $tip->type,
],
], 201);
}
/**
* Process tip payment
*/
public function processPayment(Request $request, DeliveryTip $tip): JsonResponse
{
$validated = $request->validate([
'payment_reference' => 'required|string',
'payment_method' => 'required|string',
]);
$success = $this->tipService->markTipAsPaid(
$tip,
$validated['payment_reference'],
$validated['payment_method']
);
if (! $success) {
return response()->json([
'success' => false,
'message' => 'Failed to process tip payment',
], 500);
}
return response()->json([
'success' => true,
'message' => 'Tip payment processed successfully',
]);
}
/**
* Get tip details
*/
public function show($uuid): JsonResponse
{
$tip = DeliveryTip::where('uuid', $uuid)->first();
return response()->json([
'success' => true,
'data' => [
'id' => $tip->uuid,
'amount' => $tip->amount,
'currency' => $tip->currency,
'type' => $tip->type,
'calculation_type' => $tip->calculation_type,
'rider_amount' => $tip->rider_amount,
'payment_status' => $tip->payment_status,
'message' => $tip->message,
'created_at' => $tip->created_at->toIso8601String(),
],
]);
}
/**
* Calculate tip amount from percentage
*/
public function calculate(Request $request): JsonResponse
{
$validated = $request->validate([
'order_value' => 'required|numeric|min:0',
'percentage' => 'required|numeric|min:1|max:50',
]);
$amount = $this->tipService->calculateTipFromPercentage(
$validated['order_value'],
$validated['percentage']
);
return response()->json([
'success' => true,
'data' => [
'amount' => $amount,
'currency' => config('restaurant-delivery.pricing.currency'),
'currency_symbol' => config('restaurant-delivery.pricing.currency_symbol'),
],
]);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Modules\RestaurantDelivery\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\JsonResponse;
use Modules\RestaurantDelivery\Http\Requests\ZonePricingRule\ZonePricingRuleStoreRequest;
use Modules\RestaurantDelivery\Http\Requests\ZonePricingRule\ZonePricingRuleUpdateRequest;
use Modules\RestaurantDelivery\Repositories\ZonePricingRuleRepository;
class ZonePricingRuleController extends Controller
{
public function __construct(private ZonePricingRuleRepository $repo) {}
public function index(): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getAll(request()->all()), 'ZonePricingRule has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function store(ZonePricingRuleStoreRequest $request): JsonResponse
{
try {
return $this->responseSuccess($this->repo->create($request->all()), 'ZonePricingRule has been created successfully.');
} catch (\Illuminate\Database\QueryException $exception) {
return $this->responseError([], 'Database error: '.$exception->getMessage());
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function show($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->getById($id), 'ZonePricingRule has been fetched successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function update(ZonePricingRuleUpdateRequest $request, $id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->update($id, $request->all()), 'ZonePricingRule has been updated successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
public function destroy($id): JsonResponse
{
try {
return $this->responseSuccess($this->repo->delete($id), 'ZonePricingRule has been deleted successfully.');
} catch (Exception $e) {
return $this->responseError([], $e->getMessage());
}
}
}