Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/backend/app/Http/Controllers/AgentAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public function me(): JsonResponse {
$permissions = [];
}

$agent = $this->agentService->getById($agent->id);

return response()->json([
/* @var Agent */
'agent' => $agent,
Expand Down
22 changes: 22 additions & 0 deletions src/backend/app/Http/Controllers/AgentCustomerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Http\Controllers;

use App\Http\Requests\AssignMeterToCustomerRequest;
use App\Http\Requests\CreateAgentCustomerRequest;
use App\Http\Resources\ApiResource;
use App\Models\Person\Person;
use App\Services\AgentCustomerService;
use App\Services\AgentService;
use Illuminate\Http\JsonResponse;
Expand Down Expand Up @@ -62,4 +64,24 @@ public function store(CreateAgentCustomerRequest $request): JsonResponse {
throw $e;
}
}

public function storeMeter(AssignMeterToCustomerRequest $request, int $customerId): JsonResponse {
$agent = $this->agentService->getByAuthenticatedUser();
$customer = Person::query()->where('is_customer', 1)->findOrFail($customerId);

try {
DB::connection('tenant')->beginTransaction();
$meter = $this->agentCustomerService->assignMeter($agent, $customer, $request);
DB::connection('tenant')->commit();

return ApiResource::make($meter)->response()->setStatusCode(201);
} catch (ValidationException $e) {
DB::connection('tenant')->rollBack();
throw $e;
} catch (\Exception $e) {
DB::connection('tenant')->rollBack();
Log::critical('Error while an agent was assigning a meter to a customer', ['message' => $e->getMessage()]);
throw $e;
}
}
}
26 changes: 26 additions & 0 deletions src/backend/app/Http/Requests/AssignMeterToCustomerRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AssignMeterToCustomerRequest extends FormRequest {
public function authorize(): bool {
return true;
}

/**
* @return array<string, mixed>
*/
public function rules(): array {
return [
'serial_number' => ['required', 'string', 'unique:tenant.meters,serial_number'],
'manufacturer_id' => ['required', 'integer', 'exists:tenant.manufacturers,id'],
'meter_type_id' => ['required', 'integer', 'exists:tenant.meter_types,id'],
'tariff_id' => ['required', 'integer', 'exists:tenant.tariffs,id'],
'connection_group_id' => ['required', 'integer', 'exists:tenant.connection_groups,id'],
'connection_type_id' => ['required', 'integer', 'exists:tenant.connection_types,id'],
'geo_points' => ['nullable', 'string'],
];
}
}
45 changes: 45 additions & 0 deletions src/backend/app/Services/AgentCustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Services;

use App\Events\AccessRatePaymentInitialize;
use App\Http\Requests\AssignMeterToCustomerRequest;
use App\Http\Requests\CreateAgentCustomerRequest;
use App\Models\Agent;
use App\Models\City;
use App\Models\Meter\Meter;
use App\Models\Person\Person;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -17,6 +20,9 @@ public function __construct(
private PersonService $personService,
private GeographicalInformationService $geographicalInformationService,
private AddressGeographicalInformationService $addressGeographicalInformationService,
private MeterService $meterService,
private DeviceService $deviceService,
private MeterDeviceService $meterDeviceService,
) {}

public function register(Agent $agent, CreateAgentCustomerRequest $request): Person {
Expand Down Expand Up @@ -50,6 +56,45 @@ public function register(Agent $agent, CreateAgentCustomerRequest $request): Per
return $person->fresh(['addresses.city', 'addresses.geo']);
}

public function assignMeter(Agent $agent, Person $customer, AssignMeterToCustomerRequest $request): Meter {
$primaryAddress = $customer->addresses()->where('is_primary', 1)->with('city')->first();
if ($primaryAddress === null || $primaryAddress->city === null || $primaryAddress->city->mini_grid_id !== $agent->mini_grid_id) {
throw ValidationException::withMessages(['customer' => ['Customer does not belong to the agent\'s mini-grid.']]);
}

$meter = $this->meterService->create([
'serial_number' => $request->string('serial_number')->toString(),
'manufacturer_id' => $request->integer('manufacturer_id'),
'meter_type_id' => $request->integer('meter_type_id'),
'tariff_id' => $request->integer('tariff_id'),
'connection_group_id' => $request->integer('connection_group_id'),
'connection_type_id' => $request->integer('connection_type_id'),
'in_use' => 1,
]);

$device = $this->deviceService->make([
'person_id' => $customer->id,
'device_serial' => $meter->serial_number,
]);
$this->meterDeviceService->setAssigned($device);
$this->meterDeviceService->setAssignee($meter);
$this->meterDeviceService->assign();
$this->deviceService->save($device);

$geoPoints = $request->string('geo_points')->toString();
if ($geoPoints !== '') {
$geographicalInformation = $this->geographicalInformationService->make(['points' => $geoPoints]);
$this->addressGeographicalInformationService->setAssigned($geographicalInformation);
$this->addressGeographicalInformationService->setAssignee($primaryAddress);
$this->addressGeographicalInformationService->assign();
$this->geographicalInformationService->save($geographicalInformation);
}

event(new AccessRatePaymentInitialize($meter));

return $meter->fresh(['tariff', 'device', 'meterType', 'connectionType', 'connectionGroup', 'manufacturer']);
}

/**
* @return LengthAwarePaginator<int, Person>
*/
Expand Down
3 changes: 2 additions & 1 deletion src/backend/app/Services/AgentTransactionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getAll(
static function ($q) use ($agentId) {
$q->where('agent_id', $agentId);
}
);
)->latest()->orderByDesc('id');

return $limit ? $query->paginate($limit) : $query->get();
}
Expand All @@ -63,6 +63,7 @@ public function getByCustomerId(int $agentId, ?int $customerId = null): Collecti
)
->whereHas('device', fn ($q) => $q->whereIn('device_serial', $customerDeviceSerials))
->latest()
->orderByDesc('id')
->paginate();
}

Expand Down
2 changes: 2 additions & 0 deletions src/backend/routes/resources/AgentApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
Route::group(['prefix' => 'customers'], function () {
Route::get('/', [AgentCustomerController::class, 'index']);
Route::post('/', [AgentCustomerController::class, 'store']);
Route::post('/{customerId}/meters', [AgentCustomerController::class, 'storeMeter'])
->where('customerId', '[0-9]+');
Route::get('/search', [AgentCustomerController::class, 'search']);
Route::get(
'/{customerId}/graph/{period}/{limit?}/{order?}',
Expand Down
Loading