Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
ProcessFlyGrammarActionsAsyncJob | |
0.00% |
0 / 39 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
handle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
backoff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
created | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
updateDailyUserInfo | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
6 | |||
parseDailyFields | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace App\Jobs; |
4 | |
5 | use App\Http\Models\Auth\User; |
6 | use App\Http\Models\FlyCutUsage; |
7 | use App\Http\Models\FlyGrammarActions; |
8 | use App\Traits\UsageTrait; |
9 | use Illuminate\Bus\Queueable; |
10 | use Illuminate\Contracts\Queue\ShouldQueue; |
11 | use Illuminate\Foundation\Bus\Dispatchable; |
12 | use Illuminate\Queue\InteractsWithQueue; |
13 | use Illuminate\Queue\SerializesModels; |
14 | use App\Traits\ObjectMapper; |
15 | use App\Http\Models\FlyMsgUserDailyUsage; |
16 | use App\Http\Models\Shortcut; |
17 | use App\Http\Models\UserInfo; |
18 | use Illuminate\Support\Facades\Log; |
19 | use MongoDB\BSON\UTCDateTime; |
20 | |
21 | class ProcessFlyGrammarActionsAsyncJob implements ShouldQueue |
22 | { |
23 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, ObjectMapper, UsageTrait; |
24 | |
25 | public $tries = 5; |
26 | |
27 | public function __construct(public FlyGrammarActions $flyGrammarAction) {} |
28 | |
29 | public function handle(): void |
30 | { |
31 | $this->created($this->flyGrammarAction); |
32 | } |
33 | |
34 | public function backoff() |
35 | { |
36 | return [10, 30, 60, 110, 300]; |
37 | } |
38 | |
39 | private function created(FlyGrammarActions $flyGrammarAction): void |
40 | { |
41 | $parsedFields = $this->parseDailyFields($flyGrammarAction); |
42 | $this->updateDailyUserInfo($parsedFields, $flyGrammarAction); |
43 | $user = User::find($flyGrammarAction->user_id); |
44 | $userInfo = UserInfo::where('email', $user->email)->first(); |
45 | $userInfo->last_date_user_used_flygrammar = new UTCDateTime($flyGrammarAction->created_at->timestamp * 1000); |
46 | $userInfo->save(); |
47 | } |
48 | |
49 | private function updateDailyUserInfo(array $data, FlyGrammarActions $flyGrammarAction) |
50 | { |
51 | $date = $flyGrammarAction->created_at; |
52 | $year = $date->year; |
53 | $month = $date->month; |
54 | $day = $date->day; |
55 | $findByDate = FlyMsgUserDailyUsage::where('user_id', $flyGrammarAction->user_id)->where('year', $year)->where('month', $month)->where('day', $day)->first(); |
56 | |
57 | if ($findByDate) { |
58 | $findByDate->update($data); |
59 | } else { |
60 | FlyMsgUserDailyUsage::GenerateNewRecord( |
61 | $year, |
62 | $month, |
63 | $day, |
64 | $flyGrammarAction->user_id, |
65 | $data['time_saved'], |
66 | $data['cost_savings'], |
67 | $data['characters_typed'], |
68 | $data['flycuts_created'], |
69 | $data['flyplates_added'], |
70 | $data['flyengage_count'], |
71 | $data['sentence_rewrite_count'], |
72 | $data['paragraph_rewrite_count'], |
73 | $data['flypost_count'], |
74 | $data['flycut_count'], |
75 | $data['fly_grammar_actions'] ?? 0, |
76 | $data['fly_grammar_accepted'] ?? 0, |
77 | $data['fly_grammar_autocorrect'] ?? 0, |
78 | $data['fly_grammar_autocomplete'] ?? 0, |
79 | ); |
80 | } |
81 | } |
82 | |
83 | private function parseDailyFields(FlyGrammarActions $flyGrammarAction): array |
84 | { |
85 | $user_id = $flyGrammarAction->user_id; |
86 | $date = $flyGrammarAction->created_at; |
87 | |
88 | return $this->getUsage($user_id, $date); |
89 | } |
90 | } |