1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Response\enso\ActivityLog; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Responsable; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use LaravelEnso\Core\Http\Resources\User; |
8
|
|
|
use App\Models\enso\activitylogs\ActivityLog; |
9
|
|
|
class Timeline implements Responsable |
10
|
|
|
{ |
11
|
|
|
private const Chunk = 50; |
12
|
|
|
|
13
|
|
|
private Collection $timeline; |
14
|
|
|
|
15
|
|
|
public function toResponse($request) |
16
|
|
|
{ |
17
|
|
|
$this->timeline = $this->timeline($request); |
18
|
|
|
|
19
|
|
|
return $this->days()->map(fn ($day) => [ |
|
|
|
|
20
|
|
|
'date' => $day, |
21
|
|
|
'entries' => $this->dayEntries($day) |
22
|
|
|
->map(fn ($entry) => $this->resource($entry)), |
23
|
|
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function timeline($request) |
27
|
|
|
{ |
28
|
|
|
$filters = json_decode($request->get('filters')); |
29
|
|
|
|
30
|
|
|
return ActivityLog::latest() |
31
|
|
|
->with('createdBy.person', 'createdBy.avatar') |
32
|
|
|
->skip($request->get('offset')) |
33
|
|
|
->between($filters->interval->min, $filters->interval->max) |
34
|
|
|
->forUsers($filters->userIds) |
35
|
|
|
->forEvents($filters->events) |
36
|
|
|
->forRoles($filters->roleIds) |
37
|
|
|
->take(self::Chunk) |
38
|
|
|
->get(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function resource($entry) |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
'id' => $entry->id, |
45
|
|
|
'meta' => $entry->meta, |
46
|
|
|
'time' => $entry->created_at->format('H:i A'), |
47
|
|
|
'owner' => new User($entry->createdBy), |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function dayEntries($day) |
52
|
|
|
{ |
53
|
|
|
return $this->timeline |
54
|
|
|
->filter(fn ($entry) => $entry->created_at->format('Y-m-d') === $day) |
55
|
|
|
->values(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function days() |
59
|
|
|
{ |
60
|
|
|
return $this->timeline |
61
|
|
|
->map(fn ($entry) => $entry->created_at->format('Y-m-d')) |
62
|
|
|
->unique() |
63
|
|
|
->values(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: