From b7ed2e811bc400e43accc8fb94235334a2ddc7c9 Mon Sep 17 00:00:00 2001 From: Aviad Lichtenstadt Date: Sun, 15 Dec 2024 10:31:52 +0200 Subject: [PATCH] Invite template id (#484) + test related to https://github.com/descope/etc/issues/8535 --- descope/internal/mgmt/user.go | 9 +++++++++ descope/internal/mgmt/user_test.go | 3 ++- descope/types.go | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/descope/internal/mgmt/user.go b/descope/internal/mgmt/user.go index 5d4809ff..fe8bf50a 100644 --- a/descope/internal/mgmt/user.go +++ b/descope/internal/mgmt/user.go @@ -28,6 +28,7 @@ type createUserRequest struct { roles []string tenants []*descope.AssociatedTenant invite bool + templateID string test bool customAttributes map[string]any verifiedEmail *bool @@ -76,6 +77,10 @@ func (u *user) create(ctx context.Context, loginID, email, phone, displayName, g if loginID == "" { return nil, utils.NewInvalidArgumentError("loginID") } + var templateID string + if options != nil { + templateID = options.TemplateID + } req := makeCreateUserRequest(&createUserRequest{ loginID: loginID, email: email, @@ -88,6 +93,7 @@ func (u *user) create(ctx context.Context, loginID, email, phone, displayName, g roles: roles, tenants: tenants, invite: invite, + templateID: templateID, test: test, customAttributes: customAttributes, verifiedEmail: verifiedEmail, @@ -711,6 +717,9 @@ func (u *user) History(ctx context.Context, userIDs []string) ([]*descope.UserHi func makeCreateUserRequest(createReq *createUserRequest) map[string]any { req := makeUpdateUserRequest(createReq) req["invite"] = createReq.invite + if len(createReq.templateID) > 0 { + req["templateId"] = createReq.templateID + } req["additionalLoginIds"] = createReq.additionalLoginIDs if createReq.test { req["test"] = true diff --git a/descope/internal/mgmt/user_test.go b/descope/internal/mgmt/user_test.go index e0b28508..e73adc26 100644 --- a/descope/internal/mgmt/user_test.go +++ b/descope/internal/mgmt/user_test.go @@ -39,6 +39,7 @@ func TestUserCreateSuccess(t *testing.T) { if i == 2 { assert.True(t, true, req["sendSMS"]) assert.EqualValues(t, false, req["sendMail"]) + assert.EqualValues(t, "tid", req["templateId"]) } else { assert.Nil(t, req["sendSMS"]) assert.Nil(t, req["sendMail"]) @@ -64,7 +65,7 @@ func TestUserCreateSuccess(t *testing.T) { sendSMS := true sendMail := false - res, err = m.User().Invite(context.Background(), "abc", user, &descope.InviteOptions{InviteURL: "https://some.domain.com", SendSMS: &sendSMS, SendMail: &sendMail}) + res, err = m.User().Invite(context.Background(), "abc", user, &descope.InviteOptions{TemplateID: "tid", InviteURL: "https://some.domain.com", SendSMS: &sendSMS, SendMail: &sendMail}) require.NoError(t, err) require.NotNil(t, res) require.Equal(t, "a@b.c", res.Email) diff --git a/descope/types.go b/descope/types.go index f9c08f30..1a2d0e8d 100644 --- a/descope/types.go +++ b/descope/types.go @@ -384,6 +384,7 @@ type InviteOptions struct { SendMail *bool `json:"sendMail,omitempty"` // send invite via mail, default is according to project settings SendSMS *bool `json:"sendSMS,omitempty"` // send invite via text message, default is according to project settings TemplateOptions map[string]string `json:"templateOptions,omitempty"` // for providing messaging template options (templates that are being sent via email / text message) + TemplateID string `json:"-"` } type User struct {