Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 56 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| TrackFlyMsgAIUsageEventListener | |
0.00% |
0 / 56 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 55 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Listeners; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | use App\Http\Models\FlyCutUsage; |
| 7 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | use App\Http\Models\FlyMsgAI\FlyMsgAITracking; |
| 9 | use App\Http\Services\StatisticsService; |
| 10 | |
| 11 | class TrackFlyMsgAIUsageEventListener implements ShouldQueue |
| 12 | { |
| 13 | public function __construct(protected StatisticsService $statisticsService) {} |
| 14 | |
| 15 | public function handle($event) |
| 16 | { |
| 17 | $user = $event->user; |
| 18 | $prompt_response = $event->prompt_response; |
| 19 | $browser = $event->browser; |
| 20 | $button = $event->button; |
| 21 | $prompt = $event->prompt; |
| 22 | $feature = $event->feature; |
| 23 | $uniqueId = $event->uniqueId; |
| 24 | $aiResult = $event->aiResult; |
| 25 | $context = $event->context; |
| 26 | |
| 27 | FlyMsgAITracking::create([ |
| 28 | 'user_id' => $user->id, |
| 29 | 'browser' => $browser, |
| 30 | 'button' => $button, |
| 31 | 'prompt' => $prompt, |
| 32 | 'feature' => $feature, |
| 33 | 'prompt_response' => $prompt_response, |
| 34 | 'unique_id' => $uniqueId, |
| 35 | 'prompt_plain_response' => $aiResult['response'], |
| 36 | 'prompt' => $aiResult['prompt'], |
| 37 | 'context' => $context, |
| 38 | ]); |
| 39 | // Track characters generated to contribute to time saves |
| 40 | $charactersTyped = strlen(strip_tags($prompt_response)); |
| 41 | $charactersSaved = $charactersTyped; |
| 42 | $timeSaved = $this->statisticsService->getTimeSaved($user, $charactersSaved); |
| 43 | $costSaved = $this->statisticsService->getCostSaved($user, $timeSaved, now()); |
| 44 | $user->flycutUsage()->create([ |
| 45 | 'characters_typed' => $charactersTyped, |
| 46 | 'characters_saved' => $charactersSaved, |
| 47 | 'time_saved' => $timeSaved, |
| 48 | 'cost_saved' => $costSaved, |
| 49 | 'executed_at' => Carbon::now()->toDateTimeString(), |
| 50 | 'feature' => $feature, |
| 51 | ]); |
| 52 | |
| 53 | $monthYear = now()->format('M Y'); |
| 54 | $newData = [ |
| 55 | 'month_year' => now()->format('M Y'), |
| 56 | 'characters_typed' => $charactersTyped, |
| 57 | 'characters_saved' => $charactersSaved, |
| 58 | 'time_saved' => $timeSaved, |
| 59 | 'cost_saved' => $costSaved, |
| 60 | ]; |
| 61 | |
| 62 | if ($user->charts()->exists()) { |
| 63 | $chart = $user->charts()->firstWhere('month_year', $monthYear); |
| 64 | if ($chart) { |
| 65 | $chart->update( |
| 66 | [ |
| 67 | 'month_year' => now()->format('M Y'), |
| 68 | 'characters_typed' => $charactersTyped + $chart->characters_typed, |
| 69 | 'characters_saved' => $charactersSaved + $chart->characters_saved, |
| 70 | 'time_saved' => $timeSaved + $chart->time_saved, |
| 71 | 'cost_saved' => $costSaved + $chart->cost_saved, |
| 72 | ] |
| 73 | ); |
| 74 | } else { |
| 75 | $user->charts()->create($newData); |
| 76 | } |
| 77 | } else { |
| 78 | $user->charts()->create($newData); |
| 79 | } |
| 80 | } |
| 81 | } |