@@ -6,6 +6,7 @@ use App\Models\InvestmentSubmission;
|
||||
use App\Models\User;
|
||||
use App\Services\CountryCatalogue;
|
||||
use App\Services\InvestmentWorkspace;
|
||||
use App\Services\OrganizationCountry;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -19,9 +20,10 @@ class InvestmentWorkspaceController extends Controller
|
||||
{
|
||||
public function profile(Request $request, InvestmentWorkspace $workspace): Response|JsonResponse
|
||||
{
|
||||
$data = ['countries' => app(CountryCatalogue::class)->all(), 'account' => $workspace->account($request->user()->id), 'companies' => DB::table('investment_company_declarations')
|
||||
$data = ['organizationCountryAvailable' => app(OrganizationCountry::class)->available(), 'organizations' => app(OrganizationCountry::class)->represented($request->user()), 'countries' => app(CountryCatalogue::class)->all(), 'account' => $workspace->account($request->user()->id), 'companies' => DB::table('investment_company_declarations')
|
||||
->where('user_id', $request->user()->id)->orderByDesc('created_at')->get(['id', 'version', 'payload'])->map(function ($company) {
|
||||
$company->payload = json_decode(Crypt::decryptString($company->payload), true, 512, JSON_THROW_ON_ERROR);
|
||||
$company->countrySource = $company->payload['organization_country_snapshot'] ?? null;
|
||||
|
||||
return $company;
|
||||
})];
|
||||
@@ -68,9 +70,15 @@ class InvestmentWorkspaceController extends Controller
|
||||
abort_if(! $id && DB::table('investment_company_declarations')->where('user_id', $request->user()->id)->count() >= 20, 422, 'Máximo de vinte empresas declaradas.');
|
||||
$id ??= (string) Str::uuid();
|
||||
$version = (int) $data['version'] + 1;
|
||||
$previous = $row ? json_decode(Crypt::decryptString($row->payload), true, 512, JSON_THROW_ON_ERROR) : [];
|
||||
$payload = [...$previous, ...$data['payload']];
|
||||
if (($previous['incorporation_country_code'] ?? null) !== ($payload['incorporation_country_code'] ?? null)) {
|
||||
unset($payload['organization_country_snapshot']);
|
||||
}
|
||||
$encrypted = Crypt::encryptString(json_encode($payload, JSON_THROW_ON_ERROR));
|
||||
DB::table('investment_company_declarations')->updateOrInsert(['id' => $id], ['user_id' => $request->user()->id, 'version' => $version,
|
||||
'payload' => Crypt::encryptString(json_encode($data['payload'], JSON_THROW_ON_ERROR)), 'created_at' => $row->created_at ?? now(), 'updated_at' => now()]);
|
||||
$this->audit($request, 'company_declared', 'investment_company_declarations', $id, ['version' => $version]);
|
||||
'payload' => $encrypted, 'created_at' => $row->created_at ?? now(), 'updated_at' => now()]);
|
||||
$this->audit($request, 'company_declared', 'investment_company_declarations', $id, ['version' => $version, 'before_payload' => $row?->payload, 'after_payload' => $encrypted]);
|
||||
|
||||
return ['id' => $id, 'version' => $version];
|
||||
});
|
||||
@@ -78,6 +86,36 @@ class InvestmentWorkspaceController extends Controller
|
||||
return $request->expectsJson() ? response()->json($result) : back()->with('success', 'Declaração de empresa guardada.');
|
||||
}
|
||||
|
||||
public function organizationCountry(Request $request, string $id, OrganizationCountry $service): RedirectResponse|JsonResponse
|
||||
{
|
||||
$result = $service->save($request->user(), $id, $request->all());
|
||||
|
||||
return $request->expectsJson() ? response()->json($result) : back()->with('success', 'País de constituição guardado no cadastro mestre; submissões anteriores preservadas.');
|
||||
}
|
||||
|
||||
public function reuseOrganizationCountry(Request $request, string $id, OrganizationCountry $service): RedirectResponse|JsonResponse
|
||||
{
|
||||
$data = $request->validate(['version' => 'required|integer|min:1', 'organization_id' => 'required|uuid', 'organization_version' => 'required|integer|min:1']);
|
||||
$result = DB::transaction(function () use ($request, $id, $data, $service) {
|
||||
$row = DB::table('investment_company_declarations')->where('id', $id)->where('user_id', $request->user()->id)->lockForUpdate()->first();
|
||||
abort_unless($row !== null, 404);
|
||||
abort_unless((int) $row->version === (int) $data['version'], 409, 'Declaração alterada. Recarregue.');
|
||||
$snapshot = $service->snapshot($request->user(), $data['organization_id']);
|
||||
abort_unless((int) $snapshot['organization_version'] === (int) $data['organization_version'], 409, 'País mestre alterado. Recarregue.');
|
||||
$payload = json_decode(Crypt::decryptString($row->payload), true, 512, JSON_THROW_ON_ERROR);
|
||||
$payload['incorporation_country_code'] = $snapshot['country'];
|
||||
$payload['organization_country_snapshot'] = $snapshot;
|
||||
$encrypted = Crypt::encryptString(json_encode($payload, JSON_THROW_ON_ERROR));
|
||||
DB::table('investment_company_declarations')->where('id', $id)->update(['payload' => $encrypted, 'version' => $row->version + 1, 'updated_at' => now()]);
|
||||
$this->audit($request, 'organization_country_reused', 'investment_company_declarations', $id, ['version' => $row->version + 1,
|
||||
'before_payload' => $row->payload, 'after_payload' => $encrypted, 'organization_id' => $data['organization_id'], 'organization_version' => $snapshot['organization_version']]);
|
||||
|
||||
return ['version' => $row->version + 1, 'country' => $snapshot['country']];
|
||||
});
|
||||
|
||||
return $request->expectsJson() ? response()->json($result) : back()->with('success', 'País mestre reutilizado nesta declaração; manifestações anteriores não alteradas.');
|
||||
}
|
||||
|
||||
public function notifications(Request $request): Response|JsonResponse
|
||||
{
|
||||
$query = DB::table('investment_notifications')->where('user_id', $request->user()->id);
|
||||
|
||||
Reference in New Issue
Block a user