an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

79 lines
2.1 KiB

package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// ClockEventData represents the response data returned from the ServiceTrade clock endpoint.
type ClockEventData struct {
TotalPages int `json:"totalPages"`
Page int `json:"page"`
ActivityTime *ClockActivityMap `json:"activityTime"`
PairedEvents []ClockPairedEvent `json:"pairedEvents"`
UnpairedEvents []ClockEvent `json:"unpairedEvents"`
}
// ClockActivityMap captures total elapsed seconds for each activity type.
type ClockActivityMap struct {
Onsite int64 `json:"onsite"`
Offsite int64 `json:"offsite"`
Enroute int64 `json:"enroute"`
Onbreak int64 `json:"onbreak"`
}
// ClockPairedEvent represents a paired start/end clock event.
type ClockPairedEvent struct {
ElapsedTime int64 `json:"elapsedTime"`
Start ClockEvent `json:"start"`
End ClockEvent `json:"end"`
}
// ClockEvent represents an individual clock event.
type ClockEvent struct {
EventTime int64 `json:"eventTime"`
EventType string `json:"eventType"`
Activity string `json:"activity"`
Source string `json:"source"`
User ClockUser `json:"user"`
}
// ClockUser represents a technician associated with a clock event.
type ClockUser struct {
Name string `json:"name"`
Email string `json:"email"`
}
type clockResponse struct {
Data ClockEventData `json:"data"`
}
// GetJobClockEvents retrieves paired clock events for a given job identifier.
func (s *Session) GetJobClockEvents(jobID string) (*ClockEventData, error) {
endpoint := fmt.Sprintf("/clock?jobId=%s", jobID)
resp, err := s.DoRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API returned error: %s - %s", resp.Status, string(body))
}
var result clockResponse
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error unmarshalling clock response: %w", err)
}
return &result.Data, nil
}