-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPersonController.php
More file actions
229 lines (209 loc) · 8.47 KB
/
PersonController.php
File metadata and controls
229 lines (209 loc) · 8.47 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
namespace App\Http\Controllers;
use App\Http\Requests\PersonRequest;
use App\Http\Resources\ApiResource;
use App\Models\Country;
use App\Services\AddressesService;
use App\Services\CountryService;
use App\Services\PersonAddressService;
use App\Services\PersonService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* Class PersonController.
*
* @group People
*/
class PersonController extends Controller {
public function __construct(
private AddressesService $addressService,
private PersonService $personService,
private PersonAddressService $personAddressService,
private CountryService $countryService,
) {}
/**
* List customer/other
* [ To get a list of registered customers or non-customer like contact person of Meter Manufacturer. ].
*
* @urlParam is_customer int optinal. To get a list of customers or non customer. Default : 1
* @urlParam agent_id int optional. To get a list of customers of a specific agent.
* @urlParam limit int optional. The number of items per page.
* @urlParam active_customer int optional. To get a list of active customers. Default: 0
* @urlParam village_id int optional. Filter by primary address village id.
* @urlParam total_paid_min float optional. Minimum total paid amount for the customer.
* @urlParam total_paid_max float optional. Maximum total paid amount for the customer.
* @urlParam latest_payment_from string optional. ISO date string for minimum latest payment date.
* @urlParam latest_payment_to string optional. ISO date string for maximum latest payment date.
* @urlParam registration_from string optional. ISO date string for minimum registration date.
* @urlParam registration_to string optional. ISO date string for maximum registration date.
* @urlParam device_type string optional. Filter by device/appliance type.
*
* @responseFile responses/people/people.list.json
*/
public function index(Request $request): ApiResource {
$customerType = $request->input('is_customer', 1);
$perPage = $request->input('per_page', config('settings.paginate', 15));
$agentId = $request->input('agent_id');
$activeCustomer = $request->has('active_customer') ? (bool) $request->input('active_customer') : null;
$villageId = $request->input('village_id');
$totalPaidMin = $request->input('total_paid_min');
$totalPaidMax = $request->input('total_paid_max');
$latestPaymentFrom = $request->input('latest_payment_from');
$latestPaymentTo = $request->input('latest_payment_to');
$registrationFrom = $request->input('registration_from');
$registrationTo = $request->input('registration_to');
$deviceType = $request->input('device_type');
return ApiResource::make(
$this->personService->getAll(
$perPage,
$customerType,
$agentId,
$activeCustomer,
$villageId,
$totalPaidMin,
$totalPaidMax,
$latestPaymentFrom,
$latestPaymentTo,
$registrationFrom,
$registrationTo,
$deviceType,
)
);
}
/**
* Detail
* Displays the person with following relations
* - Addresses
* - Citizenship
* - Role
* - Meter list.
*
* @apiResourceModel App\Models\Person\Person
*
* @responseFile responses/people/people.detail.json
*/
public function show(int $personId): ApiResource {
return ApiResource::make($this->personService->getDetails($personId, true));
}
/**
* Create.
*/
public function store(PersonRequest $request): JsonResponse {
try {
$customerType = $request->input('customer_type');
$addressData = $this->addressService->createAddressDataFromRequest($request);
$personData = $this->personService->createPersonDataFromRequest($request);
$miniGridId = $request->input('mini_grid_id');
DB::connection('tenant')->beginTransaction();
if ($this->personService->isMaintenancePerson($customerType)) {
$personData['mini_grid_id'] = $miniGridId;
$person = $this->personService->createMaintenancePerson($personData);
} else {
$country = $this->countryService->getByCode($request->get('country_code'));
$person = $this->personService->create($personData);
if ($country instanceof Country) {
$person = $this->personService->addCitizenship($person, $country);
}
}
$address = $this->addressService->make($addressData);
$this->personAddressService->setAssignee($person);
$this->personAddressService->setAssigned($address);
$this->personAddressService->assign();
$this->addressService->save($address);
DB::connection('tenant')->commit();
return ApiResource::make($person)->response()->setStatusCode(201);
} catch (\Exception $e) {
DB::connection('tenant')->rollBack();
throw new \Exception($e->getMessage(), $e->getCode(), $e);
}
}
/**
* Update
* Updates the given parameter of that person.
*
* @urlParam id required The ID of the person to update
*
* @bodyParam title string. The title of the person. Example: Dr.
* @bodyParam name string. The title of the person. Example: Dr.
* @bodyParam surname string. The title of the person. Example: Dr.
* @bodyParam birth_date string. The title of the person. Example: Dr.
* @bodyParam gender string. The title of the person. Example: Dr.
* @bodyParam education string. The title of the person. Example: Dr.
*
* @apiResourceModel App\Models\Person\Person
*
* @responseFile responses/people/person.update.json
*/
public function update(
int $personId,
PersonRequest $request,
): ApiResource {
$person = $this->personService->getById($personId);
$personData = $request->all();
return ApiResource::make($this->personService->update($person, $personData));
}
/**
* Transactions
* The list of all transactions(paginated) which belong to that person.
* Each page contains 7 entries of the last transaction.
*
* @bodyParam person_id int required the ID of the person. Example: 2
*
* @responseFile responses/people/person.transaction.list.json
*/
public function transactions(
int $personId,
): ApiResource {
$person = $this->personService->getById($personId);
return ApiResource::make($this->personService->getPersonTransactions($person));
}
/**
* Search
* Searches in person list according to the search term.
* Term could be one of the following attributes;
* - phone number
* - meter serial number
* - name
* - surname.
*
* @urlParam term The ID of the post. Example: John Doe
* @urlParam paginage int The page number. Example:1
*
* @responseFile responses/people/people.search.json
*/
public function search(
Request $request,
): ApiResource {
$term = $request->input('term', '');
$paginate = $request->input('paginate', 1);
$per_page = $request->input('per_page', 15);
return ApiResource::make($this->personService->searchPerson($term, $paginate, $per_page));
}
/**
* Delete
* Deletes that person with all his/her relations from the database. The person model uses soft deletes.
* That means the orinal record wont be deleted but all mentioned relations will be removed permanently.
*
* @urlParam person required The ID of the person. Example:1
*
* @throws \Exception
*
* @apiResourceModel App\Models\Person\Person
*/
public function destroy(
int $personId,
): JsonResponse {
$person = $this->personService->getById($personId);
$deleted = $this->personService->delete($person);
if (!$deleted) {
return response()->json([
'message' => 'Failed to delete person',
], 500);
}
return ApiResource::make([
'message' => 'Person deleted successfully',
'data' => $person,
])->response()->setStatusCode(200);
}
}