Make the inbox global instead of per-project
A card either sits in its owner's inbox (project_id AND status_id both NULL)
or belongs to exactly one project with a status in it (both set) -- enforced
by a CHECK constraint, never one without the other. The inbox is global to a
user now, not per-project: cards can move from a project into the inbox and
back into any status column of any project.
Backend
- migrations/009: rebuilds `cards` (SQLite can't relax NOT NULL / add a CHECK
in place) with a nullable project_id, a new owner_id (cards need direct
ownership once they can have no project), and the CHECK constraint. Cards
that had no status (the old per-project inbox) move to the new global inbox.
status_id's FK is now ON DELETE RESTRICT, not SET NULL -- nulling it alone
would violate the invariant, and there's no status-delete endpoint anyway.
- CardRepository: "column" is now (owner_id, project_id, status_id); every
method that dealt with a project's columns is generalised to also cover the
inbox and cross-project moves (orderColumn, idsInColumn, repack, ...).
- CardController/routes: single-card and ordering routes move to global,
since a card may have no project to nest them under --
GET/PATCH/DELETE /api/cards/{id}, PUT /api/cards/order (body now takes
project_id + status_id, both null for the inbox). New GET/POST
/api/inbox/cards. PATCH no longer accepts status_id -- moving a card, in or
out of a project, is exclusively PUT /api/cards/order now. A card created
directly in a project (POST /api/projects/{id}/cards) lands in its first
status, since a project card can't have no status.
- Tests: ProjectTest/CardStatusTest updated for the new routes; CardOrderTest
rewritten with full inbox/cross-project coverage. 57 tests pass.
Frontend
- New stores/inbox.ts (the global inbox) and lib/cardOrder.ts (the shared
PUT /api/cards/order call, used by both the sidebar and a project's board).
- AppSidebar: an Inbox section under the project list -- a vuedraggable list
in the same "kanban" drag group as every project's kanban columns, so a
card drags straight from the sidebar into whichever project is open, or
back out. (The empty-inbox state needed a real bugfix: it wasn't rendering
a <draggable> at all, so there was nowhere to drop a card back into an
empty inbox.) A drop reloads the inbox and, if a project is open, its cards.
- ProjectView's kanban board drops its synthetic Inbox column -- just the
real statuses now.
- DashboardView simplified to a plain grid of project tiles (name + card
count); its per-project "New" section is gone, since a project card can no
longer have no status.
- stores/cards.ts: patch/remove move to the global /api/cards/{id} routes.
Verified end-to-end against the rebuilt container (existing per-project-inbox
cards correctly migrated to the global inbox, 0 invariant violations) and the
dev server via headless Chrome: sidebar inbox -> project A "To do" -> back to
inbox -> project B "Done", full journey confirmed via the API at each step.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -13,8 +13,10 @@ use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
/**
|
||||
* CRUD for the cards within one project. Every route first checks that the
|
||||
* parent project is owned by the authenticated user; otherwise it responds 404.
|
||||
* CRUD for cards. A card either sits in the caller's inbox (no project) or
|
||||
* belongs to one of their projects with a status in it; single-card and
|
||||
* ordering routes are addressed globally (by card id, or by an explicit
|
||||
* project_id/status_id column) since a card need not have a project.
|
||||
*/
|
||||
final class CardController extends Controller
|
||||
{
|
||||
@@ -41,6 +43,9 @@ final class CardController extends Controller
|
||||
|
||||
/**
|
||||
* POST /api/projects/{projectId}/cards
|
||||
*
|
||||
* Creates the card directly in the project, in its first status (a
|
||||
* project card always has one -- see the invariant on the `cards` table).
|
||||
*/
|
||||
public function store(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
@@ -52,29 +57,62 @@ final class CardController extends Controller
|
||||
$position = $validator->optionalInt('position', 0);
|
||||
$validator->assert();
|
||||
|
||||
$card = $this->cards->create($projectId, $text, $complete, $position);
|
||||
$card = $this->cards->createInProject(
|
||||
$this->user($request)['id'],
|
||||
$projectId,
|
||||
$this->firstStatusId($projectId),
|
||||
$text,
|
||||
$complete,
|
||||
$position,
|
||||
);
|
||||
$this->projects->touch($projectId);
|
||||
|
||||
return $this->json($response, ['card' => $this->present($card)], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/projects/{projectId}/cards/{cardId}
|
||||
* GET /api/inbox/cards
|
||||
*/
|
||||
public function show(Request $request, Response $response, array $args): Response
|
||||
public function inboxIndex(Request $request, Response $response): Response
|
||||
{
|
||||
$projectId = $this->requireOwnedProjectId($request, $args);
|
||||
$cards = $this->cards->allInInbox($this->user($request)['id']);
|
||||
|
||||
return $this->json($response, ['card' => $this->present($this->requireCard($projectId, $args))]);
|
||||
return $this->json($response, ['cards' => array_map($this->present(...), $cards)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/projects/{projectId}/cards/{cardId}
|
||||
* POST /api/inbox/cards
|
||||
*/
|
||||
public function inboxStore(Request $request, Response $response): Response
|
||||
{
|
||||
$validator = new Validator($this->body($request));
|
||||
$text = $validator->requiredString('text', self::TEXT_MAX);
|
||||
$complete = $validator->optionalBool('complete') ?? false;
|
||||
$position = $validator->optionalInt('position', 0);
|
||||
$validator->assert();
|
||||
|
||||
$card = $this->cards->createInInbox($this->user($request)['id'], $text, $complete, $position);
|
||||
|
||||
return $this->json($response, ['card' => $this->present($card)], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/cards/{cardId}
|
||||
*/
|
||||
public function show(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
return $this->json($response, ['card' => $this->present($this->requireCard($request, $args))]);
|
||||
}
|
||||
|
||||
/**
|
||||
* PATCH /api/cards/{cardId}
|
||||
*
|
||||
* Text and completion only. Moving a card -- into/out of the inbox, or
|
||||
* between projects -- goes through PUT /api/cards/order.
|
||||
*/
|
||||
public function update(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$projectId = $this->requireOwnedProjectId($request, $args);
|
||||
$card = $this->requireCard($projectId, $args);
|
||||
$card = $this->requireCard($request, $args);
|
||||
|
||||
$validator = new Validator($this->body($request));
|
||||
$fields = [];
|
||||
@@ -84,63 +122,51 @@ final class CardController extends Controller
|
||||
if ($validator->has('complete')) {
|
||||
$fields['complete'] = $validator->optionalBool('complete');
|
||||
}
|
||||
if ($validator->has('status_id')) {
|
||||
$statusId = $validator->nullableInt('status_id', 1);
|
||||
if ($statusId !== null && !$validator->failed()
|
||||
&& $this->statuses->findInProject($statusId, $projectId) === null
|
||||
) {
|
||||
$validator->add('status_id', 'That status does not belong to this project.');
|
||||
}
|
||||
$fields['status_id'] = $statusId;
|
||||
}
|
||||
if ($fields === [] && !$validator->failed()) {
|
||||
$validator->add('text', 'Provide at least one of: text, complete, status_id.');
|
||||
$validator->add('text', 'Provide at least one of: text, complete.');
|
||||
}
|
||||
$validator->assert();
|
||||
|
||||
$updated = $this->cards->update($card['id'], $projectId, $fields);
|
||||
$this->projects->touch($projectId);
|
||||
$updated = $this->cards->update($card['id'], $fields);
|
||||
if ($card['project_id'] !== null) {
|
||||
$this->projects->touch($card['project_id']);
|
||||
}
|
||||
|
||||
return $this->json($response, ['card' => $this->present($updated)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE /api/projects/{projectId}/cards/{cardId}
|
||||
* DELETE /api/cards/{cardId}
|
||||
*/
|
||||
public function destroy(Request $request, Response $response, array $args): Response
|
||||
{
|
||||
$projectId = $this->requireOwnedProjectId($request, $args);
|
||||
$this->cards->delete($this->requireCard($projectId, $args)['id']);
|
||||
$this->projects->touch($projectId);
|
||||
$card = $this->requireCard($request, $args);
|
||||
$this->cards->delete($card['id']);
|
||||
if ($card['project_id'] !== null) {
|
||||
$this->projects->touch($card['project_id']);
|
||||
}
|
||||
|
||||
return $response->withStatus(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT /api/projects/{projectId}/cards/order
|
||||
* PUT /api/cards/order
|
||||
*
|
||||
* Sets the contents and order of one status column.
|
||||
* Sets the contents and order of one column.
|
||||
*
|
||||
* Body: { "status_id": 2 | null, "card_ids": [3, 1, 2] } — the cards that
|
||||
* should make up that column, in order. Positions are rewritten to 0..n-1;
|
||||
* any card moved in from another column is re-parented and its old column
|
||||
* re-packed. `status_id` is omitted or null for the inbox.
|
||||
* Body: { "project_id": 5 | null, "status_id": 2 | null, "card_ids": [3, 1, 2] }
|
||||
* -- both null for the inbox, or both set to a project owned by the
|
||||
* caller and one of its statuses. `card_ids` are the cards that should
|
||||
* make up that column, in order; any card moved in from elsewhere
|
||||
* (another project, the inbox) is re-parented and its old column
|
||||
* re-packed.
|
||||
*/
|
||||
public function reorder(Request $request, Response $response, array $args): Response
|
||||
public function reorder(Request $request, Response $response): Response
|
||||
{
|
||||
$projectId = $this->requireOwnedProjectId($request, $args);
|
||||
$ownerId = $this->user($request)['id'];
|
||||
$body = $this->body($request);
|
||||
|
||||
$statusId = null;
|
||||
if (array_key_exists('status_id', $body) && $body['status_id'] !== null) {
|
||||
if (!is_int($body['status_id'])) {
|
||||
throw new ApiException('status_id must be a status ID or null.', 422);
|
||||
}
|
||||
$statusId = $body['status_id'];
|
||||
if ($this->statuses->findInProject($statusId, $projectId) === null) {
|
||||
throw new ApiException('That status does not belong to this project.', 422);
|
||||
}
|
||||
}
|
||||
[$projectId, $statusId] = $this->targetColumn($ownerId, $body);
|
||||
|
||||
$order = $body['card_ids'] ?? null;
|
||||
if (!is_array($order) || array_filter($order, static fn ($id) => !is_int($id)) !== []) {
|
||||
@@ -150,19 +176,64 @@ final class CardController extends Controller
|
||||
if (count($order) !== count(array_unique($order))) {
|
||||
throw new ApiException('card_ids must not contain duplicates.', 422);
|
||||
}
|
||||
if (array_diff($order, $this->cards->idsForProject($projectId)) !== []) {
|
||||
throw new ApiException('Every card_id must be a card in this project.', 422);
|
||||
if (array_diff($order, $this->cards->idsOwnedBy($ownerId)) !== []) {
|
||||
throw new ApiException('Every card_id must be a card you own.', 422);
|
||||
}
|
||||
if (array_diff($this->cards->idsInColumn($projectId, $statusId), $order) !== []) {
|
||||
if (array_diff($this->cards->idsInColumn($ownerId, $projectId, $statusId), $order) !== []) {
|
||||
throw new ApiException('card_ids must include every card already in this column.', 422);
|
||||
}
|
||||
|
||||
$cards = $this->cards->orderColumn($projectId, $statusId, $order);
|
||||
$this->projects->touch($projectId);
|
||||
$cards = $this->cards->orderColumn($ownerId, $projectId, $statusId, $order);
|
||||
|
||||
if ($projectId !== null) {
|
||||
$this->projects->touch($projectId);
|
||||
}
|
||||
|
||||
return $this->json($response, ['cards' => array_map($this->present(...), $cards)]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate and resolve the { project_id, status_id } target column from
|
||||
* the request body.
|
||||
*
|
||||
* @param array<string, mixed> $body
|
||||
* @return array{0: int|null, 1: int|null}
|
||||
*/
|
||||
private function targetColumn(int $ownerId, array $body): array
|
||||
{
|
||||
$projectId = $body['project_id'] ?? null;
|
||||
$statusId = $body['status_id'] ?? null;
|
||||
|
||||
if ($projectId === null) {
|
||||
if ($statusId !== null) {
|
||||
throw new ApiException('status_id must be null when project_id is null.', 422);
|
||||
}
|
||||
|
||||
return [null, null];
|
||||
}
|
||||
|
||||
if (!is_int($projectId)) {
|
||||
throw new ApiException('project_id must be an integer or null.', 422);
|
||||
}
|
||||
if ($this->projects->findOwnedBy($projectId, $ownerId) === null) {
|
||||
throw new ApiException('Project not found.', 404);
|
||||
}
|
||||
if (!is_int($statusId)) {
|
||||
throw new ApiException('status_id is required when project_id is set.', 422);
|
||||
}
|
||||
if ($this->statuses->findInProject($statusId, $projectId) === null) {
|
||||
throw new ApiException('That status does not belong to this project.', 422);
|
||||
}
|
||||
|
||||
return [$projectId, $statusId];
|
||||
}
|
||||
|
||||
/** Every project is seeded with statuses on creation; this is never empty. */
|
||||
private function firstStatusId(int $projectId): int
|
||||
{
|
||||
return $this->statuses->allForProject($projectId)[0]['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, string> $args
|
||||
*/
|
||||
@@ -179,11 +250,11 @@ final class CardController extends Controller
|
||||
|
||||
/**
|
||||
* @param array<string, string> $args
|
||||
* @return array{id: int, project_id: int, text: string, complete: bool, position: int, status_id: int|null, status: array{id: int, name: string}|null, created_at: string, updated_at: string}
|
||||
* @return array{id: int, owner_id: int, project_id: int|null, text: string, complete: bool, position: int, status_id: int|null, status: array{id: int, name: string}|null, created_at: string, updated_at: string}
|
||||
*/
|
||||
private function requireCard(int $projectId, array $args): array
|
||||
private function requireCard(Request $request, array $args): array
|
||||
{
|
||||
$card = $this->cards->findInProject((int) $args['cardId'], $projectId);
|
||||
$card = $this->cards->findOwnedBy((int) $args['cardId'], $this->user($request)['id']);
|
||||
|
||||
if ($card === null) {
|
||||
throw new ApiException('Card not found.', 404);
|
||||
@@ -193,7 +264,7 @@ final class CardController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{id: int, project_id: int, text: string, complete: bool, position: int, status_id: int|null, status: array{id: int, name: string}|null, created_at: string, updated_at: string} $card
|
||||
* @param array{id: int, owner_id: int, project_id: int|null, text: string, complete: bool, position: int, status_id: int|null, status: array{id: int, name: string}|null, created_at: string, updated_at: string} $card
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function present(array $card): array
|
||||
|
||||
Reference in New Issue
Block a user