48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
|
package app
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/accounts"
|
||
|
)
|
||
|
|
||
|
func GetAccount(key string, principal *User) Account {
|
||
|
if key == "" {
|
||
|
return Account{}
|
||
|
}
|
||
|
a, ok := accountCache.get(key)
|
||
|
if ok {
|
||
|
return *a
|
||
|
}
|
||
|
acct, err := GetAccountByID(key, principal)
|
||
|
if err != nil {
|
||
|
return Account{}
|
||
|
}
|
||
|
return acct
|
||
|
}
|
||
|
|
||
|
func GetAccountByID(recordID string, principal *User) (Account, error) {
|
||
|
sugar.Debug("app.GetAccountByID: 📥")
|
||
|
if recordID == "" {
|
||
|
return Account{}, fmt.Errorf("app.getAccountByID: 💣 ⛔ key is blank")
|
||
|
}
|
||
|
obj, ok := accountCache.get(recordID)
|
||
|
if ok {
|
||
|
sugar.Debug("app.getAccountByID: 👍 🎯 📤")
|
||
|
return *obj, nil
|
||
|
}
|
||
|
crmParams := accounts.NewGetAccountsParamsWithTimeout(getTimeout)
|
||
|
crmParams.AccountID = &recordID
|
||
|
response, err := crmClient.Accounts.GetAccounts(crmParams, principal.Auth)
|
||
|
if err != nil {
|
||
|
return Account{}, err
|
||
|
}
|
||
|
var newObj *Account
|
||
|
for _, itm := range response.Payload.Data { // single iteration execution
|
||
|
newObj = UnMarshalAccount(itm)
|
||
|
}
|
||
|
accountCache.put(recordID, newObj)
|
||
|
sugar.Debug("app.getAccountByID: 👍 🆕 📤")
|
||
|
return *newObj, nil
|
||
|
}
|