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