lib/app/address-services.go

53 lines
2.0 KiB
Go
Raw Normal View History

2021-02-02 04:19:16 +00:00
package app
import (
"fmt"
"code.tnxs.net/taxnexus/lib/api/geo/geo_models"
)
2021-02-05 22:04:17 +00:00
// GeocodeAddressParams is a params type
2021-02-02 04:19:16 +00:00
type GeocodeAddressParams struct {
BusinessAddress *Address
Account *Account
Ref string
}
2021-02-05 22:04:17 +00:00
// GeocodeAddress returns a full Coordinate object in response to Business Address
2021-02-02 04:19:16 +00:00
func GeocodeAddress(params GeocodeAddressParams, principal *User) (*geo_models.CoordinateBasic, error) {
if params.BusinessAddress == nil {
sugar.Infof("ops.geocodeAddress: ❗ Business Address is null, ref = %s", params.Ref)
deliveryAddress, acctErr := GetDefaultDeliveryAddress(params.Account, principal)
if acctErr != nil {
return nil, fmt.Errorf("ops.geocodeAddress: 💣 ⛔ can't determine default delivery address: %w", acctErr)
}
params.BusinessAddress = deliveryAddress
}
if params.BusinessAddress.City == "" {
sugar.Infof("ops.geocodeAddress: ❗ Business Address is blank, ref = %s", params.Ref)
deliveryAddress, acctErr := GetDefaultDeliveryAddress(params.Account, principal)
if acctErr != nil {
return nil, fmt.Errorf("ops.geocodeAddress: 💣 ⛔ can't determine default delivery address: %w", acctErr)
}
params.BusinessAddress = deliveryAddress
}
var situsCoordinate *geo_models.CoordinateBasic
aCoordinate, err := GetCoordinate(params.BusinessAddress, principal)
if err == nil {
situsCoordinate = aCoordinate
} else {
sugar.Infof("ops.geocodeAddress: ❗ can't geocode address: %w", err)
deliveryAddress, acctErr := GetDefaultDeliveryAddress(params.Account, principal)
if acctErr != nil {
return nil, fmt.Errorf("ops.geocodeAddress: 💣 ⛔ can't determine default delivery address: %w", acctErr)
}
aCoordinate, coordErr := GetCoordinate(deliveryAddress, principal)
if coordErr != nil {
return nil, fmt.Errorf("ops.geocodeAddress: 💣 ⛔ can't determine default delivery coordinates: %w", acctErr)
}
sugar.Infof("ops.geocodeAddress: ❗ alternate address used for ref = %s", params.Ref)
situsCoordinate = aCoordinate
}
return situsCoordinate, nil
}