`taskstream: true` on POST /v1/projects also creates the project in TaskStream, public, as the caller (their Pireon token is passed on), and the answer carries `taskstream_key` and `taskstream_url`. The key is `taskstream_key` or one made from the default-language slug. The board is opened before the project is saved: if TaskStream refuses it, nothing is created here and the reason comes back as a validation error (502 when TaskStream does not answer). Machine tokens need the server's own TASKSTREAM_TOKEN. Updates ignore the flag, and duplicates do not inherit the link. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
138 lines
4.5 KiB
PHP
138 lines
4.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Actions\Projects\OpenInTaskStream;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Api\V1\ProjectRequest;
|
|
use App\Http\Resources\ProjectResource;
|
|
use App\Models\Project;
|
|
use App\Support\TranslatablePayload;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Projects, as the panel edits them — so the MCP server can load the
|
|
* collective's work without anyone copying it into a form by hand.
|
|
*/
|
|
class ProjectController extends Controller
|
|
{
|
|
/** Relations every answer carries, so a client can PATCH back what it read. */
|
|
protected const WITH = ['coverMedia', 'technologies', 'services', 'members'];
|
|
|
|
/**
|
|
* Filters: status, search (title in the active language), trashed.
|
|
*/
|
|
public function index(Request $request): AnonymousResourceCollection
|
|
{
|
|
$this->authorize('viewAny', Project::class);
|
|
|
|
$locale = app()->getLocale();
|
|
|
|
$projects = Project::query()
|
|
->with(self::WITH)
|
|
->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')))
|
|
->when($request->filled('search'), fn ($q) => $q->where("title->{$locale}", 'like', '%'.$request->string('search').'%'))
|
|
->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed())
|
|
->orderBy('position')
|
|
->orderByDesc('created_at')
|
|
->paginate(perPage: min($request->integer('per_page', 25), 100))
|
|
->withQueryString();
|
|
|
|
return ProjectResource::collection($projects);
|
|
}
|
|
|
|
/**
|
|
* With `taskstream: true`, the project's board is opened in TaskStream
|
|
* first; if that fails, nothing is created here either.
|
|
*/
|
|
public function store(ProjectRequest $request, OpenInTaskStream $openInTaskStream): JsonResponse
|
|
{
|
|
$this->authorize('create', Project::class);
|
|
|
|
$project = $this->fill(new Project, $request);
|
|
|
|
if ($request->boolean('taskstream')) {
|
|
$openInTaskStream->handle($project, $request->user(), $request->bearerToken(), $request->validated('taskstream_key'));
|
|
}
|
|
|
|
$project = $this->persist($project, $request);
|
|
|
|
return ProjectResource::make($project)->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function show(Project $project): ProjectResource
|
|
{
|
|
$this->authorize('view', $project);
|
|
|
|
return ProjectResource::make($project->load(self::WITH));
|
|
}
|
|
|
|
public function update(ProjectRequest $request, Project $project): ProjectResource
|
|
{
|
|
$this->authorize('update', $project);
|
|
|
|
return ProjectResource::make($this->persist($this->fill($project, $request), $request));
|
|
}
|
|
|
|
public function destroy(Project $project): JsonResponse
|
|
{
|
|
$this->authorize('delete', $project);
|
|
|
|
$project->delete();
|
|
|
|
return response()->json(status: 204);
|
|
}
|
|
|
|
public function restore(Project $project): ProjectResource
|
|
{
|
|
$this->authorize('restore', $project);
|
|
|
|
$project->restore();
|
|
|
|
return ProjectResource::make($project->load(self::WITH));
|
|
}
|
|
|
|
protected function fill(Project $project, ProjectRequest $request): Project
|
|
{
|
|
return $project->fill(TranslatablePayload::merge(
|
|
$project,
|
|
collect($request->validated())->except(['technology_ids', 'service_ids', 'members', 'taskstream', 'taskstream_key'])->all(),
|
|
$project->translatable,
|
|
));
|
|
}
|
|
|
|
/**
|
|
* The record and its relations in one transaction: a project that saved
|
|
* but lost its team halfway would be worse than one that did not save.
|
|
*/
|
|
protected function persist(Project $project, ProjectRequest $request): Project
|
|
{
|
|
$data = $request->validated();
|
|
|
|
DB::transaction(function () use ($project, $data): void {
|
|
$project->save();
|
|
|
|
if (array_key_exists('technology_ids', $data)) {
|
|
$project->technologies()->sync($data['technology_ids']);
|
|
}
|
|
|
|
if (array_key_exists('service_ids', $data)) {
|
|
$project->services()->sync($data['service_ids']);
|
|
}
|
|
|
|
if (array_key_exists('members', $data)) {
|
|
$project->members()->sync(collect($data['members'])->mapWithKeys(
|
|
fn (array $member) => [$member['id'] => ['role' => $member['role'] ?? null]],
|
|
)->all());
|
|
}
|
|
});
|
|
|
|
return $project->load(self::WITH);
|
|
}
|
|
}
|