From c6cc12ce7c90472c631305656e3b1500d120c846 Mon Sep 17 00:00:00 2001 From: Vernon Keenan Date: Sat, 30 Jan 2021 11:39:44 -0800 Subject: [PATCH] getmaps --- app/auth.go | 10 +++++----- app/config.go | 1 + app/domain-services.go | 31 +++++++++++++++++++++++++++++++ app/root.go | 30 ++++++++++++++++++++++++------ app/taxnexuscode-services.go | 34 ++++++++++++++++++++++++++++++++++ app/taxtype-services.go | 25 +++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 11 deletions(-) create mode 100644 app/domain-services.go create mode 100644 app/taxnexuscode-services.go diff --git a/app/auth.go b/app/auth.go index f0560d5..3e718b9 100644 --- a/app/auth.go +++ b/app/auth.go @@ -1,16 +1,16 @@ package app import ( - "code.tnxs.net/taxnexus/lib/api/auth/auth_client" "code.tnxs.net/taxnexus/lib/api/auth/auth_client/user" "code.tnxs.net/taxnexus/lib/api/auth/auth_models" httptransport "github.com/go-openapi/runtime/client" ) -var authClient = auth_client.Default -var authUser *User - -// CheckAPIUser is exported +// CheckAPIUser authenticates a User with the auth service +// +// When called, ChechAPIUser assumes there is an "auth" service account in the +// app configuration file. +// func CheckAPIUser(token *string) (*User, error) { sugar.Debug("app.CheckAPIUser: 📥") if authUser == nil { diff --git a/app/config.go b/app/config.go index f835fe8..5211466 100644 --- a/app/config.go +++ b/app/config.go @@ -144,6 +144,7 @@ func GetServiceAccount(name string) *ServiceAccount { if ok { return &serviceaccount } + sugar.Errorf("app.config: 💣⛔ unknown service account: %s", name) return nil } diff --git a/app/domain-services.go b/app/domain-services.go new file mode 100644 index 0000000..2d42006 --- /dev/null +++ b/app/domain-services.go @@ -0,0 +1,31 @@ +package app + +import ( + "fmt" + + "code.tnxs.net/taxnexus/lib/api/geo/geo_client/domain" +) + +// GetDomainMap returns a map of all domains +// +// Requires the presence of a "geo" service account in the +// application configuration file. +// +func GetDomainMap() (map[string]*Domain, error) { + sugar.Debug("ledger.GetDomainMap: 📥") + geoUser := apiUser("geo") + if geoUser == nil { + return nil, fmt.Errorf("app.GetDomainMap: 💣⛔ cannot get auth, check config file") + } + params := domain.NewGetDomainsParamsWithTimeout(getTimeout) + getOK, err := geoClient.Domain.GetDomains(params, geoUser.Auth) + if err != nil { + return nil, err + } + objMap := map[string]*Domain{} + for _, itm := range getOK.Payload.Data { + objMap[itm.ID] = UnMarshalDomain(itm) + } + sugar.Infof("ledger.GetDomainMap: 📀 📤 %v domains loaded", len(objMap)) + return objMap, nil +} diff --git a/app/root.go b/app/root.go index cd453fe..e69f2a0 100644 --- a/app/root.go +++ b/app/root.go @@ -5,6 +5,7 @@ import ( "strings" "time" + "code.tnxs.net/taxnexus/lib/api/auth/auth_client" "code.tnxs.net/taxnexus/lib/api/auth0/auth0_client" "code.tnxs.net/taxnexus/lib/api/crm/crm_client" "code.tnxs.net/taxnexus/lib/api/geo/geo_client" @@ -12,21 +13,24 @@ import ( "code.tnxs.net/taxnexus/lib/api/regs/regs_client" "code.tnxs.net/taxnexus/lib/api/stash/stash_client" "code.tnxs.net/taxnexus/lib/app/logger" + httptransport "github.com/go-openapi/runtime/client" "github.com/spf13/viper" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) -var sugar *zap.SugaredLogger -var config = Configuration{} var appViper = viper.New() var auth0Client = auth0_client.Default -var geoClient = geo_client.Default +var authClient = auth_client.Default +var authUser *User +var config = Configuration{} +var configured = false var crmClient = crm_client.Default -var stashClient = stash_client.Default +var geoClient = geo_client.Default var opsClient = ops_client.Default var regsClient = regs_client.Default -var configured = false +var stashClient = stash_client.Default +var sugar *zap.SugaredLogger const getTimeout = 6 * time.Minute const postTimeout = 6 * time.Minute @@ -38,7 +42,10 @@ const dateTimeFormat = "2006-01-02T15:04:05-0800" const dateTimeFormatAlt = "2006-01-02 15:04:05" const dateFormatAlt = "1/2/2006" const countyGeocodeLength = 7 -const cityGeocodeLength = 12 + +var maxTaxTypes = int64(2000) + +// const cityGeocodeLength = 12 // InitConfig exports the config initialization func func InitConfig(systemName string, initalLogLevel zapcore.Level) { @@ -65,3 +72,14 @@ func InitConfig(systemName string, initalLogLevel zapcore.Level) { sugar.Debugf("app.InitConfig: 👍 📤 serviceAccounts: %d", len(config.ServiceAccounts)) configured = true } + +func apiUser(service string) *User { + serviceAccount := GetServiceAccount(service) + if serviceAccount == nil { + return nil + } + return &User{ + APIKey: serviceAccount.APIKey, + Auth: httptransport.APIKeyAuth("X-API-Key", "header", serviceAccount.APIKey), + } +} diff --git a/app/taxnexuscode-services.go b/app/taxnexuscode-services.go new file mode 100644 index 0000000..4f71dc5 --- /dev/null +++ b/app/taxnexuscode-services.go @@ -0,0 +1,34 @@ +package app + +import ( + "fmt" + + "code.tnxs.net/taxnexus/lib/api/geo/geo_client/taxnexus_code" +) + +// GetTaxnexusCodeMap returns a map of all taxnexus codes +// +// Requires the presence of a "geo" service account in the +// application configuration file. +// +func GetTaxnexusCodeMap() (map[string]*TaxnexusCode, error) { + sugar.Debug("ledger.GetTaxnexusCodeMap: 📥") + geoUser := apiUser("geo") + if geoUser == nil { + return nil, fmt.Errorf("app.GetTaxnexusCodeMap: 💣⛔ cannot get auth, check config file") + } + params := taxnexus_code.NewGetTaxnexusCodesParams() + getOK, err := geoClient.TaxnexusCode.GetTaxnexusCodes(params, geoUser.Auth) + if err != nil { + return nil, err + } + objMap := map[string]*TaxnexusCode{} + n := 0 + for _, itm := range getOK.Payload.Data { + n++ + objMap[itm.ID] = UnMarshalTaxnexusCode(itm) + objMap[itm.Code] = objMap[itm.ID] + } + sugar.Infof("ledger.GetTaxnexusCodeMap: 📀 📤 %v taxnexus codes loaded", n) + return objMap, nil +} diff --git a/app/taxtype-services.go b/app/taxtype-services.go index 53ece4e..bfd6fc1 100644 --- a/app/taxtype-services.go +++ b/app/taxtype-services.go @@ -32,6 +32,31 @@ var telecomCategories = map[string]string{ "wireless": "wireless", } +// GetTaxTypeMap returns a map of all taxtypes +// +// Requires the presence of a "geo" service account in the +// application configuration file. +// +func GetTaxTypeMap() (map[string]*TaxType, error) { + sugar.Debug("ledger.GetTaxTypeMap: 📥") + geoUser := apiUser("geo") + if geoUser == nil { + return nil, fmt.Errorf("app.GetTaxTypeMap: 💣⛔ cannot get auth, check config file") + } + params := tax_type.NewGetTaxTypesParamsWithTimeout(getTimeout) + params.Limit = &maxTaxTypes + getOK, err := geoClient.TaxType.GetTaxTypes(params, geoUser.Auth) + if err != nil { + return nil, err + } + objMap := map[string]*TaxType{} + for _, itm := range getOK.Payload.Data { + objMap[itm.ID] = UnMarshalTaxType(itm) + } + sugar.Infof("ledger.GetTaxTypeMap: 📀 📤 %v taxtypes loaded", len(objMap)) + return objMap, nil +} + // GetTaxType is first class retrieval function func GetTaxType(recordID string, principal *User) *TaxType { obj, ok := taxTypeCache.get(recordID)