Scrum4Me/lib/active-sprint.ts
Janpeter Visser bf7162a5fc
feat(PBI-76): migrate cookie-based prefs to user-settings (Phase 2) (#189)
* feat(PBI-76): extend UserSettings schema with layout

Adds layout.splitPanePositions and layout.activeSprints. These will
hold values currently kept in client-side and server-side cookies
(Phase 2). Two new tests cover the shape.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(PBI-76): migrate SplitPane positions to user-settings store

Outside of a drag the store is the source of truth (cross-tab
updates flow in for free). During a drag we keep splits in local
state so mousemove does not round-trip through the store. On
mouseup we persist the final splits via setPref. Removes
document.cookie reads/writes — cookieKey is reused as the
store-key for backwards compat.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(PBI-76): resolveActiveSprint reads from User.settings

lib/active-sprint:
- New helpers: getActiveSprintIdFromSettings, setActiveSprintInSettings,
  clearActiveSprintInSettings — all read/write user.settings.layout.activeSprints.
- resolveActiveSprint(productId, userId) — userId now required, falls back
  to first OPEN, then most recent CLOSED sprint.
- Cookie helpers (getActiveSprintIdFromCookie/setActiveSprintCookie/
  clearActiveSprintCookie) removed.

Callers updated to pass session.userId. The cookie-based fallback path
is gone — `actions/active-sprint.ts` and `actions/sprints.ts` will be
updated in the next commit (T-917).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(PBI-76): rewrite setActiveSprint callers to use settings

setActiveSprintAction, syncActiveSprintCookieAction, and the two
sprint-creation paths in actions/sprints.ts now write through
setActiveSprintInSettings (which also emits pg_notify for cross-tab
sync) instead of dropping a cookie. The action names keep the
'cookie' suffix in the user-visible API for now — clean rename can
come later.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(PBI-76): migration helper v2 — handle legacy cookies

Bumps marker version to 'v2'. buildMigrationPatch now also scans
document.cookie for `sp:*` (split-pane positions) and
`active_sprint_*` (active sprint per product) and lifts them into
layout.splitPanePositions / layout.activeSprints. clearLegacyStorage
replaces clearLegacyLocalStorage and clears both keys and cookies.
clearLegacyLocalStorage stays as a deprecated alias so the bridge
upgrade is a single rename.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(PBI-76): align tests with new SplitPane and active-sprint flow

- split-pane.test.tsx: seed positions via Zustand store instead of
  document.cookie; mock @/actions/user-settings so the prisma client
  is not transitively initialised in jsdom.
- backlog-split-pane.test.tsx: same action mock.
- sprint-dates.test.ts: add user.findUnique/update + $executeRaw
  mocks because createSprintAction now writes to user-settings.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 21:20:29 +02:00

114 lines
3 KiB
TypeScript

import type { Prisma, SprintStatus } from '@prisma/client'
import { prisma } from '@/lib/prisma'
import {
mergeSettings,
parseUserSettings,
type UserSettings,
} from '@/lib/user-settings'
export type ActiveSprint = {
id: string
code: string
status: SprintStatus
}
async function readSettings(userId: string): Promise<UserSettings> {
const user = await prisma.user.findUnique({
where: { id: userId },
select: { settings: true },
})
return parseUserSettings(user?.settings)
}
async function writeSettings(userId: string, next: UserSettings): Promise<void> {
await prisma.user.update({
where: { id: userId },
data: { settings: next as unknown as Prisma.InputJsonValue },
})
}
async function notifyUserSettings(
userId: string,
patch: Partial<UserSettings>,
): Promise<void> {
await prisma.$executeRaw`
SELECT pg_notify('scrum4me_changes', ${JSON.stringify({
kind: 'user_settings',
userId,
patch,
})}::text)
`
}
export async function getActiveSprintIdFromSettings(
userId: string,
productId: string,
): Promise<string | null> {
const settings = await readSettings(userId)
return settings.layout?.activeSprints?.[productId] ?? null
}
export async function setActiveSprintInSettings(
userId: string,
productId: string,
sprintId: string,
): Promise<void> {
const current = await readSettings(userId)
const patch: Partial<UserSettings> = {
layout: {
activeSprints: {
...(current.layout?.activeSprints ?? {}),
[productId]: sprintId,
},
},
}
await writeSettings(userId, mergeSettings(current, patch))
await notifyUserSettings(userId, patch)
}
export async function clearActiveSprintInSettings(
userId: string,
productId: string,
): Promise<void> {
const current = await readSettings(userId)
const existing = current.layout?.activeSprints
if (!existing || !(productId in existing)) return
const nextActiveSprints = { ...existing }
delete nextActiveSprints[productId]
const next: UserSettings = {
...current,
layout: { ...current.layout, activeSprints: nextActiveSprints },
}
await writeSettings(userId, next)
await notifyUserSettings(userId, {
layout: { activeSprints: nextActiveSprints },
})
}
export async function resolveActiveSprint(
productId: string,
userId: string,
): Promise<ActiveSprint | null> {
const stored = await getActiveSprintIdFromSettings(userId, productId)
if (stored) {
const sprint = await prisma.sprint.findFirst({
where: { id: stored, product_id: productId },
select: { id: true, code: true, status: true },
})
if (sprint) return sprint
}
const open = await prisma.sprint.findFirst({
where: { product_id: productId, status: 'OPEN' },
orderBy: { created_at: 'desc' },
select: { id: true, code: true, status: true },
})
if (open) return open
const closed = await prisma.sprint.findFirst({
where: { product_id: productId, status: 'CLOSED' },
orderBy: { created_at: 'desc' },
select: { id: true, code: true, status: true },
})
return closed ?? null
}