lib/app/coordinate-services.go

52 lines
1.8 KiB
Go

package app
import (
"fmt"
"code.tnxs.net/taxnexus/lib/api/geo/geo_client/coordinate"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
)
// GetCoordinate is a first class object retrieval method
func GetCoordinate(addr *Address, principal *User) (*geo_models.CoordinateBasic, error) {
sugar.Debug("ops.getCoordinate: 📥")
if addr == nil {
return nil, fmt.Errorf("ops.getCoordinate: 💣 ⛔ Address cannot be nil")
}
if addr.ToString() == "" {
return nil, fmt.Errorf("ops.getCoordinate: 💣 ⛔ Address cannot be empty")
}
sugar.Debugf("ops.getCoordinate: 📏 address: %s", addr.ToString())
if addr.City == "" {
return nil, fmt.Errorf("ops.getCoordinate: 💣 ⛔ City cannot be blank")
}
obj, ok := coordinateCache.get(addr.ToString())
if ok {
sugar.Debugf("ops.GetCoordiante: 👍🏻 🎯 📤")
return obj, nil
}
placeName := addr.City + ", " + addr.StateCode
if addr.StateCode == "" {
placeName = addr.City + ", " + addr.State
}
coordParams := coordinate.NewGetCoordinateBasicParamsWithTimeout(getTimeout)
theAddress := addr.ToString()
coordParams.PlaceName = &placeName
coordParams.Address = &theAddress
response, geoErr := geoClient.Coordinate.GetCoordinateBasic(coordParams, principal.Auth)
if geoErr != nil {
return nil, fmt.Errorf("ops.getCoordinate: 💣 ⛔ failed, %s (%w)", *coordParams.Address, geoErr)
}
if len(response.Payload.Data) != 1 {
return nil, fmt.Errorf("ops.getCoordinate: 💣 ⛔ one and only one coordinate record should be returned")
}
sugar.Debugf("ops.getCoordinate: ✅ geo records retrieved = %d", len(response.Payload.Data))
var coord geo_models.CoordinateBasic
for _, itm := range response.Payload.Data {
coord = *itm
}
coordinateCache.put(addr.ToString(), &coord)
sugar.Debug("ops.getCoordinate: 👍 📤")
return &coord, nil
}