diff --git a/lib/create/teamRule.ts b/lib/create/teamRule.ts new file mode 100644 index 0000000..571673f --- /dev/null +++ b/lib/create/teamRule.ts @@ -0,0 +1,124 @@ +import type { CreateFlowState } from "../../app/(app)/create/types"; +import { + conflictManagementPresetFor, + decisionApproachPresetFor, +} from "./finalReviewChipPresets"; +import { buildPublishPayload } from "./buildPublishPayload"; + +/** Staging still lists the September draft under this title. */ +export const TEAM_RULE_DRAFT_TITLE = "The CommunityRule Project"; + +export const TEAM_RULE_TITLE = "CommunityRule Creative Team"; + +const TEAM_RULE_SUMMARY = + "We build and maintain CommunityRule for the communities that use it. It is free for every community that wants to write down how it decides."; + +/** Core-value catalog ids, Home hero order. */ +const TEAM_RULE_VALUE_IDS = [ + "transparency", + "forkability", + "documentation", + "maintenance", + "voluntarism", +] as const; + +const TEAM_RULE_COMMUNICATION_IDS = [ + "github-gitlab", + "matrix-element", + "email-distribution-list", + "how-we-run-meetings", +] as const; + +const TEAM_RULE_MEMBERSHIP_IDS = [ + "join-by-contributing", + "companions", +] as const; + +const TEAM_RULE_DECISION_IDS = [ + "consensus-decision-making", + "lazy-consensus", + "do-ocracy", + "changing-this-rule", +] as const; + +const TEAM_RULE_CONFLICT_IDS = [ + "conflict-ladder", + "hearing-panel", + "restorative-practices", +] as const; + +const VALUE_LABELS: Record<(typeof TEAM_RULE_VALUE_IDS)[number], string> = { + transparency: "Transparency", + forkability: "Forkability", + documentation: "Documentation", + maintenance: "Maintenance", + voluntarism: "Voluntarism", +}; + +function decisionDetailsFor( + id: (typeof TEAM_RULE_DECISION_IDS)[number], +): NonNullable[string] { + const preset = decisionApproachPresetFor(id); + return { + ...preset, + selectedApplicableScope: [...preset.applicableScope], + }; +} + +function conflictDetailsFor( + id: (typeof TEAM_RULE_CONFLICT_IDS)[number], +): NonNullable[string] { + const preset = conflictManagementPresetFor(id); + return { + ...preset, + selectedApplicableScope: [...preset.applicableScope], + }; +} + +/** Create-flow state that publishes as the team's adopted Rule. */ +function teamRuleCreateFlowState(): CreateFlowState { + return { + title: TEAM_RULE_TITLE, + summary: TEAM_RULE_SUMMARY, + communityContext: TEAM_RULE_SUMMARY, + selectedCoreValueIds: [...TEAM_RULE_VALUE_IDS], + coreValuesChipsSnapshot: TEAM_RULE_VALUE_IDS.map((id) => ({ + id, + label: VALUE_LABELS[id], + state: "selected", + })), + selectedCommunicationMethodIds: [...TEAM_RULE_COMMUNICATION_IDS], + selectedMembershipMethodIds: [...TEAM_RULE_MEMBERSHIP_IDS], + selectedDecisionApproachIds: [...TEAM_RULE_DECISION_IDS], + selectedConflictManagementIds: [...TEAM_RULE_CONFLICT_IDS], + decisionApproachDetailsById: Object.fromEntries( + TEAM_RULE_DECISION_IDS.map((id) => [id, decisionDetailsFor(id)]), + ), + conflictManagementDetailsById: Object.fromEntries( + TEAM_RULE_CONFLICT_IDS.map((id) => [id, conflictDetailsFor(id)]), + ), + }; +} + +export type AdoptedTeamRule = { + title: string; + summary: string; + document: Record; +}; + +/** + * Title, summary, and document for the Rule the team adopted on 30 Oct. + * Card set matches the Home hero; method bodies are the remade-deck presets + * those chips carry. + */ +export function buildAdoptedTeamRule(): AdoptedTeamRule { + const result = buildPublishPayload(teamRuleCreateFlowState()); + if (result.ok === false) { + throw new Error(`Failed to build the adopted team Rule: ${result.error}`); + } + return { + title: result.title, + summary: result.summary ?? TEAM_RULE_SUMMARY, + document: result.document, + }; +} diff --git a/prisma/migrations/20261112220000_adopt_team_rule/migration.sql b/prisma/migrations/20261112220000_adopt_team_rule/migration.sql new file mode 100644 index 0000000..043feda --- /dev/null +++ b/prisma/migrations/20261112220000_adopt_team_rule/migration.sql @@ -0,0 +1,11 @@ +-- The team's adopted Rule replaces the September draft at GET /api/rules. +-- Document bodies land when `prisma db seed` / `node prisma/seed.bundle.cjs` runs. +UPDATE "PublishedRule" +SET + title = 'CommunityRule Creative Team', + summary = 'We build and maintain CommunityRule for the communities that use it. It is free for every community that wants to write down how it decides.', + visibility = 'public_and_featured' +WHERE title IN ( + 'The CommunityRule Project', + 'CommunityRule Creative Team' +); diff --git a/prisma/seed.ts b/prisma/seed.ts index 220ccae..4fff2a5 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -1,5 +1,6 @@ import { PrismaClient, type Prisma } from "@prisma/client"; import { seedMethodFacets } from "./seed/methodFacets"; +import { seedTeamRule } from "./seed/teamRule"; import { seedTemplateFacets } from "./seed/templateFacets"; /** @@ -402,6 +403,12 @@ async function main() { console.log( `Seeded TemplateFacet rows: ${templateFacetSeed.rowCount}`, ); + + const teamRuleSeed = await seedTeamRule(prisma); + // eslint-disable-next-line no-console -- seed CLI feedback + console.log( + `Seeded team Rule: ${teamRuleSeed.action} ${teamRuleSeed.title} (${teamRuleSeed.id})`, + ); } main() diff --git a/prisma/seed/teamRule.ts b/prisma/seed/teamRule.ts new file mode 100644 index 0000000..ad91707 --- /dev/null +++ b/prisma/seed/teamRule.ts @@ -0,0 +1,64 @@ +import type { Prisma, PrismaClient } from "@prisma/client"; +import { publishedRuleAnalyticsFromDocument } from "../../lib/create/publishedRuleAnalytics"; +import { + TEAM_RULE_DRAFT_TITLE, + TEAM_RULE_TITLE, + buildAdoptedTeamRule, +} from "../../lib/create/teamRule"; +import { RULE_VISIBILITY } from "../../lib/ruleVisibility"; + +export type SeedTeamRuleResult = { + action: "created" | "updated"; + id: string; + title: string; +}; + +/** + * Replaces the old "The CommunityRule Project" draft with the adopted Rule, + * or inserts it when the environment has neither row. + */ +export async function seedTeamRule( + prisma: PrismaClient, +): Promise { + const adopted = buildAdoptedTeamRule(); + const document = adopted.document as Prisma.InputJsonValue; + const analytics = publishedRuleAnalyticsFromDocument(adopted.document); + + const draftRow = await prisma.publishedRule.findFirst({ + where: { title: TEAM_RULE_DRAFT_TITLE }, + orderBy: { createdAt: "asc" }, + select: { id: true }, + }); + const adoptedRow = await prisma.publishedRule.findFirst({ + where: { title: TEAM_RULE_TITLE }, + orderBy: { createdAt: "asc" }, + select: { id: true }, + }); + const existingId = draftRow?.id ?? adoptedRow?.id; + + const data = { + title: adopted.title, + summary: adopted.summary, + document, + visibility: RULE_VISIBILITY.public_and_featured, + ...analytics, + }; + + if (existingId) { + const row = await prisma.publishedRule.update({ + where: { id: existingId }, + data, + select: { id: true, title: true }, + }); + return { action: "updated", id: row.id, title: row.title }; + } + + const row = await prisma.publishedRule.create({ + data: { + userId: null, + ...data, + }, + select: { id: true, title: true }, + }); + return { action: "created", id: row.id, title: row.title }; +} diff --git a/tests/unit/seedTeamRule.test.ts b/tests/unit/seedTeamRule.test.ts new file mode 100644 index 0000000..1b7533a --- /dev/null +++ b/tests/unit/seedTeamRule.test.ts @@ -0,0 +1,102 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { RULE_VISIBILITY } from "../../lib/ruleVisibility"; +import { TEAM_RULE_DRAFT_TITLE, TEAM_RULE_TITLE } from "../../lib/create/teamRule"; + +const findFirstMock = vi.fn(); +const updateMock = vi.fn(); +const createMock = vi.fn(); + +import { seedTeamRule } from "../../prisma/seed/teamRule"; + +function prismaStub() { + return { + publishedRule: { + findFirst: findFirstMock, + update: updateMock, + create: createMock, + }, + }; +} + +beforeEach(() => { + findFirstMock.mockReset(); + updateMock.mockReset(); + createMock.mockReset(); +}); + +describe("seedTeamRule", () => { + it("replaces the old draft row in place", async () => { + findFirstMock + .mockResolvedValueOnce({ id: "draft-1" }) + .mockResolvedValueOnce(null); + updateMock.mockResolvedValueOnce({ + id: "draft-1", + title: TEAM_RULE_TITLE, + }); + + const result = await seedTeamRule(prismaStub() as never); + + expect(result).toEqual({ + action: "updated", + id: "draft-1", + title: TEAM_RULE_TITLE, + }); + expect(findFirstMock).toHaveBeenNthCalledWith(1, { + where: { title: TEAM_RULE_DRAFT_TITLE }, + orderBy: { createdAt: "asc" }, + select: { id: true }, + }); + expect(updateMock).toHaveBeenCalledWith( + expect.objectContaining({ + where: { id: "draft-1" }, + data: expect.objectContaining({ + title: TEAM_RULE_TITLE, + visibility: RULE_VISIBILITY.public_and_featured, + }), + }), + ); + expect(createMock).not.toHaveBeenCalled(); + }); + + it("updates an already-renamed adopted row", async () => { + findFirstMock + .mockResolvedValueOnce(null) + .mockResolvedValueOnce({ id: "adopted-1" }); + updateMock.mockResolvedValueOnce({ + id: "adopted-1", + title: TEAM_RULE_TITLE, + }); + + const result = await seedTeamRule(prismaStub() as never); + + expect(result.action).toBe("updated"); + expect(updateMock).toHaveBeenCalledWith( + expect.objectContaining({ where: { id: "adopted-1" } }), + ); + }); + + it("creates the adopted Rule when neither row exists", async () => { + findFirstMock.mockResolvedValueOnce(null).mockResolvedValueOnce(null); + createMock.mockResolvedValueOnce({ + id: "new-1", + title: TEAM_RULE_TITLE, + }); + + const result = await seedTeamRule(prismaStub() as never); + + expect(result).toEqual({ + action: "created", + id: "new-1", + title: TEAM_RULE_TITLE, + }); + expect(createMock).toHaveBeenCalledWith( + expect.objectContaining({ + data: expect.objectContaining({ + userId: null, + title: TEAM_RULE_TITLE, + visibility: RULE_VISIBILITY.public_and_featured, + }), + }), + ); + }); +}); diff --git a/tests/unit/teamRule.test.ts b/tests/unit/teamRule.test.ts new file mode 100644 index 0000000..a99871b --- /dev/null +++ b/tests/unit/teamRule.test.ts @@ -0,0 +1,89 @@ +import { describe, expect, it } from "vitest"; +import { parsePublishedDocumentForCommunityRuleDisplay } from "../../lib/create/publishedDocumentToDisplaySections"; +import { + TEAM_RULE_DRAFT_TITLE, + TEAM_RULE_TITLE, + buildAdoptedTeamRule, +} from "../../lib/create/teamRule"; +import heroBanner from "../../messages/en/components/heroBanner.json"; + +function chipTitles(categoryName: string): string[] { + const rule = buildAdoptedTeamRule(); + const sections = parsePublishedDocumentForCommunityRuleDisplay(rule.document); + const section = sections.find((row) => row.categoryName === categoryName); + return (section?.entries ?? []).map((entry) => entry.title); +} + +describe("adopted team Rule", () => { + it("uses the adopted title and Home hero summary", () => { + const rule = buildAdoptedTeamRule(); + expect(rule.title).toBe(TEAM_RULE_TITLE); + expect(rule.title).toBe(heroBanner.ruleCard.title); + expect(rule.summary).toBe(heroBanner.ruleCard.description); + }); + + it("contains none of the old draft's title or summary", () => { + const serialized = JSON.stringify(buildAdoptedTeamRule()); + expect(serialized).not.toContain(TEAM_RULE_DRAFT_TITLE); + expect(serialized).not.toContain( + "a free toolkit for writing down how communities make decisions", + ); + expect(serialized).not.toContain("Skill-Based Contribution"); + expect(serialized).not.toContain("Conflict Resolution Council"); + }); + + it("publishes the remade-deck cards the Home hero shows", () => { + const rule = buildAdoptedTeamRule(); + const values = rule.document.coreValues as Array<{ chipId: string }>; + expect(values.map((row) => row.chipId)).toEqual([ + "transparency", + "forkability", + "documentation", + "maintenance", + "voluntarism", + ]); + + const methods = rule.document.methodSelections as { + communication: Array<{ id: string }>; + membership: Array<{ id: string }>; + decisionApproaches: Array<{ id: string }>; + conflictManagement: Array<{ id: string }>; + }; + expect(methods.communication.map((row) => row.id)).toEqual([ + "github-gitlab", + "matrix-element", + "email-distribution-list", + "how-we-run-meetings", + ]); + expect(methods.membership.map((row) => row.id)).toEqual([ + "join-by-contributing", + "companions", + ]); + expect(methods.decisionApproaches.map((row) => row.id)).toEqual([ + "consensus-decision-making", + "lazy-consensus", + "do-ocracy", + "changing-this-rule", + ]); + expect(methods.conflictManagement.map((row) => row.id)).toEqual([ + "conflict-ladder", + "hearing-panel", + "restorative-practices", + ]); + }); + + it("displays the Home hero chip labels, using the deck name for Consensus", () => { + const groups = heroBanner.ruleCard.categories; + expect(chipTitles("Values")).toEqual(groups.values.chips); + expect(chipTitles("Communication")).toEqual(groups.communication.chips); + expect(chipTitles("Membership")).toEqual(groups.membership.chips); + expect(chipTitles("Decision-making")).toEqual( + groups.decisionMaking.chips.map((label) => + label === "Consensus" ? "Consensus decision-making" : label, + ), + ); + expect(chipTitles("Conflict management")).toEqual( + groups.conflictManagement.chips, + ); + }); +});