lib/app/account-services.go

91 lines
2.7 KiB
Go
Raw Normal View History

2021-01-27 22:39:23 +00:00
package app
import (
"fmt"
"code.tnxs.net/taxnexus/lib/api/crm/crm_client/accounts"
2021-01-28 01:58:08 +00:00
"code.tnxs.net/taxnexus/lib/api/geo/geo_client/coordinate"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
2021-01-27 22:39:23 +00:00
)
2021-01-27 22:45:50 +00:00
// GetAccount is first class retrieval function
2021-01-28 01:58:08 +00:00
func GetAccount(key string, principal *User) *Account {
2021-01-27 22:39:23 +00:00
if key == "" {
2021-01-28 01:58:08 +00:00
return nil
2021-01-27 22:39:23 +00:00
}
a, ok := accountCache.get(key)
if ok {
2021-01-28 01:58:08 +00:00
return a
2021-01-27 22:39:23 +00:00
}
acct, err := GetAccountByID(key, principal)
if err != nil {
2021-01-28 01:58:08 +00:00
return nil
2021-01-27 22:39:23 +00:00
}
return acct
}
2021-01-27 22:45:50 +00:00
// GetAccountByID is first class retrieval function
2021-01-28 01:58:08 +00:00
func GetAccountByID(recordID string, principal *User) (*Account, error) {
2021-01-27 22:39:23 +00:00
sugar.Debug("app.GetAccountByID: 📥")
if recordID == "" {
2021-01-28 01:58:08 +00:00
return nil, fmt.Errorf("app.getAccountByID: 💣 ⛔ key is blank")
2021-01-27 22:39:23 +00:00
}
obj, ok := accountCache.get(recordID)
if ok {
sugar.Debug("app.getAccountByID: 👍 🎯 📤")
2021-01-28 01:58:08 +00:00
return obj, nil
2021-01-27 22:39:23 +00:00
}
crmParams := accounts.NewGetAccountsParamsWithTimeout(getTimeout)
crmParams.AccountID = &recordID
response, err := crmClient.Accounts.GetAccounts(crmParams, principal.Auth)
if err != nil {
2021-01-28 01:58:08 +00:00
return nil, err
2021-01-27 22:39:23 +00:00
}
var newObj *Account
for _, itm := range response.Payload.Data { // single iteration execution
newObj = UnMarshalAccount(itm)
}
accountCache.put(recordID, newObj)
sugar.Debug("app.getAccountByID: 👍 🆕 📤")
2021-01-28 01:58:08 +00:00
return newObj, nil
}
// GetCoordinate is a first class retrieval function
func (obj *Account) GetCoordinate(principal *User) *Coordinate {
sugar.Debug("app.Account.getCoordinate: 📥")
if obj.CoordinateID != "" { // if CoordinateID is set, then just get it
geoParams := coordinate.NewGetCoordinatesParamsWithTimeout(getTimeout)
geoParams.CoordinateID = &obj.CoordinateID
response, err := geoClient.Coordinate.GetCoordinates(geoParams, principal.Auth)
if err != nil {
return nil
}
var obj *Coordinate
for _, itm := range response.Payload.Data { // single iteration execution
obj = UnMarshalCoordinate(itm, principal)
}
return obj
} // else get it via addresses, shipping #1, Business #2
if obj.ShippingAddress.ToString() == "" && obj.BillingAddress.ToString() == "" {
sugar.Errorf("app.Account.getCoordinate: 💣 ⛔ billing and shipping address both blank")
return nil
}
theAddress := obj.ShippingAddress.ToString()
if theAddress == "" {
theAddress = obj.BillingAddress.ToString()
}
geoParams := coordinate.NewGetCoordinatesParamsWithTimeout(getTimeout)
geoParams.Address = &theAddress
response, err := geoClient.Coordinate.GetCoordinates(geoParams, principal.Auth)
if err != nil {
sugar.Error(err)
return nil
}
var swag *geo_models.Coordinate
for _, itm := range response.Payload.Data { // single iteration execution
swag = itm
}
sugar.Debug("app.Account.getCoordinate: 👍 📤")
return UnMarshalCoordinate(swag, principal)
2021-01-27 22:39:23 +00:00
}