MCPcopy Index your code
hub / github.com/rilldata/rill / CreateOrganizationForUser

Method CreateOrganizationForUser

admin/user.go:320–391  ·  view source on GitHub ↗

CreateOrganizationForUser creates a new organization with the given name and description, and adds the user as an admin.

(ctx context.Context, userID, email, orgName, displayName, description string)

Source from the content-addressed store, hash-verified

318
319// CreateOrganizationForUser creates a new organization with the given name and description, and adds the user as an admin.
320func (s *Service) CreateOrganizationForUser(ctx context.Context, userID, email, orgName, displayName, description string) (*database.Organization, error) {
321 viewerProjectRole, err := s.DB.FindProjectRole(ctx, database.ProjectRoleNameViewer)
322 if err != nil {
323 return nil, fmt.Errorf("failed to find viewer project role: %w", err)
324 }
325
326 txCtx, tx, err := s.DB.NewTx(ctx, false)
327 if err != nil {
328 return nil, err
329 }
330 defer func() { _ = tx.Rollback() }()
331
332 defaultQuotas := s.Biller.DefaultQuotas()
333 org, err := s.DB.InsertOrganization(txCtx, &database.InsertOrganizationOptions{
334 Name: orgName,
335 DisplayName: displayName,
336 Description: description,
337 LogoAssetID: nil,
338 LogoDarkAssetID: nil,
339 FaviconAssetID: nil,
340 ThumbnailAssetID: nil,
341 CustomDomain: "",
342 DefaultProjectRoleID: &viewerProjectRole.ID,
343 QuotaProjects: deref(defaultQuotas.NumProjects, -1),
344 QuotaDeployments: deref(defaultQuotas.NumDeployments, -1),
345 QuotaSlotsTotal: deref(defaultQuotas.NumSlotsTotal, -1),
346 QuotaSlotsPerDeployment: deref(defaultQuotas.NumSlotsPerDeployment, -1),
347 QuotaOutstandingInvites: deref(defaultQuotas.NumOutstandingInvites, -1),
348 QuotaStorageLimitBytesPerDeployment: deref(defaultQuotas.StorageLimitBytesPerDeployment, -1),
349 BillingEmail: email,
350 BillingCustomerID: "", // Populated later
351 PaymentCustomerID: "", // Populated later
352 CreatedByUserID: &userID,
353 })
354 if err != nil {
355 return nil, err
356 }
357
358 err = s.prepareOrganization(txCtx, org.ID, userID)
359 if err != nil {
360 return nil, err
361 }
362
363 err = tx.Commit()
364 if err != nil {
365 return nil, err
366 }
367
368 s.Logger.Info("created org", zap.String("name", orgName), zap.String("user_id", userID), zap.String("user_email", email))
369
370 // raise never subscribed billing issue in sync to prevent race condition where first project is deployed before issue is raised and thus start trial job not submitted
371 if s.Biller.Name() != "noop" {
372 _, err := s.DB.UpsertBillingIssue(ctx, &database.UpsertBillingIssueOptions{
373 OrgID: org.ID,
374 Type: database.BillingIssueTypeNeverSubscribed,
375 Metadata: database.BillingIssueMetadataNeverSubscribed{},
376 EventTime: org.CreatedOn,
377 })

Callers 1

CreateOrganizationMethod · 0.80

Calls 15

prepareOrganizationMethod · 0.95
derefFunction · 0.85
InfoMethod · 0.80
FindProjectRoleMethod · 0.65
ErrorfMethod · 0.65
NewTxMethod · 0.65
RollbackMethod · 0.65
DefaultQuotasMethod · 0.65
InsertOrganizationMethod · 0.65
CommitMethod · 0.65
StringMethod · 0.65
NameMethod · 0.65

Tested by

no test coverage detected