-
-
Notifications
You must be signed in to change notification settings - Fork 636
Expand file tree
/
Copy pathAttendeeRepository.php
More file actions
142 lines (122 loc) · 6.06 KB
/
AttendeeRepository.php
File metadata and controls
142 lines (122 loc) · 6.06 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace HiEvents\Repository\Eloquent;
use HiEvents\DomainObjects\AttendeeCheckInDomainObject;
use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Generated\AttendeeDomainObjectAbstract;
use HiEvents\DomainObjects\Status\AttendeeStatus;
use HiEvents\DomainObjects\Status\OrderStatus;
use HiEvents\Http\DTO\QueryParamsDTO;
use HiEvents\Models\Attendee;
use HiEvents\Repository\Eloquent\Value\Relationship;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
/**
* @extends BaseRepository<AttendeeDomainObject>
*/
class AttendeeRepository extends BaseRepository implements AttendeeRepositoryInterface
{
protected function getModel(): string
{
return Attendee::class;
}
public function getDomainObject(): string
{
return AttendeeDomainObject::class;
}
public function findByEventIdForExport(int $eventId): Collection
{
$this->applyConditions([
'attendees.event_id' => $eventId,
]);
$this->model->select('attendees.*');
$this->model->join('orders', 'orders.id', '=', 'attendees.order_id');
$this->model->whereIn('orders.status', [
OrderStatus::AWAITING_OFFLINE_PAYMENT->name,
OrderStatus::COMPLETED->name,
OrderStatus::CANCELLED->name
]);
$model = $this->model->limit(10000)->get();
$this->resetModel();
return $this->handleResults($model);
}
public function findByEventId(int $eventId, QueryParamsDTO $params): LengthAwarePaginator
{
$where = [
['attendees.event_id', '=', $eventId]
];
if ($params->query) {
$where[] = static function (Builder $builder) use ($params) {
$builder
->where(
DB::raw(
sprintf(
"(%s||' '||%s)",
'attendees.' . AttendeeDomainObjectAbstract::FIRST_NAME,
'attendees.' . AttendeeDomainObjectAbstract::LAST_NAME,
)
), 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::LAST_NAME, 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::FIRST_NAME, 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::PUBLIC_ID, 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::EMAIL, 'ilike', '%' . $params->query . '%');
};
}
$this->model = $this->model->select('attendees.*')
->join('orders', 'orders.id', '=', 'attendees.order_id')
->whereIn('orders.status', [OrderStatus::COMPLETED->name, OrderStatus::CANCELLED->name, OrderStatus::AWAITING_OFFLINE_PAYMENT->name]);
if ($params->filter_fields && $params->filter_fields->isNotEmpty()) {
$this->applyFilterFields($params, AttendeeDomainObject::getAllowedFilterFields(), prefix: 'attendees');
}
$sortBy = $params->sort_by ?? AttendeeDomainObject::getDefaultSort();
$sortDirection = $params->sort_direction ?? AttendeeDomainObject::getDefaultSortDirection();
if ($sortBy === AttendeeDomainObject::TICKET_NAME_SORT_KEY) {
$this->model = $this->model
->leftJoin('products', 'products.id', '=', 'attendees.product_id')
->orderBy('products.title', $sortDirection);
} else {
$this->model = $this->model->orderBy('attendees.' . $sortBy, $sortDirection);
}
return $this->paginateWhere(
where: $where,
limit: $params->per_page,
page: $params->page,
);
}
public function getAttendeesByCheckInShortId(string $shortId, QueryParamsDTO $params): Paginator
{
$where = [];
if ($params->query) {
$where[] = static function (Builder $builder) use ($params) {
$builder
->where(
DB::raw(
sprintf(
"(%s||' '||%s)",
'attendees.' . AttendeeDomainObjectAbstract::FIRST_NAME,
'attendees.' . AttendeeDomainObjectAbstract::LAST_NAME,
)
), 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::LAST_NAME, 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::FIRST_NAME, 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::PUBLIC_ID, 'ilike', '%' . $params->query . '%')
->orWhere('attendees.' . AttendeeDomainObjectAbstract::EMAIL, 'ilike', '%' . $params->query . '%');
};
}
$this->model = $this->model->select('attendees.*')
->join('orders', 'orders.id', '=', 'attendees.order_id')
->join('product_check_in_lists', 'product_check_in_lists.product_id', '=', 'attendees.product_id')
->join('check_in_lists', 'check_in_lists.id', '=', 'product_check_in_lists.check_in_list_id')
->where('check_in_lists.short_id', $shortId)
->whereIn('attendees.status',[AttendeeStatus::ACTIVE->name, AttendeeStatus::CANCELLED->name, AttendeeStatus::AWAITING_PAYMENT->name])
->whereIn('orders.status', [OrderStatus::COMPLETED->name, OrderStatus::AWAITING_OFFLINE_PAYMENT->name]);
$this->loadRelation(new Relationship(AttendeeCheckInDomainObject::class, name: 'check_ins'));
return $this->simplePaginateWhere(
where: $where,
limit: min($params->per_page, 250),
);
}
}