-
-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathMessageRepository.php
More file actions
77 lines (65 loc) · 2.39 KB
/
MessageRepository.php
File metadata and controls
77 lines (65 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace HiEvents\Repository\Eloquent;
use Carbon\Carbon;
use HiEvents\DomainObjects\Generated\MessageDomainObjectAbstract;
use HiEvents\DomainObjects\MessageDomainObject;
use HiEvents\DomainObjects\Status\MessageStatus;
use HiEvents\Http\DTO\QueryParamsDTO;
use HiEvents\Models\Message;
use HiEvents\Repository\Interfaces\MessageRepositoryInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
/**
* @extends BaseRepository<MessageDomainObject>
*/
class MessageRepository extends BaseRepository implements MessageRepositoryInterface
{
protected function getModel(): string
{
return Message::class;
}
public function getDomainObject(): string
{
return MessageDomainObject::class;
}
public function findByEventId(int $eventId, QueryParamsDTO $params): LengthAwarePaginator
{
$where = [
[MessageDomainObjectAbstract::EVENT_ID, '=', $eventId]
];
if ($params->query) {
$where[] = static function (Builder $builder) use ($params) {
$builder
->where(MessageDomainObjectAbstract::SUBJECT, 'ilike', '%' . $params->query . '%')
->orWhere(MessageDomainObjectAbstract::MESSAGE, 'ilike', '%' . $params->query . '%');
};
}
if ($params->filter_fields && $params->filter_fields->isNotEmpty()) {
$this->applyFilterFields($params, MessageDomainObject::getAllowedFilterFields());
}
$this->model = $this->model->orderBy(
$params->sort_by ?? MessageDomainObject::getDefaultSort(),
$params->sort_direction ?? 'desc',
);
return $this->paginateWhere(
where: $where,
limit: $params->per_page,
page: $params->page,
);
}
public function countMessagesInLast24Hours(int $accountId): int
{
$count = $this->model
->join('events', 'messages.event_id', '=', 'events.id')
->where('events.account_id', $accountId)
->where('messages.created_at', '>=', Carbon::now()->subHours(24))
->whereIn('messages.status', [
MessageStatus::PROCESSING->name,
MessageStatus::SENT->name,
MessageStatus::PENDING_REVIEW->name,
])
->count();
$this->resetModel();
return $count;
}
}