1 | <?php
|
---|
2 |
|
---|
3 | namespace App\Http\Controllers;
|
---|
4 |
|
---|
5 | use App\Http\Controllers\Utils\UtilController;
|
---|
6 | use App\Models\Artist;
|
---|
7 | use App\Models\Manager;
|
---|
8 | use App\Models\ManagerInvite;
|
---|
9 | use App\Models\User;
|
---|
10 | use App\Notifications\ManagerArtistInviteNotification;
|
---|
11 | use App\Notifications\ManagerRegisterArtistInviteNotification;
|
---|
12 | use Carbon\Carbon;
|
---|
13 | use Illuminate\Database\Eloquent\ModelNotFoundException;
|
---|
14 | use Illuminate\Http\RedirectResponse;
|
---|
15 | use Illuminate\Http\Request;
|
---|
16 | use Illuminate\Support\Facades\Auth;
|
---|
17 | use Exception;
|
---|
18 | use Illuminate\Support\Facades\Hash;
|
---|
19 | use Illuminate\Support\Facades\Validator;
|
---|
20 |
|
---|
21 | class InviteManagerController extends Controller
|
---|
22 | {
|
---|
23 | //
|
---|
24 | public function __construct()
|
---|
25 | {
|
---|
26 | $this->middleware(['auth', 'verified', 'onboarding']);
|
---|
27 | $this->middleware(['role:artist'])->only('storeInvitation');
|
---|
28 | $this->middleware(['role:manager'])->only('accept');
|
---|
29 | }
|
---|
30 |
|
---|
31 | public function storeInvitation(Request $request): RedirectResponse
|
---|
32 | {
|
---|
33 | $validator = Validator::make($request->all(), [
|
---|
34 | 'email' => ['required', 'email', 'max:255'],
|
---|
35 | ]);
|
---|
36 |
|
---|
37 | if ($validator->fails()) {
|
---|
38 | return redirect()->back()
|
---|
39 | ->with('error', $validator->messages()->all());
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (Artist::where('user_id', Auth::id())
|
---|
43 | ->whereNotNull('manager_id')
|
---|
44 | ->exists()) {
|
---|
45 | return redirect()->back()
|
---|
46 | ->with('error', 'You already have a manager. Cannot invite more than one!');
|
---|
47 | }
|
---|
48 |
|
---|
49 | $invitation = new ManagerInvite();
|
---|
50 | $invitation->email = $request->email;
|
---|
51 |
|
---|
52 | $artist = Artist::find(Auth::id());
|
---|
53 | $invitation->artist()->associate($artist);
|
---|
54 |
|
---|
55 | $token = UtilController::generateToken($request->email);
|
---|
56 | $invitation->invitation_token = Hash::make($token);
|
---|
57 |
|
---|
58 | try {
|
---|
59 | $invitation->save();
|
---|
60 | } catch (Exception $e) {
|
---|
61 | return redirect()->back()
|
---|
62 | ->with('error', 'Issue while inviting the manager - ' . $e->getMessage());
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (User::where('email', $request->email)->exists()) {
|
---|
66 | try {
|
---|
67 | $invitation->notify(new ManagerArtistInviteNotification($token));
|
---|
68 | } catch (Exception $e) {
|
---|
69 | return redirect()->back()
|
---|
70 | ->with('error', $e->getMessage());
|
---|
71 | }
|
---|
72 | } else {
|
---|
73 | try {
|
---|
74 | $invitation->notify(new ManagerRegisterArtistInviteNotification($token));
|
---|
75 | } catch (Exception $e) {
|
---|
76 | return redirect()->back()
|
---|
77 | ->with('error', $e->getMessage());
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | return redirect()->back()
|
---|
82 | ->with('success', 'Manager invited successfully!');
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Accept the invitation token sent by the artist
|
---|
87 | * @param $token - invitation token
|
---|
88 | */
|
---|
89 | public function accept(Request $request, $token)
|
---|
90 | {
|
---|
91 | if (!$request->hasValidSignature()) {
|
---|
92 | abort(401);
|
---|
93 | }
|
---|
94 |
|
---|
95 | $invitations = ManagerInvite::where([
|
---|
96 | ['email', $request->email],
|
---|
97 | ['artist_id', $request->artist_id],
|
---|
98 | ])->get();
|
---|
99 |
|
---|
100 | foreach ($invitations as $invitation) {
|
---|
101 | if (Hash::check($token, $invitation->invitation_token)) {
|
---|
102 | $artist = Artist::where('user_id', $invitation->artist_id)->firstOrFail();
|
---|
103 | $user_manager = User::where('email', $invitation->email)->firstOrFail();
|
---|
104 | $manager = Manager::where('user_id', $user_manager->id)->firstOrFail();
|
---|
105 |
|
---|
106 | $artist->manager()->associate($manager);
|
---|
107 |
|
---|
108 | try {
|
---|
109 | $artist->save();
|
---|
110 | } catch (Exception $e) {
|
---|
111 | return redirect()->back()
|
---|
112 | ->with('error', $e->getMessage());
|
---|
113 | }
|
---|
114 |
|
---|
115 | $invitation->registered_at = Carbon::now();
|
---|
116 |
|
---|
117 | try {
|
---|
118 | $invitation->save();
|
---|
119 | } catch (Exception $e) {
|
---|
120 | return redirect()->back()
|
---|
121 | ->with('error', $e->getMessage());
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | return redirect('/explore')
|
---|
127 | ->with('success', 'You have been added as a manager of ' . $artist->user->name . ' 🥳');
|
---|
128 | }
|
---|
129 | }
|
---|