lib/rules/auth0.go

72 lines
1.8 KiB
Go
Raw 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"
2021-01-19 01:50:45 +00:00
httptransport "github.com/go-openapi/runtime/client"
2021-01-18 03:13:54 +00:00
"go.temporal.io/sdk/workflow"
)
// StoreAuth0UserActivity creates a new Auth0 user tied to a User record
2021-01-18 19:46:19 +00:00
func StoreAuth0UserActivity(ctx workflow.Context, w app.UserChannelWrapper) error { //nolint:gocritic // don't care
2021-01-18 03:13:54 +00:00
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},
}
2021-01-19 01:50:45 +00:00
_, err = devopsClient.User.PutUsers(putUserParams,
httptransport.APIKeyAuth(
"X-API-Key",
"header",
w.APIKey,
))
2021-01-18 03:13:54 +00:00
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,
2021-01-19 01:50:45 +00:00
httptransport.APIKeyAuth(
"X-API-Key",
"header",
w.APIKey,
),
2021-01-18 03:13:54 +00:00
)
if err != nil {
return err
}
return nil
}