From e2e2d9842a4104974d7dffbf9a349179826c6b7a Mon Sep 17 00:00:00 2001 From: Jefferson Oliveira Date: Mon, 10 Nov 2025 16:09:35 -0300 Subject: [PATCH] feat: Logs de atividades --- .../AgentQuotasPolicyController.php | 24 +++--- app/Event/QuotaActivityLog.php | 19 +++++ app/Listener/StoreQuotaActivityLog.php | 40 ++++++++++ app/Model/QuotaActivityLog.php | 42 +++++++++++ .../AgentQuotasPolicyRepository.php | 32 ++++++++ app/Repository/QuotasPolicyRepository.php | 15 ++++ app/Service/AgentQuotasPolicyService.php | 74 +++++++++++++++++++ config/autoload/listeners.php | 1 + ...14149_create_quota_activity_logs_table.php | 30 ++++++++ 9 files changed, 264 insertions(+), 13 deletions(-) create mode 100644 app/Event/QuotaActivityLog.php create mode 100644 app/Listener/StoreQuotaActivityLog.php create mode 100644 app/Model/QuotaActivityLog.php create mode 100644 app/Repository/AgentQuotasPolicyRepository.php create mode 100644 app/Repository/QuotasPolicyRepository.php create mode 100644 app/Service/AgentQuotasPolicyService.php create mode 100644 migrations/2025_11_06_114149_create_quota_activity_logs_table.php diff --git a/app/Controller/AgentQuotasPolicyController.php b/app/Controller/AgentQuotasPolicyController.php index dce5191..cd6a8c2 100755 --- a/app/Controller/AgentQuotasPolicyController.php +++ b/app/Controller/AgentQuotasPolicyController.php @@ -5,9 +5,8 @@ namespace App\Controller; use App\Model\AgentQuotasPolicy; -use App\Model\QuotasPolicy; use App\Schema\AgentQuotasPolicySchema; -use Carbon\Carbon; +use App\Service\AgentQuotasPolicyService; use Exception; use Hyperf\Database\Model\Collection; use Hyperf\Swagger\Annotation as SA; @@ -16,6 +15,10 @@ #[SA\HyperfServer('http')] class AgentQuotasPolicyController extends AbstractController { + public function __construct( + private AgentQuotasPolicyService $service + ) {} + #[SA\Get( path: '/agent-quotas', description: 'Retorna uma lista de todos os relacionamento entre agentes e cotas', @@ -33,7 +36,7 @@ class AgentQuotasPolicyController extends AbstractController )] public function index(): Collection { - return AgentQuotasPolicy::get(); + return $this->service->listAll(); } #[SA\Get( @@ -62,7 +65,7 @@ public function index(): Collection )] public function show(int $id): AgentQuotasPolicy { - return AgentQuotasPolicy::findOrFail($id); + return $this->service->getById($id); } #[SA\Post( @@ -88,14 +91,7 @@ public function show(int $id): AgentQuotasPolicy public function store(): AgentQuotasPolicy { $data = $this->request->all(); - $attributes = [ - 'agent_id' => $data['agent_id'], - 'quotas_policy_id' => $data['quotas_policy_id'], - ]; - - $quota = QuotasPolicy::find($data['quotas_policy_id']); - $data['end_date'] = (new Carbon($data['start_date']))->addYears($quota->validity_duration); - return AgentQuotasPolicy::updateOrCreate($attributes, $data); + return $this->service->store($data); } /** @@ -103,7 +99,9 @@ public function store(): AgentQuotasPolicy */ public function delete(int $id): void { + $data = $this->request->all(); + + $this->service->delete($id, $data); $this->response->withStatus(204); - AgentQuotasPolicy::findOrFail($id)?->delete(); } } diff --git a/app/Event/QuotaActivityLog.php b/app/Event/QuotaActivityLog.php new file mode 100644 index 0000000..7747099 --- /dev/null +++ b/app/Event/QuotaActivityLog.php @@ -0,0 +1,19 @@ +action = $action; + $this->data = $data; + $this->agentQuotaPolicyId = $agentQuotaPolicyId; + } +} diff --git a/app/Listener/StoreQuotaActivityLog.php b/app/Listener/StoreQuotaActivityLog.php new file mode 100644 index 0000000..72eafbc --- /dev/null +++ b/app/Listener/StoreQuotaActivityLog.php @@ -0,0 +1,40 @@ + 'criação', + 'update' => 'atualização', + 'delete' => 'remoção', + ]; + + public function __construct( + private StdoutLoggerInterface $logger + ) {} + + public function listen(): array + { + return [ + \App\Event\QuotaActivityLog::class, + ]; + } + + public function process(object $event): void + { + QuotaActivityLog::create([ + 'action' => $event->action, + 'data' => $event->data, + 'agent_quota_policy_id' => $event->agentQuotaPolicyId, + ]); + + $this->logger->info("Uma atividade de " . self::ACTIVITY[$event->action] . " de cota foi registrada. ID da atividade: {$event->agentQuotaPolicyId}."); + } +} diff --git a/app/Model/QuotaActivityLog.php b/app/Model/QuotaActivityLog.php new file mode 100644 index 0000000..3fad1e0 --- /dev/null +++ b/app/Model/QuotaActivityLog.php @@ -0,0 +1,42 @@ + 'array', + ]; +} diff --git a/app/Repository/AgentQuotasPolicyRepository.php b/app/Repository/AgentQuotasPolicyRepository.php new file mode 100644 index 0000000..da393c1 --- /dev/null +++ b/app/Repository/AgentQuotasPolicyRepository.php @@ -0,0 +1,32 @@ +where('quotas_policy_id', $quotaId) + ->first(); + } + + public function createOrUpdate(array $attributes, array $data): AgentQuotasPolicy + { + return AgentQuotasPolicy::updateOrCreate($attributes, $data); + } +} diff --git a/app/Repository/QuotasPolicyRepository.php b/app/Repository/QuotasPolicyRepository.php new file mode 100644 index 0000000..f00df48 --- /dev/null +++ b/app/Repository/QuotasPolicyRepository.php @@ -0,0 +1,15 @@ +agentQuotasRepo->all(); + } + + public function getById(int $id) + { + return $this->agentQuotasRepo->find($id); + } + + public function store(array $data) + { + $existing = $this->agentQuotasRepo->findExisting( + (int)$data['agent_id'], + (int)$data['quotas_policy_id'] + ); + + $attributes = [ + 'agent_id' => $data['agent_id'], + 'quotas_policy_id' => $data['quotas_policy_id'], + ]; + + $quota = $this->quotaRepo->find((int)$data['quotas_policy_id']); + $data['end_date'] = (new Carbon($data['start_date']))->addYears($quota->validity_duration); + $agentQuota = $this->agentQuotasRepo->createOrUpdate($attributes, $data); + + $this->eventDispatcher->dispatch( + new QuotaActivityLog( + action: $existing ? 'update' : 'create', + data: $agentQuota->toArray(), + agentQuotaPolicyId: $agentQuota->id, + ) + ); + + return $agentQuota; + } + + public function delete(int $id, array $data): void + { + $agentQuota = $this->agentQuotasRepo->find($id); + $agentQuota->delete(); + + $agentQuota = $agentQuota->fresh()->toArray(); + $agentQuota['deleted_by'] = $data['deleted_by']; + + $this->eventDispatcher->dispatch( + new QuotaActivityLog( + action: 'delete', + data: $agentQuota, + agentQuotaPolicyId: $id, + ) + ); + } +} diff --git a/config/autoload/listeners.php b/config/autoload/listeners.php index a25251b..13187a5 100755 --- a/config/autoload/listeners.php +++ b/config/autoload/listeners.php @@ -15,4 +15,5 @@ return [ ErrorExceptionHandler::class, FailToHandleListener::class, + App\Listener\StoreQuotaActivityLog::class, ]; diff --git a/migrations/2025_11_06_114149_create_quota_activity_logs_table.php b/migrations/2025_11_06_114149_create_quota_activity_logs_table.php new file mode 100644 index 0000000..c85b514 --- /dev/null +++ b/migrations/2025_11_06_114149_create_quota_activity_logs_table.php @@ -0,0 +1,30 @@ +bigIncrements('id'); + $table->string('action'); + $table->json('data'); + $table->unsignedBigInteger('agent_quota_policy_id'); + $table->timestamp('created_at')->useCurrent(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('quota_activity_logs'); + } +};