11"use server" ;
22
3- import { performance } from "node:perf_hooks " ;
3+ import type { newOrgSequence } from "@/jobs/tasks/marketing/new-org-sequence " ;
44import { auth } from "@/utils/auth" ;
55import { db } from "@comp/db" ;
6+ import { tasks } from "@trigger.dev/sdk/v3" ;
67import { revalidatePath } from "next/cache" ;
78import { headers } from "next/headers" ;
9+ import { performance } from "node:perf_hooks" ;
810import { Resend } from "resend" ;
911import { authActionClient } from "../safe-action" ;
1012import { organizationSchema } from "../schema" ;
1113import { createStripeCustomer } from "./lib/create-stripe-customer" ;
12- import {
13- createControlArtifacts ,
14- createFrameworkInstance ,
15- createOrganizationPolicies ,
16- createOrganizationTasks ,
17- getRelevantControls ,
18- } from "./lib/utils" ;
19- import { env } from "@/env.mjs" ;
20- import ky from "ky" ;
21- import { tasks } from "@trigger.dev/sdk/v3" ;
22- import type { newOrgSequence } from "@/jobs/tasks/marketing/new-org-sequence" ;
14+ import { initializeOrganization } from "./lib/initialize-organization" ;
2315
2416export const createOrganizationAction = authActionClient
2517 . schema ( organizationSchema )
@@ -31,7 +23,7 @@ export const createOrganizationAction = authActionClient
3123 } ,
3224 } )
3325 . action ( async ( { parsedInput, ctx } ) => {
34- const { name, frameworks , website } = parsedInput ;
26+ const { name, frameworkIds , website } = parsedInput ;
3527 const { id : userId } = ctx . user ;
3628
3729 if ( ! name ) {
@@ -109,109 +101,25 @@ export const createOrganizationAction = authActionClient
109101 }
110102
111103 start = performance . now ( ) ;
104+
105+ // Update stripe ID and website
112106 await db . organization . update ( {
113107 where : { id : organizationId } ,
114108 data : { stripeCustomerId, website } ,
115109 } ) ;
110+
111+
116112 timings . updateOrganizationWithStripeId =
117113 ( performance . now ( ) - start ) / 1000 ;
118114
119115 // --- Main Creation Logic (Inside Transaction) ---
120116 const transactionStart = performance . now ( ) ;
121- const result = await db . $transaction (
122- async ( tx ) => {
123- // REVISIT: Consider if more granular error handling/logging is needed within the transaction
124117
125- start = performance . now ( ) ;
126- const relevantControls = getRelevantControls ( frameworks ) ;
127- const getRelevantControlsTime =
128- ( performance . now ( ) - start ) / 1000 ;
129118
130- start = performance . now ( ) ;
131- // Pass the transaction client `tx` to the helper
132- const organizationFrameworks = await Promise . all (
133- frameworks . map (
134- ( frameworkId ) =>
135- createFrameworkInstance (
136- organizationId ,
137- frameworkId ,
138- tx ,
139- ) , // Pass tx
140- ) ,
141- ) ;
142- const createFrameworkInstancesTime =
143- ( performance . now ( ) - start ) / 1000 ;
119+ // Initialize Organization
120+ await initializeOrganization ( { frameworkIds, organizationId} ) ;
144121
145- // Fetch DB controls needed for Task creation
146- const dbControlsList = await tx . control . findMany ( {
147- where : {
148- organizationId,
149- name : { in : relevantControls . map ( ( c ) => c . name ) } ,
150- } ,
151- select : { id : true , name : true } ,
152- } ) ;
153- const dbControlsMap = new Map < string , { id : string } > ( ) ;
154- for ( const control of dbControlsList ) {
155- dbControlsMap . set ( control . name , control ) ;
156- }
157-
158- // Run policy and task creation in parallel
159- start = performance . now ( ) ;
160- const [ policiesForFrameworks , tasksCreationResult ] =
161- await Promise . all ( [
162- createOrganizationPolicies (
163- organizationId ,
164- relevantControls ,
165- userId ,
166- tx ,
167- ) , // Pass tx
168- createOrganizationTasks (
169- organizationId ,
170- relevantControls ,
171- dbControlsMap , // Pass the map
172- userId ,
173- tx ,
174- ) ,
175- ] ) ;
176- const createPoliciesAndTasksParallelTime =
177- ( performance . now ( ) - start ) / 1000 ;
178-
179- start = performance . now ( ) ;
180- // Pass the transaction client `tx` to the helper
181- await createControlArtifacts (
182- organizationId ,
183- organizationFrameworks . map ( ( framework ) => framework . id ) ,
184- relevantControls ,
185- policiesForFrameworks ,
186- tx , // Pass tx
187- ) ;
188- const createControlArtifactsTime =
189- ( performance . now ( ) - start ) / 1000 ;
190-
191- // Return timings calculated inside the transaction scope
192- return {
193- getRelevantControlsTime,
194- createFrameworkInstancesTime,
195- createPoliciesAndTasksParallelTime,
196- createControlArtifactsTime,
197- organizationFrameworks, // Need this for the final return value potentially
198- } ;
199- } ,
200- {
201- maxWait : 15000 ,
202- timeout : 40000 ,
203- } ,
204- ) ;
205122 timings . transaction = ( performance . now ( ) - transactionStart ) / 1000 ;
206-
207- // Assign timings from the transaction result
208- timings . getRelevantControls = result . getRelevantControlsTime ;
209- timings . createFrameworkInstances =
210- result . createFrameworkInstancesTime ;
211- timings . createPoliciesAndTasksParallel =
212- result . createPoliciesAndTasksParallelTime ;
213- timings . createControlArtifacts = result . createControlArtifactsTime ;
214-
215123 timings . total = ( performance . now ( ) - totalStart ) / 1000 ;
216124 console . log ( "createOrganizationAction timings (s):" , timings ) ;
217125 console . warn (
0 commit comments