-
Notifications
You must be signed in to change notification settings - Fork 0
feat(stages): customizable pipeline stages with colours, reorder, and migration-on-delete #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3e36711
feat(stages): add color column, won/lost CHECK constraint, color palette
Timmyy3000 ceef17c
feat(stages): lib/stages business logic with assertStageInOrg
Timmyy3000 d79bcf5
feat(stages): UI server actions + extend ENTITY_TYPES with stage/pipe…
Timmyy3000 8b1bda0
feat(mcp): stage CRUD tools (create, update, reorder, delete)
Timmyy3000 0573d18
feat(stages): interactive Settings UI for stage editing
Timmyy3000 61b8336
test(stages): unit tests for lib/stages + MCP wrapper smoke + audit a…
Timmyy3000 a4dc681
feat(stages): propagate stage.color to all StagePill consumers
Timmyy3000 39e6076
fix(stages): three review findings from enkii
Timmyy3000 dea842f
Merge remote-tracking branch 'origin/main' into ft/pipeline-stages
Timmyy3000 0d55b20
chore(migrations): renumber stage migration to 0006 after main rebase
Timmyy3000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| "use server"; | ||
|
|
||
| import { revalidatePath } from "next/cache"; | ||
| import { z } from "zod"; | ||
| import { db } from "@/db/client"; | ||
| import { diffChangedFields, recordAudit, userActor } from "@/lib/audit"; | ||
| import { requireOrgSession } from "@/lib/session"; | ||
| import { isStageColor, type StageColor } from "@/lib/stage-colors"; | ||
| import { | ||
| createStage, | ||
| deleteStage, | ||
| previewStageFlagToggle, | ||
| reorderStage, | ||
| StageOpError, | ||
| updateStage, | ||
| type ReorderDirection, | ||
| } from "@/lib/stages"; | ||
|
|
||
| const NameSchema = z.string().trim().min(1).max(80); | ||
|
|
||
| function normalizeColor(value: FormDataEntryValue | null): StageColor | null | undefined { | ||
| if (value === null) return undefined; | ||
| const s = String(value); | ||
| if (s === "") return null; | ||
| if (!isStageColor(s)) return undefined; | ||
| return s; | ||
| } | ||
|
|
||
| function ok<T>(data: T): { ok: true; data: T } { | ||
| return { ok: true, data }; | ||
| } | ||
| function err(code: StageOpError["code"], message: string): { ok: false; code: StageOpError["code"]; message: string } { | ||
| return { ok: false, code, message }; | ||
| } | ||
| function mapError(e: unknown): { ok: false; code: StageOpError["code"]; message: string } { | ||
| if (e instanceof StageOpError) return err(e.code, e.message); | ||
| throw e; | ||
| } | ||
|
|
||
| function revalidateAll(): void { | ||
| revalidatePath("/settings/pipelines"); | ||
| revalidatePath("/deals"); | ||
| revalidatePath("/companies", "layout"); | ||
| } | ||
|
|
||
| export async function addStageAction(formData: FormData) { | ||
| const session = await requireOrgSession(); | ||
| const pipelineId = z.string().uuid().parse(formData.get("pipelineId")); | ||
| const name = NameSchema.parse(formData.get("name")); | ||
| const color = normalizeColor(formData.get("color")); | ||
| const isWon = formData.get("isWon") === "true"; | ||
| const isLost = formData.get("isLost") === "true"; | ||
|
|
||
| try { | ||
| const result = await createStage(db(), session.organizationId, { | ||
| pipelineId, | ||
| name, | ||
| color: color ?? null, | ||
| isWon, | ||
| isLost, | ||
| }); | ||
| await recordAudit(db(), { | ||
| organizationId: session.organizationId, | ||
| actor: userActor(session.user.id, session.user.name ?? null), | ||
| entityType: "stage", | ||
| entityId: result.after.id, | ||
| action: "create", | ||
| changes: { after: result.after }, | ||
| }); | ||
| revalidateAll(); | ||
| return ok(result.after); | ||
| } catch (e) { | ||
| return mapError(e); | ||
| } | ||
| } | ||
|
|
||
| export async function updateStageAction(formData: FormData) { | ||
| const session = await requireOrgSession(); | ||
| const id = z.string().uuid().parse(formData.get("id")); | ||
| const nameRaw = formData.get("name"); | ||
| const colorRaw = formData.get("color"); | ||
| const isWonRaw = formData.get("isWon"); | ||
| const isLostRaw = formData.get("isLost"); | ||
|
|
||
| try { | ||
| const result = await updateStage(db(), session.organizationId, { | ||
| id, | ||
| name: nameRaw === null ? undefined : NameSchema.parse(nameRaw), | ||
| color: colorRaw === null ? undefined : normalizeColor(colorRaw), | ||
| isWon: isWonRaw === null ? undefined : isWonRaw === "true", | ||
| isLost: isLostRaw === null ? undefined : isLostRaw === "true", | ||
| }); | ||
| if (!result) return err("not_found", "Stage not found"); | ||
| await recordAudit(db(), { | ||
| organizationId: session.organizationId, | ||
| actor: userActor(session.user.id, session.user.name ?? null), | ||
| entityType: "stage", | ||
| entityId: result.after.id, | ||
| action: "update", | ||
| changes: diffChangedFields(result.before, result.after), | ||
| }); | ||
| revalidateAll(); | ||
| return ok(result.after); | ||
| } catch (e) { | ||
| return mapError(e); | ||
| } | ||
| } | ||
|
|
||
| const DirectionSchema = z.enum(["up", "down"]); | ||
|
|
||
| export async function reorderStageAction(input: { | ||
| stageId: string; | ||
| direction: ReorderDirection; | ||
| }) { | ||
| const session = await requireOrgSession(); | ||
| const stageId = z.string().uuid().parse(input.stageId); | ||
| const direction = DirectionSchema.parse(input.direction); | ||
|
|
||
| try { | ||
| const result = await reorderStage(db(), session.organizationId, stageId, direction); | ||
| if (!result) return err("not_found", "Stage not found"); | ||
| if (result.before.id === result.after.id && result.before.order === result.after.order) { | ||
| // boundary no-op; nothing to audit, nothing to revalidate | ||
| return ok(result.after); | ||
| } | ||
| await recordAudit(db(), { | ||
| organizationId: session.organizationId, | ||
| actor: userActor(session.user.id, session.user.name ?? null), | ||
| entityType: "stage", | ||
| entityId: result.after.id, | ||
| action: "update", | ||
| changes: { before: { order: result.before.order }, after: { order: result.after.order } }, | ||
| }); | ||
| revalidateAll(); | ||
| return ok(result.after); | ||
| } catch (e) { | ||
| return mapError(e); | ||
| } | ||
| } | ||
|
|
||
| export async function deleteStageAction(input: { | ||
| stageId: string; | ||
| destinationStageId: string; | ||
| }) { | ||
| const session = await requireOrgSession(); | ||
| const stageId = z.string().uuid().parse(input.stageId); | ||
| const destinationStageId = z.string().uuid().parse(input.destinationStageId); | ||
|
|
||
| try { | ||
| const result = await deleteStage(db(), session.organizationId, stageId, destinationStageId); | ||
| if (!result) return err("not_found", "Stage not found"); | ||
|
|
||
| const actor = userActor(session.user.id, session.user.name ?? null); | ||
| for (const dealId of result.migratedDealIds) { | ||
| await recordAudit(db(), { | ||
| organizationId: session.organizationId, | ||
| actor, | ||
| entityType: "deal", | ||
| entityId: dealId, | ||
| action: "update", | ||
| changes: { before: { stageId }, after: { stageId: destinationStageId } }, | ||
| }); | ||
| } | ||
| await recordAudit(db(), { | ||
| organizationId: session.organizationId, | ||
| actor, | ||
| entityType: "stage", | ||
| entityId: stageId, | ||
| action: "delete", | ||
| changes: { before: result.before }, | ||
| }); | ||
| revalidateAll(); | ||
| return ok({ migratedDealCount: result.migratedDealIds.length }); | ||
| } catch (e) { | ||
| return mapError(e); | ||
| } | ||
| } | ||
|
|
||
| export async function previewStageFlagToggleAction(input: { stageId: string }) { | ||
| const session = await requireOrgSession(); | ||
| const stageId = z.string().uuid().parse(input.stageId); | ||
| const result = await previewStageFlagToggle(db(), session.organizationId, stageId); | ||
| if (!result) return err("not_found", "Stage not found"); | ||
| return ok(result); | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
colorstripped in staging mappingDealsViewreceivesstageswithcolorfrom the server component (added in this PR), but the mappingstages.map((s) => ({ id: s.id, name: s.name }))passed toKanbanBoarddrops thecolorfield.KanbanBoardnow renders<StagePill value={stage.name} color={stage.color} />wherestage.coloris alwaysundefined, so the kanban headers fall through to the name-based STAGE_CONFIG lookup. Custom colors set in Settings → Pipelines are invisible in board view.Fix: add
colorto the mapping:stages.map((s) => ({ id: s.id, name: s.name, color: s.color })).