Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 98 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| CopyTemplateController | |
0.00% |
0 / 98 |
|
0.00% |
0 / 4 |
272 | |
0.00% |
0 / 1 |
| copyTemplate | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
20 | |||
| updateTemplate | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
12 | |||
| copyCategory | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
42 | |||
| dispatchShortcutEvent | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\v1; |
| 4 | |
| 5 | use Illuminate\Http\Request; |
| 6 | use App\Http\Models\Shortcut; |
| 7 | use App\Http\Models\Template; |
| 8 | use Illuminate\Http\Response; |
| 9 | use App\Http\Models\Auth\User; |
| 10 | use App\Traits\SubscriptionTrait; |
| 11 | use Illuminate\Http\JsonResponse; |
| 12 | use Illuminate\Support\Facades\DB; |
| 13 | use App\Http\Controllers\Controller; |
| 14 | use Illuminate\Support\Facades\Cache; |
| 15 | use App\Http\Models\Auth\BusinessUser; |
| 16 | use App\Http\Requests\CopyTemplateRequest; |
| 17 | |
| 18 | class CopyTemplateController extends Controller |
| 19 | { |
| 20 | use SubscriptionTrait; |
| 21 | |
| 22 | public function copyTemplate(CopyTemplateRequest $request): JsonResponse |
| 23 | { |
| 24 | $data = []; |
| 25 | |
| 26 | if ($request['type'] == 'sub_categories_lv2') { |
| 27 | $data['sub_categories_lv2_id'] = $request['category_id']; |
| 28 | } elseif ($request['type'] == 'sub_categories_lv1') { |
| 29 | $data['sub_categories_lv1_id'] = $request['category_id']; |
| 30 | } else { |
| 31 | $data['category_id'] = $request['category_id']; |
| 32 | } |
| 33 | |
| 34 | $searchArray = ['_id' => $request->template_id]; |
| 35 | $template = Template::where($searchArray)->first(); |
| 36 | if (! $template) { |
| 37 | return response()->json(false); |
| 38 | } |
| 39 | |
| 40 | $user = $request->user(); |
| 41 | $userId = $user->getKey(); |
| 42 | |
| 43 | $shortcutData = [ |
| 44 | 'title' => $template->title, |
| 45 | 'shortcut' => $template->shortcut, |
| 46 | 'first_line' => $template->first_line, |
| 47 | 'text' => $template->text, |
| 48 | 'html' => $template->html, |
| 49 | 'version' => 1, |
| 50 | 'uploads' => $template->uploads, |
| 51 | 'type' => $request->type, |
| 52 | 'template_id' => $template->id, |
| 53 | 'template_version' => $template->version, |
| 54 | 'template_category_id' => $template->category_id, |
| 55 | 'user_defined' => false, |
| 56 | 'user_id' => $userId, |
| 57 | ] + $data; |
| 58 | |
| 59 | Shortcut::create($shortcutData); |
| 60 | $this->dispatchShortcutEvent(); |
| 61 | |
| 62 | return response()->json(true); |
| 63 | } |
| 64 | |
| 65 | public function updateTemplate(Request $request): JsonResponse |
| 66 | { |
| 67 | $data = $request->validate([ |
| 68 | 'template_id' => 'required|string|exists:templates,_id', |
| 69 | ]); |
| 70 | |
| 71 | $searchArray = ['_id' => $data['template_id']]; |
| 72 | $template = Template::where($searchArray)->first(); |
| 73 | if (! $template) { |
| 74 | return response()->json(['error' => 'This FlyPlate is not available on your account'], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 75 | } |
| 76 | |
| 77 | $searchArray = ['template_id' => $data['template_id']]; |
| 78 | $shortcut = Shortcut::where($searchArray)->first(); |
| 79 | |
| 80 | if (! $shortcut) { |
| 81 | return response()->json(['error' => 'This FlyCut is not available on your account'], Response::HTTP_UNPROCESSABLE_ENTITY); |
| 82 | } |
| 83 | |
| 84 | $shortcutData = [ |
| 85 | 'title' => $template->title, |
| 86 | 'shortcut' => $template->shortcut, |
| 87 | 'first_line' => $template->first_line, |
| 88 | 'text' => $template->text, |
| 89 | 'html' => $template->html, |
| 90 | 'version' => $shortcut->version + 1, |
| 91 | 'uploads' => $template->uploads, |
| 92 | 'type' => $template->type, |
| 93 | 'template_version' => $template->version, |
| 94 | ]; |
| 95 | |
| 96 | $searchArray = ['template_id' => $data['template_id']]; |
| 97 | Shortcut::where($searchArray)->update($shortcutData); |
| 98 | $this->dispatchShortcutEvent(); |
| 99 | |
| 100 | $userId = $request->user()->getKey(); |
| 101 | |
| 102 | Cache::forget('template_' . $userId); |
| 103 | |
| 104 | return response()->json(true); |
| 105 | } |
| 106 | |
| 107 | public function copyCategory(Request $request): JsonResponse |
| 108 | { |
| 109 | $data = $request->validate([ |
| 110 | 'template_category_id' => 'required|string|exists:templates,category_id', |
| 111 | 'category_id' => 'required|string|exists:shortcut_categories,_id', |
| 112 | ], [ |
| 113 | 'template_category_id.required' => 'Flyplate(s) are required', |
| 114 | 'category_id.required' => 'Category is required', |
| 115 | ]); |
| 116 | |
| 117 | $templates = Template::where('category_id', $data['template_category_id'])->get(); |
| 118 | |
| 119 | if (! $templates || count($templates) < 1) { |
| 120 | $templates = Template::where('subcategory_lv1_id', $data['template_category_id'])->get(); |
| 121 | |
| 122 | if (! $templates || count($templates) < 1) { |
| 123 | return response()->json(false); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | $userId = $request->user()->getKey(); |
| 128 | foreach ($templates as $template) { |
| 129 | $shortcutData = [ |
| 130 | 'title' => $template->title, |
| 131 | 'shortcut' => $template->shortcut, |
| 132 | 'first_line' => $template->first_line, |
| 133 | 'text' => $template->text, |
| 134 | 'html' => $template->html, |
| 135 | 'version' => 1, |
| 136 | 'category_id' => $data['category_id'], |
| 137 | 'uploads' => $template->uploads, |
| 138 | 'type' => $template->type, |
| 139 | 'template_id' => $template->id, |
| 140 | 'template_version' => $template->version, |
| 141 | 'template_category_id' => $template->category_id, |
| 142 | 'user_defined' => false, |
| 143 | 'user_id' => $userId, |
| 144 | ]; |
| 145 | |
| 146 | $searchArray = ['template_id' => $template->id]; |
| 147 | Shortcut::updateOrCreate($searchArray, $shortcutData); |
| 148 | } |
| 149 | |
| 150 | $this->dispatchShortcutEvent(); |
| 151 | |
| 152 | Cache::forget('template_' . $userId); |
| 153 | |
| 154 | return response()->json(true); |
| 155 | } |
| 156 | |
| 157 | public function dispatchShortcutEvent() |
| 158 | { |
| 159 | $user = request()->user(); |
| 160 | if ($user instanceof User) { |
| 161 | event(new \App\Events\User\ShortcutUpdated($user)); |
| 162 | } |
| 163 | if ($user instanceof BusinessUser) { |
| 164 | event(new \App\Events\Business\ShortcutUpdated($user)); |
| 165 | } |
| 166 | } |
| 167 | } |