@@ -0,0 +1,27 @@
|
||||
<?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;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user