Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
28 / 32 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| AIRequestLogRepository | |
87.50% |
28 / 32 |
|
60.00% |
3 / 5 |
13.33 | |
0.00% |
0 / 1 |
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateStatus | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| findByUser | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| findById | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAllPaginated | |
86.36% |
19 / 22 |
|
0.00% |
0 / 1 |
8.16 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Repositories; |
| 4 | |
| 5 | use App\Http\Models\FlyMsgAI\AIRequestLog; |
| 6 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 7 | |
| 8 | /** |
| 9 | * Data access layer for AI request log records. |
| 10 | */ |
| 11 | class AIRequestLogRepository |
| 12 | { |
| 13 | /** |
| 14 | * Create a new AI request log entry. |
| 15 | * |
| 16 | * @param array<string, mixed> $data Log fields (see AIRequestLog::$fillable) |
| 17 | */ |
| 18 | public function create(array $data): AIRequestLog |
| 19 | { |
| 20 | return AIRequestLog::create($data); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Update the status and optional extra fields of an existing log entry. |
| 25 | * |
| 26 | * @param string $id The log document ID |
| 27 | * @param string $status New status (success | error | pending) |
| 28 | * @param array<string, mixed> $extra Additional fields to update |
| 29 | */ |
| 30 | public function updateStatus(string $id, string $status, array $extra = []): bool |
| 31 | { |
| 32 | $log = AIRequestLog::find($id); |
| 33 | |
| 34 | if (! $log) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | return $log->update(array_merge(['status' => $status], $extra)); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Find log entries for a specific user, ordered by most recent first. |
| 43 | * |
| 44 | * @return \Illuminate\Database\Eloquent\Collection<int, AIRequestLog> |
| 45 | */ |
| 46 | public function findByUser(string $userId, int $limit = 50): \Illuminate\Database\Eloquent\Collection |
| 47 | { |
| 48 | return AIRequestLog::where('user_id', $userId) |
| 49 | ->orderBy('created_at', 'desc') |
| 50 | ->limit($limit) |
| 51 | ->get(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Find a single log entry by ID. |
| 56 | */ |
| 57 | public function findById(string $id): ?AIRequestLog |
| 58 | { |
| 59 | return AIRequestLog::find($id); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Get paginated log entries with optional filters. |
| 64 | * |
| 65 | * @param array{ |
| 66 | * user_id?: string, |
| 67 | * company_id?: string, |
| 68 | * feature?: string, |
| 69 | * status?: string, |
| 70 | * search?: string, |
| 71 | * sort_order?: string, |
| 72 | * date_from?: string, |
| 73 | * date_to?: string |
| 74 | * } $filters Optional filter criteria |
| 75 | * @param int $perPage Number of items per page |
| 76 | */ |
| 77 | public function getAllPaginated(array $filters = [], int $perPage = 25): LengthAwarePaginator |
| 78 | { |
| 79 | $query = AIRequestLog::query(); |
| 80 | |
| 81 | if (! empty($filters['user_id'])) { |
| 82 | $query->where('user_id', $filters['user_id']); |
| 83 | } |
| 84 | |
| 85 | if (! empty($filters['company_id'])) { |
| 86 | $query->where('company_id', $filters['company_id']); |
| 87 | } |
| 88 | |
| 89 | if (! empty($filters['feature'])) { |
| 90 | $query->where('feature', $filters['feature']); |
| 91 | } |
| 92 | |
| 93 | if (! empty($filters['status'])) { |
| 94 | $query->where('status', $filters['status']); |
| 95 | } |
| 96 | |
| 97 | if (! empty($filters['search'])) { |
| 98 | $search = $filters['search']; |
| 99 | $query->where(function ($q) use ($search) { |
| 100 | $q->where('prompt_input', 'like', "%{$search}%") |
| 101 | ->orWhere('prompt_output', 'like', "%{$search}%"); |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | if (! empty($filters['date_from'])) { |
| 106 | $query->where('created_at', '>=', $filters['date_from']); |
| 107 | } |
| 108 | |
| 109 | if (! empty($filters['date_to'])) { |
| 110 | $query->where('created_at', '<=', $filters['date_to']); |
| 111 | } |
| 112 | |
| 113 | $sortOrder = $filters['sort_order'] ?? 'desc'; |
| 114 | $query->orderBy('created_at', $sortOrder); |
| 115 | |
| 116 | return $query->paginate($perPage); |
| 117 | } |
| 118 | } |