(ctx context.Context, req *adminv1.SudoExtendTrialRequest)
| 661 | } |
| 662 | |
| 663 | func (s *Server) SudoExtendTrial(ctx context.Context, req *adminv1.SudoExtendTrialRequest) (*adminv1.SudoExtendTrialResponse, error) { |
| 664 | observability.AddRequestAttributes(ctx, attribute.String("args.org", req.Org)) |
| 665 | days := int(req.Days) |
| 666 | observability.AddRequestAttributes(ctx, attribute.Int("args.days", days)) |
| 667 | |
| 668 | claims := auth.GetClaims(ctx) |
| 669 | if !claims.Superuser(ctx) { |
| 670 | return nil, status.Error(codes.PermissionDenied, "only superusers can extend trial") |
| 671 | } |
| 672 | |
| 673 | org, err := s.admin.DB.FindOrganizationByName(ctx, req.Org) |
| 674 | if err != nil { |
| 675 | return nil, err |
| 676 | } |
| 677 | |
| 678 | ns, err := s.admin.DB.FindBillingIssueByTypeForOrg(ctx, org.ID, database.BillingIssueTypeNeverSubscribed) |
| 679 | if err != nil && !errors.Is(err, database.ErrNotFound) { |
| 680 | return nil, err |
| 681 | } |
| 682 | if ns != nil { |
| 683 | return nil, status.Errorf(codes.FailedPrecondition, "organization %s never subscribed to a plan", org.Name) |
| 684 | } |
| 685 | |
| 686 | // find existing trial end date |
| 687 | currentEndDate := time.Time{} |
| 688 | onTrial, err := s.admin.DB.FindBillingIssueByTypeForOrg(ctx, org.ID, database.BillingIssueTypeOnTrial) |
| 689 | if err != nil && !errors.Is(err, database.ErrNotFound) { |
| 690 | return nil, err |
| 691 | } |
| 692 | if onTrial != nil { |
| 693 | currentEndDate = onTrial.Metadata.(*database.BillingIssueMetadataOnTrial).GracePeriodEndDate |
| 694 | } |
| 695 | |
| 696 | if currentEndDate.IsZero() { |
| 697 | trialEnded, err := s.admin.DB.FindBillingIssueByTypeForOrg(ctx, org.ID, database.BillingIssueTypeTrialEnded) |
| 698 | if err != nil && !errors.Is(err, database.ErrNotFound) { |
| 699 | return nil, err |
| 700 | } |
| 701 | if trialEnded != nil { |
| 702 | currentEndDate = trialEnded.Metadata.(*database.BillingIssueMetadataTrialEnded).GracePeriodEndDate |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if currentEndDate.IsZero() { |
| 707 | subCancelled, err := s.admin.DB.FindBillingIssueByTypeForOrg(ctx, org.ID, database.BillingIssueTypeSubscriptionCancelled) |
| 708 | if err != nil && !errors.Is(err, database.ErrNotFound) { |
| 709 | return nil, err |
| 710 | } |
| 711 | if subCancelled != nil { |
| 712 | currentEndDate = subCancelled.Metadata.(*database.BillingIssueMetadataSubscriptionCancelled).EndDate |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | if currentEndDate.IsZero() || currentEndDate.Before(time.Now()) { |
| 717 | currentEndDate = time.Now().Truncate(24*time.Hour).AddDate(0, 0, 1) |
| 718 | } |
| 719 | |
| 720 | newEndDate := currentEndDate.AddDate(0, 0, days) |
nothing calls this directly
no test coverage detected