lib/app/county-services.go

62 lines
1.5 KiB
Go
Raw Permalink Normal View History

2021-01-27 22:39:23 +00:00
package app
import (
"fmt"
"code.tnxs.net/taxnexus/lib/api/geo/geo_client/county"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
)
2021-01-27 22:45:50 +00:00
// GetCountyNameByGeocode is first class retrieval function
2021-01-27 22:39:23 +00:00
func GetCountyNameByGeocode(key string, principal *User) string {
if key == "" {
return ""
}
if len(key) == countyGeocodeLength {
c, ok := countyCache.get(key)
if ok {
return c.Name
}
obj, err := GetCountyByGeocode(key, principal)
if err != nil {
return ""
}
return obj.Name
}
obj, ok := placeCache.get(key)
if ok {
return obj.SalesTaxRate.County
}
2021-01-27 22:45:50 +00:00
place, err := GetPlaceByGeocode(key, principal)
2021-01-27 22:39:23 +00:00
if err != nil {
return ""
}
return place.SalesTaxRate.County
}
2021-01-27 22:45:50 +00:00
// GetCountyByGeocode is first class retrieval function
2021-01-27 22:39:23 +00:00
func GetCountyByGeocode(recordID string, principal *User) (geo_models.County, error) {
2021-01-28 19:17:39 +00:00
sugar.Debug("app.getCountyByGeocode: 📥")
2021-01-27 22:39:23 +00:00
if recordID == "" {
2021-01-28 19:17:39 +00:00
return geo_models.County{}, fmt.Errorf("app.getCountyByID: 💣 ⛔ key is blank")
2021-01-27 22:39:23 +00:00
}
obj, ok := countyCache.get(recordID)
if ok {
2021-01-28 19:17:39 +00:00
sugar.Debug("app.getCountyByGeocode: 👍 🎯 📤")
2021-01-27 22:39:23 +00:00
return *obj, nil
}
geoParams := county.NewGetCountiesParamsWithTimeout(getTimeout)
geoParams.Geocode = &recordID
response, err := geoClient.County.GetCounties(geoParams, principal.Auth)
if err != nil {
return geo_models.County{}, err
}
var newObj *geo_models.County
for _, itm := range response.Payload.Data { // single iteration execution
newObj = itm
}
countyCache.put(recordID, newObj)
2021-01-28 19:17:39 +00:00
sugar.Debug("app.getCountyByGeocode: 👍 🆕 📤")
2021-01-27 22:39:23 +00:00
return *newObj, nil
}