2018-06-23 05:31:09 +00:00
package connectwise
import (
2018-06-23 05:35:42 +00:00
"encoding/json"
2018-06-23 05:31:09 +00:00
"fmt"
)
2018-07-07 00:26:11 +00:00
//TimeEntry is a struct to hold the unmarshaled JSON data when making a call to the Time API
2018-06-23 05:35:42 +00:00
type TimeEntry struct {
2018-08-26 00:17:29 +00:00
ID int ` json:"id,omitempty" `
2018-06-23 05:35:42 +00:00
Company struct {
2018-08-26 00:17:29 +00:00
ID int ` json:"id,omitempty" `
Identifier string ` json:"identifier,omitempty" `
Name string ` json:"name,omitempty" `
2018-06-23 05:35:42 +00:00
Info struct {
2018-08-26 00:17:29 +00:00
CompanyHref string ` json:"company_href,omitempty" `
MobileGUID string ` json:"mobileGuid,omitempty" `
} ` json:"_info,omitempty" `
} ` json:"company,omitempty" `
ChargeToID int ` json:"chargeToId,omitempty" `
ChargeToType string ` json:"chargeToType,omitempty" `
2018-06-23 05:35:42 +00:00
Member struct {
2018-08-26 00:17:29 +00:00
ID int ` json:"id,omitempty" `
Identifier string ` json:"identifier,omitempty" `
Name string ` json:"name,omitempty" `
2018-06-23 05:35:42 +00:00
Info struct {
2018-08-26 00:17:29 +00:00
MemberHref string ` json:"member_href,omitempty" `
ImageHref string ` json:"image_href,omitempty" `
} ` json:"_info,omitempty" `
} ` json:"member,omitempty" `
LocationID int ` json:"locationId,omitempty" `
BusinessUnitID int ` json:"businessUnitId,omitempty" `
2018-06-23 05:35:42 +00:00
WorkType struct {
2018-08-26 00:17:29 +00:00
ID int ` json:"id,omitempty" `
Name string ` json:"name,omitempty" `
2018-06-23 05:35:42 +00:00
Info struct {
2018-08-26 00:17:29 +00:00
WorkTypeHref string ` json:"workType_href,omitempty" `
} ` json:"_info,omitempty" `
} ` json:"workType,omitempty" `
2018-06-23 05:35:42 +00:00
WorkRole struct {
2018-08-26 00:17:29 +00:00
ID int ` json:"id,omitempty" `
Name string ` json:"name,omitempty" `
2018-06-23 05:35:42 +00:00
Info struct {
2018-08-26 00:17:29 +00:00
WorkRoleHref string ` json:"workRole_href,omitempty" `
} ` json:"_info,omitempty" `
} ` json:"workRole,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" `
2018-06-23 05:35:42 +00:00
TimeSheet struct {
2018-08-26 00:17:29 +00:00
ID int ` json:"id,omitempty" `
Name string ` json:"name,omitempty" `
2018-06-23 05:35:42 +00:00
Info struct {
2018-08-26 00:17:29 +00:00
TimeSheetHref string ` json:"timeSheet_href,omitempty" `
} ` json:"_info,omitempty" `
} ` json:"timeSheet,omitempty" `
Status string ` json:"status,omitempty" `
2018-06-23 05:35:42 +00:00
Info struct {
2018-08-26 00:17:29 +00:00
LastUpdated string ` json:"lastUpdated,omitempty" `
UpdatedBy string ` json:"updatedBy,omitempty" `
ChargeToMobileGUID string ` json:"chargeToMobileGuid,omitempty" `
} ` json:"_info,omitempty" `
2018-06-25 18:54:10 +00:00
CustomFields [ ] struct {
2018-06-23 05:39:02 +00:00
ID int
Caption string
Type string
EntryMethod string
NumberOfDecimals int
Value string
2018-08-26 00:17:29 +00:00
} ` 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" `
2018-06-23 05:35:42 +00:00
}
2018-07-07 00:26:11 +00:00
//GetTimeEntryByID expects a time entry ID and will return a pointer to a TimeEntry struct
2018-07-07 00:29:53 +00:00
func ( cw * Site ) GetTimeEntryByID ( timeEntryID int ) ( * TimeEntry , error ) {
2018-07-08 21:16:59 +00:00
req := cw . NewRequest ( fmt . Sprintf ( "/time/entries/%d" , timeEntryID ) , "GET" , nil )
2018-07-07 23:18:59 +00:00
err := req . Do ( )
2018-07-06 03:20:52 +00:00
if err != nil {
2018-07-07 23:18:59 +00:00
return nil , fmt . Errorf ( "request failed for %s: %s" , req . RestAction , err )
2018-07-06 03:20:52 +00:00
}
2018-06-23 05:31:09 +00:00
2018-07-07 23:18:59 +00:00
timeEntry := & TimeEntry { }
err = json . Unmarshal ( req . Body , timeEntry )
2018-07-06 03:20:52 +00:00
if err != nil {
2018-07-06 14:37:53 +00:00
return nil , fmt . Errorf ( "failed to unmarshal body into struct: %s" , err )
2018-07-06 03:20:52 +00:00
}
2018-06-23 05:31:09 +00:00
2018-07-07 23:18:59 +00:00
return timeEntry , nil
2018-06-23 05:31:09 +00:00
}
2018-08-23 04:02:22 +00:00
func ( cw * Site ) GetTimeEntriesByMember ( memberIdentifier string ) ( * [ ] TimeEntry , error ) {
req := cw . NewRequest ( "/time/entries" , "GET" , nil )
req . URLValues . Add ( "conditions" , "member/identifier=\"" + memberIdentifier + "\"" )
req . URLValues . Add ( "orderBy" , "id desc" )
err := req . Do ( )
if err != nil {
return nil , fmt . Errorf ( "request failed for %s: %s" , req . RestAction , err )
}
timeEntries := & [ ] TimeEntry { }
err = json . Unmarshal ( req . Body , timeEntries )
if err != nil {
2018-08-26 00:17:29 +00:00
fmt . Println ( string ( req . Body ) )
2018-08-23 04:02:22 +00:00
return nil , fmt . Errorf ( "failed to unmarshal body into struct: %s" , err )
2018-08-26 00:17:29 +00:00
2018-08-23 04:02:22 +00:00
}
return timeEntries , nil
}
2018-08-26 00:17:29 +00:00
//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
}