(ctx context.Context, projectID, reportName, ownerID, ownerEmail string, emails []string)
| 761 | } |
| 762 | |
| 763 | func (s *Server) createUnsubMagicTokens(ctx context.Context, projectID, reportName, ownerID, ownerEmail string, emails []string) (map[string]string, error) { |
| 764 | var createdByUserID *string |
| 765 | if ownerID != "" { |
| 766 | createdByUserID = &ownerID |
| 767 | } |
| 768 | ttl := 3 * 30 * 24 * time.Hour // approx 3 months |
| 769 | mgcOpts := &admin.IssueMagicAuthTokenOptions{ |
| 770 | ProjectID: projectID, |
| 771 | CreatedByUserID: createdByUserID, |
| 772 | Internal: true, |
| 773 | TTL: &ttl, |
| 774 | } |
| 775 | |
| 776 | // issue magic tokens |
| 777 | cctx, tx, err := s.admin.DB.NewTx(ctx, false) |
| 778 | if err != nil { |
| 779 | return nil, fmt.Errorf("failed to start transaction: %w", err) |
| 780 | } |
| 781 | defer func() { _ = tx.Rollback() }() |
| 782 | |
| 783 | emailTokens := make(map[string]string) |
| 784 | for _, email := range emails { |
| 785 | if ownerEmail != "" && strings.EqualFold(ownerEmail, email) { |
| 786 | // skip creating unsubscribe token for owner email |
| 787 | continue |
| 788 | } |
| 789 | // set user attrs as per the email |
| 790 | mgcOpts.Attributes = map[string]interface{}{ |
| 791 | "name": "", |
| 792 | "email": email, |
| 793 | "domain": email[strings.LastIndex(email, "@")+1:], |
| 794 | "groups": []string{}, |
| 795 | "admin": false, |
| 796 | } |
| 797 | |
| 798 | tkn, err := s.admin.IssueMagicAuthToken(cctx, mgcOpts) |
| 799 | if err != nil { |
| 800 | return nil, fmt.Errorf("failed to issue magic auth token for email %s: %w", email, err) |
| 801 | } |
| 802 | |
| 803 | emailTokens[email] = tkn.Token().String() |
| 804 | |
| 805 | _, err = s.admin.DB.InsertNotificationToken(cctx, &database.InsertNotificationTokenOptions{ |
| 806 | ResourceKind: runtime.ResourceKindReport, |
| 807 | ResourceName: reportName, |
| 808 | RecipientEmail: email, |
| 809 | MagicAuthTokenID: tkn.Token().ID.String(), |
| 810 | }) |
| 811 | if err != nil { |
| 812 | return nil, fmt.Errorf("failed to insert report token for email %s: %w", email, err) |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | err = tx.Commit() |
| 817 | if err != nil { |
| 818 | return nil, fmt.Errorf("failed to commit transaction: %w", err) |
| 819 | } |
| 820 |
no test coverage detected