services finale

v0.1.41 v0.1.41
Vernon Keenan 2021-01-28 10:05:25 -08:00
parent a4a48a0051
commit 5968d9f892
7 changed files with 129 additions and 18 deletions

29
app/company-cache.go Normal file
View File

@ -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
}

57
app/company-services.go Normal file
View File

@ -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)
}

View File

@ -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
}

23
app/invoice-services.go Normal file
View File

@ -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
}

View File

@ -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

View File

@ -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,

View File

@ -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)
}
}