feat: consolidar desenvolvimentos do portal InvestMoz

This commit is contained in:
2026-11-03 23:32:32 +02:00
parent 1da273f1f3
commit 36c8cadb49
345 changed files with 110714 additions and 2799 deletions
+25 -3
View File
@@ -48,7 +48,7 @@ class OrganizationCountry
public function save(User $actor, string $id, array $data): array
{
$this->authorize($actor, $id, true);
$data = Validator::make($data, ['version' => 'required|integer|min:1',
$data = Validator::make($data, ['version' => 'required|integer|min:1', 'operation_key' => ['sometimes', 'required', 'uuid'],
'country' => ['required', 'string', app(CountryCatalogue::class)->rule()],
'source_reference' => 'required|string|min:5|max:2000'], [
'country.required' => 'Selecione o país de constituição da organização.',
@@ -57,9 +57,17 @@ class OrganizationCountry
])->validate();
return DB::transaction(function () use ($actor, $id, $data) {
DB::table('users')->where('id', $actor->id)->lockForUpdate()->firstOrFail();
DB::table('memberships')->where('organization_id', $id)->where('user_id', $actor->id)->lockForUpdate()->get();
$this->authorize($actor, $id, true);
$row = DB::table('organizations')->where('id', $id)->lockForUpdate()->firstOrFail();
$this->authorize($actor, $id, true);
$commands = app(InvestmentProfileCommands::class);
$command = ['kind' => 'organization_country', 'target_id' => $id, 'version' => (int) $data['version'], 'payload' => ['country' => $data['country'], 'source_reference' => $data['source_reference']]];
$replay = $commands->replay($actor, $data['operation_key'] ?? null, $command);
if ($replay !== null) {
return ['version' => $replay['version'], 'country' => $data['country']];
}
abort_unless((int) $row->version_no === (int) $data['version'], 409, 'Organização alterada. Recarregue.');
$provenance = ['kind' => 'representative_declaration', 'reference' => $data['source_reference'],
'actor_id' => $actor->id, 'at' => now()->toIso8601String(), 'source_system' => $row->source_system,
@@ -72,6 +80,8 @@ class OrganizationCountry
'after_data' => json_encode(['organization_id' => $id, 'version' => $row->version_no + 1,
'snapshot' => Crypt::encryptString(json_encode(['country' => $data['country'], 'provenance' => $provenance], JSON_THROW_ON_ERROR))])]);
$commands->record($actor, $data['operation_key'] ?? null, $command, ['version' => $row->version_no + 1]);
return ['version' => $row->version_no + 1, 'country' => $data['country']];
});
}
@@ -79,11 +89,23 @@ class OrganizationCountry
/** @return array<string, mixed> */
public function snapshot(User $actor, string $id): array
{
$snapshot = $this->readableSnapshot($actor, $id);
abort_unless($snapshot['country'] !== null, 422, 'País de constituição não informado no cadastro mestre.');
return $snapshot;
}
/** Caller owns a transaction; authorize again after waiting for the source lock.
* @return array<string, mixed> */
public function readableSnapshot(User $actor, string $id): array
{
$this->authorize($actor, $id);
DB::table('memberships')->where('organization_id', $id)->where('user_id', $actor->id)->lockForUpdate()->get();
$this->authorize($actor, $id);
$org = DB::table('organizations')->where('id', $id)->lockForUpdate()->firstOrFail();
abort_unless($org->incorporation_country_code !== null, 422, 'País de constituição não informado no cadastro mestre.');
$this->authorize($actor, $id);
return ['organization_id' => $id, 'organization_version' => $org->version_no, 'country' => $org->incorporation_country_code,
return ['organization_id' => $org->id, 'organization_version' => $org->version_no, 'country' => $org->incorporation_country_code,
'provenance' => json_decode($org->incorporation_country_provenance, true), 'reused_at' => now()->toIso8601String(), 'reused_by' => $actor->id];
}
}