65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Team;
|
|
use App\Models\TeamInvite;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
use Inertia\Inertia;
|
|
|
|
class TeamInviteController extends Controller
|
|
{
|
|
public function store(Request $request, Team $team)
|
|
{
|
|
$validated = $request->validate([
|
|
'email' => 'required|email|max:255',
|
|
]);
|
|
|
|
abort_unless(auth()->user()->can('manage', $team), 403);
|
|
|
|
$invited = Arr::get($validated, 'email');
|
|
$inviter = auth()->user()->id;
|
|
|
|
$exists = TeamInvite::whereInvitedEmail($invited)
|
|
->whereTeamId($team->id)
|
|
->exists();
|
|
|
|
if ($exists) {
|
|
Inertia::flash('warning', 'This Person is already invited to this team.');
|
|
|
|
return;
|
|
}
|
|
|
|
$team->invites()->create([
|
|
'invited_email' => $invited,
|
|
'invited_by_user_id' => $inviter,
|
|
]);
|
|
|
|
Inertia::flash('success', 'User invited to your team.');
|
|
}
|
|
|
|
public function destroy(Request $request, Team $team): RedirectResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'email' => 'required|email|max:255',
|
|
]);
|
|
|
|
$teamInvite = $team->invites()->whereInvitedEmail(Arr::get($validated, 'email'))->firstOrFail();
|
|
|
|
try {
|
|
$teamInvite->delete();
|
|
} catch (\Exception $exception) {
|
|
report($exception);
|
|
Inertia::flash('error', 'could not delete user invite.');
|
|
|
|
return to_route('teams.show', $team);
|
|
}
|
|
|
|
Inertia::flash('success', 'successfully deleted user invite.');
|
|
|
|
return to_route('teams.show', $team);
|
|
}
|
|
}
|