|
2 | 2 |
|
3 | 3 | namespace App\Services; |
4 | 4 |
|
| 5 | +use App\Http\Requests\CreateAgentCustomerRequest; |
5 | 6 | use App\Models\Agent; |
| 7 | +use App\Models\City; |
6 | 8 | use App\Models\Person\Person; |
7 | 9 | use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
| 10 | +use Illuminate\Database\Eloquent\Builder; |
| 11 | +use Illuminate\Validation\ValidationException; |
8 | 12 |
|
9 | 13 | class AgentCustomerService { |
10 | | - public function __construct(private Person $person) {} |
| 14 | + public function __construct( |
| 15 | + private Person $person, |
| 16 | + private City $city, |
| 17 | + private PersonService $personService, |
| 18 | + private GeographicalInformationService $geographicalInformationService, |
| 19 | + private AddressGeographicalInformationService $addressGeographicalInformationService, |
| 20 | + ) {} |
| 21 | + |
| 22 | + public function register(Agent $agent, CreateAgentCustomerRequest $request): Person { |
| 23 | + $cityId = $request->integer('city_id'); |
| 24 | + $phone = $request->string('phone')->toString(); |
| 25 | + $geoPoints = $request->string('geo_points')->toString(); |
| 26 | + |
| 27 | + $city = $this->city->newQuery()->findOrFail($cityId); |
| 28 | + if ($city->mini_grid_id !== $agent->mini_grid_id) { |
| 29 | + throw ValidationException::withMessages(['city_id' => ['Selected city does not belong to the agent\'s mini-grid.']]); |
| 30 | + } |
| 31 | + |
| 32 | + if ($this->personService->getByPhoneNumber($phone) instanceof Person) { |
| 33 | + throw ValidationException::withMessages(['phone' => ['A customer with this phone number already exists.']]); |
| 34 | + } |
| 35 | + |
| 36 | + $request->merge(['is_customer' => 1]); |
| 37 | + $person = $this->personService->createFromRequest($request); |
| 38 | + |
| 39 | + if ($geoPoints !== '') { |
| 40 | + $address = $person->addresses()->where('is_primary', 1)->first(); |
| 41 | + $geographicalInformation = $this->geographicalInformationService->make([ |
| 42 | + 'points' => $geoPoints, |
| 43 | + ]); |
| 44 | + $this->addressGeographicalInformationService->setAssigned($geographicalInformation); |
| 45 | + $this->addressGeographicalInformationService->setAssignee($address); |
| 46 | + $this->addressGeographicalInformationService->assign(); |
| 47 | + $this->geographicalInformationService->save($geographicalInformation); |
| 48 | + } |
| 49 | + |
| 50 | + return $person->fresh(['addresses.city', 'addresses.geo']); |
| 51 | + } |
11 | 52 |
|
12 | 53 | /** |
13 | 54 | * @return LengthAwarePaginator<int, Person> |
14 | 55 | */ |
15 | 56 | public function list(Agent $agent): LengthAwarePaginator { |
16 | | - $miniGridId = $agent->mini_grid_id; |
| 57 | + return $this->scopedQuery($agent)->paginate(config('settings.paginate')); |
| 58 | + } |
| 59 | + |
| 60 | + public function findForAgent(Agent $agent, int $customerId): Person { |
| 61 | + return $this->scopedQuery($agent)->findOrFail($customerId); |
| 62 | + } |
17 | 63 |
|
| 64 | + /** |
| 65 | + * @return Builder<Person> |
| 66 | + */ |
| 67 | + private function scopedQuery(Agent $agent): Builder { |
18 | 68 | return $this->person->newQuery()->with([ |
19 | 69 | 'devices', |
20 | 70 | 'addresses' => fn ($q) => $q->where('is_primary', 1)->with('city'), |
21 | 71 | ]) |
22 | 72 | ->where('is_customer', 1) |
23 | 73 | ->whereHas( |
24 | 74 | 'addresses', |
25 | | - fn ($q) => $q->whereHas('city', fn ($q) => $q->where('mini_grid_id', $miniGridId)) |
26 | | - ) |
27 | | - ->paginate(config('settings.paginate')); |
| 75 | + fn ($q) => $q->whereHas('city', fn ($q) => $q->where('mini_grid_id', $agent->mini_grid_id)) |
| 76 | + ); |
28 | 77 | } |
29 | 78 |
|
30 | 79 | /** |
|
0 commit comments