From 37245451a3c7643ce3a557cdf0f94a6ac53b885e Mon Sep 17 00:00:00 2001 From: Vernon Keenan Date: Sun, 17 Jan 2021 19:13:54 -0800 Subject: [PATCH] docs, fix empty --- app/account-helpers.go | 4 +- app/account.go | 3 - app/accountingrule-helpers.go | 3 + app/accountingruleset-helpers.go | 3 + app/applog-helpers.go | 8 +- app/auth0.go | 67 ++++++++++++++++ app/authority-helpers.go | 3 + app/contact-helpers.go | 3 + app/country-helpers.go | 3 + app/county-helpers.go | 3 + app/domain-helpers.go | 3 + app/emailmessage-helpers.go | 1 + app/filing-helpers.go | 4 +- app/filingscheduleitem-helpers.go | 1 + app/filingtype-helpers.go | 10 ++- app/filingtypeinstance-helpers.go | 1 + app/folder-helpers.go | 1 + app/glaccount-helpers.go | 1 + app/glbalance-helpers.go | 3 + app/invoice-helpers.go | 1 + app/invoiceitem-helpers.go | 1 + app/journalentry-helpers.go | 4 +- app/journalitem-helpers.go | 3 +- app/lead-helpers.go | 2 + app/license-helpers.go | 3 + app/licensetype-helpers.go | 2 + app/notebook-helpers.go | 7 +- app/order-helpers.go | 5 +- app/orderitem-helpers.go | 2 + app/outgoingemail-helpers.go | 1 + app/outgoingemailreceipt-helpers.go | 1 + app/pdf-helpers.go | 3 +- app/period-helpers.go | 3 + app/place-helpers.go | 3 + app/po-helpers.go | 3 + app/poitem-helpers.go | 3 + app/product-helpers.go | 3 + app/quote-helpers.go | 1 + app/quoteitem-helpers.go | 1 + app/ratingengine-helpers.go | 1 + app/ratingengineitem-helpers.go | 1 + app/root.go | 15 +++- app/state-helpers.go | 2 + app/submission-helpers.go | 2 + app/taxinstance-helpers.go | 2 + app/taxnexuscode-helpers.go | 3 + app/taxtransaction-helpers.go | 1 + app/taxtypeaccount-helpers.go | 3 + app/tenantuser-helpers.go | 3 + app/total-helpers.go | 1 + app/transaction-helpers.go | 3 + app/userauth.go | 31 ++++++++ app/userrole-helpers.go | 1 + rules/account.go | 7 +- rules/auth0.go | 61 +++++++++++++++ rules/new-tenant.go | 1 + rules/new-tenantuser.go | 1 + rules/new-user.go | 46 +++++++++++ rules/root.go | 5 +- rules/user.go | 116 ++++++++++++++++++++++++++++ 60 files changed, 458 insertions(+), 25 deletions(-) create mode 100644 app/userauth.go create mode 100644 rules/auth0.go create mode 100644 rules/new-tenant.go create mode 100644 rules/new-tenantuser.go create mode 100644 rules/new-user.go create mode 100644 rules/user.go diff --git a/app/account-helpers.go b/app/account-helpers.go index a627fa1..138a11a 100644 --- a/app/account-helpers.go +++ b/app/account-helpers.go @@ -117,8 +117,8 @@ func UnMarshalAccount(s *crm_models.Account) *Account { //nolint:funlen // too b } } -// MarshalToSwagger encodes a first class object to swagger -func (obj *Account) MarshalToSwagger() *crm_models.Account { +// Marshal encodes a first class object to swagger +func (obj *Account) Marshal() *crm_models.Account { return &crm_models.Account{ ID: obj.ID, AccountNumber: obj.AccountNumber, diff --git a/app/account.go b/app/account.go index c7e9fbf..60ed664 100644 --- a/app/account.go +++ b/app/account.go @@ -6,9 +6,6 @@ import ( "code.tnxs.net/taxnexus/lib/api/crm/crm_models" ) -// NewAccountActivity is an activity identifier -const NewAccountActivity = "NEW_ACCOUNT_ACTIVITY" - // AccountChannelWrapper wraps the object with workflow params type AccountChannelWrapper struct { Obj crm_models.Account diff --git a/app/accountingrule-helpers.go b/app/accountingrule-helpers.go index 8c7e469..8bfae82 100644 --- a/app/accountingrule-helpers.go +++ b/app/accountingrule-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalAccountingRule decodes swagger to first class object func UnMarshalAccountingRule(s *ledger_models.AccountingRule) *AccountingRule { if s.ID == "" { s.ID = uuid.New().String() @@ -43,6 +44,8 @@ func UnMarshalAccountingRule(s *ledger_models.AccountingRule) *AccountingRule { }, } } + +// MarshalToSwagger encodes first class object func (obj *AccountingRule) MarshalToSwagger() *ledger_models.AccountingRule { return &ledger_models.AccountingRule{ ID: obj.ID, diff --git a/app/accountingruleset-helpers.go b/app/accountingruleset-helpers.go index 1b39160..14ea6cc 100644 --- a/app/accountingruleset-helpers.go +++ b/app/accountingruleset-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalAccountingRuleset decodes swagger to first class object func UnMarshalAccountingRuleset(s *ledger_models.AccountingRuleset) *AccountingRuleset { if s.ID == "" { s.ID = uuid.New().String() @@ -37,6 +38,8 @@ func UnMarshalAccountingRuleset(s *ledger_models.AccountingRuleset) *AccountingR }, } } + +// MarshalToSwagger encodes first class object func (obj *AccountingRuleset) MarshalToSwagger() *ledger_models.AccountingRuleset { theItems := []*ledger_models.AccountingRulesetItem{} for _, itm := range obj.Items { diff --git a/app/applog-helpers.go b/app/applog-helpers.go index 57639aa..798d0d9 100644 --- a/app/applog-helpers.go +++ b/app/applog-helpers.go @@ -8,13 +8,14 @@ import ( "github.com/google/uuid" ) -func UnMarshalAppLog(s *workflow_models.AppLog) *AppLog { +// UnMarshalLog decodes swagger to first class object +func UnMarshalLog(s *workflow_models.AppLog) *Log { if s.ID == "" { s.ID = uuid.New().String() } createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate) sourceTimestamp, e1 := time.Parse(dateTimeFormat, s.SourceTimestamp) - return &AppLog{ + return &Log{ AccountID: s.AccountID, CompanyID: s.CompanyID, CreatedByID: s.CreatedByID, @@ -35,7 +36,8 @@ func UnMarshalAppLog(s *workflow_models.AppLog) *AppLog { } } -func (obj *AppLog) MarshalToSwagger() *workflow_models.AppLog { +// MarshalToSwagger encodes first class object +func (obj *Log) MarshalToSwagger() *workflow_models.AppLog { return &workflow_models.AppLog{ ID: obj.ID, AccountID: obj.AccountID, diff --git a/app/auth0.go b/app/auth0.go index cac5f08..5c4fcf8 100644 --- a/app/auth0.go +++ b/app/auth0.go @@ -7,8 +7,75 @@ import ( "net/http" "strings" "time" + + "code.tnxs.net/taxnexus/lib/api/auth0/auth0_client/user" + "code.tnxs.net/taxnexus/lib/api/auth0/auth0_models" ) +// GetAuth0UserByEmail looks up Auth0 user by email +func GetAuth0UserByEmail(email string) (string, error) { + sugar.Info("app.GetAuth0UserByEmail: 📥") + authToken := GetAuth0AccessToken() + if authToken == "" { + return "", fmt.Errorf("app.GetAuth0UserByEmail: 💣⛔ Error obtaining bearer token") + } + auth0Params := user.NewGetUserByEmailParamsWithTimeout(getTimeout) + auth0Params.Authorization = "Bearer " + authToken + auth0Params.Email = email + auth0Response, err := auth0Client.User.GetUserByEmail(auth0Params) + if err != nil { + return "", err + } + if len(auth0Response.Payload) == 0 { + sugar.Infof("app.GetAuth0UserByEmail: 👍📤 %s not found", email) + return constNotFound, nil + } + sugar.Infof("app.GetAuth0UserByEmail: 👍📤 %s found %s", email, auth0Response.Payload[0].UserID) + return auth0Response.Payload[0].UserID, nil +} + +// CreateNewAuth0User creates a new Auth0 User +func CreateNewAuth0User(newUser *NewAuth0User) (string, error) { + sugar.Info("app.CreateNewAuth0User: 📥") + authToken := GetAuth0AccessToken() + if authToken == "" { + return "", fmt.Errorf("app.CreateNewAuth0User: 💣⛔ Error obtaining bearer token") + } + auth0Params := user.NewPostUsersParamsWithTimeout(postTimeout) + auth0Params.Authorization = "Bearer " + authToken + auth0Params.UserRequest.Users = []*auth0_models.NewUser{ + { + Email: newUser.Email, + Name: newUser.Name, + FamilyName: newUser.FamilyName, + GivenName: newUser.GivenName, + Nickname: newUser.Nickname, + Password: newUser.Password, + PhoneNumber: newUser.PhoneNumber, + Blocked: newUser.Blocked, + Connection: newUser.Connection, + EmailVerified: newUser.EmailVerified, + PhoneVerified: newUser.PhoneVerified, + Picture: newUser.Picture, + UserID: newUser.UserID, + Username: newUser.Username, + VerifyEmail: newUser.VerifyEmail, + }, + } + _, err := auth0Client.User.PostUsers(auth0Params) + if err != nil { + return "", err + } + auth0UserID, err := GetAuth0UserByEmail(newUser.Email) + if err != nil { + return "", err + } + if auth0UserID != constNotFound { + return "", fmt.Errorf("app.CreateNewAuth0User: 💣⛔ Auth0 user exists: %s", auth0UserID) + } + return auth0UserID, nil +} + type auth0TokenResponse struct { AccessToken string TokenType string diff --git a/app/authority-helpers.go b/app/authority-helpers.go index d426ee5..1227e8c 100644 --- a/app/authority-helpers.go +++ b/app/authority-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalSwaggerAuthority decodes swagger to first class object func UnMarshalSwaggerAuthority(s *regs_models.Authority) *Authority { if s.ID == "" { s.ID = uuid.New().String() @@ -71,6 +72,8 @@ func UnMarshalSwaggerAuthority(s *regs_models.Authority) *Authority { }, } } + +// MarshalToSwagger encodes first class object func (obj *Authority) MarshalToSwagger() *regs_models.Authority { return ®s_models.Authority{ ID: obj.ID, diff --git a/app/contact-helpers.go b/app/contact-helpers.go index 19b8780..96648f1 100644 --- a/app/contact-helpers.go +++ b/app/contact-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalContact decodes swagger to first class object func UnMarshalContact(s *crm_models.Contact) *Contact { if s.ID == "" { s.ID = uuid.New().String() @@ -75,6 +76,8 @@ func UnMarshalContact(s *crm_models.Contact) *Contact { }, } } + +// MarshalToSwagger encodes first class object func (obj *Contact) MarshalToSwagger() *crm_models.Contact { return &crm_models.Contact{ ID: obj.ID, diff --git a/app/country-helpers.go b/app/country-helpers.go index d137f00..3b3ea92 100644 --- a/app/country-helpers.go +++ b/app/country-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalCountry decodes swagger to first class object func UnMarshalCountry(s *geo_models.Country) *Country { if s.ID == "" { s.ID = uuid.New().String() @@ -58,6 +59,8 @@ func UnMarshalCountry(s *geo_models.Country) *Country { }, } } + +// MarshalToSwagger encodes first class object func (obj *Country) MarshalToSwagger() *geo_models.Country { taxInstances := []*geo_models.TaxInstance{} for _, itm := range obj.TaxInstances { diff --git a/app/county-helpers.go b/app/county-helpers.go index e0d303d..882044a 100644 --- a/app/county-helpers.go +++ b/app/county-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalCounty decodes swagger to first class object func UnMarshalCounty(s *geo_models.County) *County { if s.ID == "" { s.ID = uuid.New().String() @@ -69,6 +70,8 @@ func UnMarshalCounty(s *geo_models.County) *County { }, } } + +// MarshalToSwagger encodes first class object func (obj *County) MarshalToSwagger() *geo_models.County { theInstances := []*geo_models.TaxInstance{} for _, itm := range obj.TaxInstances { diff --git a/app/domain-helpers.go b/app/domain-helpers.go index 4f8004f..1029240 100644 --- a/app/domain-helpers.go +++ b/app/domain-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/geo/geo_models" +// UnMarshalDomain decodes swagger to first class object func UnMarshalDomain(swag *geo_models.Domain) *Domain { return &Domain{ Description: swag.Description, @@ -10,6 +11,8 @@ func UnMarshalDomain(swag *geo_models.Domain) *Domain { Name: swag.Name, } } + +// MarshalToSwagger encodes first class object func (obj *Domain) MarshalToSwagger() *geo_models.Domain { return &geo_models.Domain{ ID: obj.ID, diff --git a/app/emailmessage-helpers.go b/app/emailmessage-helpers.go index 6d96c8a..47b0198 100644 --- a/app/emailmessage-helpers.go +++ b/app/emailmessage-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/workflow/workflow_models" +// MarshalToSwagger encodes first class object func (obj *EmailMessage) MarshalToSwagger() *workflow_models.EmailMessage { headers := &workflow_models.Headers{} // for _, itm := range obj.Headers { diff --git a/app/filing-helpers.go b/app/filing-helpers.go index a1d0bf4..022e648 100644 --- a/app/filing-helpers.go +++ b/app/filing-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalSwaggerFiling decodes swagger to first class object func UnMarshalSwaggerFiling(s *regs_models.Filing) *Filing { if s.ID == "" { s.ID = uuid.New().String() @@ -78,8 +79,9 @@ func UnMarshalSwaggerFiling(s *regs_models.Filing) *Filing { Valid: e3 == nil, }, } - } + +// MarshalToSwagger encodes first class object func (obj *Filing) MarshalToSwagger() *regs_models.Filing { return ®s_models.Filing{ ID: obj.ID, diff --git a/app/filingscheduleitem-helpers.go b/app/filingscheduleitem-helpers.go index e69de29..4879f7a 100644 --- a/app/filingscheduleitem-helpers.go +++ b/app/filingscheduleitem-helpers.go @@ -0,0 +1 @@ +package app diff --git a/app/filingtype-helpers.go b/app/filingtype-helpers.go index 0a31d3f..0a47074 100644 --- a/app/filingtype-helpers.go +++ b/app/filingtype-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalFilingType decodes swagger to first class object func UnMarshalFilingType(s *regs_models.FilingType) *FilingType { if s.ID == "" { s.ID = uuid.New().String() @@ -30,15 +31,15 @@ func UnMarshalFilingType(s *regs_models.FilingType) *FilingType { } for _, itm := range s.DueDates { dueDates = []*FilingScheduleItem{} - dueDate, e0 := time.Parse(dateTimeFormat, itm.DueDate) - if e0 != nil { - dueDate, e0 = time.Parse(dateFormatAlt, itm.DueDate) + dueDate, e2 := time.Parse(dateTimeFormat, itm.DueDate) + if e2 != nil { + dueDate, e2 = time.Parse(dateFormatAlt, itm.DueDate) } dueDates = append(dueDates, &FilingScheduleItem{ Description: itm.Description, DueDate: sql.NullTime{ Time: dueDate, - Valid: e0 == nil, + Valid: e2 == nil, }, }) } @@ -78,6 +79,7 @@ func UnMarshalFilingType(s *regs_models.FilingType) *FilingType { } } +// MarshalToSwagger encodes first class object func (obj *FilingType) MarshalToSwagger() *regs_models.FilingType { var instances []*regs_models.FilingTypeInstance if obj.Instances != nil { diff --git a/app/filingtypeinstance-helpers.go b/app/filingtypeinstance-helpers.go index e69de29..4879f7a 100644 --- a/app/filingtypeinstance-helpers.go +++ b/app/filingtypeinstance-helpers.go @@ -0,0 +1 @@ +package app diff --git a/app/folder-helpers.go b/app/folder-helpers.go index e69de29..4879f7a 100644 --- a/app/folder-helpers.go +++ b/app/folder-helpers.go @@ -0,0 +1 @@ +package app diff --git a/app/glaccount-helpers.go b/app/glaccount-helpers.go index c6f9651..6e4df53 100644 --- a/app/glaccount-helpers.go +++ b/app/glaccount-helpers.go @@ -45,6 +45,7 @@ func UnMarshalGlAccount(s *ledger_models.GlAccount) *GlAccount { } } +// MarshalToSwagger encodes first class object func (obj *GlAccount) MarshalToSwagger() *ledger_models.GlAccount { return &ledger_models.GlAccount{ ID: obj.ID, diff --git a/app/glbalance-helpers.go b/app/glbalance-helpers.go index 825422c..09d1207 100644 --- a/app/glbalance-helpers.go +++ b/app/glbalance-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalGlBalance decodes swagger to first class object func UnMarshalGlBalance(s *ledger_models.GlBalance) *GlBalance { if s.ID == "" { s.ID = uuid.New().String() @@ -46,6 +47,8 @@ func UnMarshalGlBalance(s *ledger_models.GlBalance) *GlBalance { }, } } + +// MarshalToSwagger encodes first class object func (obj *GlBalance) MarshalToSwagger() *ledger_models.GlBalance { return &ledger_models.GlBalance{ ID: obj.ID, diff --git a/app/invoice-helpers.go b/app/invoice-helpers.go index a83b084..8b927c9 100644 --- a/app/invoice-helpers.go +++ b/app/invoice-helpers.go @@ -4,6 +4,7 @@ import "code.tnxs.net/taxnexus/lib/api/ops/ops_models" // Invoice unMarshal does validation in the Ops microservice only +// MarshalToSwagger encodes first class object func (obj *Invoice) MarshalToSwagger() *ops_models.Invoice { //nolint:funlen // big object var items []*ops_models.InvoiceItem if obj.Items != nil { diff --git a/app/invoiceitem-helpers.go b/app/invoiceitem-helpers.go index f1d6e67..a47dcc4 100644 --- a/app/invoiceitem-helpers.go +++ b/app/invoiceitem-helpers.go @@ -4,6 +4,7 @@ import "code.tnxs.net/taxnexus/lib/api/ops/ops_models" // Invoice unMarshal does validation in the Ops microservice only +// MarshalToSwagger encodes first class object func (obj *InvoiceItem) MarshalToSwagger() *ops_models.InvoiceItem { return &ops_models.InvoiceItem{ ID: obj.ID, diff --git a/app/journalentry-helpers.go b/app/journalentry-helpers.go index 79ab73f..cbd53ef 100644 --- a/app/journalentry-helpers.go +++ b/app/journalentry-helpers.go @@ -9,6 +9,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalJournalEntry decodes swagger to first class object func UnMarshalJournalEntry(s *ledger_models.JournalEntry) *JournalEntry { if s.ID == "" { s.ID = uuid.New().String() @@ -60,6 +61,7 @@ func UnMarshalJournalEntry(s *ledger_models.JournalEntry) *JournalEntry { } } +// MarshalToSwagger encodes first class object func (obj *JournalEntry) MarshalToSwagger() *ledger_models.JournalEntry { swag := &ledger_models.JournalEntry{ ID: obj.ID, @@ -84,7 +86,7 @@ func (obj *JournalEntry) MarshalToSwagger() *ledger_models.JournalEntry { TenantID: obj.TenantID, } for _, itm := range obj.Items { - swag.Items = append(swag.Items, itm.MarshalToSwgger()) + swag.Items = append(swag.Items, itm.MarshalToSwagger()) } return swag } diff --git a/app/journalitem-helpers.go b/app/journalitem-helpers.go index 29b0294..a04c98c 100644 --- a/app/journalitem-helpers.go +++ b/app/journalitem-helpers.go @@ -45,7 +45,8 @@ func UnMarshalJournalItem(s *ledger_models.JournalItem) *JournalItem { } } -func (obj *JournalItem) MarshalToSwgger() *ledger_models.JournalItem { +// MarshalToSwagger encodes first class object +func (obj *JournalItem) MarshalToSwagger() *ledger_models.JournalItem { return &ledger_models.JournalItem{ ID: obj.ID, CreatedByID: obj.CreatedByID, diff --git a/app/lead-helpers.go b/app/lead-helpers.go index ccc0303..8ac9831 100644 --- a/app/lead-helpers.go +++ b/app/lead-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalLead decodes swagger to first class object func UnMarshalLead(s *crm_models.Lead) *Lead { if s.ID == "" { s.ID = uuid.New().String() @@ -52,6 +53,7 @@ func UnMarshalLead(s *crm_models.Lead) *Lead { } } +// MarshalToSwagger encodes first class object func (obj *Lead) MarshalToSwagger() *crm_models.Lead { return &crm_models.Lead{ ID: obj.ID, diff --git a/app/license-helpers.go b/app/license-helpers.go index 6b8c9f9..9895f13 100644 --- a/app/license-helpers.go +++ b/app/license-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalLicense decodes swagger to first class object func UnMarshalLicense(s *regs_models.License) *License { if s.ID == "" { s.ID = uuid.New().String() @@ -57,6 +58,8 @@ func UnMarshalLicense(s *regs_models.License) *License { }, } } + +// MarshalToSwagger encodes first class object func (obj *License) MarshalToSwagger() *regs_models.License { return ®s_models.License{ ID: obj.ID, diff --git a/app/licensetype-helpers.go b/app/licensetype-helpers.go index cfc02fe..46f96aa 100644 --- a/app/licensetype-helpers.go +++ b/app/licensetype-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalLicenseType decodes swagger to first class object func UnMarshalLicenseType(s *regs_models.LicenseType) *LicenseType { if s.ID == "" { s.ID = uuid.New().String() @@ -57,6 +58,7 @@ func UnMarshalLicenseType(s *regs_models.LicenseType) *LicenseType { } } +// MarshalToSwagger encodes first class object func (obj *LicenseType) MarshalToSwagger() *regs_models.LicenseType { var jurisdictions []*regs_models.GeoLicenseTypeInstance if obj.Jurisdictions != nil { diff --git a/app/notebook-helpers.go b/app/notebook-helpers.go index 9f952cb..987bc7f 100644 --- a/app/notebook-helpers.go +++ b/app/notebook-helpers.go @@ -7,6 +7,7 @@ import ( "code.tnxs.net/taxnexus/lib/api/regs/regs_models" ) +// UnMarshalNotebook decodes swagger to first class object func UnMarshalNotebook(s *regs_models.Notebook) *Notebook { createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate) lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate) @@ -23,7 +24,6 @@ func UnMarshalNotebook(s *regs_models.Notebook) *Notebook { Title: itm.Title, NotebookID: itm.NotebookID, }) - } } return &Notebook{ @@ -61,7 +61,9 @@ func UnMarshalNotebook(s *regs_models.Notebook) *Notebook { }, } } -func (obj Notebook) MarshalToSwagger() *regs_models.Notebook { + +// MarshalToSwagger encodes first class object +func (obj *Notebook) MarshalToSwagger() *regs_models.Notebook { var items []*regs_models.NotebookItem if obj.Items != nil { items = []*regs_models.NotebookItem{} @@ -72,7 +74,6 @@ func (obj Notebook) MarshalToSwagger() *regs_models.Notebook { Title: itm.Title, NotebookID: itm.NotebookID, }) - } } return ®s_models.Notebook{ diff --git a/app/order-helpers.go b/app/order-helpers.go index 2fd2981..b3e36ac 100644 --- a/app/order-helpers.go +++ b/app/order-helpers.go @@ -8,13 +8,14 @@ import ( "github.com/google/uuid" ) +// UnMarshalOrder decodes swagger to first class object func UnMarshalOrder(s *ops_models.Order) *Order { //nolint:funlen // big object if s.ID == "" { s.ID = uuid.New().String() } var items []*OrderItem if s.Items != nil { - items = make([]*OrderItem, 5) + items = []*OrderItem{} for _, itm := range s.Items { items = append(items, UnMarshalOrderItem(itm)) } @@ -137,6 +138,8 @@ func UnMarshalOrder(s *ops_models.Order) *Order { //nolint:funlen // big object }, } } + +// MarshalToSwagger encodes first class object func (obj *Order) MarshalToSwagger() *ops_models.Order { var items []*ops_models.OrderItem if obj.Items != nil { diff --git a/app/orderitem-helpers.go b/app/orderitem-helpers.go index ddf070a..9ab37b7 100644 --- a/app/orderitem-helpers.go +++ b/app/orderitem-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalOrderItem decodes swagger to first class object func UnMarshalOrderItem(s *ops_models.OrderItem) *OrderItem { if s.ID == "" { s.ID = uuid.New().String() @@ -82,6 +83,7 @@ func UnMarshalOrderItem(s *ops_models.OrderItem) *OrderItem { } } +// MarshalToSwagger encodes first class object func (obj *OrderItem) MarshalToSwagger() *ops_models.OrderItem { return &ops_models.OrderItem{ ID: obj.ID, diff --git a/app/outgoingemail-helpers.go b/app/outgoingemail-helpers.go index 8880f35..8d34025 100644 --- a/app/outgoingemail-helpers.go +++ b/app/outgoingemail-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/workflow/workflow_models" +// UnMarshalSwaggerOutgoingEmailMessage decodes swagger to first class object func UnMarshalSwaggerOutgoingEmailMessage(s *workflow_models.OutgoingEmailMessage) *OutgoingEmailMessage { return &OutgoingEmailMessage{ ID: s.ID, diff --git a/app/outgoingemailreceipt-helpers.go b/app/outgoingemailreceipt-helpers.go index 7ea1e36..b88a45c 100644 --- a/app/outgoingemailreceipt-helpers.go +++ b/app/outgoingemailreceipt-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/workflow/workflow_models" +// MarshalToSwagger encodes first class object func (obj *OutgoingEmailMessageReceipt) MarshalToSwagger() *workflow_models.EmailMessage { return &workflow_models.EmailMessage{ ID: obj.ID, diff --git a/app/pdf-helpers.go b/app/pdf-helpers.go index f778e81..db083a3 100644 --- a/app/pdf-helpers.go +++ b/app/pdf-helpers.go @@ -2,8 +2,8 @@ package app import "code.tnxs.net/taxnexus/lib/api/stash/stash_models" +// MarshalToDocument encodes first class object to swagger func (obj *PDF) MarshalToDocument() *stash_models.Document { - return &stash_models.Document{ ID: obj.ID, Filename: obj.Filename, @@ -13,6 +13,7 @@ func (obj *PDF) MarshalToDocument() *stash_models.Document { } } +// UnMarshalNewPDF decodes swagger to first class object func UnMarshalNewPDF(swag *stash_models.NewPDF) *PDF { return &PDF{ Description: swag.Description, diff --git a/app/period-helpers.go b/app/period-helpers.go index 5f759af..da169a5 100644 --- a/app/period-helpers.go +++ b/app/period-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalPeriod decodes swagger to first class object func UnMarshalPeriod(s *ledger_models.Period) *Period { if s.ID == "" { s.ID = uuid.New().String() @@ -51,6 +52,8 @@ func UnMarshalPeriod(s *ledger_models.Period) *Period { }, } } + +// MarshalToSwagger encodes first class object func (obj *Period) MarshalToSwagger() *ledger_models.Period { return &ledger_models.Period{ ID: obj.ID, diff --git a/app/place-helpers.go b/app/place-helpers.go index c8974fb..c56cf70 100644 --- a/app/place-helpers.go +++ b/app/place-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalPlace decodes swagger to first class object func UnMarshalPlace(s *geo_models.Place) *Place { if s.ID == "" { s.ID = uuid.New().String() @@ -73,6 +74,8 @@ func UnMarshalPlace(s *geo_models.Place) *Place { }, } } + +// MarshalToSwagger encodes first class object func (obj *Place) MarshalToSwagger() *geo_models.Place { if obj == nil { return nil diff --git a/app/po-helpers.go b/app/po-helpers.go index 7789a92..9a08744 100644 --- a/app/po-helpers.go +++ b/app/po-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalPurchaseOrder decodes swagger to first class object func UnMarshalPurchaseOrder(s *ops_models.PurchaseOrder) (*PurchaseOrder, error) { //nolint:funlen // big object if s.ID == "" { s.ID = uuid.New().String() @@ -120,6 +121,8 @@ func UnMarshalPurchaseOrder(s *ops_models.PurchaseOrder) (*PurchaseOrder, error) }, }, nil } + +// MarshalToSwagger encodes first class object func (obj *PurchaseOrder) MarshalToSwagger() *ops_models.PurchaseOrder { var items []*ops_models.PurchaseOrderItem if obj.Items != nil { diff --git a/app/poitem-helpers.go b/app/poitem-helpers.go index fa8c44c..d98527d 100644 --- a/app/poitem-helpers.go +++ b/app/poitem-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalPurchaseOrderItem decodes swagger to first class object func UnMarshalPurchaseOrderItem(s *ops_models.PurchaseOrderItem) *PurchaseOrderItem { if s.ID == "" { s.ID = uuid.New().String() @@ -57,6 +58,8 @@ func UnMarshalPurchaseOrderItem(s *ops_models.PurchaseOrderItem) *PurchaseOrderI }, } } + +// MarshalToSwagger encodes first class object func (obj *PurchaseOrderItem) MarshalToSwagger() *ops_models.PurchaseOrderItem { return &ops_models.PurchaseOrderItem{ ID: obj.ID, diff --git a/app/product-helpers.go b/app/product-helpers.go index 15ed7db..2862c34 100644 --- a/app/product-helpers.go +++ b/app/product-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalProduct decodes swagger to first class object func UnMarshalProduct(s *ops_models.Product) *Product { if s.ID == "" { s.ID = uuid.New().String() @@ -57,6 +58,8 @@ func UnMarshalProduct(s *ops_models.Product) *Product { }, } } + +// MarshalToSwagger encodes first class object func (obj *Product) MarshalToSwagger() *ops_models.Product { return &ops_models.Product{ ID: obj.ID, diff --git a/app/quote-helpers.go b/app/quote-helpers.go index 70465c4..141d266 100644 --- a/app/quote-helpers.go +++ b/app/quote-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/ops/ops_models" +// MarshalToSwagger encodes first class object func (obj *Quote) MarshalToSwagger() *ops_models.Quote { var items []*ops_models.QuoteItem if obj.Items != nil { diff --git a/app/quoteitem-helpers.go b/app/quoteitem-helpers.go index 9a24264..cf8b10c 100644 --- a/app/quoteitem-helpers.go +++ b/app/quoteitem-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/ops/ops_models" +// MarshalToSwagger encodes first class object func (obj *QuoteItem) MarshalToSwagger() *ops_models.QuoteItem { return &ops_models.QuoteItem{ ID: obj.ID, diff --git a/app/ratingengine-helpers.go b/app/ratingengine-helpers.go index e69de29..4879f7a 100644 --- a/app/ratingengine-helpers.go +++ b/app/ratingengine-helpers.go @@ -0,0 +1 @@ +package app diff --git a/app/ratingengineitem-helpers.go b/app/ratingengineitem-helpers.go index e69de29..4879f7a 100644 --- a/app/ratingengineitem-helpers.go +++ b/app/ratingengineitem-helpers.go @@ -0,0 +1 @@ +package app diff --git a/app/root.go b/app/root.go index 1008406..5ee852b 100644 --- a/app/root.go +++ b/app/root.go @@ -3,7 +3,9 @@ package app import ( "strings" + "time" + "code.tnxs.net/taxnexus/lib/api/auth0/auth0_client" "code.tnxs.net/taxnexus/lib/app/logger" "github.com/spf13/viper" "go.uber.org/zap" @@ -13,14 +15,17 @@ import ( var sugar *zap.SugaredLogger var config = Configuration{} var appViper = viper.New() +var auth0Client = auth0_client.Default +var configured = false +const getTimeout = 6 * time.Minute +const postTimeout = 6 * time.Minute +const constNotFound = "not found" const dateFormat = "2006-01-02" const dateTimeFormat = "2006-01-02T15:04:05-0800" const dateTimeFormatAlt = "2006-01-02 15:04:05" const dateFormatAlt = "1/2/2006" -var configured = false - // InitConfig exports the config initialization func func InitConfig(systemName string) { if configured { @@ -46,3 +51,9 @@ func InitConfig(systemName string) { sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts)) configured = true } + +// FirstClassObject is a struct +type FirstClassObject interface { + Marshal() interface{} + UnMarshal() interface{} +} diff --git a/app/state-helpers.go b/app/state-helpers.go index 44ea4e0..c7cc62a 100644 --- a/app/state-helpers.go +++ b/app/state-helpers.go @@ -7,6 +7,7 @@ import ( "code.tnxs.net/taxnexus/lib/api/geo/geo_models" ) +// UnMarshalSwaggerState decodes swagger to first class object func UnMarshalSwaggerState(s *geo_models.State) *State { createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate) lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate) @@ -65,6 +66,7 @@ func UnMarshalSwaggerState(s *geo_models.State) *State { } } +// MarshalToSwagger encodes first class object func (obj *State) MarshalToSwagger() *geo_models.State { taxInstances := []*geo_models.TaxInstance{} for _, itm := range obj.TaxInstances { diff --git a/app/submission-helpers.go b/app/submission-helpers.go index 064ce47..8493578 100644 --- a/app/submission-helpers.go +++ b/app/submission-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalSubmission decodes swagger to first class object func UnMarshalSubmission(s *regs_models.Submission) *Submission { if s.ID == "" { s.ID = uuid.New().String() @@ -57,6 +58,7 @@ func UnMarshalSubmission(s *regs_models.Submission) *Submission { } } +// MarshalToSwagger encodes first class object func (obj *Submission) MarshalToSwagger() *regs_models.Submission { return ®s_models.Submission{ ID: obj.ID, diff --git a/app/taxinstance-helpers.go b/app/taxinstance-helpers.go index 13260c0..49a2606 100644 --- a/app/taxinstance-helpers.go +++ b/app/taxinstance-helpers.go @@ -34,6 +34,8 @@ func UnMarshalTaxInstance(s *geo_models.TaxInstance) *TaxInstance { }, } } + +// MarshalToSwagger encodes first class object func (obj *TaxInstance) MarshalToSwagger() *geo_models.TaxInstance { return &geo_models.TaxInstance{ ID: obj.ID, diff --git a/app/taxnexuscode-helpers.go b/app/taxnexuscode-helpers.go index c3b7d47..aa7e5af 100644 --- a/app/taxnexuscode-helpers.go +++ b/app/taxnexuscode-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalTaxnexusCode decodes swagger to first class object func UnMarshalTaxnexusCode(s *geo_models.TaxnexusCode) *TaxnexusCode { if s.ID == "" { s.ID = uuid.New().String() @@ -45,6 +46,8 @@ func UnMarshalTaxnexusCode(s *geo_models.TaxnexusCode) *TaxnexusCode { }, } } + +// MarshalToSwagger encodes first class object func (obj *TaxnexusCode) MarshalToSwagger() *geo_models.TaxnexusCode { swag := geo_models.TaxnexusCode{ ID: obj.ID, diff --git a/app/taxtransaction-helpers.go b/app/taxtransaction-helpers.go index e8ced4f..528e604 100644 --- a/app/taxtransaction-helpers.go +++ b/app/taxtransaction-helpers.go @@ -6,6 +6,7 @@ import ( // TaxTransaction is not ingested, hence no UnMarshalTaxTransaction +// MarshalToSwagger encodes first class object func (obj *TaxTransaction) MarshalToSwagger() *ops_models.TaxTransaction { return &ops_models.TaxTransaction{ ID: obj.ID, diff --git a/app/taxtypeaccount-helpers.go b/app/taxtypeaccount-helpers.go index 20d3a50..4a3be0d 100644 --- a/app/taxtypeaccount-helpers.go +++ b/app/taxtypeaccount-helpers.go @@ -8,6 +8,7 @@ import ( "github.com/google/uuid" ) +// UnMarshalTaxTypeAccount encodes first class object func UnMarshalTaxTypeAccount(s *regs_models.TaxTypeAccount) *TaxTypeAccount { if s.ID == "" { s.ID = uuid.New().String() @@ -64,6 +65,8 @@ func UnMarshalTaxTypeAccount(s *regs_models.TaxTypeAccount) *TaxTypeAccount { }, } } + +// MarshalToSwagger encodes first class object func (obj *TaxTypeAccount) MarshalToSwagger() *regs_models.TaxTypeAccount { return ®s_models.TaxTypeAccount{ ID: obj.ID, diff --git a/app/tenantuser-helpers.go b/app/tenantuser-helpers.go index 57fc348..58a80ed 100644 --- a/app/tenantuser-helpers.go +++ b/app/tenantuser-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/devops/devops_models" +// UnMarshalTenantUser encodes first class object func UnMarshalTenantUser(s *devops_models.TenantUser) *TenantUser { return &TenantUser{ AccessLevel: s.AccessLevel, @@ -22,6 +23,8 @@ func UnMarshalTenantUser(s *devops_models.TenantUser) *TenantUser { Username: s.Username, } } + +// MarshalToSwagger encodes first class object func (obj *TenantUser) MarshalToSwagger() *devops_models.TenantUser { return &devops_models.TenantUser{ AccessLevel: obj.AccessLevel, diff --git a/app/total-helpers.go b/app/total-helpers.go index d74c5f2..f94d078 100644 --- a/app/total-helpers.go +++ b/app/total-helpers.go @@ -2,6 +2,7 @@ package app import "code.tnxs.net/taxnexus/lib/api/ops/ops_models" +// MarshalToSwagger encodes first class object func (obj *Total) MarshalToSwagger() *ops_models.Total { var items []*ops_models.TotalItem if obj.TotalItems != nil { diff --git a/app/transaction-helpers.go b/app/transaction-helpers.go index 1819324..156b50b 100644 --- a/app/transaction-helpers.go +++ b/app/transaction-helpers.go @@ -7,6 +7,7 @@ import ( "code.tnxs.net/taxnexus/lib/api/regs/regs_models" ) +// UnMarshalTransaction encodes first class object func UnMarshalTransaction(s *regs_models.Transaction) *Transaction { createdDate, e0 := time.Parse(dateTimeFormat, s.CreatedDate) lastModfiedDate, e1 := time.Parse(dateTimeFormat, s.LastModifiedDate) @@ -29,6 +30,8 @@ func UnMarshalTransaction(s *regs_models.Transaction) *Transaction { }, } } + +// MarshalToSwagger encodes first class object func (obj *Transaction) MarshalToSwagger() *regs_models.Transaction { return ®s_models.Transaction{ ID: obj.ID, diff --git a/app/userauth.go b/app/userauth.go new file mode 100644 index 0000000..b6fc762 --- /dev/null +++ b/app/userauth.go @@ -0,0 +1,31 @@ +package app + +// UserAuth is a first class object type +type UserAuth struct { + AccountID string + APIKey string + ContactID string + Email string + Roles []string + Tenants []string + UserID string +} + +// NewAuth0User is a first class object type +type NewAuth0User struct { + Blocked bool `json:"blocked,omitempty"` + Connection string `json:"connection,omitempty"` + Email string `json:"email,omitempty"` + EmailVerified bool `json:"email_verified,omitempty"` + FamilyName string `json:"family_name,omitempty"` + GivenName string `json:"given_name,omitempty"` + Name string `json:"name,omitempty"` + Nickname string `json:"nickname,omitempty"` + Password string `json:"password,omitempty"` + PhoneNumber string `json:"phone_number,omitempty"` + PhoneVerified bool `json:"phone_verified,omitempty"` + Picture string `json:"picture,omitempty"` + UserID string `json:"user_id,omitempty"` + Username string `json:"username,omitempty"` + VerifyEmail bool `json:"verify_email,omitempty"` +} diff --git a/app/userrole-helpers.go b/app/userrole-helpers.go index e69de29..4879f7a 100644 --- a/app/userrole-helpers.go +++ b/app/userrole-helpers.go @@ -0,0 +1 @@ +package app diff --git a/rules/account.go b/rules/account.go index a813e7e..a731e03 100644 --- a/rules/account.go +++ b/rules/account.go @@ -7,7 +7,10 @@ import ( "go.temporal.io/sdk/workflow" ) -// StoreAccountActivity posts a new lead object to datastore +// StoreAccountActivityID is an activity identifier +const StoreAccountActivityID = "STORE_ACCOUNT_ACTIVITY" + +// StoreAccountActivity posts a new account object to datastore func StoreAccountActivity(ctx workflow.Context, w app.AccountChannelWrapper) error { //nolint:gocritic // what we want postAccountParams := accounts.NewPostAccountsParamsWithTimeout(postTimeout) postAccountParams.AccountRequest = &crm_models.AccountRequest{ @@ -17,6 +20,6 @@ func StoreAccountActivity(ctx workflow.Context, w app.AccountChannelWrapper) err if err != nil { return err } - sugar.Info("crm.storeAccount: 👍 📤") + sugar.Info("rules.StoreAccountActivity: 👍 📤") return nil } diff --git a/rules/auth0.go b/rules/auth0.go new file mode 100644 index 0000000..9d274a8 --- /dev/null +++ b/rules/auth0.go @@ -0,0 +1,61 @@ +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 +} diff --git a/rules/new-tenant.go b/rules/new-tenant.go new file mode 100644 index 0000000..1e8c7fa --- /dev/null +++ b/rules/new-tenant.go @@ -0,0 +1 @@ +package rules diff --git a/rules/new-tenantuser.go b/rules/new-tenantuser.go new file mode 100644 index 0000000..1e8c7fa --- /dev/null +++ b/rules/new-tenantuser.go @@ -0,0 +1 @@ +package rules diff --git a/rules/new-user.go b/rules/new-user.go new file mode 100644 index 0000000..200628b --- /dev/null +++ b/rules/new-user.go @@ -0,0 +1,46 @@ +package rules + +import ( + "time" + + "code.tnxs.net/taxnexus/lib/api/devops/devops_models" + "code.tnxs.net/taxnexus/lib/app" + "go.temporal.io/sdk/workflow" +) + +// NewUserChannelWrapper wraps a contact, account, payment method and a user identifier (app.User) +type NewUserChannelWrapper struct { + User devops_models.User + Principal app.User + SagaID string + SagaType string +} + +// NewUserWorkflow is a Temporal workflow +func NewUserWorkflow(ctx workflow.Context, payload *NewUserChannelWrapper) error { + ctx = workflow.WithActivityOptions( + ctx, + workflow.ActivityOptions{ + StartToCloseTimeout: time.Minute, + }) + err := workflow.ExecuteActivity(ctx, + StoreUserActivity, + &app.UserChannelWrapper{ + Obj: payload.User, + Principal: payload.Principal, + SagaID: payload.SagaID, + SagaType: payload.SagaType, + }).Get(ctx, nil) + if err != nil { + return err + } + err = workflow.ExecuteActivity( + ctx, + NotifyLeadActivity, + payload, + ).Get(ctx, nil) + if err != nil { + return err + } + return nil +} diff --git a/rules/root.go b/rules/root.go index b99ff51..68e1ff1 100644 --- a/rules/root.go +++ b/rules/root.go @@ -5,6 +5,7 @@ import ( "time" "code.tnxs.net/taxnexus/lib/api/crm/crm_client" + "code.tnxs.net/taxnexus/lib/api/devops/devops_client" "code.tnxs.net/taxnexus/lib/api/ops/ops_client" "code.tnxs.net/taxnexus/lib/api/workflow/workflow_client" "code.tnxs.net/taxnexus/lib/app" @@ -14,13 +15,15 @@ import ( ) // const dateFormat = "2006-01-02" -// const getTimeout = 6 * time.Minute +const getTimeout = 6 * time.Minute + // const dateTimeFormat = "2006-01-02T15:04:05-0800" const postTimeout = 6 * time.Minute var sugar = logger.New(zapcore.InfoLevel) var crmClient = crm_client.Default +var devopsClient = devops_client.Default var opsClient = ops_client.Default var workflowClient = workflow_client.Default diff --git a/rules/user.go b/rules/user.go new file mode 100644 index 0000000..96796a6 --- /dev/null +++ b/rules/user.go @@ -0,0 +1,116 @@ +package rules + +import ( + "bytes" + "context" + "fmt" + "html/template" + + "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" +) + +// StoreUserActivity posts a new user object to datastore and creates the auth0 user +func StoreUserActivity(ctx workflow.Context, w app.UserChannelWrapper) error { //nolint:gocritic // what we want + postUserParams := user.NewPostUsersParamsWithTimeout(postTimeout) + if w.Obj.Name == "" { + w.Obj.Name = w.Obj.FirstName + " " + w.Obj.LastName + } + postUserParams.UserRequest = &devops_models.UserRequest{ + Data: []*devops_models.User{&w.Obj}, + } + devopsResponse, err := devopsClient.User.PostUsers(postUserParams, w.Principal.Auth) + if err != nil { + return err + } + var newUser *devops_models.User + for _, itm := range devopsResponse.Payload.Data { // single iteration execution + newUser = itm + } + // + // create new Auth0 user + // + auth0UserID, err := app.GetAuth0UserByEmail(newUser.Email) + if err != nil { + return err + } + if auth0UserID != "not found" { + return fmt.Errorf("app.StoreUserActivity: 💣 ⛔ Auth0 user exists: %s", auth0UserID) + } + newAuth0UserID, err := app.CreateNewAuth0User(&app.NewAuth0User{ + Email: newUser.Email, + EmailVerified: true, + Name: newUser.Name, + UserID: newUser.ID, + Username: newUser.Email, + FamilyName: newUser.LastName, + GivenName: newUser.FirstName, + }) + if err != nil { + return err + } + newUser.Auth0UserID = newAuth0UserID + // + // update Taxnexus user + // + putUserParams := user.NewPutUsersParamsWithTimeout(getTimeout) + putUserParams.UserRequest = &devops_models.UserRequest{ + Data: []*devops_models.User{newUser}, + } + _, err = devopsClient.User.PutUsers(putUserParams, w.Principal.Auth) + if err != nil { + return err + } + // + // send emails + // + sugar.Info("crm.storeUser: 👍 📤") + return nil +} + +// NotifyUserActivity sends an email to a new lead +func NotifyUserActivity(ctx context.Context, w *app.UserChannelWrapper) error { + sugar.Info("workflow.notifyUser: 📥") + var buf bytes.Buffer + const textBody = ` + +Alert! New User Inquiry from Taxnexus.io website. + + Taxnexus ID: {{.ID}} + First Name: {{.FirstName}} + Last Name: {{.LastName}} + Email: {{.Email}} + Message: {{.Description}} + +-- end -- +` + t := template.Must(template.New("textBody").Parse(textBody)) + err := t.Execute(&buf, w) + if err != nil { + return err + } + emailParams := outgoing_email_message.NewPostOutgoingEmailMessagesParamsWithTimeout(postTimeout) + emailParams.OutgoingEmailMessageRequest = &workflow_models.OutgoingEmailMessageRequest{ + Data: []*workflow_models.OutgoingEmailMessage{ + { + Subject: "New lead from " + w.Obj.Name, + ValidatedFromAddress: "support@taxnexus.net", + ToAddress: "info@taxnexus.net", + FromName: "Taxnexus Onboarding", + Text: buf.String(), + HTML: "
" + buf.String() + "
", + }, + }, + } + _, err = workflowClient.OutgoingEmailMessage.PostOutgoingEmailMessages(emailParams, w.Principal.Auth) + if err != nil { + return err + } + sugar.Info("workflow.notifyUser: 👍 📤") + return nil +}