(ctx context.Context, projectID, alertName, ownerID string, emails []string, userAttributes map[string]any)
| 695 | } |
| 696 | |
| 697 | func (s *Server) createMagicTokensAlert(ctx context.Context, projectID, alertName, ownerID string, emails []string, userAttributes map[string]any) (map[string]string, error) { |
| 698 | var createdByUserID *string |
| 699 | if ownerID != "" { |
| 700 | createdByUserID = &ownerID |
| 701 | } |
| 702 | ttl := 3 * 30 * 24 * time.Hour // approx 3 months |
| 703 | mgcOpts := &admin.IssueMagicAuthTokenOptions{ |
| 704 | ProjectID: projectID, |
| 705 | CreatedByUserID: createdByUserID, |
| 706 | Resources: []database.ResourceName{{ |
| 707 | Type: runtime.ResourceKindAlert, |
| 708 | Name: alertName, |
| 709 | }}, |
| 710 | Internal: true, |
| 711 | TTL: &ttl, |
| 712 | } |
| 713 | |
| 714 | // Use the passed user attributes if available |
| 715 | if userAttributes != nil { |
| 716 | mgcOpts.Attributes = userAttributes |
| 717 | } |
| 718 | |
| 719 | // issue magic tokens for new external emails |
| 720 | cctx, tx, err := s.admin.DB.NewTx(ctx, false) |
| 721 | if err != nil { |
| 722 | return nil, fmt.Errorf("failed to start transaction: %w", err) |
| 723 | } |
| 724 | defer func() { _ = tx.Rollback() }() |
| 725 | |
| 726 | emailTokens := make(map[string]string) |
| 727 | for _, email := range emails { |
| 728 | // If no user attributes were passed, create basic attributes for the email |
| 729 | if userAttributes == nil { |
| 730 | mgcOpts.Attributes = map[string]interface{}{ |
| 731 | "name": "", |
| 732 | "email": email, |
| 733 | "domain": email[strings.LastIndex(email, "@")+1:], |
| 734 | "groups": []string{}, |
| 735 | "admin": false, |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | tkn, err := s.admin.IssueMagicAuthToken(cctx, mgcOpts) |
| 740 | if err != nil { |
| 741 | return nil, fmt.Errorf("failed to issue magic auth token for email %s: %w", email, err) |
| 742 | } |
| 743 | |
| 744 | emailTokens[email] = tkn.Token().String() |
| 745 | |
| 746 | _, err = s.admin.DB.InsertNotificationToken(cctx, &database.InsertNotificationTokenOptions{ |
| 747 | ResourceKind: runtime.ResourceKindAlert, |
| 748 | ResourceName: alertName, |
| 749 | RecipientEmail: email, |
| 750 | MagicAuthTokenID: tkn.Token().ID.String(), |
| 751 | }) |
| 752 | if err != nil { |
| 753 | return nil, fmt.Errorf("failed to insert alert token for email %s: %w", email, err) |
| 754 | } |
no test coverage detected