feat: integrate private offers with existing editorial workflow
This commit is contained in:
@@ -9,7 +9,7 @@ use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
/** Phase 1 adapter: the existing opportunity owns identity, versions and audit. */
|
||||
/** Private commercial adapter: the existing opportunity owns identity, workflow and audit. */
|
||||
final class PrivateOfferPreparation
|
||||
{
|
||||
/** @return array<string,mixed> */
|
||||
@@ -58,17 +58,31 @@ final class PrivateOfferPreparation
|
||||
return $record;
|
||||
}
|
||||
|
||||
/** Register private submission only: no editorial task, approval or publication.
|
||||
/** Register submission through the existing editorial engine when available.
|
||||
* @param array<string,mixed> $input */
|
||||
public function submit(User $actor, int $id, array $input): Opportunity
|
||||
{
|
||||
$data = Validator::make($input, ['etag' => 'required|string|size:64', 'confirm' => 'required|accepted'])->validate();
|
||||
if (app(PortalEditorialWorkflow::class)->available()) {
|
||||
$record = $this->record($actor, $id);
|
||||
$access = app(PortalProfileAccess::class);
|
||||
if ($access->state($record) === 'submitted') {
|
||||
abort_unless($access->promoterRecordScope($actor, $record) && $access->canCreate($actor, $record->organization_id, $record->opportunity_category)
|
||||
&& $access->operational($actor, 'edit', $id), 403);
|
||||
abort_unless(hash_equals(app(OpportunityManagement::class)->etag($record), $data['etag']), 409);
|
||||
|
||||
return $record;
|
||||
}
|
||||
|
||||
return app(OpportunityProfileWorkflow::class)->transition($actor, $id, [...$data, 'action' => 'submit', 'reason' => 'Submissão privada da oferta conferida pelo promotor.']);
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($actor, $id, $data) {
|
||||
$record = Opportunity::whereKey($id)->lockForUpdate()->firstOrFail();
|
||||
abort_unless($record->isPrivateOfferPreparation(), 404);
|
||||
$access = app(PortalProfileAccess::class);
|
||||
$access->authorize($actor, 'view', $record);
|
||||
abort_if(($record->workflow_context['private_offer_preparation']['version'] ?? 1) >= 2, 503, 'O circuito editorial está indisponível. A versão e o histórico foram preservados; não é permitida a retoma pelo percurso anterior.');
|
||||
abort_unless($access->promoterRecordScope($actor, $record)
|
||||
&& $access->canCreate($actor, $record->organization_id, $record->opportunity_category)
|
||||
&& $access->operational($actor, 'edit', $record->id), 403);
|
||||
@@ -106,14 +120,93 @@ final class PrivateOfferPreparation
|
||||
});
|
||||
}
|
||||
|
||||
/** Adapter of the five common fields; never relaxes the full canonical contract.
|
||||
* @return array<string,mixed> */
|
||||
public function snapshot(Opportunity $record): array
|
||||
{
|
||||
$values = app(OpportunityManagement::class)->values($record);
|
||||
Validator::make($values, ['title' => 'required|string|max:150', 'description' => 'required|string|max:50000',
|
||||
'opportunity_category' => 'required', 'private_offer.price_mode' => 'required|in:amount,negotiable', 'private_offer.contact_id' => 'required|uuid'])->validate();
|
||||
$offer = $this->validate($values['private_offer'], true);
|
||||
if (! app(OpportunityContacts::class)->related($record, $offer['contact_id'])) {
|
||||
throw ValidationException::withMessages(['private_offer.contact_id' => 'O contacto já não está disponível neste âmbito.']);
|
||||
}
|
||||
$contact = DB::table('contacts')->where('id', $offer['contact_id'])->first();
|
||||
$path = app(InvestmentGeography::class)->path($values['primary_territory_id'] ?? null, true);
|
||||
if (! empty($values['primary_territory_id']) && $path === []) {
|
||||
throw ValidationException::withMessages(['province_id' => 'Reconcilie a localização antes da decisão.']);
|
||||
}
|
||||
if ($path !== []) {
|
||||
app(InvestmentGeography::class)->selection(array_combine(array_slice(array_keys(InvestmentGeography::FIELDS), 0, count($path)), array_column($path, 'id')), true);
|
||||
}
|
||||
$payload = ['title' => $values['title'], 'description' => $values['description'], 'opportunity_category' => $values['opportunity_category'],
|
||||
'owner_user_id' => $record->owner_user_id, 'organization_id' => $record->organization_id, 'private_offer' => $offer,
|
||||
'contact_fingerprint' => hash('sha256', json_encode($contact, JSON_THROW_ON_ERROR)), 'territory' => $path];
|
||||
$normalize = function (mixed $value) use (&$normalize): mixed {
|
||||
if (is_object($value)) {
|
||||
$value = get_object_vars($value);
|
||||
}
|
||||
if (! is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
if (! array_is_list($value)) {
|
||||
ksort($value);
|
||||
}
|
||||
|
||||
return array_map($normalize, $value);
|
||||
};
|
||||
$payload = $normalize($payload);
|
||||
|
||||
return ['payload' => $payload, 'hash' => hash('sha256', json_encode(['contract' => 'private-offer-editorial-v2', 'payload' => $payload], JSON_THROW_ON_ERROR)),
|
||||
'record_version' => $record->management_version, 'contract_version' => 'private-offer-editorial-v2', 'resources' => ['documents' => [], 'images' => [], 'locations' => []]];
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
public function presentation(User $actor, Opportunity $record): array
|
||||
{
|
||||
app(PortalProfileAccess::class)->authorize($actor, 'view', $record);
|
||||
$values = app(OpportunityManagement::class)->values($record);
|
||||
$fresh = null;
|
||||
$contactId = $values['private_offer']['contact_id'] ?? null;
|
||||
$contactLabel = $contactId && app(OpportunityContacts::class)->related($record, $contactId)
|
||||
? DB::table('contacts')->where('id', $contactId)->value('full_name') : null;
|
||||
if (isset($record->workflow_context['approval'])) {
|
||||
try {
|
||||
$fresh = hash_equals($record->workflow_context['approval']['hash'], $this->snapshot($record)['hash']);
|
||||
} catch (ValidationException $error) {
|
||||
$fresh = false;
|
||||
}
|
||||
}
|
||||
$history = $record->workflow_context['events'] ?? [];
|
||||
$names = User::whereIn('id', array_filter([...array_column($history, 'actor_id'), ...array_column($history, 'assignee_user_id')]))->pluck('name', 'id');
|
||||
$history = array_map(fn ($event) => [...$event, 'actor_name' => $names[$event['actor_id']] ?? 'Interveniente indisponível',
|
||||
'assignee_name' => isset($event['assignee_user_id']) ? ($names[$event['assignee_user_id']] ?? 'Interveniente indisponível') : null], $history);
|
||||
$versions = [...($record->workflow_context['decision_history'] ?? []), ...($record->workflow_context['submission_history'] ?? [])];
|
||||
if (isset($record->workflow_context['submission'])) {
|
||||
$versions[] = ['submission' => $record->workflow_context['submission'], 'approval' => $record->workflow_context['approval'] ?? null,
|
||||
'rejection' => $record->workflow_context['rejection'] ?? null];
|
||||
}
|
||||
$uniqueVersions = [];
|
||||
foreach ($versions as $version) {
|
||||
if (! isset($version['submission']['payload']['private_offer'])) {
|
||||
continue;
|
||||
}
|
||||
$key = $version['submission']['record_version'].':'.$version['submission']['hash'].':'.($version['submission']['review_cycle'] ?? 0);
|
||||
$previous = $uniqueVersions[$key] ?? [];
|
||||
$uniqueVersions[$key] = ['submission' => $version['submission'], 'approval' => $version['approval'] ?? $previous['approval'] ?? null,
|
||||
'rejection' => $version['rejection'] ?? $previous['rejection'] ?? null];
|
||||
}
|
||||
$workflow = app(OpportunityProfileWorkflow::class);
|
||||
$actions = $workflow->actions($actor, $record);
|
||||
|
||||
return ['id' => $record->id, 'reference' => $record->reference, 'values' => app(OpportunityManagement::class)->values($record),
|
||||
'etag' => app(OpportunityManagement::class)->etag($record), 'state' => data_get($record->workflow_context, 'private_offer_preparation.state'),
|
||||
return ['id' => $record->id, 'reference' => $record->reference, 'values' => $values,
|
||||
'etag' => app(OpportunityManagement::class)->etag($record), 'state' => app(PortalProfileAccess::class)->state($record),
|
||||
'canEdit' => app(PortalProfileAccess::class)->canEdit($actor, $record), 'organization_id' => $record->organization_id,
|
||||
'territory' => app(InvestmentGeography::class)->path($record->primary_territory_id, true)];
|
||||
'actions' => $actions,
|
||||
'reviewers' => in_array('assign_review', $actions, true) ? $workflow->participants($actor, $record)['validators'] : [],
|
||||
'reauthenticationRequired' => app(OpportunityProfileWorkflow::class)->reauthenticationRequired($actor, $record),
|
||||
'history' => $history, 'submittedVersions' => array_values($uniqueVersions), 'correctionItems' => $record->workflow_context['correction_items'] ?? [],
|
||||
'approval' => $record->workflow_context['approval'] ?? null, 'approvalFresh' => $fresh, 'contactLabel' => $contactLabel, 'tasks' => app(OpportunityWorkflowTasks::class)->listing($record),
|
||||
'territory' => app(InvestmentGeography::class)->path($values['primary_territory_id'] ?? null, true)];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user