1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service\Planning; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Siak\Tontine\Exception\MessageException; |
10
|
|
|
use Siak\Tontine\Model\MemberDef; |
11
|
|
|
use Siak\Tontine\Model\Round; |
12
|
|
|
use Siak\Tontine\Service\TenantService; |
13
|
|
|
use Siak\Tontine\Service\Traits\WithTrait; |
14
|
|
|
use Siak\Tontine\Validation\SearchSanitizer; |
15
|
|
|
|
16
|
|
|
class MemberService |
17
|
|
|
{ |
18
|
|
|
use WithTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param TenantService $tenantService |
22
|
|
|
* @param BillSyncService $billSyncService |
23
|
|
|
* @param SearchSanitizer $searchSanitizer |
24
|
|
|
*/ |
25
|
|
|
public function __construct(private TenantService $tenantService, |
26
|
|
|
private BillSyncService $billSyncService, private SearchSanitizer $searchSanitizer) |
27
|
|
|
{} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Round $round |
31
|
|
|
* @param string $search |
32
|
|
|
* @param bool $filter|null |
33
|
|
|
* |
34
|
|
|
* @return Relation |
35
|
|
|
*/ |
36
|
|
|
private function getQuery(Round $round, string $search, ?bool $filter): Relation |
37
|
|
|
{ |
38
|
|
|
$memberCallback = fn($q) => $q->where('round_id', $round->id); |
39
|
|
|
return $round->guild->members() |
40
|
|
|
->with(['members' => $memberCallback]) |
41
|
|
|
->search($this->searchSanitizer->sanitize($search)) |
42
|
|
|
->when($filter === true, fn(Builder $query) => $query |
43
|
|
|
->whereHas('members', $memberCallback)) |
44
|
|
|
->when($filter === false, fn(Builder $query) => $query |
45
|
|
|
->whereDoesntHave('members', $memberCallback)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get a paginated list of members. |
50
|
|
|
* |
51
|
|
|
* @param Round $round |
52
|
|
|
* @param string $search |
53
|
|
|
* @param bool $filter|null |
54
|
|
|
* @param int $page |
55
|
|
|
* |
56
|
|
|
* @return Collection |
57
|
|
|
*/ |
58
|
|
|
public function getMemberDefs(Round $round, string $search, ?bool $filter, int $page = 0): Collection |
59
|
|
|
{ |
60
|
|
|
return $this->getQuery($round, $search, $filter) |
61
|
|
|
->page($page, $this->tenantService->getLimit()) |
62
|
|
|
->orderBy('name', 'asc') |
63
|
|
|
->get(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the number of members. |
68
|
|
|
* |
69
|
|
|
* @param Round $round |
70
|
|
|
* @param string $search |
71
|
|
|
* @param bool $filter|null |
72
|
|
|
* |
73
|
|
|
* @return int |
74
|
|
|
*/ |
75
|
|
|
public function getMemberDefCount(Round $round, string $search, ?bool $filter): int |
76
|
|
|
{ |
77
|
|
|
return $this->getQuery($round, $search, $filter)->count(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get a single member. |
82
|
|
|
* |
83
|
|
|
* @param Round $round |
84
|
|
|
* @param int $memberId |
85
|
|
|
* |
86
|
|
|
* @return MemberDef|null |
87
|
|
|
*/ |
88
|
|
|
public function getMemberDef(Round $round, int $memberId): ?MemberDef |
89
|
|
|
{ |
90
|
|
|
return $this->getQuery($round, '', null)->find($memberId); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add a new member. |
95
|
|
|
* |
96
|
|
|
* @param Round $round |
97
|
|
|
* @param int $defId |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
|
|
public function enableMember(Round $round, int $defId): void |
102
|
|
|
{ |
103
|
|
|
$def = $this->getMemberDef($round, $defId); |
104
|
|
|
if(!$def || $def->members->count() > 0) |
105
|
|
|
{ |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
DB::transaction(function() use($round, $def) { |
110
|
|
|
$member = $def->members()->create(['round_id' => $round->id]); |
111
|
|
|
|
112
|
|
|
$this->billSyncService->memberEnabled($round, $member); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Delete a member. |
118
|
|
|
* |
119
|
|
|
* @param Round $round |
120
|
|
|
* @param int $defId |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function disableMember(Round $round, int $defId): void |
125
|
|
|
{ |
126
|
|
|
$def = $this->getMemberDef($round, $defId); |
127
|
|
|
if(!$def || $def->members->count() === 0) |
128
|
|
|
{ |
129
|
|
|
return; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$member = $def->members->first(); |
133
|
|
|
try |
134
|
|
|
{ |
135
|
|
|
DB::transaction(function() use($round, $member) { |
136
|
|
|
$this->billSyncService->memberRemoved($round, $member); |
137
|
|
|
|
138
|
|
|
$member->delete(); |
139
|
|
|
}); |
140
|
|
|
} |
141
|
|
|
catch(Exception) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
throw new MessageException(trans('tontine.member.errors.cannot_remove')); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|