added activity log to vouchers, added rate limits to voucher redeem form
This commit is contained in:
parent
348868d6bd
commit
7c4fedfa93
5 changed files with 55 additions and 30 deletions
|
@ -2,9 +2,11 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
|
||||
/**
|
||||
* Class Voucher
|
||||
|
@ -12,7 +14,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|||
*/
|
||||
class Voucher extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
use HasFactory, LogsActivity;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
|
@ -41,12 +43,21 @@ class Voucher extends Model
|
|||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany
|
||||
*/
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getStatus(){
|
||||
public function getStatus()
|
||||
{
|
||||
if ($this->users()->count() >= $this->uses) return 'USES_LIMIT_REACHED';
|
||||
if (!is_null($this->expires_at)){
|
||||
if (!is_null($this->expires_at)) {
|
||||
if ($this->expires_at->isPast()) return 'EXPIRED';
|
||||
}
|
||||
|
||||
|
@ -56,12 +67,15 @@ class Voucher extends Model
|
|||
/**
|
||||
* @param User $user
|
||||
* @return float
|
||||
* @throws Exception
|
||||
*/
|
||||
public function redeem(User $user){
|
||||
public function redeem(User $user)
|
||||
{
|
||||
try {
|
||||
$user->increment('credits' , $this->credits);
|
||||
$user->increment('credits', $this->credits);
|
||||
$this->users()->attach($user);
|
||||
}catch (\Exception $exception) {
|
||||
$this->logRedeem($user);
|
||||
} catch (Exception $exception) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
|
@ -69,10 +83,16 @@ class Voucher extends Model
|
|||
}
|
||||
|
||||
/**
|
||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
* @param User $user
|
||||
* @return null
|
||||
*/
|
||||
public function users()
|
||||
private function logRedeem(User $user)
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
activity()
|
||||
->performedOn($this)
|
||||
->causedBy($user)
|
||||
->log('redeemed');
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -78,6 +78,9 @@
|
|||
@case('created')
|
||||
<small><i class="fas text-success fa-plus mr-2"></i></small>
|
||||
@break
|
||||
@case('redeemed')
|
||||
<small><i class="fas text-success fa-money-check-alt mr-2"></i></small>
|
||||
@break
|
||||
@case('deleted')
|
||||
<small><i class="fas text-danger fa-times mr-2"></i></small>
|
||||
@break
|
||||
|
|
|
@ -117,6 +117,9 @@
|
|||
@case('created')
|
||||
<small><i class="fas text-success fa-plus mr-2"></i></small>
|
||||
@break
|
||||
@case('redeemed')
|
||||
<small><i class="fas text-success fa-money-check-alt mr-2"></i></small>
|
||||
@break
|
||||
@case('deleted')
|
||||
<small><i class="fas text-danger fa-times mr-2"></i></small>
|
||||
@break
|
||||
|
|
|
@ -20,7 +20,8 @@
|
|||
<i class="fas fa-money-check-alt"></i>
|
||||
</div>
|
||||
</div>
|
||||
<input id="redeemVoucherCode" name="code" placeholder="SUMMER" type="text" class="form-control">
|
||||
<input id="redeemVoucherCode" name="code" placeholder="SUMMER" type="text"
|
||||
class="form-control">
|
||||
</div>
|
||||
<span id="redeemVoucherCodeError" class="text-danger"></span>
|
||||
<span id="redeemVoucherCodeSuccess" class="text-success"></span>
|
||||
|
@ -31,7 +32,9 @@
|
|||
<!-- Modal footer -->
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
|
||||
<button name="submit" id="redeemVoucherSubmit" onclick="redeemVoucherCode()" type="button" class="btn btn-primary">Redeem</button>
|
||||
<button name="submit" id="redeemVoucherSubmit" onclick="redeemVoucherCode()" type="button"
|
||||
class="btn btn-primary">Redeem
|
||||
</button>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
@ -40,35 +43,35 @@
|
|||
|
||||
|
||||
<script>
|
||||
function redeemVoucherCode(){
|
||||
function redeemVoucherCode() {
|
||||
let form = document.getElementById('redeemVoucherForm')
|
||||
let button = document.getElementById('redeemVoucherSubmit')
|
||||
let input = document.getElementById('redeemVoucherCode')
|
||||
|
||||
console.log(form.method , form.action)
|
||||
console.log(form.method, form.action)
|
||||
button.disabled = true
|
||||
|
||||
$.ajax({
|
||||
method : form.method,
|
||||
url : form.action,
|
||||
method: form.method,
|
||||
url: form.action,
|
||||
dataType: 'json',
|
||||
data: {
|
||||
code : input.value
|
||||
code: input.value
|
||||
},
|
||||
success : function (response) {
|
||||
success: function (response) {
|
||||
resetForm()
|
||||
redeemVoucherSetSuccess(response)
|
||||
},
|
||||
error : function (jqXHR, textStatus, errorThrown) {
|
||||
error: function (jqXHR, textStatus, errorThrown) {
|
||||
resetForm()
|
||||
redeemVoucherSetError(jqXHR.responseJSON)
|
||||
redeemVoucherSetError(jqXHR)
|
||||
console.error(jqXHR.responseJSON)
|
||||
},
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
function resetForm(){
|
||||
function resetForm() {
|
||||
let button = document.getElementById('redeemVoucherSubmit')
|
||||
let input = document.getElementById('redeemVoucherCode')
|
||||
let successLabel = document.getElementById('redeemVoucherCodeSuccess')
|
||||
|
@ -81,15 +84,16 @@
|
|||
button.disabled = false
|
||||
}
|
||||
|
||||
function redeemVoucherSetError(error){
|
||||
function redeemVoucherSetError(error) {
|
||||
let input = document.getElementById('redeemVoucherCode')
|
||||
let errorLabel = document.getElementById('redeemVoucherCodeError')
|
||||
|
||||
input.classList.add("is-invalid")
|
||||
errorLabel.innerHTML = error.errors.code[0]
|
||||
|
||||
errorLabel.innerHTML = error.status === 422 ? error.responseJSON.errors.code[0] : error.responseJSON.message
|
||||
}
|
||||
|
||||
function redeemVoucherSetSuccess(response){
|
||||
function redeemVoucherSetSuccess(response) {
|
||||
let input = document.getElementById('redeemVoucherCode')
|
||||
let successLabel = document.getElementById('redeemVoucherCodeSuccess')
|
||||
|
||||
|
|
|
@ -19,9 +19,9 @@ use App\Http\Controllers\NotificationController;
|
|||
use App\Http\Controllers\ProfileController;
|
||||
use App\Http\Controllers\ServerController;
|
||||
use App\Http\Controllers\StoreController;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
@ -67,12 +67,7 @@ Route::middleware('auth')->group(function () {
|
|||
Route::get('/auth/callback', [SocialiteController::class, 'callback'])->name('auth.callback');
|
||||
|
||||
#voucher redeem
|
||||
Route::post('/voucher/redeem' , [VoucherController::class , 'redeem'])->name('voucher.redeem');
|
||||
|
||||
Route::get('/test' , function (Request $request) {
|
||||
$voucher = \App\Models\Voucher::first();
|
||||
dd($request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty());
|
||||
});
|
||||
Route::post('/voucher/redeem', [VoucherController::class, 'redeem'])->middleware('throttle:5,1')->name('voucher.redeem');
|
||||
|
||||
#admin
|
||||
Route::prefix('admin')->name('admin.')->middleware('admin')->group(function () {
|
||||
|
|
Loading…
Add table
Reference in a new issue