|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\V1; |
| 4 | + |
| 5 | +use App\Modules\Frontdesk\Models\FrontdeskStation; |
| 6 | +use App\Modules\Frontdesk\Models\VisitorLog; |
| 7 | +use Illuminate\Http\JsonResponse; |
| 8 | +use Illuminate\Http\Request; |
| 9 | + |
| 10 | +class FrontdeskApiController extends ApiController |
| 11 | +{ |
| 12 | + /** |
| 13 | + * GET /api/v1/frontdesk/stations |
| 14 | + */ |
| 15 | + public function stations(Request $request): JsonResponse |
| 16 | + { |
| 17 | + $tenantId = app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 18 | + |
| 19 | + $paginator = FrontdeskStation::where('tenant_id', $tenantId)->latest()->paginate(20); |
| 20 | + |
| 21 | + return $this->paginated($paginator); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * POST /api/v1/frontdesk/visitors/check-in |
| 26 | + */ |
| 27 | + public function checkIn(Request $request): JsonResponse |
| 28 | + { |
| 29 | + $validated = $request->validate([ |
| 30 | + 'visitor_name' => 'required|string|max:255', |
| 31 | + 'visitor_email' => 'nullable|email|max:255', |
| 32 | + 'visitor_phone' => 'nullable|string|max:50', |
| 33 | + 'visitor_company' => 'nullable|string|max:255', |
| 34 | + 'visit_purpose' => 'required|string|max:255', |
| 35 | + 'station_id' => 'required|integer|exists:frontdesk_stations,id', |
| 36 | + 'host_employee_id'=> 'nullable|integer|exists:users,id', |
| 37 | + 'badge_number' => 'nullable|string|max:50', |
| 38 | + 'notes' => 'nullable|string', |
| 39 | + ]); |
| 40 | + |
| 41 | + $tenantId = app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 42 | + |
| 43 | + $validated['tenant_id'] = $tenantId; |
| 44 | + $validated['status'] = 'checked_in'; |
| 45 | + $validated['check_in_at'] = now(); |
| 46 | + |
| 47 | + $visitor = VisitorLog::create($validated); |
| 48 | + |
| 49 | + return $this->success($visitor, 201); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * POST /api/v1/frontdesk/visitors/{id}/checkout |
| 54 | + */ |
| 55 | + public function checkOut(int $id): JsonResponse |
| 56 | + { |
| 57 | + $visitor = VisitorLog::findOrFail($id); |
| 58 | + $visitor->update([ |
| 59 | + 'status' => 'checked_out', |
| 60 | + 'check_out_at' => now(), |
| 61 | + ]); |
| 62 | + |
| 63 | + return $this->success($visitor); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * GET /api/v1/frontdesk/visitors |
| 68 | + */ |
| 69 | + public function visitors(Request $request): JsonResponse |
| 70 | + { |
| 71 | + $tenantId = app()->has('tenant') ? app('tenant')->id : $request->user()->tenant_id; |
| 72 | + |
| 73 | + $query = VisitorLog::where('tenant_id', $tenantId); |
| 74 | + |
| 75 | + if ($stationId = $request->query('station_id')) { |
| 76 | + $query->where('station_id', $stationId); |
| 77 | + } |
| 78 | + |
| 79 | + if ($date = $request->query('date')) { |
| 80 | + $query->whereDate('check_in_at', $date); |
| 81 | + } |
| 82 | + |
| 83 | + $paginator = $query->latest('check_in_at')->paginate(20); |
| 84 | + |
| 85 | + return $this->paginated($paginator); |
| 86 | + } |
| 87 | +} |
0 commit comments