102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
import { isDeepStrictEqual } from "node:util";
|
|
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" | "unchanged";
|
|
id: string;
|
|
title: string;
|
|
};
|
|
|
|
/**
|
|
* 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,
|
|
): Promise<SeedTeamRuleResult> {
|
|
const adopted = buildAdoptedTeamRule();
|
|
const document = adopted.document as Prisma.InputJsonValue;
|
|
const analytics = publishedRuleAnalyticsFromDocument(adopted.document);
|
|
const data = {
|
|
title: adopted.title,
|
|
summary: adopted.summary,
|
|
document,
|
|
visibility: RULE_VISIBILITY.public_and_featured,
|
|
communityLocation: analytics.communityLocation,
|
|
orgType: analytics.orgType,
|
|
};
|
|
|
|
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: "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 };
|
|
}
|