From 5968d9f89253047bafed99b49f475af08673f29a Mon Sep 17 00:00:00 2001 From: Vernon Keenan Date: Thu, 28 Jan 2021 10:05:25 -0800 Subject: [PATCH] services finale --- app/company-cache.go | 29 +++++++++++++++++++++ app/company-services.go | 57 +++++++++++++++++++++++++++++++++++++++++ app/contact-services.go | 8 +++--- app/invoice-services.go | 23 +++++++++++++++++ app/root.go | 2 ++ app/stash-services.go | 10 ++++---- app/taxtype-services.go | 18 ++++++------- 7 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 app/company-cache.go create mode 100644 app/company-services.go create mode 100644 app/invoice-services.go diff --git a/app/company-cache.go b/app/company-cache.go new file mode 100644 index 0000000..23df5d7 --- /dev/null +++ b/app/company-cache.go @@ -0,0 +1,29 @@ +package app + +import ( + "sync" + + "code.tnxs.net/taxnexus/lib/api/crm/crm_models" +) + +var companyCache = companyCacheType{ + obj: map[string]*crm_models.Company{}, +} + +type companyCacheType struct { + sync.RWMutex + obj map[string]*crm_models.Company +} + +func (m *companyCacheType) get(recordID string) (*crm_models.Company, bool) { + m.RLock() + defer m.RUnlock() + r, ok := m.obj[recordID] + return r, ok +} + +func (m *companyCacheType) put(recordID string, itm *crm_models.Company) { + m.Lock() + defer m.Unlock() + m.obj[recordID] = itm +} diff --git a/app/company-services.go b/app/company-services.go new file mode 100644 index 0000000..154caa7 --- /dev/null +++ b/app/company-services.go @@ -0,0 +1,57 @@ +package app + +import ( + "fmt" + + "code.tnxs.net/taxnexus/lib/api/crm/crm_client/companies" + "code.tnxs.net/taxnexus/lib/api/crm/crm_models" +) + +// GetCompany is a first class object retrieval function +func GetCompany(id string, principal *User) *crm_models.Company { + if id == "" { + return nil + } + c, ok := companyCache.get(id) + if ok { + return c + } + c, err := GetCompanyByID(id, principal) + if err != nil { + return nil + } + return c +} + +// GetCompanyByID is a first class object retrieval function +func GetCompanyByID(key string, principal *User) (*crm_models.Company, error) { + sugar.Debugf("app.getCompanyByID: 📥") + if key == "" { + return nil, fmt.Errorf("app.getCompanyByID: 💣 ⛔ key is blank") + } + cacheObj, ok := companyCache.get(key) + if ok { + sugar.Debugf("app.getCompanyByID: 👍 🎯 📤") + return cacheObj, nil + } + params := companies.NewGetCompaniesParamsWithTimeout(getTimeout) + params.CompanyID = &key + response, err := crmClient.Companies.GetCompanies(params, principal.Auth) + if err != nil { + return nil, err + } + var obj *crm_models.Company + for _, itm := range response.Payload.Data { // single iteration execution + obj = itm + } + companyCache.put(key, obj) + sugar.Debugf("app.getCompanyByID: 👍 🆕 📤") + return obj, nil +} + +const defaultCompanyID = "6ff8326f-79b7-40ae-afc7-390eca182b1b" // todo #3 Don't hardcode company ID + +// GetDefaultCompany returns the default company +func GetDefaultCompany(principal *User) *crm_models.Company { + return GetCompany(defaultCompanyID, principal) +} diff --git a/app/contact-services.go b/app/contact-services.go index 547b042..43c0c24 100644 --- a/app/contact-services.go +++ b/app/contact-services.go @@ -24,13 +24,13 @@ func GetContact(id string, principal *User) *Contact { // GetContactByID is a first class object retrieval function func GetContactByID(key string, principal *User) (*Contact, error) { - sugar.Debugf("render.getContactByID: 📥") + sugar.Debugf("app.getContactByID: 📥") if key == "" { - return nil, fmt.Errorf("render.getContactByID: 💣 ⛔ key is blank") + return nil, fmt.Errorf("app.getContactByID: 💣 ⛔ key is blank") } obj, ok := contactCache.get(key) if ok { - sugar.Debugf("render.getContactByID: 👍 🎯 📤") + sugar.Debugf("app.getContactByID: 👍 🎯 📤") return obj, nil } params := contacts.NewGetContactsParams() @@ -44,6 +44,6 @@ func GetContactByID(key string, principal *User) (*Contact, error) { newObj = UnMarshalContact(itm) } contactCache.put(key, newObj) - sugar.Debugf("render.getContactByID: 👍 🆕 📤") + sugar.Debugf("app.getContactByID: 👍 🆕 📤") return newObj, nil } diff --git a/app/invoice-services.go b/app/invoice-services.go new file mode 100644 index 0000000..d1c93ae --- /dev/null +++ b/app/invoice-services.go @@ -0,0 +1,23 @@ +package app + +import ( + "code.tnxs.net/taxnexus/lib/api/ops/ops_client/invoice" + "code.tnxs.net/taxnexus/lib/api/ops/ops_models" +) + +// GetInvoiceByID is a first class object retrieval method +func GetInvoiceByID(recordID string, principal *User) (*ops_models.Invoice, error) { + sugar.Debugf("app.getFilingByID: 📥") + opsParams := invoice.NewGetInvoicesParamsWithTimeout(getTimeout) + opsParams.InvoiceID = &recordID + response, err := opsClient.Invoice.GetInvoices(opsParams, principal.Auth) + if err != nil { + sugar.Errorf("app.getInvoiceByID: 💣 ⛔ opsClient.Invoice.GetInvoices error %w", err) + return nil, err + } + swag := &ops_models.Invoice{} + for _, itm := range response.Payload.Data { + swag = itm + } + return swag, nil +} diff --git a/app/root.go b/app/root.go index c68b1fa..ca1b0a8 100644 --- a/app/root.go +++ b/app/root.go @@ -8,6 +8,7 @@ import ( "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" + "code.tnxs.net/taxnexus/lib/api/ops/ops_client" "code.tnxs.net/taxnexus/lib/api/stash/stash_client" "code.tnxs.net/taxnexus/lib/app/logger" httptransport "github.com/go-openapi/runtime/client" @@ -23,6 +24,7 @@ var auth0Client = auth0_client.Default var geoClient = geo_client.Default var crmClient = crm_client.Default var stashClient = stash_client.Default +var opsClient = ops_client.Default var configured = false var apiUsers map[string]User diff --git a/app/stash-services.go b/app/stash-services.go index 4cecba4..ea051e0 100644 --- a/app/stash-services.go +++ b/app/stash-services.go @@ -15,7 +15,7 @@ type StashPDFParams struct { // StashPDF stores a PDF in the stash database func StashPDF(params StashPDFParams) (*Document, error) { - sugar.Debugf("render.stashPDF: 📥") + sugar.Debugf("app.stashPDF: 📥") var title string var fileName string var description string @@ -25,12 +25,12 @@ func StashPDF(params StashPDFParams) (*Document, error) { title = "Taxnexus Report for " + params.Account.Name fileName = "Taxnexus Report for " + params.Account.Name + ".pdf" description = "Account Report generated by render" - ref = "render.getAccounts" + ref = "app.getAccounts" case "tax_summary": title = "Taxnexus Tax Report for " + params.Account.Name fileName = params.Account.Name + " Tax Report.pdf" description = "CDTFA Quarterly District Sales and Use Tax for " - ref = "render.getTaxes" + ref = "app.getTaxes" } stashParams := stash_pdf.NewPostPdfsParamsWithTimeout(getTimeout) @@ -50,14 +50,14 @@ func StashPDF(params StashPDFParams) (*Document, error) { } response, err := stashClient.StashPdf.PostPdfs(stashParams, params.Principal.Auth) if err != nil { - sugar.Errorf("render.stashPDF: 💣 ⛔ post PDF to stash error %w", err) + sugar.Errorf("app.stashPDF: 💣 ⛔ post PDF to stash error %w", err) return nil, err } var newObj *stash_models.Document for _, itm := range response.Payload.Data { // single iteration execution newObj = itm } - sugar.Debugf("render.stashPDF: 👍 📤") + sugar.Debugf("app.stashPDF: 👍 📤") return &Document{ ID: newObj.ID, Filename: params.Account.Name + fileName, diff --git a/app/taxtype-services.go b/app/taxtype-services.go index 80b5cba..53ece4e 100644 --- a/app/taxtype-services.go +++ b/app/taxtype-services.go @@ -47,13 +47,13 @@ func GetTaxType(recordID string, principal *User) *TaxType { // GetTaxTypeByID is first class retrieval function func GetTaxTypeByID(recordID string, principal *User) (*TaxType, error) { - sugar.Debugf("render.GetTaxTypesByID: 📥") + sugar.Debugf("app.GetTaxTypesByID: 📥") if recordID == "" { - return nil, fmt.Errorf("render.getTaxTypeByID: 💣 ⛔ key is blank") + return nil, fmt.Errorf("app.getTaxTypeByID: 💣 ⛔ key is blank") } obj, ok := taxTypeCache.get(recordID) if ok { - sugar.Debugf("render.getTaxTypeByID: 👍 🎯 📤") + sugar.Debugf("app.getTaxTypeByID: 👍 🎯 📤") return obj, nil } geoParams := tax_type.NewGetTaxTypesParamsWithTimeout(getTimeout) @@ -67,7 +67,7 @@ func GetTaxTypeByID(recordID string, principal *User) (*TaxType, error) { newObj = UnMarshalTaxType(itm) } taxTypeCache.put(recordID, newObj) - sugar.Debugf("render.getTaxTypeByID: 👍 🆕 📤") + sugar.Debugf("app.getTaxTypeByID: 👍 🆕 📤") return newObj, nil } @@ -77,7 +77,7 @@ func GetCannabisTaxTypes(taxTypes []*TaxType) []*TaxType { for _, tt := range taxTypes { if tt != nil { if inMap(cannabisCategories, tt.Category) && isActive(tt) { - sugar.Debugf("render.getCannabisTaxTypes: ➡ ✅ cannabis %s", tt.Name) + sugar.Debugf("app.getCannabisTaxTypes: ➡ ✅ cannabis %s", tt.Name) objList = append(objList, tt) } } @@ -94,7 +94,7 @@ func GetExciseTaxTypes(taxTypes []*TaxType) []*TaxType { for _, tt := range taxTypes { if tt != nil { if inMap(exciseCategories, tt.Category) && isActive(tt) { - sugar.Debugf("render.getExciseTaxTypes: ➡ ✅ excise %s", tt.Name) + sugar.Debugf("app.getExciseTaxTypes: ➡ ✅ excise %s", tt.Name) objList = append(objList, tt) } } @@ -111,7 +111,7 @@ func GetBusinessTaxTypes(taxTypes []*TaxType) []*TaxType { for _, tt := range taxTypes { if tt != nil { if tt.Category == "business" && isActive(tt) { - sugar.Debugf("render.getBusinessTaxTypes: ➡ ✅ business %s", tt.Name) + sugar.Debugf("app.getBusinessTaxTypes: ➡ ✅ business %s", tt.Name) objList = append(objList, tt) } } @@ -128,7 +128,7 @@ func GetMerchTaxTypes(taxTypes []*TaxType) []*TaxType { for _, tt := range taxTypes { if tt != nil { if inMap(merchCategories, tt.Category) && isActive(tt) { - sugar.Debugf("render.getMerchTaxTypes: ➡ ✅ merch %s", tt.Name) + sugar.Debugf("app.getMerchTaxTypes: ➡ ✅ merch %s", tt.Name) objList = append(objList, tt) } } @@ -145,7 +145,7 @@ func GetTelecomTaxTypes(taxTypes []*TaxType) []*TaxType { for _, tt := range taxTypes { if tt != nil { if inMap(telecomCategories, tt.Category) && isActive(tt) { - sugar.Debugf("render.getTelecomTaxTypes: ➡ ✅ telecom %s", tt.Name) + sugar.Debugf("app.getTelecomTaxTypes: ➡ ✅ telecom %s", tt.Name) objList = append(objList, tt) } }