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)
| 318 | |
| 319 | // CreateOrganizationForUser creates a new organization with the given name and description, and adds the user as an admin. |
| 320 | func (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 | }) |
no test coverage detected