Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 15 |
CRAP | |
0.00% |
0 / 1 |
FlyShareController | |
0.00% |
0 / 82 |
|
0.00% |
0 / 15 |
420 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFlyshareToken | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
cloneShortcuts | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
share | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
reshare | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
acceptShare | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getShortcuts | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
getAllShortcuts | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getSharedWithOthersShortcuts | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getShortcutsOthersShared | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
addToShortcut | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
20 | |||
resetInShortcut | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
sharesHistory | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
deleteClonedShortcut | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
deleteSharesShortcut | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Http\Controllers\v1; |
4 | |
5 | use App\Events\User\FlycutResharedEvent; |
6 | use App\Events\User\FlyCutSharedEvent; |
7 | use App\Events\User\FlyShareLinkGenerated; |
8 | use App\Events\User\ShortcutUpdated; |
9 | use App\Http\Controllers\Controller; |
10 | use App\Http\Models\ClonedSharedShortcut; |
11 | use App\Http\Models\FlyShare; |
12 | use App\Http\Models\SharesShortcut; |
13 | use App\Http\Models\Shortcut; |
14 | use App\Http\Requests\FlycutReshareRequest; |
15 | use App\Http\Requests\FlyShareAddToShortcutRequest; |
16 | use App\Http\Requests\FlyShareRequest; |
17 | use App\Http\Requests\FlyShareResetInShortcutRequest; |
18 | use App\Http\Requests\FlyShareShortcutRequest; |
19 | use App\Http\Requests\FlyShareTokenRequest; |
20 | use App\Http\Services\FlyShareService; |
21 | use Illuminate\Http\JsonResponse; |
22 | use Illuminate\Http\Request; |
23 | |
24 | class FlyShareController extends Controller |
25 | { |
26 | /** |
27 | * The flyshare service implementation. |
28 | * |
29 | * @var FlyShareService |
30 | */ |
31 | protected $flyshareService; |
32 | |
33 | /** |
34 | * Create a new controller instance. |
35 | * |
36 | * @return void |
37 | */ |
38 | public function __construct(FlyShareService $flyshareService) |
39 | { |
40 | $this->flyshareService = $flyshareService; |
41 | } |
42 | |
43 | /** |
44 | * Get the token for the flyshare link. |
45 | */ |
46 | public function getFlyshareToken(FlyShareTokenRequest $request): JsonResponse |
47 | { |
48 | //TODO Check if the shortcuts already have a generated link |
49 | |
50 | $flyshare = $request->user()->flyshares()->create([ |
51 | 'shortcut_ids' => $request->shortcut_ids, |
52 | ]); |
53 | |
54 | return response()->json([ |
55 | 'token' => $flyshare['id'], |
56 | ]); |
57 | } |
58 | |
59 | /** |
60 | * Clone shortcuts. |
61 | */ |
62 | public function cloneShortcuts(Request $request, FlyShare $flyshare): JsonResponse |
63 | { |
64 | FlyShareLinkGenerated::dispatch($flyshare); |
65 | |
66 | return response()->json([ |
67 | 'status' => true, |
68 | ]); |
69 | } |
70 | |
71 | /** |
72 | * Share shortcuts by email |
73 | */ |
74 | public function share(FlyShareRequest $request, FlyShare $flyshare): JsonResponse |
75 | { |
76 | FlyCutSharedEvent::dispatch( |
77 | $flyshare, |
78 | $request->emails, |
79 | $request->first_name, |
80 | $request->last_name, |
81 | $request->message ?? '' |
82 | ); |
83 | |
84 | return response()->json([ |
85 | 'status' => true, |
86 | ]); |
87 | } |
88 | |
89 | /** |
90 | * Re-send shortcut invitation by email |
91 | */ |
92 | public function reshare(FlycutReshareRequest $request, FlyShare $flyshare): JsonResponse |
93 | { |
94 | FlycutResharedEvent::dispatch( |
95 | $flyshare, |
96 | $request->email, |
97 | $request->cloned_shortcut_id |
98 | ); |
99 | |
100 | return response()->json([ |
101 | 'status' => true, |
102 | ]); |
103 | } |
104 | |
105 | /** |
106 | * Accept the flyshare |
107 | */ |
108 | public function acceptShare(Request $request, FlyShare $flyshare): JsonResponse |
109 | { |
110 | $status = $this->flyshareService->acceptSharedClones($request->user(), $flyshare); |
111 | |
112 | return response()->json([ |
113 | 'status' => $status, |
114 | ]); |
115 | } |
116 | |
117 | /** |
118 | * Get the the shortcut ids for the flyshare. |
119 | */ |
120 | public function getShortcuts(FlyShareShortcutRequest $request, FlyShare $flyshare): JsonResponse |
121 | { |
122 | $user = $flyshare->user; |
123 | |
124 | return response()->json([ |
125 | 'flysharerName' => "{$user->first_name} {$user->last_name}", |
126 | 'shortcutIds' => $flyshare->clonedSharedShortcuts->map(function ($shortcut) { |
127 | return $shortcut->_id; |
128 | }), |
129 | ]); |
130 | } |
131 | |
132 | /** |
133 | * Get all the shortcuts associated with all the flyshares for the auth user. |
134 | */ |
135 | public function getAllShortcuts(Request $request): JsonResponse |
136 | { |
137 | $shortcutShared = $this->flyshareService->getSharedToOthersShortcuts($request->user()); |
138 | |
139 | $shortcutReceived = $this->flyshareService->getSharedWithShortcuts($request->user()); |
140 | |
141 | $allShortcuts = $shortcutShared->merge($shortcutReceived)->unique()->sortByDesc('created_at')->values()->all(); |
142 | |
143 | return response()->json($allShortcuts); |
144 | } |
145 | |
146 | /** |
147 | * Get shortcuts auth user shared with others. |
148 | */ |
149 | public function getSharedWithOthersShortcuts(Request $request): JsonResponse |
150 | { |
151 | $shortcutShared = $this->flyshareService->getSharedToOthersShortcuts($request->user()); |
152 | |
153 | return response()->json($shortcutShared); |
154 | } |
155 | |
156 | /** |
157 | * Get shortcuts others shared with auth user. |
158 | */ |
159 | public function getShortcutsOthersShared(Request $request): JsonResponse |
160 | { |
161 | $shortcutReceived = $this->flyshareService->getSharedWithShortcuts($request->user()); |
162 | |
163 | return response()->json($shortcutReceived); |
164 | } |
165 | |
166 | /** |
167 | * Add shared shortcut to user's shortcut |
168 | */ |
169 | public function addToShortcut(FlyShareAddToShortcutRequest $request, SharesShortcut $sharesShortcut): JsonResponse |
170 | { |
171 | $data = [ |
172 | 'version' => 1, |
173 | 'reference_shares_shortcut_id' => $sharesShortcut->_id, |
174 | ]; |
175 | |
176 | switch ($request->type) { |
177 | case 'sub_categories_lv2': |
178 | $data['sub_categories_lv2_id'] = $request->category_id; |
179 | break; |
180 | case 'sub_categories_lv1': |
181 | $data['sub_categories_lv1_id'] = $request->category_id; |
182 | break; |
183 | |
184 | default: |
185 | $data['category_id'] = $request->category_id; |
186 | break; |
187 | } |
188 | |
189 | $shortcut = Shortcut::create( |
190 | collect($sharesShortcut->replicate())->except(['shared_with_others'])->merge($data)->toArray() |
191 | ); |
192 | |
193 | $user = $request->user(); |
194 | |
195 | ShortcutUpdated::dispatch($user); |
196 | |
197 | return response()->json(filled($shortcut)); |
198 | } |
199 | |
200 | /** |
201 | * Reset shortcut to user's shortcut |
202 | */ |
203 | public function resetInShortcut(FlyShareResetInShortcutRequest $request, SharesShortcut $sharesShortcut): JsonResponse |
204 | { |
205 | $updateCount = Shortcut::where('reference_shares_shortcut_id', $sharesShortcut->_id)->increment( |
206 | 'version', |
207 | 1, |
208 | collect($sharesShortcut->replicate())->except(['shared_with_others', 'version'])->toArray() |
209 | ); |
210 | |
211 | if (! $updateCount || $updateCount < 1) { |
212 | return response()->json(false); |
213 | } |
214 | |
215 | ShortcutUpdated::dispatch($request->user()); |
216 | |
217 | return response()->json(true); |
218 | } |
219 | |
220 | /** |
221 | * Get the share history for the shortcut |
222 | */ |
223 | public function sharesHistory(Request $request, ClonedSharedShortcut $clonedSharedShortcut): JsonResponse |
224 | { |
225 | $history = $this->flyshareService->getSharesHistory($clonedSharedShortcut); |
226 | |
227 | return response()->json($history); |
228 | } |
229 | |
230 | /** |
231 | * Delete cloned shortcut shared with others |
232 | */ |
233 | public function deleteClonedShortcut(Request $request, ClonedSharedShortcut $clonedSharedShortcut): JsonResponse |
234 | { |
235 | $deleted = $clonedSharedShortcut->delete(); |
236 | |
237 | return response()->json($deleted); |
238 | } |
239 | |
240 | /** |
241 | * Delete shares shortcut received from others |
242 | */ |
243 | public function deleteSharesShortcut(Request $request, SharesShortcut $sharesShortcut): JsonResponse |
244 | { |
245 | $deleted = $sharesShortcut->delete(); |
246 | |
247 | return response()->json($deleted); |
248 | } |
249 | } |