Add PostTimeEntry

This commit is contained in:
Steven Polley 2018-08-25 18:17:29 -06:00
parent c341f047a5
commit a12e38597f

View File

@ -7,73 +7,73 @@ import (
//TimeEntry is a struct to hold the unmarshaled JSON data when making a call to the Time API //TimeEntry is a struct to hold the unmarshaled JSON data when making a call to the Time API
type TimeEntry struct { type TimeEntry struct {
ID int `json:"id"` ID int `json:"id,omitempty"`
Company struct { Company struct {
ID int `json:"id"` ID int `json:"id,omitempty"`
Identifier string `json:"identifier"` Identifier string `json:"identifier,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Info struct { Info struct {
CompanyHref string `json:"company_href"` CompanyHref string `json:"company_href,omitempty"`
MobileGUID string `json:"mobileGuid"` MobileGUID string `json:"mobileGuid,omitempty"`
} `json:"_info"` } `json:"_info,omitempty"`
} `json:"company"` } `json:"company,omitempty"`
ChargeToID int `json:"chargeToId"` ChargeToID int `json:"chargeToId,omitempty"`
ChargeToType string `json:"chargeToType"` ChargeToType string `json:"chargeToType,omitempty"`
Member struct { Member struct {
ID int `json:"id"` ID int `json:"id,omitempty"`
Identifier string `json:"identifier"` Identifier string `json:"identifier,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Info struct { Info struct {
MemberHref string `json:"member_href"` MemberHref string `json:"member_href,omitempty"`
ImageHref string `json:"image_href"` ImageHref string `json:"image_href,omitempty"`
} `json:"_info"` } `json:"_info,omitempty"`
} `json:"member"` } `json:"member,omitempty"`
LocationID int `json:"locationId"` LocationID int `json:"locationId,omitempty"`
BusinessUnitID int `json:"businessUnitId"` BusinessUnitID int `json:"businessUnitId,omitempty"`
WorkType struct { WorkType struct {
ID int `json:"id"` ID int `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Info struct { Info struct {
WorkTypeHref string `json:"workType_href"` WorkTypeHref string `json:"workType_href,omitempty"`
} `json:"_info"` } `json:"_info,omitempty"`
} `json:"workType"` } `json:"workType,omitempty"`
WorkRole struct { WorkRole struct {
ID int `json:"id"` ID int `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Info struct { Info struct {
WorkRoleHref string `json:"workRole_href"` WorkRoleHref string `json:"workRole_href,omitempty"`
} `json:"_info"` } `json:"_info,omitempty"`
} `json:"workRole"` } `json:"workRole,omitempty"`
TimeStart string `json:"timeStart"` TimeStart string `json:"timeStart,omitempty"`
TimeEnd string `json:"timeEnd"` TimeEnd string `json:"timeEnd,omitempty"`
HoursDeduct float64 `json:"hoursDeduct"` HoursDeduct float64 `json:"hoursDeduct,omitempty"`
ActualHours float64 `json:"actualHours"` ActualHours float64 `json:"actualHours,omitempty"`
BillableOption string `json:"billableOption"` BillableOption string `json:"billableOption,omitempty"`
Notes string `json:"notes"` Notes string `json:"notes,omitempty"`
AddToDetailDescriptionFlag bool `json:"addToDetailDescriptionFlag"` AddToDetailDescriptionFlag bool `json:"addToDetailDescriptionFlag,omitempty"`
AddToInternalAnalysisFlag bool `json:"addToInternalAnalysisFlag"` AddToInternalAnalysisFlag bool `json:"addToInternalAnalysisFlag,omitempty"`
AddToResolutionFlag bool `json:"addToResolutionFlag"` AddToResolutionFlag bool `json:"addToResolutionFlag,omitempty"`
EmailResourceFlag bool `json:"emailResourceFlag"` EmailResourceFlag bool `json:"emailResourceFlag,omitempty"`
EmailContactFlag bool `json:"emailContactFlag"` EmailContactFlag bool `json:"emailContactFlag,omitempty"`
EmailCcFlag bool `json:"emailCcFlag"` EmailCcFlag bool `json:"emailCcFlag,omitempty"`
HoursBilled float64 `json:"hoursBilled"` HoursBilled float64 `json:"hoursBilled,omitempty"`
EnteredBy string `json:"enteredBy"` EnteredBy string `json:"enteredBy,omitempty"`
DateEntered string `json:"dateEntered"` DateEntered string `json:"dateEntered,omitempty"`
MobileGUID string `json:"mobileGuid"` MobileGUID string `json:"mobileGuid,omitempty"`
HourlyRate float64 `json:"hourlyRate"` HourlyRate float64 `json:"hourlyRate,omitempty"`
TimeSheet struct { TimeSheet struct {
ID int `json:"id"` ID int `json:"id,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
Info struct { Info struct {
TimeSheetHref string `json:"timeSheet_href"` TimeSheetHref string `json:"timeSheet_href,omitempty"`
} `json:"_info"` } `json:"_info,omitempty"`
} `json:"timeSheet"` } `json:"timeSheet,omitempty"`
Status string `json:"status"` Status string `json:"status,omitempty"`
Info struct { Info struct {
LastUpdated string `json:"lastUpdated"` LastUpdated string `json:"lastUpdated,omitempty"`
UpdatedBy string `json:"updatedBy"` UpdatedBy string `json:"updatedBy,omitempty"`
ChargeToMobileGUID string `json:"chargeToMobileGuid"` ChargeToMobileGUID string `json:"chargeToMobileGuid,omitempty"`
} `json:"_info"` } `json:"_info,omitempty"`
CustomFields []struct { CustomFields []struct {
ID int ID int
Caption string Caption string
@ -81,7 +81,62 @@ type TimeEntry struct {
EntryMethod string EntryMethod string
NumberOfDecimals int NumberOfDecimals int
Value string Value string
} } `json:",omitempty"`
}
//TimeEntryPost - We need a special struct for posting anything because Golang will not omit an empty struct, and CW doesn't like that. FFS KEN THOMPSON
type TimeEntryPost struct {
ID int `json:"id,omitempty"`
/*
Company struct {
ID int `json:"id,omitempty"`
Identifier string `json:"identifier,omitempty"`
Name string `json:"name,omitempty"`
} `json:"company,omitempty"`*/
ChargeToID int `json:"chargeToId,omitempty"`
ChargeToType string `json:"chargeToType,omitempty"`
/*Member struct {
ID int `json:"id,omitempty"`
Identifier string `json:"identifier,omitempty"`
Name string `json:"name,omitempty"`
} `json:"member,omitempty"`
*/
LocationID int `json:"locationId,omitempty"`
BusinessUnitID int `json:"businessUnitId,omitempty"`
/* WorkType struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Info struct {
WorkTypeHref string `json:"workType_href,omitempty"`
} `json:"_info,omitempty"`
} `json:"workType,omitempty"`
*/
TimeStart string `json:"timeStart,omitempty"`
TimeEnd string `json:"timeEnd,omitempty"`
HoursDeduct float64 `json:"hoursDeduct,omitempty"`
ActualHours float64 `json:"actualHours,omitempty"`
BillableOption string `json:"billableOption,omitempty"`
Notes string `json:"notes,omitempty"`
AddToDetailDescriptionFlag bool `json:"addToDetailDescriptionFlag,omitempty"`
AddToInternalAnalysisFlag bool `json:"addToInternalAnalysisFlag,omitempty"`
AddToResolutionFlag bool `json:"addToResolutionFlag,omitempty"`
EmailResourceFlag bool `json:"emailResourceFlag,omitempty"`
EmailContactFlag bool `json:"emailContactFlag,omitempty"`
EmailCcFlag bool `json:"emailCcFlag,omitempty"`
HoursBilled float64 `json:"hoursBilled,omitempty"`
EnteredBy string `json:"enteredBy,omitempty"`
DateEntered string `json:"dateEntered,omitempty"`
MobileGUID string `json:"mobileGuid,omitempty"`
HourlyRate float64 `json:"hourlyRate,omitempty"`
/*TimeSheet struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Info struct {
TimeSheetHref string `json:"timeSheet_href,omitempty"`
} `json:"_info,omitempty"`
} `json:"timeSheet,omitempty"`
*/
Status string `json:"status,omitempty"`
} }
//GetTimeEntryByID expects a time entry ID and will return a pointer to a TimeEntry struct //GetTimeEntryByID expects a time entry ID and will return a pointer to a TimeEntry struct
@ -113,8 +168,36 @@ func (cw *Site) GetTimeEntriesByMember(memberIdentifier string) (*[]TimeEntry, e
timeEntries := &[]TimeEntry{} timeEntries := &[]TimeEntry{}
err = json.Unmarshal(req.Body, timeEntries) err = json.Unmarshal(req.Body, timeEntries)
if err != nil { if err != nil {
fmt.Println(string(req.Body))
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err) return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
} }
return timeEntries, nil return timeEntries, nil
} }
//PostTimeEntry accepts a preconfigured pointer to connectwise.TimeEntry struct with all the data that is to be posted.
func (cw *Site) PostTimeEntry(timeEntry *TimeEntryPost) (*TimeEntry, error) {
js, err := json.Marshal(timeEntry)
if err != nil {
return nil, fmt.Errorf("could not marshal timeEntry struct to json bytes: %s", err)
}
fmt.Println(string(js))
req := cw.NewRequest("/time/entries", "POST", js)
err = req.Do()
if err != nil {
return nil, fmt.Errorf("post request failed for %s: %s", req.RestAction, err)
}
fmt.Println(string(req.Body))
te := &TimeEntry{}
err = json.Unmarshal(req.Body, te)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal body into struct: %s", err)
}
return te, nil
}