Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| AIRequestLogResource | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| toArray | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Resources\v2; |
| 4 | |
| 5 | use App\Http\Models\Admin\Company; |
| 6 | use App\Http\Models\Auth\User; |
| 7 | use Illuminate\Http\Request; |
| 8 | use Illuminate\Http\Resources\Json\JsonResource; |
| 9 | |
| 10 | /** |
| 11 | * Resource for transforming AIRequestLog models for API responses. |
| 12 | * |
| 13 | * @property string $_id The log entry ID |
| 14 | * @property string $user_id The authenticated user's ID |
| 15 | * @property string|null $company_id The user's company ID |
| 16 | * @property string $feature Feature that triggered the request |
| 17 | * @property string $provider AI provider used |
| 18 | * @property string $model Model identifier |
| 19 | * @property string $prompt_input The full prompt sent to the AI |
| 20 | * @property string|null $prompt_output The AI-generated response text |
| 21 | * @property mixed|null $context Additional context data |
| 22 | * @property string $status Request status (pending, success, error) |
| 23 | * @property int|null $execution_duration_ms Execution time in milliseconds |
| 24 | * @property int $prompt_tokens Input token count |
| 25 | * @property int $completion_tokens Output token count |
| 26 | * @property int $total_tokens Total token count |
| 27 | * @property float $estimated_cost_usd Estimated cost in USD |
| 28 | * @property bool $regenerated Whether this was a regenerated request |
| 29 | * @property string|null $regenerated_from_id Original log ID if regenerated |
| 30 | * @property \Carbon\Carbon|null $created_at When the log was created |
| 31 | * @property \Carbon\Carbon|null $updated_at When the log was last updated |
| 32 | */ |
| 33 | class AIRequestLogResource extends JsonResource |
| 34 | { |
| 35 | /** |
| 36 | * Transform the resource into an array. |
| 37 | * |
| 38 | * @return array<string, mixed> |
| 39 | */ |
| 40 | public function toArray(Request $request): array |
| 41 | { |
| 42 | $user = $this->user_id ? User::find($this->user_id) : null; |
| 43 | $company = $this->company_id ? Company::find($this->company_id) : null; |
| 44 | |
| 45 | return [ |
| 46 | 'id' => (string) $this->_id, |
| 47 | 'user_id' => $this->user_id, |
| 48 | 'user_name' => $user?->name, |
| 49 | 'user_email' => $user?->email, |
| 50 | 'company_id' => $this->company_id, |
| 51 | 'company_name' => $company?->name, |
| 52 | 'feature' => $this->feature, |
| 53 | 'provider' => $this->provider, |
| 54 | 'model' => $this->model, |
| 55 | 'prompt_input' => $this->prompt_input, |
| 56 | 'prompt_output' => $this->prompt_output, |
| 57 | 'context' => $this->context, |
| 58 | 'status' => $this->status, |
| 59 | 'execution_duration_ms' => $this->execution_duration_ms, |
| 60 | 'prompt_tokens' => $this->prompt_tokens, |
| 61 | 'completion_tokens' => $this->completion_tokens, |
| 62 | 'total_tokens' => $this->total_tokens, |
| 63 | 'estimated_cost_usd' => $this->estimated_cost_usd, |
| 64 | 'regenerated' => (bool) $this->regenerated, |
| 65 | 'regenerated_from_id' => $this->regenerated_from_id, |
| 66 | 'created_at' => $this->created_at?->timestamp, |
| 67 | 'updated_at' => $this->updated_at?->timestamp, |
| 68 | ]; |
| 69 | } |
| 70 | } |