@@ -8,83 +8,86 @@ import { authActionClient } from "../safe-action";
88import { organizationSchema } from "../schema" ;
99import { createStripeCustomer } from "./lib/create-stripe-customer" ;
1010import { initializeOrganization } from "./lib/initialize-organization" ;
11+ import { generateAgentFile } from "@/jobs/tasks/device/generate-agent-file" ;
1112
1213export const createOrganizationAction = authActionClient
13- . schema ( organizationSchema )
14- . metadata ( {
15- name : "create-organization" ,
16- track : {
17- event : "create-organization" ,
18- channel : "server" ,
19- } ,
20- } )
21- . action ( async ( { parsedInput, ctx } ) => {
22- const { frameworkIds } = parsedInput ;
23-
24- try {
25- const session = await auth . api . getSession ( {
26- headers : await headers ( ) ,
27- } ) ;
28-
29- if ( ! session ?. session . activeOrganizationId ) {
30- throw new Error ( "User is not part of an organization" ) ;
31- }
32-
33- await db . onboarding . create ( {
34- data : {
35- organizationId : session . session . activeOrganizationId ,
36- completed : false ,
37- } ,
38- } ) ;
39-
40- const organizationId = session . session . activeOrganizationId ;
41-
42- const stripeCustomerId = await createStripeCustomer ( {
43- name : "My Organization" ,
44- email : session . user . email ,
45- organizationId,
46- } ) ;
47-
48- if ( ! stripeCustomerId ) {
49- throw new Error ( "Failed to create Stripe customer" ) ;
50- }
51-
52- await db . organization . update ( {
53- where : { id : organizationId } ,
54- data : { stripeCustomerId } ,
55- } ) ;
56-
57- await initializeOrganization ( { frameworkIds, organizationId } ) ;
58-
59- await auth . api . setActiveOrganization ( {
60- headers : await headers ( ) ,
61- body : {
62- organizationId,
63- } ,
64- } ) ;
65-
66- const userOrgs = await db . member . findMany ( {
67- where : {
68- userId : session . user . id ,
69- } ,
70- select : {
71- organizationId : true ,
72- } ,
73- } ) ;
74-
75- for ( const org of userOrgs ) {
76- revalidatePath ( `/${ org . organizationId } ` ) ;
77- }
78-
79- return {
80- success : true ,
81- organizationId,
82- } ;
83- } catch ( error ) {
84- console . error ( "Error during organization creation/update:" , error ) ;
85-
86- throw new Error (
87- "Failed to create or update organization structure" ,
88- ) ;
89- }
90- } ) ;
14+ . schema ( organizationSchema )
15+ . metadata ( {
16+ name : "create-organization" ,
17+ track : {
18+ event : "create-organization" ,
19+ channel : "server" ,
20+ } ,
21+ } )
22+ . action ( async ( { parsedInput, ctx } ) => {
23+ const { frameworkIds } = parsedInput ;
24+
25+ try {
26+ const session = await auth . api . getSession ( {
27+ headers : await headers ( ) ,
28+ } ) ;
29+
30+ if ( ! session ?. session . activeOrganizationId ) {
31+ throw new Error ( "User is not part of an organization" ) ;
32+ }
33+
34+ await db . onboarding . create ( {
35+ data : {
36+ organizationId : session . session . activeOrganizationId ,
37+ completed : false ,
38+ } ,
39+ } ) ;
40+
41+ const organizationId = session . session . activeOrganizationId ;
42+
43+ const stripeCustomerId = await createStripeCustomer ( {
44+ name : "My Organization" ,
45+ email : session . user . email ,
46+ organizationId,
47+ } ) ;
48+
49+ if ( ! stripeCustomerId ) {
50+ throw new Error ( "Failed to create Stripe customer" ) ;
51+ }
52+
53+ await db . organization . update ( {
54+ where : { id : organizationId } ,
55+ data : { stripeCustomerId } ,
56+ } ) ;
57+
58+ await initializeOrganization ( { frameworkIds, organizationId } ) ;
59+
60+ await auth . api . setActiveOrganization ( {
61+ headers : await headers ( ) ,
62+ body : {
63+ organizationId,
64+ } ,
65+ } ) ;
66+
67+ const userOrgs = await db . member . findMany ( {
68+ where : {
69+ userId : session . user . id ,
70+ } ,
71+ select : {
72+ organizationId : true ,
73+ } ,
74+ } ) ;
75+
76+ for ( const org of userOrgs ) {
77+ revalidatePath ( `/${ org . organizationId } ` ) ;
78+ }
79+
80+ await generateAgentFile . trigger ( {
81+ organizationId,
82+ } ) ;
83+
84+ return {
85+ success : true ,
86+ organizationId,
87+ } ;
88+ } catch ( error ) {
89+ console . error ( "Error during organization creation/update:" , error ) ;
90+
91+ throw new Error ( "Failed to create or update organization structure" ) ;
92+ }
93+ } ) ;
0 commit comments