feat: integrate private offers with existing editorial workflow
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Jobs\NotifyPrivateOfferDecision;
|
||||
use App\Models\DevelopmentCorridor;
|
||||
use App\Models\EconomicCluster;
|
||||
use App\Models\InvestmentDossier;
|
||||
@@ -237,12 +238,50 @@ final class PortalEditorialWorkflow
|
||||
}
|
||||
$context = $record->workflow_context ?? [];
|
||||
$recipients = array_unique(array_filter([$record->owner_user_id, $context['assignment']['assignee_user_id'] ?? null, $context['publication_assignment']['assignee_user_id'] ?? null]));
|
||||
$profiles = app(PortalProfileAccess::class);
|
||||
if ($record->isPrivateOfferPreparation() && $action === 'assign_review' && isset($context['last_decision']['previous_assignee_user_id'])) {
|
||||
$recipients[] = $context['last_decision']['previous_assignee_user_id'];
|
||||
}
|
||||
if ($record->isPrivateOfferPreparation() && $action === 'submit') {
|
||||
$coordinators = DB::table('portal_role_assignments')->where('role', 'validator')->whereNull('revoked_at')
|
||||
->where('starts_at', '<=', now())->where(fn ($q) => $q->whereNull('expires_at')->orWhere('expires_at', '>', now()))
|
||||
->whereJsonContains('responsibilities', 'validation_coordinator')->pluck('user_id');
|
||||
foreach (User::whereIn('id', $coordinators)->get() as $candidate) {
|
||||
if ($profiles->scoped($candidate, 'validator', $record->organization_id, $record->opportunity_category)
|
||||
&& $profiles->operational($candidate, 'validate', $record->id) && ! $profiles->conflict($candidate, $record)) {
|
||||
$recipients[] = $candidate->id;
|
||||
}
|
||||
}
|
||||
}
|
||||
$recipients = array_unique($recipients);
|
||||
foreach ($recipients as $recipient) {
|
||||
if ((int) $recipient === $actor->id) {
|
||||
continue;
|
||||
}
|
||||
DB::table('portal_editorial_notifications')->insertOrIgnore(['user_id' => $recipient, 'edition_id' => null, 'opportunity_id' => $record->id, 'opportunity_version' => $record->management_version,
|
||||
if ($record->isPrivateOfferPreparation()) {
|
||||
$candidate = User::query()->whereKey($recipient)->first();
|
||||
if (! $candidate || ! $profiles->canView($candidate, $record) || ! $this->canEnter($candidate)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
$inserted = DB::table('portal_editorial_notifications')->insertOrIgnore(['user_id' => $recipient, 'edition_id' => null, 'opportunity_id' => $record->id, 'opportunity_version' => $record->management_version,
|
||||
'content_hash' => $context['submission']['hash'] ?? $context['approval']['hash'] ?? null, 'action' => $action, 'created_at' => now()]);
|
||||
if ($inserted && $record->isPrivateOfferPreparation()) {
|
||||
$notice = DB::table('portal_editorial_notifications')->where('user_id', $recipient)->where('opportunity_id', $record->id)
|
||||
->where('opportunity_version', $record->management_version)->where('action', $action)->first();
|
||||
if ($notice) {
|
||||
DB::afterCommit(function () use ($notice, $actor, $record): void {
|
||||
try {
|
||||
NotifyPrivateOfferDecision::dispatch($notice->id);
|
||||
} catch (\Throwable $error) {
|
||||
// A committed decision must not become an uncertain HTTP failure.
|
||||
app(PortalProfileAccess::class)->audit($actor, 'offer_notification_delivery_failed', [
|
||||
'record_id' => $record->id, 'notice_id' => $notice->id, 'error_type' => get_class($error),
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,6 +373,9 @@ final class PortalEditorialWorkflow
|
||||
}
|
||||
$edition = $notice->edition_id ? DB::table('portal_editorial_editions')->where('id', $notice->edition_id)->first() : null;
|
||||
$subjectHref = ($edition->subject_kind ?? null) ? '/management/editorial/subjects/'.$edition->subject_kind.'/'.$edition->subject_key.'?edition='.$edition->id : null;
|
||||
if ($notice->opportunity_id !== null && Opportunity::query()->whereKey($notice->opportunity_id)->first()?->isPrivateOfferPreparation()) {
|
||||
$subjectHref = '/minhas-oportunidades/ofertas/'.$notice->opportunity_id;
|
||||
}
|
||||
$result[] = ['id' => $notice->id, 'action' => $notice->action, 'read_at' => $notice->read_at,
|
||||
'href' => $subjectHref ?? ($notice->opportunity_id !== null ? '/minhas-oportunidades/'.$notice->opportunity_id : (($edition->demographic_import_id ?? null) ? '/management/editorial/demography/'.$edition->demographic_import_id : (($edition->dossier_id ?? null) ? '/management/editorial/portfolio/'.$edition->dossier_id : '/management/editorial/resources/'.$edition->resource_id)).'?edition='.$notice->edition_id), 'reference' => $notice->opportunity_id !== null ? 'Revisão '.$notice->opportunity_version : 'Edição '.$edition->revision_no];
|
||||
if (count($result) === 50) {
|
||||
|
||||
Reference in New Issue
Block a user