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