2021-01-28 18:05:25 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/companies"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetCompany is a first class object retrieval function
|
2021-01-28 20:00:21 +00:00
|
|
|
func GetCompany(id string, principal *User) *Company {
|
2021-01-28 18:05:25 +00:00
|
|
|
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
|
2021-01-28 20:00:21 +00:00
|
|
|
func GetCompanyByID(key string, principal *User) (*Company, error) {
|
2021-01-28 18:05:25 +00:00
|
|
|
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
|
|
|
|
}
|
2021-01-28 20:00:21 +00:00
|
|
|
var obj *Company
|
2021-01-28 18:05:25 +00:00
|
|
|
for _, itm := range response.Payload.Data { // single iteration execution
|
2021-01-28 20:00:21 +00:00
|
|
|
obj = UnMarshalCompany(itm, principal)
|
2021-01-28 18:05:25 +00:00
|
|
|
}
|
2021-01-28 20:00:21 +00:00
|
|
|
finalObj := obj.Enrich(principal)
|
|
|
|
companyCache.put(key, finalObj)
|
2021-01-28 18:05:25 +00:00
|
|
|
sugar.Debugf("app.getCompanyByID: 👍 🆕 📤")
|
2021-01-28 20:00:21 +00:00
|
|
|
return finalObj, nil
|
2021-01-28 18:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const defaultCompanyID = "6ff8326f-79b7-40ae-afc7-390eca182b1b" // todo #3 Don't hardcode company ID
|
|
|
|
|
|
|
|
// GetDefaultCompany returns the default company
|
2021-01-28 20:00:21 +00:00
|
|
|
func GetDefaultCompany(principal *User) *Company {
|
2021-01-28 18:05:25 +00:00
|
|
|
return GetCompany(defaultCompanyID, principal)
|
|
|
|
}
|
2021-01-28 20:00:21 +00:00
|
|
|
|
|
|
|
// Enrich adds subordinate objects to a first class object and returns a copy
|
|
|
|
func (obj *Company) Enrich(principal *User) *Company {
|
|
|
|
return &Company{
|
|
|
|
ID: obj.ID,
|
|
|
|
Account: GetAccount(obj.AccountID, principal),
|
|
|
|
AccountID: obj.AccountID,
|
|
|
|
AccountNumberPrefix: obj.AccountNumberPrefix,
|
|
|
|
AdvancePeriodID: obj.AdvancePeriodID,
|
|
|
|
BillingAddress: obj.BillingAddress,
|
|
|
|
BillingAdvice: obj.BillingAdvice,
|
|
|
|
BillingContactID: obj.BillingContactID,
|
|
|
|
BillingContact: GetContact(obj.BillingContactID, principal),
|
|
|
|
BillingEmail: obj.BillingEmail,
|
|
|
|
BillingPhone: obj.BillingPhone,
|
|
|
|
BillingWebsite: obj.BillingWebsite,
|
|
|
|
CoaTemplateID: obj.CoaTemplateID,
|
|
|
|
ColorAccent1: obj.ColorAccent1,
|
|
|
|
ColorAccent2: obj.ColorAccent2,
|
|
|
|
ColorPrimary: obj.ColorPrimary,
|
|
|
|
ClosedPeriodID: obj.ClosedPeriodID,
|
|
|
|
CreatedByID: obj.CreatedByID,
|
|
|
|
CreatedDate: obj.CreatedDate,
|
|
|
|
CurrentPeriodID: obj.CurrentPeriodID,
|
|
|
|
CurrentPeriodStatus: obj.CurrentPeriodStatus,
|
|
|
|
CustomerSuccessID: obj.CustomerSuccessID,
|
|
|
|
CustomerSuccess: GetContact(obj.CustomerSuccessID, principal),
|
|
|
|
DateClosed: obj.DateClosed,
|
|
|
|
DefaultAddress: obj.DefaultAddress,
|
|
|
|
DefaultCompany: obj.DefaultCompany,
|
|
|
|
FontBody: obj.FontBody,
|
|
|
|
FontHeading: obj.FontHeading,
|
|
|
|
FontHeadingNarrow: obj.FontHeadingNarrow,
|
|
|
|
FontLink: obj.FontLink,
|
|
|
|
FontMono: obj.FontMono,
|
|
|
|
International: obj.International,
|
|
|
|
LastAccountNumber: obj.LastAccountNumber,
|
|
|
|
LastModifiedByID: obj.LastModifiedByID,
|
|
|
|
LastModifiedDate: obj.LastModifiedDate,
|
|
|
|
LastTaxtypeNumber: obj.LastTaxtypeNumber,
|
|
|
|
Logo: obj.Logo,
|
|
|
|
Name: obj.Name,
|
|
|
|
OwnerID: obj.OwnerID,
|
|
|
|
Preparer: GetContact(obj.PreparerID, principal),
|
|
|
|
PreparerID: obj.PreparerID,
|
|
|
|
PricebookID: obj.PricebookID,
|
|
|
|
TenantID: obj.TenantID,
|
|
|
|
UserTechLead: GetContact(obj.UserTechLeadID, principal),
|
|
|
|
UserTechLeadID: obj.UserTechLeadID,
|
|
|
|
}
|
|
|
|
}
|