github.com/schmorrison/Zoho@v1.1.4/shifts/timesheets.go (about)

     1  package shifts
     2  
     3  import (
     4  	"fmt"
     5  
     6  	zoho "github.com/schmorrison/Zoho"
     7  )
     8  
     9  // GetAllTimesheets returns a list of all employee timesheets
    10  // https://www.zoho.com/shifts/api/v1/timesheets-api/#get-all-time-entries
    11  func (s *API) GetAllTimesheets(
    12  	params map[string]zoho.Parameter,
    13  ) (data GetTimesheetsResponse, err error) {
    14  	endpoint := zoho.Endpoint{
    15  		Name: "GetAllTimesheets",
    16  		URL: fmt.Sprintf(
    17  			"https://shifts.zoho.%s/api/v1/%s/%s",
    18  			s.ZohoTLD,
    19  			s.OrganizationID,
    20  			TimesheetsModule,
    21  		),
    22  		Method:       zoho.HTTPGet,
    23  		ResponseData: &GetTimesheetsResponse{},
    24  		URLParameters: map[string]zoho.Parameter{
    25  			"schedules":   "",
    26  			"status":      "", // pending, approved
    27  			"employee_id": "",
    28  			"start_date":  "", // yyyy-mm-dd
    29  			"end_date":    "", // yyyy-mm-dd
    30  			"limit":       "50",
    31  			"page":        "1",
    32  		},
    33  	}
    34  
    35  	if len(params) > 0 {
    36  		for k, v := range params {
    37  			endpoint.URLParameters[k] = v
    38  		}
    39  	}
    40  
    41  	err = s.Zoho.HTTPRequest(&endpoint)
    42  	if err != nil {
    43  		return GetTimesheetsResponse{}, fmt.Errorf("failed to retrieve timesheets: %s", err)
    44  	}
    45  	if v, ok := endpoint.ResponseData.(*GetTimesheetsResponse); ok {
    46  		return *v, nil
    47  	}
    48  	return GetTimesheetsResponse{}, fmt.Errorf("data retrieved was not 'GetTimesheetsResponse'")
    49  }
    50  
    51  type GetTimesheetsResponse struct {
    52  	TimeEntries []struct {
    53  		ID               string  `json:"id,omitempty"`
    54  		StartTime        *Time   `json:"start_time,omitempty"`
    55  		EndTime          *Time   `json:"end_time,omitempty"`
    56  		EmployeeID       string  `json:"employee_id,omitempty"`
    57  		Employee         string  `json:"employee,omitempty"`
    58  		ScheduleID       string  `json:"schedule_id,omitempty"`
    59  		Schedule         string  `json:"schedule,omitempty"`
    60  		PositionID       string  `json:"position_id,omitempty"`
    61  		Position         string  `json:"position,omitempty"`
    62  		JobSiteID        string  `json:"job_site_id,omitempty"`
    63  		JobSite          string  `json:"job_site,omitempty"`
    64  		Type             string  `json:"type,omitempty"`
    65  		Duration         float64 `json:"duration,string,omitempty"`
    66  		BreakDuration    float64 `json:"break_duration,string,omitempty"`
    67  		Notes            string  `json:"notes,omitempty"`
    68  		Status           string  `json:"status,omitempty"`
    69  		TimeoffRequestID string  `json:"timeoff_request_id,omitempty"`
    70  		TimeoffTypeID    string  `json:"timeoff_type_id,omitempty"`
    71  		TimeoffType      string  `json:"timeoff_type,omitempty"`
    72  		InLat            float64 `json:"in_lat,omitempty"`
    73  		InLng            float64 `json:"in_lng,omitempty"`
    74  		OutLat           float64 `json:"out_lat,omitempty"`
    75  		OutLng           float64 `json:"out_lng,omitempty"`
    76  		Latitude         float64 `json:"latitude,string,omitempty"`
    77  		Longitude        float64 `json:"longitude,string,omitempty"`
    78  		ShiftID          string  `json:"shift_id,omitempty"`
    79  		Breaks           []struct {
    80  			BreakID      string `json:"break_id,omitempty"`
    81  			BreakName    string `json:"break_name,omitempty"`
    82  			DurationMins int    `json:"duration_mins,omitempty"`
    83  			StartTime    *Time  `json:"start_time,omitempty"`
    84  			EndTime      *Time  `json:"end_time,omitempty"`
    85  			IsPaid       bool   `json:"is_paid,omitempty"`
    86  		} `json:"breaks,omitempty"`
    87  	} `json:"time_entries,omitempty"`
    88  	Meta struct {
    89  		Count int `json:"count,omitempty"`
    90  		Limit int `json:"limit,omitempty"`
    91  		Page  int `json:"page,omitempty"`
    92  	} `json:"meta,omitempty"`
    93  }
    94  
    95  // CreateTimesheet adds a new record to the list of employee timesheets
    96  // https://www.zoho.com/shifts/api/v1/timesheets-api/#create-a-time-entry
    97  func (s *API) CreateTimesheet(
    98  	request CreateTimesheetRequest,
    99  ) (data CreateTimesheetResponse, err error) {
   100  	endpoint := zoho.Endpoint{
   101  		Name: "CreateTimesheet",
   102  		URL: fmt.Sprintf(
   103  			"https://shifts.zoho.%s/api/v1/%s/%s",
   104  			s.ZohoTLD,
   105  			s.OrganizationID,
   106  			TimesheetsModule,
   107  		),
   108  		Method:       zoho.HTTPPost,
   109  		ResponseData: &CreateTimesheetResponse{},
   110  		RequestBody:  request,
   111  	}
   112  
   113  	if request.StartTime.IsZero() || request.EmployeeID == "" || request.ScheduleID == "" ||
   114  		request.PositionID == "" {
   115  		return CreateTimesheetResponse{}, fmt.Errorf(
   116  			"failed to create timesheet: start_time, employee_id, schedule_id, and position_id are required fields",
   117  		)
   118  	}
   119  
   120  	err = s.Zoho.HTTPRequest(&endpoint)
   121  	if err != nil {
   122  		return CreateTimesheetResponse{}, fmt.Errorf("failed to create timesheet: %s", err)
   123  	}
   124  
   125  	if v, ok := endpoint.ResponseData.(*CreateTimesheetResponse); ok {
   126  		return *v, nil
   127  	}
   128  
   129  	return CreateTimesheetResponse{}, fmt.Errorf("data retrieved was not 'CreateTimesheetResponse'")
   130  }
   131  
   132  type CreateTimesheetRequest struct {
   133  	StartTime  *Time  `json:"start_time"` // required
   134  	EndTime    *Time  `json:"end_time,omitempty"`
   135  	EmployeeID string `json:"employee_id"` // required
   136  	ScheduleID string `json:"schedule_id"` // required
   137  	PositionID string `json:"position_id"` // required
   138  	JobSiteID  string `json:"job_site_id,omitempty"`
   139  	Notes      string `json:"notes,omitempty"`
   140  	ShiftID    string `json:"shift_id,omitempty"`
   141  	Breaks     []struct {
   142  		BreakID      string `json:"break_id,omitempty"`
   143  		DurationMins int    `json:"duration_mins,omitempty"`
   144  		StartTime    *Time  `json:"start_time,omitempty"`
   145  		EndTime      *Time  `json:"end_time,omitempty"`
   146  	} `json:"breaks,omitempty"`
   147  }
   148  
   149  type CreateTimesheetResponse struct {
   150  	ID               string  `json:"id,omitempty"`
   151  	StartTime        *Time   `json:"start_time,omitempty"`
   152  	EndTime          *Time   `json:"end_time,omitempty"`
   153  	EmployeeID       string  `json:"employee_id,omitempty"`
   154  	Employee         string  `json:"employee,omitempty"`
   155  	ScheduleID       string  `json:"schedule_id,omitempty"`
   156  	Schedule         string  `json:"schedule,omitempty"`
   157  	PositionID       string  `json:"position_id,omitempty"`
   158  	Position         string  `json:"position,omitempty"`
   159  	JobSiteID        string  `json:"job_site_id,omitempty"`
   160  	JobSite          string  `json:"job_site,omitempty"`
   161  	Type             string  `json:"type,omitempty"`
   162  	Duration         float64 `json:"duration,string,omitempty"`
   163  	BreakDuration    float64 `json:"break_duration,string,omitempty"`
   164  	Notes            string  `json:"notes,omitempty"`
   165  	Status           string  `json:"status,omitempty"`
   166  	TimeoffRequestID string  `json:"timeoff_request_id,omitempty"`
   167  	TimeoffTypeID    string  `json:"timeoff_type_id,omitempty"`
   168  	TimeoffType      string  `json:"timeoff_type,omitempty"`
   169  	InLat            float64 `json:"in_lat,omitempty"`
   170  	InLng            float64 `json:"in_lng,omitempty"`
   171  	OutLat           float64 `json:"out_lat,omitempty"`
   172  	OutLng           float64 `json:"out_lng,omitempty"`
   173  	Latitude         float64 `json:"latitude,string,omitempty"`
   174  	Longitude        float64 `json:"longitude,string,omitempty"`
   175  	ShiftID          string  `json:"shift_id,omitempty"`
   176  	Breaks           []struct {
   177  		BreakID      string `json:"break_id,omitempty"`
   178  		BreakName    string `json:"break_name,omitempty"`
   179  		DurationMins int    `json:"duration_mins,omitempty"`
   180  		StartTime    *Time  `json:"start_time,omitempty"`
   181  		EndTime      *Time  `json:"end_time,omitempty"`
   182  		IsPaid       bool   `json:"is_paid,omitempty"`
   183  	} `json:"breaks,omitempty"`
   184  }
   185  
   186  // GetTimesheet retrieves the timesheet record with the given ID
   187  // https://www.zoho.com/shifts/api/v1/timesheets-api/#get-a-time-entry
   188  func (s *API) GetTimesheet(id string) (data GetTimesheetResponse, err error) {
   189  	endpoint := zoho.Endpoint{
   190  		Name: "GetTimesheet",
   191  		URL: fmt.Sprintf(
   192  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   193  			s.ZohoTLD,
   194  			s.OrganizationID,
   195  			TimesheetsModule,
   196  			id,
   197  		),
   198  		Method:       zoho.HTTPGet,
   199  		ResponseData: &GetTimesheetResponse{},
   200  	}
   201  
   202  	err = s.Zoho.HTTPRequest(&endpoint)
   203  	if err != nil {
   204  		return GetTimesheetResponse{}, fmt.Errorf("failed to retrieve timesheet with id: %s", err)
   205  	}
   206  
   207  	if v, ok := endpoint.ResponseData.(*GetTimesheetResponse); ok {
   208  		return *v, nil
   209  	}
   210  
   211  	return GetTimesheetResponse{}, fmt.Errorf("data returned was not 'GetTimesheetResponse'")
   212  }
   213  
   214  type GetTimesheetResponse struct {
   215  	ID               string  `json:"id,omitempty"`
   216  	StartTime        *Time   `json:"start_time,omitempty"`
   217  	EndTime          *Time   `json:"end_time,omitempty"`
   218  	EmployeeID       string  `json:"employee_id,omitempty"`
   219  	Employee         string  `json:"employee,omitempty"`
   220  	ScheduleID       string  `json:"schedule_id,omitempty"`
   221  	Schedule         string  `json:"schedule,omitempty"`
   222  	PositionID       string  `json:"position_id,omitempty"`
   223  	Position         string  `json:"position,omitempty"`
   224  	JobSiteID        string  `json:"job_site_id,omitempty"`
   225  	JobSite          string  `json:"job_site,omitempty"`
   226  	Type             string  `json:"type,omitempty"`
   227  	Duration         float64 `json:"duration,string,omitempty"`
   228  	BreakDuration    float64 `json:"break_duration,string,omitempty"`
   229  	Notes            string  `json:"notes,omitempty"`
   230  	Status           string  `json:"status,omitempty"`
   231  	TimeoffRequestID string  `json:"timeoff_request_id,omitempty"`
   232  	TimeoffTypeID    string  `json:"timeoff_type_id,omitempty"`
   233  	TimeoffType      string  `json:"timeoff_type,omitempty"`
   234  	InLat            float64 `json:"in_lat,omitempty"`
   235  	InLng            float64 `json:"in_lng,omitempty"`
   236  	OutLat           float64 `json:"out_lat,omitempty"`
   237  	OutLng           float64 `json:"out_lng,omitempty"`
   238  	Latitude         float64 `json:"latitude,string,omitempty"`
   239  	Longitude        float64 `json:"longitude,string,omitempty"`
   240  	ShiftID          string  `json:"shift_id,omitempty"`
   241  	Breaks           []struct {
   242  		BreakID      string `json:"break_id,omitempty"`
   243  		BreakName    string `json:"break_name,omitempty"`
   244  		DurationMins int    `json:"duration_mins,omitempty"`
   245  		StartTime    *Time  `json:"start_time,omitempty"`
   246  		EndTime      *Time  `json:"end_time,omitempty"`
   247  		IsPaid       bool   `json:"is_paid,omitempty"`
   248  	} `json:"breaks,omitempty"`
   249  }
   250  
   251  // UpdateTimesheet modifies the timesheet with the given ID
   252  // https://www.zoho.com/shifts/api/v1/timesheets-api/#update-a-time-entry
   253  func (s *API) UpdateTimesheet(
   254  	id string,
   255  	request UpdateTimesheetRequest,
   256  ) (data UpdateTimesheetResponse, err error) {
   257  	endpoint := zoho.Endpoint{
   258  		Name: "UpdateTimesheet",
   259  		URL: fmt.Sprintf(
   260  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   261  			s.ZohoTLD,
   262  			s.OrganizationID,
   263  			TimesheetsModule,
   264  			id,
   265  		),
   266  		Method:       zoho.HTTPPut,
   267  		ResponseData: &UpdateTimesheetResponse{},
   268  		RequestBody:  request,
   269  	}
   270  
   271  	err = s.Zoho.HTTPRequest(&endpoint)
   272  	if err != nil {
   273  		return UpdateTimesheetResponse{}, fmt.Errorf("failed to update timesheet: %s", err)
   274  	}
   275  
   276  	if v, ok := endpoint.ResponseData.(*UpdateTimesheetResponse); ok {
   277  		return *v, nil
   278  	}
   279  
   280  	return UpdateTimesheetResponse{}, fmt.Errorf("data retrieved was not 'UpdateTimesheetResponse'")
   281  }
   282  
   283  type UpdateTimesheetRequest struct {
   284  	StartTime  *Time  `json:"start_time,omitempty"`
   285  	EndTime    *Time  `json:"end_time,omitempty"`
   286  	EmployeeID string `json:"employee_id,omitempty"`
   287  	ScheduleID string `json:"schedule_id,omitempty"`
   288  	PositionID string `json:"position_id,omitempty"`
   289  	JobSiteID  string `json:"job_site_id,omitempty"`
   290  	Notes      string `json:"notes,omitempty"`
   291  	ShiftID    string `json:"shift_id,omitempty"`
   292  	Breaks     []struct {
   293  		BreakID      string `json:"break_id,omitempty"`
   294  		DurationMins int    `json:"duration_mins,omitempty"`
   295  		StartTime    *Time  `json:"start_time,omitempty"`
   296  		EndTime      *Time  `json:"end_time,omitempty"`
   297  	} `json:"breaks,omitempty"`
   298  }
   299  
   300  type UpdateTimesheetResponse struct {
   301  	ID               string  `json:"id,omitempty"`
   302  	StartTime        *Time   `json:"start_time,omitempty"`
   303  	EndTime          *Time   `json:"end_time,omitempty"`
   304  	EmployeeID       string  `json:"employee_id,omitempty"`
   305  	Employee         string  `json:"employee,omitempty"`
   306  	ScheduleID       string  `json:"schedule_id,omitempty"`
   307  	Schedule         string  `json:"schedule,omitempty"`
   308  	PositionID       string  `json:"position_id,omitempty"`
   309  	Position         string  `json:"position,omitempty"`
   310  	JobSiteID        string  `json:"job_site_id,omitempty"`
   311  	JobSite          string  `json:"job_site,omitempty"`
   312  	Type             string  `json:"type,omitempty"`
   313  	Duration         float64 `json:"duration,string,omitempty"`
   314  	BreakDuration    float64 `json:"break_duration,string,omitempty"`
   315  	Notes            string  `json:"notes,omitempty"`
   316  	Status           string  `json:"status,omitempty"`
   317  	TimeoffRequestID string  `json:"timeoff_request_id,omitempty"`
   318  	TimeoffTypeID    string  `json:"timeoff_type_id,omitempty"`
   319  	TimeoffType      string  `json:"timeoff_type,omitempty"`
   320  	InLat            float64 `json:"in_lat,omitempty"`
   321  	InLng            float64 `json:"in_lng,omitempty"`
   322  	OutLat           float64 `json:"out_lat,omitempty"`
   323  	OutLng           float64 `json:"out_lng,omitempty"`
   324  	Latitude         float64 `json:"latitude,string,omitempty"`
   325  	Longitude        float64 `json:"longitude,string,omitempty"`
   326  	ShiftID          string  `json:"shift_id,omitempty"`
   327  	Breaks           []struct {
   328  		BreakID      string `json:"break_id,omitempty"`
   329  		BreakName    string `json:"break_name,omitempty"`
   330  		DurationMins int    `json:"duration_mins,omitempty"`
   331  		StartTime    *Time  `json:"start_time,omitempty"`
   332  		EndTime      *Time  `json:"end_time,omitempty"`
   333  		IsPaid       bool   `json:"is_paid,omitempty"`
   334  	} `json:"breaks,omitempty"`
   335  }
   336  
   337  // DeleteTimesheet deletes the timesheet record with the given ID
   338  // https://www.zoho.com/shifts/api/v1/timesheets-api/#delete-a-time-entry
   339  func (s *API) DeleteTimesheet(id string) (data DeleteTimesheetResponse, err error) {
   340  	endpoint := zoho.Endpoint{
   341  		Name: "DeleteTimesheet",
   342  		URL: fmt.Sprintf(
   343  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   344  			s.ZohoTLD,
   345  			s.OrganizationID,
   346  			TimesheetsModule,
   347  			id,
   348  		),
   349  		Method:       zoho.HTTPDelete,
   350  		ResponseData: &DeleteTimesheetResponse{},
   351  	}
   352  
   353  	err = s.Zoho.HTTPRequest(&endpoint)
   354  	if err != nil {
   355  		return DeleteTimesheetResponse{}, fmt.Errorf("failed to delete timesheet with id: %s", err)
   356  	}
   357  
   358  	if v, ok := endpoint.ResponseData.(*DeleteTimesheetResponse); ok {
   359  		return *v, nil
   360  	}
   361  
   362  	return DeleteTimesheetResponse{}, fmt.Errorf("data returned was not 'DeleteTimesheetResponse'")
   363  }
   364  
   365  type DeleteTimesheetResponse struct {
   366  	Message string `json:"message,omitempty"`
   367  }