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
24 changes: 11 additions & 13 deletions app/Controller/AgentQuotasPolicyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -33,7 +36,7 @@ class AgentQuotasPolicyController extends AbstractController
)]
public function index(): Collection
{
return AgentQuotasPolicy::get();
return $this->service->listAll();
}

#[SA\Get(
Expand Down Expand Up @@ -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(
Expand All @@ -88,22 +91,17 @@ 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);
}

/**
* @throws Exception
*/
public function delete(int $id): void
{
$data = $this->request->all();

$this->service->delete($id, $data);
$this->response->withStatus(204);
AgentQuotasPolicy::findOrFail($id)?->delete();
}
}
19 changes: 19 additions & 0 deletions app/Event/QuotaActivityLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace App\Event;

class QuotaActivityLog
{
public string $action;
public ?array $data;
public int $agentQuotaPolicyId;

public function __construct(string $action, ?array $data, int $agentQuotaPolicyId)
{
$this->action = $action;
$this->data = $data;
$this->agentQuotaPolicyId = $agentQuotaPolicyId;
}
}
40 changes: 40 additions & 0 deletions app/Listener/StoreQuotaActivityLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Listener;

use App\Model\QuotaActivityLog;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Event\Contract\ListenerInterface;

class StoreQuotaActivityLog implements ListenerInterface
{
const ACTIVITY = [
'create' => '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}.");
}
}
42 changes: 42 additions & 0 deletions app/Model/QuotaActivityLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
* @property int $id
* @property string $action
* @property string $before
* @property string $after
* @property int $agent_quota_id
* @property \Carbon\Carbon $created_at
* @property \Carbon\Carbon $updated_at
*/
class QuotaActivityLog extends Model
{
const UPDATED_AT = null;

/**
* The table associated with the model.
*/
protected ?string $table = 'quota_activity_logs';

/**
* The attributes that are mass assignable.
*/
protected array $fillable = [
'action',
'data',
'agent_quota_policy_id',
];

/**
* The attributes that should be cast to native types.
*/
protected array $casts = [
'data' => 'array',
];
}
32 changes: 32 additions & 0 deletions app/Repository/AgentQuotasPolicyRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Repository;

use App\Model\AgentQuotasPolicy;

class AgentQuotasPolicyRepository
{
public function all()
{
return AgentQuotasPolicy::get();
}

public function find(int $id): AgentQuotasPolicy
{
return AgentQuotasPolicy::findOrFail($id);
}

public function findExisting(int $agentId, int $quotaId)
{
return AgentQuotasPolicy::where('agent_id', $agentId)
->where('quotas_policy_id', $quotaId)
->first();
}

public function createOrUpdate(array $attributes, array $data): AgentQuotasPolicy
{
return AgentQuotasPolicy::updateOrCreate($attributes, $data);
}
}
15 changes: 15 additions & 0 deletions app/Repository/QuotasPolicyRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace App\Repository;

use App\Model\QuotasPolicy;

class QuotasPolicyRepository
{
public function find(int $id): QuotasPolicy
{
return QuotasPolicy::findOrFail($id);
}
}
74 changes: 74 additions & 0 deletions app/Service/AgentQuotasPolicyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace App\Service;

use App\Event\QuotaActivityLog;
use App\Repository\AgentQuotasPolicyRepository;
use App\Repository\QuotasPolicyRepository;
use Carbon\Carbon;
use Psr\EventDispatcher\EventDispatcherInterface;

class AgentQuotasPolicyService
{
public function __construct(
private AgentQuotasPolicyRepository $agentQuotasRepo,
private EventDispatcherInterface $eventDispatcher,
private QuotasPolicyRepository $quotaRepo,
) {}

public function listAll()
{
return $this->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,
)
);
}
}
1 change: 1 addition & 0 deletions config/autoload/listeners.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
return [
ErrorExceptionHandler::class,
FailToHandleListener::class,
App\Listener\StoreQuotaActivityLog::class,
];
30 changes: 30 additions & 0 deletions migrations/2025_11_06_114149_create_quota_activity_logs_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('quota_activity_logs', function (Blueprint $table) {
$table->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');
}
};