2021-01-27 22:39:23 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/accounts"
|
|
|
|
)
|
|
|
|
|
2021-01-27 22:45:50 +00:00
|
|
|
// GetAccount is first class retrieval function
|
2021-01-27 22:39:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:45:50 +00:00
|
|
|
// GetAccountByID is first class retrieval function
|
2021-01-27 22:39:23 +00:00
|
|
|
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
|
|
|
|
}
|