Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions app/Actions/Project/PublishEmbargoProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Actions\Project;

use App\Exceptions\EmbargoPublicationFailed;
use App\Jobs\ProcessSubmission;
use App\Models\Project;
use App\Models\Validation;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
Expand All @@ -17,9 +19,9 @@ class PublishEmbargoProject
* so we dispatch asynchronously. When there is no draft, we dispatch synchronously so a
* "publish now" path can take effect immediately.
*
* @return array{hasDraft: bool, dispatched: 'sync'|'async'}
* @return array{hasDraft: bool, dispatched: 'sync'|'async', validation: Validation}
*/
public function publish(Project $project): array
public function publish(Project $project, bool $restoreReleaseDateOnValidationFailure = false): array
{
if ($project->is_public) {
throw ValidationException::withMessages([
Expand All @@ -45,8 +47,8 @@ public function publish(Project $project): array
]);
}

$validation = $this->validateForPublication($project, $restoreReleaseDateOnValidationFailure);
$releaseDate = now()->startOfDay()->toDateString();

$hasDraft = $project->draft_id !== null;

DB::transaction(function () use ($project, $releaseDate) {
Expand All @@ -73,6 +75,7 @@ public function publish(Project $project): array
return [
'hasDraft' => true,
'dispatched' => 'async',
'validation' => $validation,
];
}

Expand All @@ -81,6 +84,46 @@ public function publish(Project $project): array
return [
'hasDraft' => false,
'dispatched' => 'sync',
'validation' => $validation,
];
}

private function validateForPublication(Project $project, bool $restoreReleaseDateOnValidationFailure): Validation
{
$validation = $project->validation;

if (! $validation) {
throw ValidationException::withMessages([
'publish' => 'Project validation not found. Please ensure the project is properly configured.',
]);
}

$originalReleaseDate = $project->release_date;

$project->release_date = now()->startOfDay()->toDateString();
$project->save();

$validation->process();
$publishAttemptValidation = $validation->fresh();

if (! $publishAttemptValidation['report']['project']['status']) {
if ($restoreReleaseDateOnValidationFailure) {
$project->release_date = $originalReleaseDate;
$project->save();
$project->refresh();

if ($project->validation) {
$project->validation->process();
}
}

throw new EmbargoPublicationFailed(
$project,
'Validation failing. Please provide all the required data and try again. If the problem persists, please contact us at info.nmrxiv@uni-jena.de',
$publishAttemptValidation,
);
}

return $publishAttemptValidation;
}
}
35 changes: 32 additions & 3 deletions app/Console/Commands/PublishEmbargoProjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace App\Console\Commands;

use App\Actions\Project\PublishEmbargoProject;
use App\Exceptions\EmbargoPublicationFailed;
use App\Models\EmbargoReminder;
use App\Models\Project;
use App\Models\User;
use App\Models\Validation;
use App\Notifications\EmbargoPublicationFailedNotification;
use App\Notifications\EmbargoReleaseReminderNotification;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Notification;
use Throwable;

class PublishEmbargoProjects extends Command
{
Expand Down Expand Up @@ -120,21 +125,45 @@ private function publishReadyProjects(PublishEmbargoProject $embargoPublisher, C
try {
$this->info("Publishing embargo project: {$project->name} (ID: {$project->id})");

$result = $embargoPublisher->publish($project);
$embargoPublisher->publish($project, restoreReleaseDateOnValidationFailure: true);

$publishedCount++;
$this->info("Successfully queued project for publication: {$project->name}");

} catch (\InvalidArgumentException $e) {
} catch (EmbargoPublicationFailed $e) {
$this->error("Failed to publish project {$project->name}: {$e->getMessage()}");
} catch (\Exception $e) {
$this->notifyPublicationFailure($project, $e->getMessage(), $e->validation, $e);
} catch (Throwable $e) {
$this->error("Unexpected error publishing project {$project->name}: {$e->getMessage()}");
$this->notifyPublicationFailure($project, $e->getMessage(), $project->validation?->fresh(), $e);
}
}

return $publishedCount;
}

private function notifyPublicationFailure(Project $project, string $reason, ?Validation $validation = null, ?Throwable $exception = null): void
{
$project->loadMissing('owner');
$exceptionClass = $exception ? $exception::class : null;

if ($project->owner) {
Notification::send(
$project->owner,
new EmbargoPublicationFailedNotification($project, $reason, $validation, $exceptionClass)
);
}

$admins = User::role(['super-admin'])
->when($project->owner, fn ($query) => $query->whereKeyNot($project->owner->getKey()))
->get();

Notification::send(
$admins,
new EmbargoPublicationFailedNotification($project, $reason, $validation, $exceptionClass, admin: true)
);
}

/**
* Clean up old embargo reminder records for projects that are no longer embargoed
*/
Expand Down
20 changes: 20 additions & 0 deletions app/Exceptions/EmbargoPublicationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Exceptions;

use App\Models\Project;
use App\Models\Validation;
use RuntimeException;
use Throwable;

class EmbargoPublicationFailed extends RuntimeException
{
public function __construct(
public Project $project,
string $message,
public ?Validation $validation = null,
?Throwable $previous = null,
) {
parent::__construct($message, 0, $previous);
}
}
56 changes: 16 additions & 40 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Actions\Project\PublishProject;
use App\Actions\Project\RestoreProject;
use App\Actions\Project\UpdateProject;
use App\Exceptions\EmbargoPublicationFailed;
use App\Http\Resources\ProjectResource;
use App\Http\Resources\StudyResource;
use App\Jobs\ProcessSubmission;
Expand Down Expand Up @@ -650,63 +651,38 @@ public function publishEmbargoProject(Request $request, Project $project, Publis
throw new AuthorizationException;
}

$validation = $project->validation;
if (! $validation) {
try {
$result = $embargoPublisher->publish($project, restoreReleaseDateOnValidationFailure: true);
} catch (EmbargoPublicationFailed $e) {
if ($this->publishPrefersJsonResponse($request)) {
return response()->json([
'errors' => 'Project validation not found. Please ensure the project is properly configured.',
'errors' => $e->getMessage(),
'validation' => $e->validation,
], 422);
}

session()->now(
'publish_validation_hints',
$this->publishValidationHintsFromReport($e->validation?->report)
);

throw ValidationException::withMessages([
'publish' => 'Project validation not found. Please ensure the project is properly configured.',
'publish' => $e->getMessage(),
]);
}

$originalReleaseDate = $project->release_date;
$project->release_date = now()->startOfDay()->toDateString();
$project->save();

$validation->process();
$publishAttemptValidation = $validation->fresh();

if (! $publishAttemptValidation['report']['project']['status']) {
$project->release_date = $originalReleaseDate;
$project->save();
$project->refresh();

// The publish-now attempt validates against today's release date, which can
// change the persisted validation report (e.g. DOI required). If publishing
// fails, restore the validation report to match the original release date.
if ($project->validation) {
$project->validation->process();
}

$message = 'Validation failing. Please provide all the required data and try again. If the problem persists, please contact us at info.nmrxiv@uni-jena.de';

} catch (ValidationException $e) {
if ($this->publishPrefersJsonResponse($request)) {
return response()->json([
'errors' => $message,
'validation' => $publishAttemptValidation,
'errors' => $e->errors()['publish'][0] ?? $e->getMessage(),
], 422);
}

session()->now(
'publish_validation_hints',
$this->publishValidationHintsFromReport($publishAttemptValidation->report)
);

throw ValidationException::withMessages([
'publish' => $message,
]);
throw $e;
}

$result = $embargoPublisher->publish($project);

return $this->publishPrefersJsonResponse($request)
? response()->json([
'project' => $project->fresh(),
'validation' => $publishAttemptValidation,
'validation' => $result['validation'],
])
: back()->with('success', $result['dispatched'] === 'async'
? 'Your submission has been queued for processing.'
Expand Down
Loading
Loading