forked from bai/curriculum-project-hub
fix: bootstrap fresh alpha silo
This commit is contained in:
@@ -49,6 +49,7 @@ export async function bootstrapAlphaSilo(
|
||||
} = {},
|
||||
): Promise<AlphaSiloBootstrapResult> {
|
||||
const normalized = validateInput(input);
|
||||
await removePristineLegacyMigrationOrganization(prisma);
|
||||
const existing = await loadExistingSilo(prisma, normalized.organization.id, normalized.owner.openId);
|
||||
let initialized = false;
|
||||
let ownerUserId: string;
|
||||
@@ -113,6 +114,63 @@ export async function bootstrapAlphaSilo(
|
||||
};
|
||||
}
|
||||
|
||||
const LEGACY_MIGRATION_ORGANIZATION = {
|
||||
id: "org_default",
|
||||
slug: "legacy-default",
|
||||
name: "Legacy Default Organization",
|
||||
inboxId: "folder_inbox_2b99350e0db97ad0cbcb55c20ee8bafa",
|
||||
} as const;
|
||||
|
||||
/**
|
||||
* A fresh database still receives the compatibility Organization inserted by
|
||||
* the tenant-root migration, plus its later default settings and Inbox. An
|
||||
* Alpha Silo bootstrap may replace only that exact, otherwise-unused scaffold.
|
||||
* Any tenant data or shape drift remains a hard manual-repair error.
|
||||
*/
|
||||
async function removePristineLegacyMigrationOrganization(prisma: PrismaClient): Promise<void> {
|
||||
await prisma.$transaction(async (tx) => {
|
||||
const organization = await tx.organization.findUnique({
|
||||
where: { id: LEGACY_MIGRATION_ORGANIZATION.id },
|
||||
include: {
|
||||
memberships: { take: 1, select: { id: true } },
|
||||
projects: { take: 1, select: { id: true } },
|
||||
teams: { take: 1, select: { id: true } },
|
||||
externalDirectoryConnections: { take: 1, select: { id: true } },
|
||||
providerConnections: { take: 1, select: { id: true } },
|
||||
feishuApplicationConnection: { select: { id: true } },
|
||||
auditEntries: { take: 1, select: { id: true } },
|
||||
projectSettings: true,
|
||||
folders: {
|
||||
take: 2,
|
||||
select: { id: true, parentId: true, name: true, sortKey: true, archivedAt: true },
|
||||
},
|
||||
},
|
||||
});
|
||||
if (organization === null) return;
|
||||
const pristine =
|
||||
organization.slug === LEGACY_MIGRATION_ORGANIZATION.slug &&
|
||||
organization.name === LEGACY_MIGRATION_ORGANIZATION.name &&
|
||||
organization.status === "ACTIVE" &&
|
||||
organization.memberships.length === 0 &&
|
||||
organization.projects.length === 0 &&
|
||||
organization.teams.length === 0 &&
|
||||
organization.externalDirectoryConnections.length === 0 &&
|
||||
organization.providerConnections.length === 0 &&
|
||||
organization.feishuApplicationConnection === null &&
|
||||
organization.auditEntries.length === 0 &&
|
||||
organization.projectSettings?.membersCanCreateProjects === true &&
|
||||
organization.folders.length === 1 &&
|
||||
organization.folders[0]?.id === LEGACY_MIGRATION_ORGANIZATION.inboxId &&
|
||||
organization.folders[0].parentId === null &&
|
||||
organization.folders[0].name === "Inbox" &&
|
||||
organization.folders[0].sortKey === "000000" &&
|
||||
organization.folders[0].archivedAt === null;
|
||||
if (pristine) {
|
||||
await tx.organization.delete({ where: { id: organization.id } });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureBootstrapTeams(
|
||||
prisma: PrismaClient,
|
||||
organizationId: string,
|
||||
|
||||
Reference in New Issue
Block a user