Files
Investidor-V2/app/Services/InvestmentWorkspace.php
nala 250ac35fd1
tests / ci (push) Canceled after 0s
first commit
2026-09-28 10:01:09 +02:00

39 lines
1.9 KiB
PHP

<?php
namespace App\Services;
use App\Models\InvestmentSpecificNeed;
use App\Models\InvestmentSubmission;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class InvestmentWorkspace
{
/** @param array<string,mixed> $payload */
public function notify(int $userId, InvestmentSubmission $submission, string $kind, string $eventKey, array $payload = []): void
{
DB::table('investment_notifications')->insertOrIgnore(['id' => (string) Str::uuid(), 'user_id' => $userId,
'submission_id' => $submission->id, 'kind' => $kind, 'event_key' => $eventKey,
'payload' => Crypt::encryptString(json_encode($payload, JSON_THROW_ON_ERROR)), 'created_at' => now()]);
}
public function snapshot(InvestmentSubmission $submission): void
{
$snapshot = ['payload' => $submission->payload, 'status' => $submission->status,
'zone_origin' => DB::table('investment_submission_zones')->where('submission_id', $submission->id)->first(['zone_id', 'zone_version', 'intent']),
'needs' => InvestmentSpecificNeed::where('submission_id', $submission->id)->get()->toArray(),
'attachment_ids' => DB::table('investment_submission_attachments')->where('submission_id', $submission->id)->pluck('id')->all()];
DB::table('investment_submission_revisions')->insert(['submission_id' => $submission->id, 'version' => $submission->version,
'snapshot' => Crypt::encryptString(json_encode($snapshot, JSON_THROW_ON_ERROR)), 'created_at' => now()]);
}
/** @return array<string,mixed> */
public function account(int $userId): array
{
$row = DB::table('investment_accounts')->where('user_id', $userId)->first();
return ['version' => $row->version ?? 0, 'payload' => $row ? json_decode(Crypt::decryptString($row->payload), true, 512, JSON_THROW_ON_ERROR) : []];
}
}