1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Meeting\Member; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Siak\Tontine\Model\Round; |
9
|
|
|
use Siak\Tontine\Model\Member; |
10
|
|
|
use Siak\Tontine\Service\TenantService; |
11
|
|
|
use Siak\Tontine\Service\Traits\WithTrait; |
12
|
|
|
use Siak\Tontine\Validation\SearchSanitizer; |
13
|
|
|
|
14
|
|
|
use function tap; |
15
|
|
|
|
16
|
|
|
class MemberService |
17
|
|
|
{ |
18
|
|
|
use WithTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param TenantService $tenantService |
22
|
|
|
* @param SearchSanitizer $searchSanitizer |
23
|
|
|
*/ |
24
|
|
|
public function __construct(private TenantService $tenantService, |
25
|
|
|
private SearchSanitizer $searchSanitizer) |
26
|
|
|
{} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Round $round |
30
|
|
|
* @param string $search |
31
|
|
|
* |
32
|
|
|
* @return Builder|Relation |
33
|
|
|
*/ |
34
|
|
|
private function getQuery(Round $round, string $search): Builder|Relation |
35
|
|
|
{ |
36
|
|
|
return $round->members() |
37
|
|
|
->search($this->searchSanitizer->sanitize($search)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a paginated list of members. |
42
|
|
|
* |
43
|
|
|
* @param Round $round |
44
|
|
|
* @param string $search |
45
|
|
|
* @param int $page |
46
|
|
|
* |
47
|
|
|
* @return Collection |
48
|
|
|
*/ |
49
|
|
|
public function getMembers(Round $round, string $search = '', int $page = 0): Collection |
50
|
|
|
{ |
51
|
|
|
return tap($this->getQuery($round, $search), |
52
|
|
|
fn($query) => $this->addWith($query)) |
53
|
|
|
->page($page, $this->tenantService->getLimit()) |
54
|
|
|
->orderBy('name', 'asc') |
55
|
|
|
->get(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the number of members. |
60
|
|
|
* |
61
|
|
|
* @param Round $round |
62
|
|
|
* @param string $search |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function getMemberCount(Round $round, string $search = ''): int |
67
|
|
|
{ |
68
|
|
|
return $this->getQuery($round, $search)->count(); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get a list of members for dropdown. |
73
|
|
|
* |
74
|
|
|
* @param Round $round |
75
|
|
|
* |
76
|
|
|
* @return Collection |
77
|
|
|
*/ |
78
|
|
|
public function getMemberList(Round $round): Collection |
79
|
|
|
{ |
80
|
|
|
return $round->members() |
81
|
|
|
->orderBy('name', 'asc') |
82
|
|
|
->get() |
83
|
|
|
->pluck('name', 'id'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get a single member. |
88
|
|
|
* |
89
|
|
|
* @param Round $round |
90
|
|
|
* @param int $memberId |
91
|
|
|
* |
92
|
|
|
* @return Member|null |
93
|
|
|
*/ |
94
|
|
|
public function getMember(Round $round, int $memberId): ?Member |
95
|
|
|
{ |
96
|
|
|
return tap($round->members(), fn($query) => $this |
97
|
|
|
->addWith($query))->find($memberId); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|