144 lines
7.1 KiB
PHP
144 lines
7.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Exceptions\OptimisticLockConflictException;
|
|
use App\Models\Project;
|
|
use App\Models\ProjectCostSnapshot;
|
|
use App\Models\ProjectEvent;
|
|
use App\Models\ProjectListingLink;
|
|
use App\Models\ProjectRelease;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Releases pin an exact immutable revision+cost bundle as "live" for one listing
|
|
* mapping. Uploading a new revision never moves this pointer on its own; only an
|
|
* explicit release or revert does, and both create a new append-only
|
|
* project_releases row rather than mutating history, under an optimistic
|
|
* concurrency check on the listing link's lock_version.
|
|
*/
|
|
class ProjectReleaseController extends Controller
|
|
{
|
|
public function store(Request $request, Project $project, ProjectListingLink $listingLink): RedirectResponse
|
|
{
|
|
abort_unless($listingLink->project_id === $project->id, 404);
|
|
abort_unless($request->user()->can('release', $project), 403);
|
|
|
|
$data = $request->validate([
|
|
'revision_id' => ['required', 'integer'],
|
|
'cost_snapshot_id' => ['required', 'integer'],
|
|
'lock_version' => ['required', 'integer'],
|
|
'notes' => ['nullable', 'string'],
|
|
]);
|
|
|
|
if ($listingLink->isDetached()) {
|
|
return back()->withErrors(['listing_link' => 'That listing mapping has been detached and can no longer be released to.'])->setStatusCode(422);
|
|
}
|
|
|
|
$revision = $project->revisions()->find($data['revision_id']);
|
|
if (! $revision) {
|
|
return back()->withErrors(['revision_id' => 'That revision does not belong to this project.'])->setStatusCode(422);
|
|
}
|
|
if (! $revision->isApproved()) {
|
|
return back()->withErrors(['revision_id' => 'Only an approved revision can be released.'])->setStatusCode(422);
|
|
}
|
|
|
|
$costSnapshot = $project->costSnapshots()->find($data['cost_snapshot_id']);
|
|
if (! $costSnapshot || (int) $costSnapshot->revision_id !== $revision->id) {
|
|
return back()->withErrors(['cost_snapshot_id' => 'That cost snapshot does not match this project and revision.'])->setStatusCode(422);
|
|
}
|
|
if (! $costSnapshot->isComplete()) {
|
|
return back()->withErrors(['cost_snapshot_id' => 'The cost snapshot must have filament, packaging and shipping all costed (even as an explicit zero) before it can be released.'])->setStatusCode(422);
|
|
}
|
|
|
|
try {
|
|
$release = DB::transaction(function () use ($request, $project, $listingLink, $revision, $costSnapshot, $data): ProjectRelease {
|
|
Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
|
$locked = ProjectListingLink::query()->whereKey($listingLink->id)->lockForUpdate()->firstOrFail();
|
|
if ((int) $locked->lock_version !== (int) $data['lock_version']) {
|
|
throw new OptimisticLockConflictException('The listing mapping changed since you loaded it.');
|
|
}
|
|
|
|
$release = ProjectRelease::create([
|
|
'project_id' => $project->id,
|
|
'revision_id' => $revision->id,
|
|
'cost_snapshot_id' => $costSnapshot->id,
|
|
'action' => ProjectRelease::ACTION_RELEASE,
|
|
'released_by' => $request->user()->id,
|
|
'released_at' => now(),
|
|
'notes' => $data['notes'] ?? null,
|
|
]);
|
|
$locked->update(['live_release_id' => $release->id, 'lock_version' => $locked->lock_version + 1]);
|
|
|
|
ProjectEvent::record($project->id, 'released', $request->user()->id, ProjectRelease::class, $release->id, [
|
|
'listing_link_id' => $listingLink->id, 'revision_id' => $revision->id,
|
|
]);
|
|
|
|
return $release;
|
|
});
|
|
} catch (OptimisticLockConflictException $exception) {
|
|
return back()->withErrors(['lock_version' => $exception->getMessage()])->setStatusCode(409);
|
|
}
|
|
|
|
return redirect()->route('projects.show', $project)->with('status', 'Revision released.');
|
|
}
|
|
|
|
public function revert(Request $request, Project $project, ProjectListingLink $listingLink): RedirectResponse
|
|
{
|
|
abort_unless($listingLink->project_id === $project->id, 404);
|
|
abort_unless($request->user()->can('release', $project), 403);
|
|
|
|
$data = $request->validate([
|
|
'release_id' => ['required', 'integer'],
|
|
'lock_version' => ['required', 'integer'],
|
|
'notes' => ['nullable', 'string'],
|
|
]);
|
|
|
|
if ($listingLink->isDetached()) {
|
|
return back()->withErrors(['listing_link' => 'That listing mapping has been detached and can no longer be reverted.'])->setStatusCode(422);
|
|
}
|
|
|
|
$target = $project->releases()->find($data['release_id']);
|
|
if (! $target) {
|
|
return back()->withErrors(['release_id' => 'That release does not belong to this project.'])->setStatusCode(422);
|
|
}
|
|
$targetSnapshot = $target->cost_snapshot_id ? ProjectCostSnapshot::find($target->cost_snapshot_id) : null;
|
|
if (! $targetSnapshot || ! $targetSnapshot->isComplete()) {
|
|
return back()->withErrors(['release_id' => 'That release no longer has a complete cost snapshot and cannot be restored.'])->setStatusCode(422);
|
|
}
|
|
|
|
try {
|
|
$release = DB::transaction(function () use ($request, $project, $listingLink, $target, $data): ProjectRelease {
|
|
Project::query()->whereKey($project->id)->lockForUpdate()->firstOrFail();
|
|
$locked = ProjectListingLink::query()->whereKey($listingLink->id)->lockForUpdate()->firstOrFail();
|
|
if ((int) $locked->lock_version !== (int) $data['lock_version']) {
|
|
throw new OptimisticLockConflictException('The listing mapping changed since you loaded it.');
|
|
}
|
|
|
|
$release = ProjectRelease::create([
|
|
'project_id' => $project->id,
|
|
'revision_id' => $target->revision_id,
|
|
'cost_snapshot_id' => $target->cost_snapshot_id,
|
|
'action' => ProjectRelease::ACTION_REVERT,
|
|
'released_by' => $request->user()->id,
|
|
'released_at' => now(),
|
|
'notes' => $data['notes'] ?? null,
|
|
]);
|
|
$locked->update(['live_release_id' => $release->id, 'lock_version' => $locked->lock_version + 1]);
|
|
|
|
ProjectEvent::record($project->id, 'release_reverted', $request->user()->id, ProjectRelease::class, $release->id, [
|
|
'listing_link_id' => $listingLink->id, 'reverted_to_release_id' => $target->id,
|
|
]);
|
|
|
|
return $release;
|
|
});
|
|
} catch (OptimisticLockConflictException $exception) {
|
|
return back()->withErrors(['lock_version' => $exception->getMessage()])->setStatusCode(409);
|
|
}
|
|
|
|
return redirect()->route('projects.show', $project)->with('status', 'Reverted to a previous release.');
|
|
}
|
|
}
|