update: add coupon per user limit

This commit is contained in:
tokumeikoi
2021-08-28 16:32:55 +09:00
parent 2d5fb03937
commit 5a3b897c57
8 changed files with 76 additions and 41 deletions

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Services\CouponService;
use Illuminate\Http\Request;
use App\Models\Coupon;
@ -13,26 +14,12 @@ class CouponController extends Controller
if (empty($request->input('code'))) {
abort(500, __('Coupon cannot be empty'));
}
$coupon = Coupon::where('code', $request->input('code'))->first();
if (!$coupon) {
abort(500, __('Invalid coupon'));
}
if ($coupon->limit_use <= 0 && $coupon->limit_use !== NULL) {
abort(500, __('This coupon is no longer available'));
}
if (time() < $coupon->started_at) {
abort(500, __('This coupon has not yet started'));
}
if (time() > $coupon->ended_at) {
abort(500, __('This coupon has expired'));
}
if ($coupon->limit_plan_ids) {
if (!in_array($request->input('plan_id'), $coupon->limit_plan_ids)) {
abort(500, __('The coupon code cannot be used for this subscription'));
}
}
$couponService = new CouponService($request->input('code'));
$couponService->setPlanId($request->input('plan_id'));
$couponService->setUserId($request->session()->get('id'));
$couponService->check();
return response([
'data' => $coupon
'data' => $couponService->getCoupon()
]);
}
}

View File

@ -20,6 +20,7 @@ class CouponSave extends FormRequest
'started_at' => 'required|integer',
'ended_at' => 'required|integer',
'limit_use' => 'nullable|integer',
'limit_use_with_user' => 'nullable|integer',
'limit_plan_ids' => 'nullable|array',
'code' => ''
];