attributes->get('_jwt_user_id'); $promos = DB::connection('primary')->table('promos') ->where(function ($q) use ($passengerId) { $q->where('passengerID', $passengerId) ->orWhere('passengerID', 'none'); }) ->where(function ($q) { $q->whereNull('validity_end_date') ->orWhere('validity_end_date', '>=', now()->toDateString()); }) ->get(); return response()->json(['status' => 'success', 'message' => $promos]); } /** GET /v2/promos/check?code=XXX */ public function check(Request $request): JsonResponse { $request->validate(['code' => 'required|string']); $promo = DB::connection('primary')->table('promos') ->where('promo_code', $request->input('code')) ->where(function ($q) { $q->whereNull('validity_end_date') ->orWhere('validity_end_date', '>=', now()->toDateString()); }) ->first(); if (!$promo) { return response()->json(['status' => 'failure', 'message' => 'Invalid promo code'], 404); } return response()->json([ 'status' => 'success', 'data' => [ 'code' => $promo->promo_code, 'amount' => $promo->amount, 'description' => $promo->description, ], ]); } /** POST /v2/promos */ public function store(Request $request): JsonResponse { $request->validate([ 'promo_code' => 'required|string|max:14', 'amount' => 'required|string', ]); $passengerId = $request->attributes->get('_jwt_user_id'); $exists = DB::connection('primary')->table('promos') ->where('passengerID', $passengerId)->exists(); if ($exists) { return response()->json(['status' => 'failure', 'message' => 'Promo already assigned'], 409); } DB::connection('primary')->table('promos')->insert([ 'promo_code' => $request->input('promo_code'), 'amount' => $request->input('amount'), 'description' => $request->input('description'), 'passengerID' => $passengerId, 'validity_start_date' => $request->input('start_date'), 'validity_end_date' => $request->input('end_date'), ]); return response()->json(['status' => 'success'], 201); } /** PUT /v2/promos/{id} */ public function update(Request $request, int $id): JsonResponse { $passengerId = $request->attributes->get('_jwt_user_id'); DB::connection('primary')->table('promos') ->where('id', $id) ->where('passengerID', $passengerId) ->update(array_filter([ 'promo_code' => $request->input('promo_code'), 'amount' => $request->input('amount'), 'description' => $request->input('description'), 'validity_end_date' => $request->input('end_date'), ])); return response()->json(['status' => 'success']); } /** DELETE /v2/promos/{id} */ public function destroy(Request $request, int $id): JsonResponse { $passengerId = $request->attributes->get('_jwt_user_id'); DB::connection('primary')->table('promos') ->where('id', $id) ->where('passengerID', $passengerId) ->delete(); return response()->json(['status' => 'success']); } }