Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 127 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
SettingController | |
0.00% |
0 / 127 |
|
0.00% |
0 / 10 |
702 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
update | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
getGlobalSettings | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
cacheUserSettings | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
details | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
generateJwt | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
2 | |||
base64UrlEncode | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
updateCompany | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
loadCompanySettings | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
getCompanySettings | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Http\Controllers\v1; |
4 | |
5 | use App\Http\Controllers\Controller; |
6 | use App\Http\Models\Auth\User; |
7 | use App\Http\Models\FlyCutUsage; |
8 | use App\Http\Models\Setting; |
9 | use App\Http\Requests\CompanySettingFormRequest; |
10 | use App\Http\Requests\SettingFormRequest; |
11 | use App\Http\Services\StatisticsService; |
12 | use App\Http\Services\WPSService; |
13 | use App\Traits\SubscriptionTrait; |
14 | use Illuminate\Http\JsonResponse; |
15 | use Illuminate\Http\Request; |
16 | use Illuminate\Support\Facades\Cache; |
17 | use Illuminate\Support\Facades\Config; |
18 | use Illuminate\Support\Facades\DB; |
19 | |
20 | class SettingController extends Controller |
21 | { |
22 | use SubscriptionTrait; |
23 | |
24 | public function __construct( |
25 | private StatisticsService $statisticsService, |
26 | private WPSService $wpsService |
27 | ) {} |
28 | |
29 | public function update(SettingFormRequest $request): JsonResponse |
30 | { |
31 | $data = $request->validated(); |
32 | $user = $request->user(); |
33 | $userId = $user->getKey(); |
34 | $searchArray = ['user_id' => $userId]; |
35 | |
36 | if (!empty($data['wage_per_hour'])) { |
37 | $currentWagePerHour = $this->statisticsService->getWagePerHour($user, now()); |
38 | |
39 | $setting = Setting::where($searchArray)->first(); |
40 | |
41 | if ($setting) { |
42 | $data['wage_per_hour_history'] = $setting->wage_per_hour_history ?? []; |
43 | } |
44 | |
45 | if (count($data['wage_per_hour_history']) == 0) { |
46 | $data['wage_per_hour_history'] = [[ |
47 | 'wage_per_hour' => $currentWagePerHour, |
48 | 'date' => $user->created_at->format('Y-m-d H:i:s'), |
49 | ]]; |
50 | } |
51 | |
52 | $data['wage_per_hour_history'] = array_merge($data['wage_per_hour_history'], [[ |
53 | 'wage_per_hour' => $data['wage_per_hour'], |
54 | 'date' => now()->format('Y-m-d H:i:s'), |
55 | ]]); |
56 | } |
57 | |
58 | $result = Setting::where($searchArray)->update($data); |
59 | |
60 | Cache::forget('setting_' . $userId); |
61 | Cache::forget('setting_details_' . $userId); |
62 | |
63 | $this->cacheUserSettings($user); |
64 | |
65 | return response()->json((bool) $result); |
66 | } |
67 | |
68 | private function getGlobalSettings() |
69 | { |
70 | $default = Setting::where('user_id', 'app_settings')->first(); |
71 | |
72 | return json_decode(json_encode($default), true); |
73 | } |
74 | |
75 | private function cacheUserSettings($user) |
76 | { |
77 | $cacheKey = 'setting_details_' . $user->id; |
78 | |
79 | if (Cache::has($cacheKey)) { |
80 | Cache::forget($cacheKey); |
81 | } |
82 | |
83 | return Cache::remember($cacheKey, Config::get('cache.expiry'), function () use ($user) { |
84 | $default = $this->getGlobalSettings(); |
85 | $result = DB::table('settings')->where('user_id', $user->id)->first(); |
86 | $result->flyperksJwt = ''; |
87 | $userSettings['words_per_minute'] = $this->statisticsService->getWordsPerMinute($user); |
88 | $userSettings['wage_per_hour'] = $this->statisticsService->getWagePerHour($user, now()); |
89 | $userSettings['user_dictionary'] = $this->wpsService->getUserDictionary($user); |
90 | $showSentenceRewrite = true; // (!empty($user->company_id) && $user->company_id === '66b9c9697c1c3df8fe04c5c2') || str_ends_with($user->email, '@vengreso.com'); |
91 | $userSettings['show_rewrite'] = $showSentenceRewrite; |
92 | // $userSettings['show_write_message'] = empty($user->company_id) ? 'Unlimited access to July 15th' : null; |
93 | // $userSettings['show_write_message'] = '15% off coupon code: <strong><span onclick="copyToClipboard(\'JULY15\')">JULY15</span></strong>'; |
94 | $plan = $this->getCurrentPlan($user); |
95 | $userSettings['show_write_upgrade_button'] = empty($user->company_id) && $plan->identifier !== 'sales-pro-monthly' && $plan->identifier !== 'sales-pro-yearly'; |
96 | // $userSettings['show_write_upgrade_button'] = true; |
97 | $userSettings['show_upgrade_button'] = empty($user->company_id) && $plan->identifier !== 'sales-pro-monthly' && $plan->identifier !== 'sales-pro-yearly'; |
98 | if ($user->company_id) { |
99 | $result->company = $this->loadCompanySettings($user->company_id); |
100 | } |
101 | $userSettingsStdClassToArray = json_decode(json_encode($result), true); |
102 | $default['features'] = array_merge($default['features'], $userSettingsStdClassToArray['features'] ?? []); |
103 | $default['features']['flySentenceRewrite'] = $showSentenceRewrite; |
104 | $default['features']['flyParagraphRewrite'] = $showSentenceRewrite; |
105 | $result = array_merge($default, $userSettingsStdClassToArray, $userSettings); |
106 | if (!$userSettings['show_rewrite']) { |
107 | $result['fly_rewrite'] = 'disabled'; |
108 | } |
109 | return $result; |
110 | }); |
111 | } |
112 | |
113 | public function details(Request $request) |
114 | { |
115 | $user = $request->user(); |
116 | |
117 | $result = $this->cacheUserSettings($user); |
118 | |
119 | return response()->json($result); |
120 | } |
121 | |
122 | public function generateJwt($user) |
123 | { |
124 | // Header |
125 | $header = json_encode([ |
126 | 'typ' => 'JWT', |
127 | 'alg' => 'HS256', |
128 | ]); |
129 | $base64UrlHeader = $this->base64UrlEncode($header); |
130 | |
131 | // Payload |
132 | $now = time(); |
133 | $common = [ |
134 | 'iat' => $now, |
135 | 'jti' => md5($now . rand()), |
136 | ]; |
137 | |
138 | $secret = 'GPn5ch5eLuyKXrtwDcesqkY8NQzYzDHm3HNdmTHAZ08='; |
139 | $companyExternalId = 1414; |
140 | $companyName = 'vengreso'; |
141 | $companyWebsite = 'https://vengreso.com'; |
142 | |
143 | $user_attributes = [ |
144 | 'user_external_id' => $user['id'], |
145 | 'user_email' => $user['email'], |
146 | 'user_first_name' => $user['first_name'], |
147 | 'user_last_name' => $user['last_name'], |
148 | 'company_external_id' => $user['id'], |
149 | 'company_name' => $companyName . '_' . $user['email'], |
150 | 'company_website' => 'https://' . $companyName . $user['email'] . '.com', |
151 | ]; |
152 | |
153 | $payload = json_encode(array_merge($common, $user_attributes)); |
154 | $base64UrlPayload = $this->base64UrlEncode($payload); |
155 | |
156 | // JWT |
157 | $message = $base64UrlHeader . '.' . $base64UrlPayload; |
158 | $signature = hash_hmac('sha256', $message, $secret, true); |
159 | $base64UrlSignature = $this->base64UrlEncode($signature); |
160 | $jwt = $base64UrlHeader . '.' . $base64UrlPayload . '.' . $base64UrlSignature; |
161 | |
162 | return $jwt; |
163 | } |
164 | |
165 | public function base64UrlEncode($text) |
166 | { |
167 | return str_replace( |
168 | ['+', '/', '='], |
169 | ['-', '_', ''], |
170 | base64_encode($text) |
171 | ); |
172 | } |
173 | |
174 | public function updateCompany(CompanySettingFormRequest $request): JsonResponse |
175 | { |
176 | $data = $request->validated(); |
177 | $companyId = $request->company_id; |
178 | $company = $request->company; |
179 | $searchArray = ['company_id' => $companyId]; |
180 | |
181 | $setting = Setting::updateOrCreate($searchArray, [ |
182 | 'company_id' => $companyId, |
183 | ]); |
184 | |
185 | if (!empty($data['wage_per_hour'])) { |
186 | $currentWagePerHour = $this->statisticsService->getCompanyWagePerHour($companyId, now()); |
187 | |
188 | if ($setting) { |
189 | $data['wage_per_hour_history'] = $setting->wage_per_hour_history ?? []; |
190 | } |
191 | |
192 | if (count($data['wage_per_hour_history']) == 0) { |
193 | $data['wage_per_hour_history'] = [[ |
194 | 'wage_per_hour' => $currentWagePerHour, |
195 | 'date' => $company->created_at->format('Y-m-d H:i:s'), |
196 | ]]; |
197 | } |
198 | |
199 | $data['wage_per_hour_history'] = array_merge($data['wage_per_hour_history'], [[ |
200 | 'wage_per_hour' => $data['wage_per_hour'], |
201 | 'date' => now()->format('Y-m-d H:i:s'), |
202 | ]]); |
203 | } |
204 | |
205 | $setting->fill($data); |
206 | $setting->save(); |
207 | |
208 | return response()->json($setting->fresh()); |
209 | } |
210 | |
211 | private function loadCompanySettings($companyId) |
212 | { |
213 | $settings = Setting::where('company_id', $companyId)->first(); |
214 | |
215 | $default = $this->statisticsService->getCompanyDefaultSetting(); |
216 | |
217 | if (!$settings) { |
218 | $settings = $default; |
219 | } else { |
220 | $settings = $settings->toArray(); |
221 | } |
222 | |
223 | $settings = array_merge($default, $settings); |
224 | |
225 | if (!$settings['wage_per_hour']) { |
226 | $settings['wage_per_hour'] = $default['wage_per_hour']; |
227 | } |
228 | |
229 | if (!$settings['words_per_minute']) { |
230 | $settings['words_per_minute'] = $default['words_per_minute']; |
231 | } |
232 | |
233 | return $settings; |
234 | } |
235 | |
236 | public function getCompanySettings(Request $request): JsonResponse |
237 | { |
238 | $companyId = $request->company_id; |
239 | $settings = $this->loadCompanySettings($companyId); |
240 | |
241 | return response()->json($settings); |
242 | } |
243 | } |