add: invite retraction

This commit is contained in:
Roardom
2023-06-13 08:14:28 +00:00
parent 531628124e
commit b6bb83e68e
7 changed files with 83 additions and 2 deletions
+27 -1
View File
@@ -38,7 +38,7 @@ class InviteController extends Controller
$owner = User::where('username', '=', $username)->firstOrFail();
abort_unless($user->group->is_modo || $user->id === $owner->id, 403);
$invites = Invite::with(['sender', 'receiver'])->where('user_id', '=', $owner->id)->latest()->paginate(25);
$invites = Invite::withTrashed()->with(['sender', 'receiver'])->where('user_id', '=', $owner->id)->latest()->paginate(25);
return view('user.invite.index', ['user' => $owner, 'invites' => $invites, 'route' => 'invite']);
}
@@ -138,6 +138,32 @@ class InviteController extends Controller
->withSuccess(trans('user.invite-sent-success'));
}
/**
* Retract a sent invite.
*/
public function destroy(Request $request, int $id): \Illuminate\Http\RedirectResponse
{
$user = $request->user();
$invite = Invite::findOrFail($id);
abort_unless($user->group->is_modo || $invite->user->id === $user->id, 403);
if ($invite->accepted_by !== null) {
return to_route('invites.index', ['username' => $user->username])
->withErrors(trans('user.invite-already-used'));
}
if ($invite->expires_on < now()) {
return to_route('invites.index', ['username' => $user->username])
->withErrors(trans('user.invite-expired'));
}
$invite->delete();
return to_route('invites.index', ['username' => $user->username])
->withSuccess('Invite deleted successfully.');
}
/**
* Resend Invite.
*/
+1 -1
View File
@@ -53,7 +53,7 @@ class InviteLogSearch extends Component
final public function getInvitesProperty(): \Illuminate\Contracts\Pagination\LengthAwarePaginator
{
return Invite::query()
return Invite::withTrashed()
->with(['sender', 'receiver'])
->when($this->sender, fn ($query) => $query->whereIn('user_id', User::select('id')->where('username', '=', $this->sender)))
->when($this->email, fn ($query) => $query->where('email', 'LIKE', '%'.$this->email.'%'))
+2
View File
@@ -16,11 +16,13 @@ namespace App\Models;
use App\Traits\Auditable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Invite extends Model
{
use Auditable;
use HasFactory;
use SoftDeletes;
/**
* Belongs To A User.
@@ -0,0 +1,17 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('invites', function (Blueprint $table): void {
$table->softDeletes();
});
}
};
@@ -89,6 +89,7 @@
<th>{{ __('user.expires-on') }}</th>
<th>{{ __('user.accepted-by') }}</th>
<th>{{ __('user.accepted-at') }}</th>
<th>{{ __('user.deleted-on') }}</th>
</tr>
</thead>
<tbody>
@@ -122,6 +123,11 @@
{{ $invite->accepted_at ?? 'N/A' }}
</time>
</td>
<td>
<time datetime="{{ $invite->deleted_at ?? '' }}">
{{ $invite->deleted_at ?? 'N/A' }}
</time>
</td>
</tr>
@empty
<tr>
@@ -42,6 +42,7 @@
<th>{{ __('user.expires-on') }}</th>
<th>{{ __('user.accepted-by') }}</th>
<th>{{ __('user.accepted-at') }}</th>
<th>{{ __('user.deleted-on') }}</th>
<th>{{ __('common.actions') }}</th>
</tr>
</thead>
@@ -63,6 +64,7 @@
@endif
</td>
<td>{{ $invite->accepted_at ?? 'N/A' }}</td>
<td>{{ $invite->deleted_at ?? 'N/A' }}</td>
<td>
<menu class="data-table__actions">
<li class="data-table__action">
@@ -91,6 +93,33 @@
</button>
</form>
</li>
<li class="data-table__action">
<form
action="{{ route('invites.destroy', ['id' => $invite->id]) }}"
method="POST"
x-data
>
@csrf
@method('DELETE')
<button
x-on:click.prevent="Swal.fire({
title: 'Are you sure?',
text: `Are you sure you want to retract the invite to: ${atob('{{ base64_encode($invite->email) }}')}? This will forfeit the invite.`,
icon: 'warning',
showConfirmButton: true,
showCancelButton: true,
}).then((result) => {
if (result.isConfirmed) {
$root.submit();
}
})"
class="form__button form__button--text"
@disabled($invite->accepted_at !== null || $invite->expires_on < now() || $invite->deleted_at !== null)
>
{{ __('common.delete') }}
</button>
</form>
</li>
</menu>
</td>
</tr>
+1
View File
@@ -514,6 +514,7 @@ Route::group(['middleware' => 'language'], function (): void {
Route::get('/create', [App\Http\Controllers\User\InviteController::class, 'create'])->name('create');
Route::post('/store', [App\Http\Controllers\User\InviteController::class, 'store'])->name('store');
Route::post('/{id}/send', [App\Http\Controllers\User\InviteController::class, 'send'])->where('id', '[0-9]+')->name('send');
Route::delete('/{id}', [App\Http\Controllers\User\InviteController::class, 'destroy'])->name('destroy');
Route::get('/{username}', [App\Http\Controllers\User\InviteController::class, 'index'])->name('index');
});