Skip to content

Commit e0c2b72

Browse files
authored
Merge pull request #673 from trycompai/claudio/comp-131-enable-diffingadding-additional-framework-to-existing-org
[dev] [claudfuen] claudio/comp-131-enable-diffingadding-additional-framework-to-existing-org
2 parents ab32bcd + 19e591e commit e0c2b72

File tree

10 files changed

+698
-383
lines changed

10 files changed

+698
-383
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"use server";
2+
3+
import { db } from "@comp/db";
4+
import { Prisma } from "@comp/db/types";
5+
import { revalidatePath } from "next/cache";
6+
import { z } from "zod";
7+
import { addFrameworksSchema } from "@/actions/schema";
8+
import { _upsertOrgFrameworkStructureCore, UpsertOrgFrameworkStructureCoreInput } from "./lib/initialize-organization";
9+
10+
// Duplicating the InitializeOrganizationInput type for clarity, can be shared if preferred
11+
export type AddFrameworksInput = z.infer<typeof addFrameworksSchema>;
12+
13+
/**
14+
* Adds specified frameworks and their related entities (controls, policies, tasks)
15+
* to an existing organization. It ensures that entities are not duplicated if they
16+
* already exist (e.g., from a shared template or a previous addition).
17+
*/
18+
export const addFrameworksToOrganizationAction = async (
19+
input: AddFrameworksInput,
20+
): Promise<{ success: boolean; error?: string }> => {
21+
try {
22+
const validatedInput = addFrameworksSchema.parse(input);
23+
const { frameworkIds, organizationId } = validatedInput;
24+
25+
await db.$transaction(async (tx) => {
26+
// 1. Fetch FrameworkEditorFrameworks and their requirements for the given frameworkIds, filtering by visible: true
27+
const frameworksAndRequirements = await tx.frameworkEditorFramework.findMany({
28+
where: {
29+
id: { in: frameworkIds },
30+
visible: true,
31+
},
32+
include: {
33+
requirements: true,
34+
},
35+
});
36+
37+
if (frameworksAndRequirements.length === 0) {
38+
throw new Error("No valid or visible frameworks found for the provided IDs.");
39+
}
40+
41+
const finalFrameworkEditorIds = frameworksAndRequirements.map(f => f.id);
42+
43+
// 2. Call the renamed core function
44+
await _upsertOrgFrameworkStructureCore({
45+
organizationId,
46+
targetFrameworkEditorIds: finalFrameworkEditorIds,
47+
frameworkEditorFrameworks: frameworksAndRequirements,
48+
tx: tx as unknown as Prisma.TransactionClient // Use the transaction client from this action
49+
});
50+
51+
// The rest of the logic (creating instances, relations) is now inside _upsertOrgFrameworkStructureCore
52+
});
53+
54+
revalidatePath("/"); // Revalidate all paths, or be more specific e.g. /${organizationId}/frameworks
55+
return { success: true };
56+
57+
} catch (error) {
58+
console.error("Error in addFrameworksToOrganizationAction:", error);
59+
if (error instanceof z.ZodError) {
60+
return { success: false, error: error.errors.map(e => e.message).join(", ") };
61+
} else if (error instanceof Error) {
62+
return { success: false, error: error.message };
63+
}
64+
return { success: false, error: "An unexpected error occurred." };
65+
}
66+
};

0 commit comments

Comments
 (0)