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:
+163
-111
@@ -9,12 +9,14 @@ use PDO;
|
||||
/**
|
||||
* Data access for the `cards` table.
|
||||
*
|
||||
* `position` is a dense 0..n-1 rank *within a column* — the cards sharing a
|
||||
* (project_id, status_id). The inbox is the column where status_id IS NULL.
|
||||
* 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. `position` is a dense 0..n-1 rank within a
|
||||
* "column": the cards sharing an (owner_id, project_id, status_id).
|
||||
*
|
||||
* @phpstan-type CardStatus array{id: int, name: string}
|
||||
* @phpstan-type CardRow array{
|
||||
* id: int, project_id: int, text: string, complete: bool, position: int,
|
||||
* id: int, owner_id: int, project_id: int|null, text: string, complete: bool, position: int,
|
||||
* status_id: int|null, status: CardStatus|null,
|
||||
* created_at: string, updated_at: string
|
||||
* }
|
||||
@@ -22,7 +24,7 @@ use PDO;
|
||||
final class CardRepository
|
||||
{
|
||||
private const SELECT = <<<'SQL'
|
||||
SELECT c.id, c.project_id, c.text, c.complete, c.position, c.status_id,
|
||||
SELECT c.id, c.owner_id, c.project_id, c.text, c.complete, c.position, c.status_id,
|
||||
c.created_at, c.updated_at,
|
||||
s.name AS status_name
|
||||
FROM cards c
|
||||
@@ -34,10 +36,6 @@ final class CardRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* Every card in the project, grouped by column (inbox first) and ordered by
|
||||
* position within each. Consumers that want a different order (e.g. the
|
||||
* alphabetical "all tasks" list) re-sort client-side.
|
||||
*
|
||||
* @return CardRow[]
|
||||
*/
|
||||
public function allForProject(int $projectId): array
|
||||
@@ -51,12 +49,24 @@ final class CardRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* The owner's global inbox, in position order.
|
||||
*
|
||||
* @return CardRow[]
|
||||
*/
|
||||
public function allInInbox(int $ownerId): array
|
||||
{
|
||||
return $this->columnCards($ownerId, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A card owned by the caller, wherever it is (inbox or a project).
|
||||
*
|
||||
* @return CardRow|null
|
||||
*/
|
||||
public function findInProject(int $id, int $projectId): ?array
|
||||
public function findOwnedBy(int $id, int $ownerId): ?array
|
||||
{
|
||||
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE c.id = :id AND c.project_id = :project');
|
||||
$stmt->execute(['id' => $id, 'project' => $projectId]);
|
||||
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE c.id = :id AND c.owner_id = :owner');
|
||||
$stmt->execute(['id' => $id, 'owner' => $ownerId]);
|
||||
|
||||
$row = $stmt->fetch();
|
||||
|
||||
@@ -64,46 +74,44 @@ final class CardRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a card directly in a project, at the end of the given status.
|
||||
*
|
||||
* @return CardRow
|
||||
*/
|
||||
public function create(int $projectId, string $text, bool $complete, ?int $position): array
|
||||
{
|
||||
// A new card has no status — it goes to the end of the inbox column.
|
||||
$position ??= $this->nextPositionInColumn($projectId, null);
|
||||
public function createInProject(
|
||||
int $ownerId,
|
||||
int $projectId,
|
||||
int $statusId,
|
||||
string $text,
|
||||
bool $complete,
|
||||
?int $position,
|
||||
): array {
|
||||
$position ??= $this->nextPositionInColumn($ownerId, $projectId, $statusId);
|
||||
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO cards (project_id, text, complete, position)
|
||||
VALUES (:project, :text, :complete, :position)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'project' => $projectId,
|
||||
'text' => $text,
|
||||
'complete' => $complete ? 1 : 0,
|
||||
'position' => $position,
|
||||
]);
|
||||
|
||||
/** @var CardRow $card */
|
||||
$card = $this->findInProject((int) $this->pdo->lastInsertId(), $projectId);
|
||||
|
||||
return $card;
|
||||
return $this->insert($ownerId, $projectId, $statusId, $text, $complete, $position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update simple fields, and/or move the card to another column. A status
|
||||
* change drops the card at the end of the destination column and re-packs
|
||||
* the one it left. Precise slotting within a column is done via orderColumn().
|
||||
* Create a card in the owner's inbox.
|
||||
*
|
||||
* @param array{text?: string, complete?: bool, status_id?: int|null} $fields
|
||||
* @return CardRow
|
||||
*/
|
||||
public function update(int $id, int $projectId, array $fields): array
|
||||
public function createInInbox(int $ownerId, string $text, bool $complete, ?int $position): array
|
||||
{
|
||||
/** @var CardRow $card */
|
||||
$card = $this->findInProject($id, $projectId);
|
||||
$position ??= $this->nextPositionInColumn($ownerId, null, null);
|
||||
|
||||
$movesColumn = array_key_exists('status_id', $fields) && $fields['status_id'] !== $card['status_id'];
|
||||
$sourceColumn = $card['status_id'];
|
||||
return $this->insert($ownerId, null, null, $text, $complete, $position);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update text/complete. Moving a card between columns -- including in or
|
||||
* out of the inbox -- is done via orderColumn(), not here.
|
||||
*
|
||||
* @param array{text?: string, complete?: bool} $fields
|
||||
* @return CardRow
|
||||
*/
|
||||
public function update(int $id, array $fields): array
|
||||
{
|
||||
$sets = ['updated_at = ' . $this->nowExpr()];
|
||||
$params = ['id' => $id];
|
||||
|
||||
@@ -115,40 +123,13 @@ final class CardRepository
|
||||
$sets[] = 'complete = :complete';
|
||||
$params['complete'] = $fields['complete'] ? 1 : 0;
|
||||
}
|
||||
if (array_key_exists('status_id', $fields)) {
|
||||
$sets[] = 'status_id = :status_id';
|
||||
$params['status_id'] = $fields['status_id'];
|
||||
}
|
||||
if ($movesColumn) {
|
||||
$sets[] = 'position = :position';
|
||||
$params['position'] = $this->nextPositionInColumn($projectId, $fields['status_id']);
|
||||
}
|
||||
|
||||
$sql = 'UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id';
|
||||
$this->pdo->prepare('UPDATE cards SET ' . implode(', ', $sets) . ' WHERE id = :id')->execute($params);
|
||||
|
||||
if (!$movesColumn) {
|
||||
$this->pdo->prepare($sql)->execute($params);
|
||||
/** @var CardRow $card */
|
||||
$card = $this->find($id);
|
||||
|
||||
/** @var CardRow $updated */
|
||||
$updated = $this->findInProject($id, $projectId);
|
||||
|
||||
return $updated;
|
||||
}
|
||||
|
||||
$this->pdo->beginTransaction();
|
||||
try {
|
||||
$this->pdo->prepare($sql)->execute($params);
|
||||
$this->repack($projectId, $sourceColumn);
|
||||
$this->pdo->commit();
|
||||
} catch (\Throwable $e) {
|
||||
$this->pdo->rollBack();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
/** @var CardRow $updated */
|
||||
$updated = $this->findInProject($id, $projectId);
|
||||
|
||||
return $updated;
|
||||
return $card;
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
@@ -157,14 +138,14 @@ final class CardRepository
|
||||
}
|
||||
|
||||
/**
|
||||
* IDs of every card in the project.
|
||||
* Every card id owned by this user, wherever it is.
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public function idsForProject(int $projectId): array
|
||||
public function idsOwnedBy(int $ownerId): array
|
||||
{
|
||||
$stmt = $this->pdo->prepare('SELECT id FROM cards WHERE project_id = :project');
|
||||
$stmt->execute(['project' => $projectId]);
|
||||
$stmt = $this->pdo->prepare('SELECT id FROM cards WHERE owner_id = :owner');
|
||||
$stmt->execute(['owner' => $ownerId]);
|
||||
|
||||
return array_map(intval(...), $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||
}
|
||||
@@ -174,50 +155,54 @@ final class CardRepository
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public function idsInColumn(int $projectId, ?int $statusId): array
|
||||
public function idsInColumn(int $ownerId, ?int $projectId, ?int $statusId): array
|
||||
{
|
||||
[$match, $params] = $this->columnMatch($statusId);
|
||||
[$match, $params] = $this->columnMatch($projectId, $statusId);
|
||||
$stmt = $this->pdo->prepare(
|
||||
"SELECT id FROM cards WHERE project_id = :project AND {$match} ORDER BY position ASC, id ASC"
|
||||
"SELECT id FROM cards WHERE owner_id = :owner AND {$match} ORDER BY position ASC, id ASC"
|
||||
);
|
||||
$stmt->execute(['project' => $projectId] + $params);
|
||||
$stmt->execute(['owner' => $ownerId] + $params);
|
||||
|
||||
return array_map(intval(...), $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the contents and order of one column. Every id in $orderedIds is moved
|
||||
* into $statusId at positions 0..n-1; any other column those cards came from
|
||||
* is re-packed. One transaction.
|
||||
* Set the contents and order of one column -- the inbox (both null) or a
|
||||
* project's status (both set). Every id in $orderedIds is moved there at
|
||||
* positions 0..n-1; any other column those cards came from (another
|
||||
* project's status, the inbox, ...) is re-packed. One transaction.
|
||||
*
|
||||
* @param int[] $orderedIds
|
||||
* @return CardRow[] the project's cards, grouped by column
|
||||
* @return CardRow[] the column's cards, in their new order
|
||||
*/
|
||||
public function orderColumn(int $projectId, ?int $statusId, array $orderedIds): array
|
||||
public function orderColumn(int $ownerId, ?int $projectId, ?int $statusId, array $orderedIds): array
|
||||
{
|
||||
$orderedIds = array_values($orderedIds);
|
||||
|
||||
$this->pdo->beginTransaction();
|
||||
try {
|
||||
$sourceColumns = $this->columnsOf($projectId, $orderedIds);
|
||||
$sources = $this->columnsOf($ownerId, $orderedIds);
|
||||
|
||||
$place = $this->pdo->prepare(
|
||||
'UPDATE cards SET status_id = :status, position = :position, updated_at = ' . $this->nowExpr() . '
|
||||
WHERE id = :id AND project_id = :project'
|
||||
'UPDATE cards SET project_id = :project, status_id = :status, position = :position,
|
||||
updated_at = ' . $this->nowExpr() . '
|
||||
WHERE id = :id AND owner_id = :owner'
|
||||
);
|
||||
foreach ($orderedIds as $position => $id) {
|
||||
$place->execute([
|
||||
'project' => $projectId,
|
||||
'status' => $statusId,
|
||||
'position' => $position,
|
||||
'id' => $id,
|
||||
'project' => $projectId,
|
||||
'owner' => $ownerId,
|
||||
]);
|
||||
}
|
||||
|
||||
foreach ($sourceColumns as $source) {
|
||||
if ($source !== $statusId) {
|
||||
$this->repack($projectId, $source);
|
||||
foreach ($sources as [$sourceProject, $sourceStatus]) {
|
||||
if ($sourceProject === $projectId && $sourceStatus === $statusId) {
|
||||
continue;
|
||||
}
|
||||
$this->repack($ownerId, $sourceProject, $sourceStatus);
|
||||
}
|
||||
|
||||
$this->pdo->commit();
|
||||
@@ -226,16 +211,60 @@ final class CardRepository
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return $this->allForProject($projectId);
|
||||
return $this->columnCards($ownerId, $projectId, $statusId);
|
||||
}
|
||||
|
||||
private function insert(
|
||||
int $ownerId,
|
||||
?int $projectId,
|
||||
?int $statusId,
|
||||
string $text,
|
||||
bool $complete,
|
||||
int $position,
|
||||
): array {
|
||||
$stmt = $this->pdo->prepare(
|
||||
'INSERT INTO cards (owner_id, project_id, status_id, text, complete, position)
|
||||
VALUES (:owner, :project, :status, :text, :complete, :position)'
|
||||
);
|
||||
$stmt->execute([
|
||||
'owner' => $ownerId,
|
||||
'project' => $projectId,
|
||||
'status' => $statusId,
|
||||
'text' => $text,
|
||||
'complete' => $complete ? 1 : 0,
|
||||
'position' => $position,
|
||||
]);
|
||||
|
||||
/** @var CardRow $card */
|
||||
$card = $this->find((int) $this->pdo->lastInsertId());
|
||||
|
||||
return $card;
|
||||
}
|
||||
|
||||
/**
|
||||
* The distinct status_id values (columns) the given cards currently sit in.
|
||||
* @return CardRow[]
|
||||
*/
|
||||
private function columnCards(int $ownerId, ?int $projectId, ?int $statusId): array
|
||||
{
|
||||
// Qualified with c. -- self::SELECT joins card_statuses, which also
|
||||
// has a project_id, so the bare column name is ambiguous here.
|
||||
[$match, $params] = $this->columnMatch($projectId, $statusId, 'c.');
|
||||
$stmt = $this->pdo->prepare(
|
||||
self::SELECT . " WHERE c.owner_id = :owner AND {$match} ORDER BY c.position ASC, c.id ASC"
|
||||
);
|
||||
$stmt->execute(['owner' => $ownerId] + $params);
|
||||
|
||||
return array_map($this->cast(...), $stmt->fetchAll());
|
||||
}
|
||||
|
||||
/**
|
||||
* The distinct (project_id, status_id) columns the given cards currently
|
||||
* sit in.
|
||||
*
|
||||
* @param int[] $ids
|
||||
* @return array<int, int|null>
|
||||
* @return list<array{0: int|null, 1: int|null}>
|
||||
*/
|
||||
private function columnsOf(int $projectId, array $ids): array
|
||||
private function columnsOf(int $ownerId, array $ids): array
|
||||
{
|
||||
if ($ids === []) {
|
||||
return [];
|
||||
@@ -243,20 +272,23 @@ final class CardRepository
|
||||
|
||||
$placeholders = implode(',', array_fill(0, count($ids), '?'));
|
||||
$stmt = $this->pdo->prepare(
|
||||
"SELECT DISTINCT status_id FROM cards WHERE project_id = ? AND id IN ($placeholders)"
|
||||
"SELECT DISTINCT project_id, status_id FROM cards WHERE owner_id = ? AND id IN ($placeholders)"
|
||||
);
|
||||
$stmt->execute([$projectId, ...$ids]);
|
||||
$stmt->execute([$ownerId, ...$ids]);
|
||||
|
||||
return array_map(
|
||||
static fn ($value) => $value === null ? null : (int) $value,
|
||||
$stmt->fetchAll(PDO::FETCH_COLUMN),
|
||||
static fn (array $row): array => [
|
||||
$row['project_id'] === null ? null : (int) $row['project_id'],
|
||||
$row['status_id'] === null ? null : (int) $row['status_id'],
|
||||
],
|
||||
$stmt->fetchAll(PDO::FETCH_ASSOC),
|
||||
);
|
||||
}
|
||||
|
||||
/** Rewrite one column's positions to a dense 0..n-1 in current order. */
|
||||
private function repack(int $projectId, ?int $statusId): void
|
||||
private function repack(int $ownerId, ?int $projectId, ?int $statusId): void
|
||||
{
|
||||
$ids = $this->idsInColumn($projectId, $statusId);
|
||||
$ids = $this->idsInColumn($ownerId, $projectId, $statusId);
|
||||
|
||||
$update = $this->pdo->prepare('UPDATE cards SET position = :position WHERE id = :id');
|
||||
foreach ($ids as $position => $id) {
|
||||
@@ -264,28 +296,47 @@ final class CardRepository
|
||||
}
|
||||
}
|
||||
|
||||
private function nextPositionInColumn(int $projectId, ?int $statusId): int
|
||||
private function nextPositionInColumn(int $ownerId, ?int $projectId, ?int $statusId): int
|
||||
{
|
||||
[$match, $params] = $this->columnMatch($statusId);
|
||||
[$match, $params] = $this->columnMatch($projectId, $statusId);
|
||||
$stmt = $this->pdo->prepare(
|
||||
"SELECT COALESCE(MAX(position), -1) + 1 FROM cards WHERE project_id = :project AND {$match}"
|
||||
"SELECT COALESCE(MAX(position), -1) + 1 FROM cards WHERE owner_id = :owner AND {$match}"
|
||||
);
|
||||
$stmt->execute(['project' => $projectId] + $params);
|
||||
$stmt->execute(['owner' => $ownerId] + $params);
|
||||
|
||||
return (int) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
/**
|
||||
* A WHERE fragment matching one column, since SQLite needs `IS NULL` (not
|
||||
* `= NULL`) for the inbox.
|
||||
* A WHERE fragment matching one column. The inbox is (project_id IS NULL
|
||||
* AND status_id IS NULL); a project column is an exact pair -- the CHECK
|
||||
* constraint guarantees they're never mismatched. $prefix qualifies the
|
||||
* column names (e.g. "c.") for queries that join card_statuses, which
|
||||
* also has a project_id.
|
||||
*
|
||||
* @return array{0: string, 1: array<string, int>}
|
||||
*/
|
||||
private function columnMatch(?int $statusId): array
|
||||
private function columnMatch(?int $projectId, ?int $statusId, string $prefix = ''): array
|
||||
{
|
||||
return $statusId === null
|
||||
? ['status_id IS NULL', []]
|
||||
: ['status_id = :status', ['status' => $statusId]];
|
||||
return $projectId === null
|
||||
? ["{$prefix}project_id IS NULL AND {$prefix}status_id IS NULL", []]
|
||||
: [
|
||||
"{$prefix}project_id = :project AND {$prefix}status_id = :status",
|
||||
['project' => $projectId, 'status' => $statusId],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return CardRow|null
|
||||
*/
|
||||
private function find(int $id): ?array
|
||||
{
|
||||
$stmt = $this->pdo->prepare(self::SELECT . ' WHERE c.id = :id');
|
||||
$stmt->execute(['id' => $id]);
|
||||
|
||||
$row = $stmt->fetch();
|
||||
|
||||
return $row === false ? null : $this->cast($row);
|
||||
}
|
||||
|
||||
private function nowExpr(): string
|
||||
@@ -300,7 +351,8 @@ final class CardRepository
|
||||
private function cast(array $row): array
|
||||
{
|
||||
$row['id'] = (int) $row['id'];
|
||||
$row['project_id'] = (int) $row['project_id'];
|
||||
$row['owner_id'] = (int) $row['owner_id'];
|
||||
$row['project_id'] = $row['project_id'] === null ? null : (int) $row['project_id'];
|
||||
$row['complete'] = (bool) $row['complete'];
|
||||
$row['position'] = (int) $row['position'];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user