lib/app/company-services.go

58 lines
1.5 KiB
Go

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