Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 113 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
TrackingExtensionController | |
0.00% |
0 / 113 |
|
0.00% |
0 / 8 |
552 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
index | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
show | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
create | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
72 | |||
update | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
delete | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
trackInstalled | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
trackUnInstalled | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace App\Http\Controllers\v1; |
4 | |
5 | use Exception; |
6 | use Carbon\Carbon; |
7 | use Illuminate\Support\Arr; |
8 | use Illuminate\Http\Request; |
9 | use App\Helpers\FlyMSGLogger; |
10 | use App\Http\Models\Shortcut; |
11 | use Illuminate\Http\JsonResponse; |
12 | use Illuminate\Support\Facades\DB; |
13 | use App\Http\Controllers\Controller; |
14 | use App\Http\Models\TrackingExtension; |
15 | use App\Http\Services\StatisticsService; |
16 | use Illuminate\Support\Facades\Validator; |
17 | use App\Events\TrackingExtensionDeployEvent; |
18 | use Symfony\Component\HttpFoundation\Response; |
19 | use App\Http\Models\UserInfo; |
20 | use App\Http\Requests\TrackingExtensionFormRequest; |
21 | use App\Services\UserInfo\UserInfoService; |
22 | use App\Traits\SubscriptionTrait; |
23 | use Illuminate\Support\Facades\Log; |
24 | |
25 | class TrackingExtensionController extends Controller |
26 | { |
27 | use SubscriptionTrait; |
28 | |
29 | public function __construct( |
30 | protected StatisticsService $statisticsService, |
31 | protected UserInfoService $userInfoService |
32 | ) {} |
33 | |
34 | public function index(Request $request): JsonResponse |
35 | { |
36 | try { |
37 | $records = DB::table('tracking_extension') |
38 | ->orderBy('tag', 'ASC') |
39 | ->orderBy('created_at', 'ASC') |
40 | ->get(); |
41 | } catch (Exception $e) { |
42 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
43 | } |
44 | |
45 | return response()->json(['data' => $records, 'message' => 'Succeed'], Response::HTTP_OK); |
46 | } |
47 | |
48 | public function show($id): JsonResponse |
49 | { |
50 | $message = 'Succeed'; |
51 | try { |
52 | $records = DB::table('tracking_extension') |
53 | ->where('_id', $id) |
54 | ->orderBy('tag', 'ASC') |
55 | ->orderBy('created_at', 'ASC') |
56 | ->get(); |
57 | } catch (Exception $e) { |
58 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
59 | } |
60 | if (is_null($records)) { |
61 | $message = 'Record not found'; |
62 | } |
63 | |
64 | return response()->json(['data' => $records, 'message' => $message], Response::HTTP_OK); |
65 | } |
66 | |
67 | public function create(TrackingExtensionFormRequest $request): JsonResponse |
68 | { |
69 | try { |
70 | $data = $request->validated(); |
71 | $user = $request->user(); |
72 | $data['value'] = (int) $data['value']; |
73 | $data['user_id'] = $user->id; |
74 | |
75 | $quota = $this->getFlyCutsQuota($user); |
76 | $quota['used'] = $quota['used'] + 1; |
77 | $quota['remaining'] = $quota['remaining'] - 1; |
78 | |
79 | if ($request->header('Browser-type')) { |
80 | $browser_type = strtolower($request->header('Browser-type')); |
81 | } |
82 | if ($request->header('browser-type')) { |
83 | $browser_type = strtolower($request->header('browser-type')); |
84 | } |
85 | if ($request['browser']) { |
86 | $browser_type = strtolower($request['browser']); |
87 | } |
88 | |
89 | $chromeVersion = $request->header('flymsg_extension_version_for_chrome') ?? $request->header('Flymsg_extension_version_for_chrome') ?? $request->header('flymsgextensionversionforchrome') ?? $request->header('flymsgExtensionVersionForChrome'); |
90 | $edgeVersion = $request->header('flymsg_extension_version_for_edge') ?? $request->header('Flymsg_extension_version_for_edge') ?? $request->header('flymsgextensionversionforedge') ?? $request->header('flymsgExtensionVersionForEdge'); |
91 | |
92 | if (empty($chromeVersion) && empty($edgeVersion)) { |
93 | Log::info('Extension version not found in headers. User: ' . $user->email, [ |
94 | 'user' => $user, |
95 | 'headers' => $request->header() |
96 | ]); |
97 | } |
98 | |
99 | $headers = [ |
100 | "extension_tracking_headers" => [ |
101 | 'flymsg_extension_version_for_edge' => $chromeVersion, |
102 | 'flymsg_extension_version_for_chrome' => $edgeVersion, |
103 | ] |
104 | ]; |
105 | $data = array_merge($headers, $data); |
106 | $shortcut = Shortcut::find($data['shortcut_id']); |
107 | if ($data['shortcut_id'] != 0000) { |
108 | $shortcut['flycutUsage_count'] = $shortcut['flycutUsage_count'] + 1; |
109 | // Update last_deployed_date for recently used. |
110 | $shortcut['last_deployed_date'] = Carbon::now()->toIso8601String(); |
111 | $shortcut->save(); |
112 | |
113 | event(new TrackingExtensionDeployEvent($user, $data, $shortcut, $browser_type)); |
114 | } |
115 | } catch (Exception $e) { |
116 | return response()->json(['data' => [], 'message' => $e->getMessage(), 'error' => 1], Response::HTTP_INTERNAL_SERVER_ERROR); |
117 | } |
118 | |
119 | return response()->json(['data' => [ |
120 | 'quota' => $quota |
121 | ], 'message' => 'Succeed', 'error' => 0], Response::HTTP_OK); |
122 | } |
123 | |
124 | public function update($id, TrackingExtensionFormRequest $request): JsonResponse |
125 | { |
126 | try { |
127 | $data = $request->validated(); |
128 | $userId = $request->user()->getKey(); |
129 | $data['user_id'] = $userId; |
130 | $record = TrackingExtension::find($id)->update($data); |
131 | } catch (Exception $e) { |
132 | return response()->json(['data' => [], 'message' => $e->getMessage(), 'error' => 1], Response::HTTP_INTERNAL_SERVER_ERROR); |
133 | } |
134 | |
135 | return response()->json(['data' => $record, 'message' => 'Succeed', 'error' => 0], Response::HTTP_OK); |
136 | } |
137 | |
138 | public function delete($id): JsonResponse |
139 | { |
140 | try { |
141 | $record = TrackingExtension::destroy([$id]); |
142 | } catch (Exception $e) { |
143 | return response()->json(['data' => [], 'message' => $e->getMessage()], Response::HTTP_INTERNAL_SERVER_ERROR); |
144 | } |
145 | |
146 | return response()->json(['data' => $record, 'message' => 'Succeed'], Response::HTTP_OK); |
147 | } |
148 | |
149 | public function trackInstalled(Request $request): JsonResponse |
150 | { |
151 | $validator = Validator::make( |
152 | $request->all(), |
153 | [ |
154 | 'browser' => 'required', |
155 | ] |
156 | ); |
157 | if ($validator->fails()) { |
158 | $status = 400; |
159 | $message = Arr::first(Arr::flatten($validator->messages()->get('*'))); |
160 | $result = [ |
161 | 'error' => $status, |
162 | 'message' => 'Missing params.' . $message, |
163 | 'data' => [], |
164 | ]; |
165 | |
166 | return response()->json($result, $status); |
167 | } |
168 | |
169 | $user = $request->user(); |
170 | $userInfo = UserInfo::where('email', $user->email)->first(); |
171 | $browser = strtolower($request['browser']); |
172 | |
173 | $properties = $this->userInfoService->addExtension($userInfo, $browser, now()); |
174 | |
175 | $userInfo->update($properties); |
176 | |
177 | return response()->json(['data' => "Label was recorded: flymsg_{$browser}_extension_installed", 'message' => 'Succeed', 'error' => 0], Response::HTTP_OK); |
178 | } |
179 | |
180 | /** |
181 | * https://vengreso.com/flymsg/uninstallFlyMSGextenstion?contact_id=62177957&browser=Chrome&email=team.user@yopmail.com |
182 | * |
183 | * @param Request $request |
184 | * |
185 | * @return JsonResponse |
186 | */ |
187 | public function trackUnInstalled(Request $request): JsonResponse |
188 | { |
189 | Log::info('trackUnInstalled', ['request' => $request->all()]); |
190 | |
191 | // Validate request |
192 | $validator = Validator::make( |
193 | $request->all(), |
194 | [ |
195 | 'browser' => 'required', |
196 | ] |
197 | ); |
198 | |
199 | if ($validator->fails()) { |
200 | $status = 400; |
201 | $message = Arr::first(Arr::flatten($validator->messages()->get('*'))); |
202 | $result = [ |
203 | 'error' => $status, |
204 | 'message' => 'Missing params.' . $message, |
205 | 'data' => [], |
206 | ]; |
207 | |
208 | return response()->json($result, $status); |
209 | } |
210 | |
211 | $browserName = strtolower($request['browser']); |
212 | |
213 | try { |
214 | $userInfo = UserInfo::where('email', $request['email'])->first(); |
215 | $properties = $this->userInfoService->removeExtension($userInfo, $browserName, now()); |
216 | $userInfo->update($properties); |
217 | } catch (Exception $e) { |
218 | FlyMSGLogger::logError("trackUninstalledError", $e); |
219 | return response()->json(['data' => [], 'message' => $e->getMessage(), 'error' => 1], Response::HTTP_INTERNAL_SERVER_ERROR); |
220 | } |
221 | |
222 | return response()->json(['data' => 'Label was recorded: ', 'message' => 'Succeed', 'error' => 0], Response::HTTP_OK); |
223 | } |
224 | } |