Write the adopted team Rule before the server listens so deploy cannot feature the old draft body.
This commit is contained in:
1 parent
29a8d2b32b
commit
1ec1f48d35
8 files changed
+232
-86
No files matched your search
@@ -1,11 +0,0 @@
|
||||
-- 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'
|
||||
);
|
||||
+65
-28
@@ -1,3 +1,4 @@
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import type { Prisma, PrismaClient } from "@prisma/client";
|
||||
import { publishedRuleAnalyticsFromDocument } from "../../lib/create/publishedRuleAnalytics";
|
||||
import {
|
||||
@@ -8,7 +9,7 @@ import {
|
||||
import { RULE_VISIBILITY } from "../../lib/ruleVisibility";
|
||||
|
||||
export type SeedTeamRuleResult = {
|
||||
action: "created" | "updated";
|
||||
action: "created" | "updated" | "unchanged";
|
||||
id: string;
|
||||
title: string;
|
||||
};
|
||||
@@ -16,6 +17,8 @@ export type SeedTeamRuleResult = {
|
||||
/**
|
||||
* Replaces the old "The CommunityRule Project" draft with the adopted Rule,
|
||||
* or inserts it when the environment has neither row.
|
||||
* Any other featured row with either title is taken out of the catalog so
|
||||
* a second copy cannot stay public with a stale document.
|
||||
*/
|
||||
export async function seedTeamRule(
|
||||
prisma: PrismaClient,
|
||||
@@ -23,42 +26,76 @@ export async function seedTeamRule(
|
||||
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,
|
||||
communityLocation: analytics.communityLocation,
|
||||
orgType: analytics.orgType,
|
||||
};
|
||||
|
||||
if (existingId) {
|
||||
const row = await prisma.publishedRule.update({
|
||||
where: { id: existingId },
|
||||
data,
|
||||
const rows = await prisma.publishedRule.findMany({
|
||||
where: {
|
||||
title: { in: [TEAM_RULE_DRAFT_TITLE, TEAM_RULE_TITLE] },
|
||||
},
|
||||
orderBy: { createdAt: "asc" },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
summary: true,
|
||||
document: true,
|
||||
visibility: true,
|
||||
communityLocation: true,
|
||||
orgType: true,
|
||||
},
|
||||
});
|
||||
|
||||
const canonical =
|
||||
rows.find((row) => row.title === TEAM_RULE_DRAFT_TITLE) ?? rows[0];
|
||||
|
||||
if (!canonical) {
|
||||
const row = await prisma.publishedRule.create({
|
||||
data: {
|
||||
userId: null,
|
||||
...data,
|
||||
},
|
||||
select: { id: true, title: true },
|
||||
});
|
||||
return { action: "updated", id: row.id, title: row.title };
|
||||
return { action: "created", 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 };
|
||||
const matches =
|
||||
canonical.title === data.title &&
|
||||
canonical.summary === data.summary &&
|
||||
canonical.visibility === data.visibility &&
|
||||
canonical.communityLocation === data.communityLocation &&
|
||||
canonical.orgType === data.orgType &&
|
||||
isDeepStrictEqual(canonical.document, data.document);
|
||||
|
||||
let action: SeedTeamRuleResult["action"] = "unchanged";
|
||||
if (!matches) {
|
||||
await prisma.publishedRule.update({
|
||||
where: { id: canonical.id },
|
||||
data,
|
||||
});
|
||||
action = "updated";
|
||||
}
|
||||
|
||||
const extraIds = rows
|
||||
.filter(
|
||||
(row) =>
|
||||
row.id !== canonical.id &&
|
||||
row.visibility === RULE_VISIBILITY.public_and_featured,
|
||||
)
|
||||
.map((row) => row.id);
|
||||
if (extraIds.length > 0) {
|
||||
await prisma.publishedRule.updateMany({
|
||||
where: { id: { in: extraIds } },
|
||||
data: { visibility: RULE_VISIBILITY.private },
|
||||
});
|
||||
action = "updated";
|
||||
}
|
||||
|
||||
return { action, id: canonical.id, title: data.title };
|
||||
}
|
||||
Reference in new issue
Block a user