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

28 lines
1.0 KiB
PHP

<?php
namespace App\Services;
use App\Models\InvestmentItem;
use App\Models\Opportunity;
use Illuminate\Support\Facades\DB;
final class InvestmentIdentity
{
public function forOpportunity(int $id): string
{
return DB::transaction(function () use ($id) {
$opportunity = Opportunity::whereKey($id)->lockForUpdate()->firstOrFail();
$mapping = DB::table('investment_item_opportunities')->where('opportunity_id', $id)->first();
if ($mapping) {
return $mapping->item_id;
}
$item = InvestmentItem::create(['code' => 'opportunity:'.$id, 'title' => $opportunity->title, 'kind' => 'opportunity']);
DB::table('investment_item_opportunities')->insert(['item_id' => $item->id, 'opportunity_id' => $id,
'mapping_evidence' => 'Direct correspondence to existing opportunity primary key; no inferred merge or change of validation status.',
'created_at' => now(), 'updated_at' => now()]);
return $item->id;
});
}
}