55 "log/slog"
66 "net/http"
77 "context"
8- "fmt"
98
109 "github.com/diggerhq/digger/opentaco/internal/domain"
1110 "github.com/diggerhq/digger/opentaco/internal/rbac"
@@ -96,40 +95,17 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
9695 )
9796
9897 // ========================================
99- // Use transaction to create org + init RBAC atomically
98+ // Create org first, then init RBAC (SQLite-friendly)
10099 // ========================================
101100 var org * domain.Organization
102101
102+ // Create organization in transaction
103103 err := h .orgRepo .WithTransaction (ctx , func (ctx context.Context , txRepo domain.OrganizationRepository ) error {
104- // Create organization within transaction
105104 createdOrg , err := txRepo .Create (ctx , req .OrgID , req .Name , userIDStr )
106105 if err != nil {
107106 return err
108107 }
109108 org = createdOrg
110-
111- // Initialize RBAC within the same transaction
112- if h .rbacManager != nil {
113- slog .Info ("Initializing RBAC for new organization" ,
114- "orgID" , req .OrgID ,
115- "adminUser" , userIDStr ,
116- )
117-
118- if err := h .rbacManager .InitializeRBAC (ctx , userIDStr , emailStr ); err != nil {
119- // IMPORTANT: Returning error here will rollback the entire transaction
120- slog .Error ("Failed to initialize RBAC, rolling back org creation" ,
121- "orgID" , req .OrgID ,
122- "error" , err ,
123- )
124- return fmt .Errorf ("failed to initialize RBAC: %w" , err )
125- }
126-
127- slog .Info ("RBAC initialized successfully" ,
128- "orgID" , req .OrgID ,
129- "adminUser" , userIDStr ,
130- )
131- }
132-
133109 return nil
134110 })
135111
@@ -146,8 +122,7 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
146122 })
147123 }
148124
149- // Any other error (including RBAC init failure) returns 500
150- slog .Error ("Failed to create organization with RBAC" ,
125+ slog .Error ("Failed to create organization" ,
151126 "orgID" , req .OrgID ,
152127 "error" , err ,
153128 )
@@ -157,7 +132,31 @@ func (h *OrgHandler) CreateOrganization(c echo.Context) error {
157132 })
158133 }
159134
160- // Success - both org and RBAC were created
135+ // Initialize RBAC after org creation (outside transaction for SQLite compatibility)
136+ if h .rbacManager != nil {
137+ slog .Info ("Initializing RBAC for new organization" ,
138+ "orgID" , req .OrgID ,
139+ "adminUser" , userIDStr ,
140+ )
141+
142+ if err := h .rbacManager .InitializeRBAC (ctx , userIDStr , emailStr ); err != nil {
143+ // Org was created but RBAC failed - log warning but don't fail the request
144+ // User can retry RBAC initialization or assign roles manually
145+ slog .Warn ("Organization created but RBAC initialization failed" ,
146+ "orgID" , req .OrgID ,
147+ "error" , err ,
148+ "recommendation" , "RBAC can be initialized later via /rbac/init endpoint" ,
149+ )
150+ // Continue with success response - org was created
151+ } else {
152+ slog .Info ("RBAC initialized successfully" ,
153+ "orgID" , req .OrgID ,
154+ "adminUser" , userIDStr ,
155+ )
156+ }
157+ }
158+
159+ // Success - org created (and RBAC initialized if available)
161160 return c .JSON (http .StatusCreated , CreateOrgResponse {
162161 OrgID : org .OrgID ,
163162 Name : org .Name ,
0 commit comments