lib/rules/auth0.go

62 lines
1.6 KiB
Go
Raw Permalink Normal View History

2021-01-18 03:13:54 +00:00
package rules
import (
"fmt"
"code.tnxs.net/taxnexus/lib/api/devops/devops_client/user"
"code.tnxs.net/taxnexus/lib/api/devops/devops_models"
"code.tnxs.net/taxnexus/lib/api/workflow/workflow_client/outgoing_email_message"
"code.tnxs.net/taxnexus/lib/api/workflow/workflow_models"
"code.tnxs.net/taxnexus/lib/app"
"go.temporal.io/sdk/workflow"
)
// StoreAuth0UserActivity creates a new Auth0 user tied to a User record
func StoreAuth0UserActivity(ctx workflow.Context, w app.UserChannelWrapper) error {
auth0UserID, err := app.GetAuth0UserByEmail(w.Obj.Email)
if err != nil {
return err
}
if auth0UserID != "not found" {
return fmt.Errorf("rules.StoreAuth0UserActivity: 💣 ⛔ Auth0 user exists: %s", auth0UserID)
}
newAuth0UserID, err := app.CreateNewAuth0User(&app.NewAuth0User{})
if err != nil {
return err
}
putUser := w.Obj
putUser.Auth0UserID = newAuth0UserID
//
// update Taxnexus user
//
putUserParams := user.NewPutUsersParamsWithTimeout(getTimeout)
putUserParams.UserRequest = &devops_models.UserRequest{
Data: []*devops_models.User{&putUser},
}
_, err = devopsClient.User.PutUsers(putUserParams, w.Principal.Auth)
if err != nil {
return err
}
//
// send emails
//
emailParams := outgoing_email_message.NewPostOutgoingEmailMessagesParamsWithTimeout(postTimeout)
emailParams.OutgoingEmailMessageRequest = &workflow_models.OutgoingEmailMessageRequest{
Data: []*workflow_models.OutgoingEmailMessage{
{
Subject: putUser.ID,
},
},
}
_, err = workflowClient.
OutgoingEmailMessage.
PostOutgoingEmailMessages(
emailParams,
w.Principal.Auth,
)
if err != nil {
return err
}
return nil
}