- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core) - Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token - Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers - Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
59 lines
1.3 KiB
Go
59 lines
1.3 KiB
Go
package mapbox
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
type Client struct {
|
|
AccessToken string
|
|
HTTPClient *http.Client
|
|
}
|
|
|
|
func New(token string) *Client {
|
|
return &Client{
|
|
AccessToken: token,
|
|
HTTPClient: &http.Client{
|
|
Timeout: 10 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
type directionsResponse struct {
|
|
Routes []struct {
|
|
Distance float64 `json:"distance"` // meters
|
|
Duration float64 `json:"duration"` // seconds
|
|
} `json:"routes"`
|
|
Code string `json:"code"`
|
|
}
|
|
|
|
// GetDrivingDistance returns distance in kilometers between two points
|
|
func (c *Client) GetDrivingDistance(lat1, lon1, lat2, lon2 float64) (float64, error) {
|
|
// Mapbox Directions API: /driving/{lon},{lat};{lon},{lat}
|
|
url := fmt.Sprintf("https://api.mapbox.com/directions/v5/mapbox/driving/%f,%f;%f,%f?access_token=%s",
|
|
lon1, lat1, lon2, lat2, c.AccessToken)
|
|
|
|
resp, err := c.HTTPClient.Get(url)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return 0, fmt.Errorf("mapbox api error: status %d", resp.StatusCode)
|
|
}
|
|
|
|
var result directionsResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if len(result.Routes) == 0 {
|
|
return 0, fmt.Errorf("no route found")
|
|
}
|
|
|
|
// Convert meters to km
|
|
return result.Routes[0].Distance / 1000.0, nil
|
|
}
|