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

     1  package shifts
     2  
     3  import (
     4  	"fmt"
     5  
     6  	zoho "github.com/schmorrison/Zoho"
     7  )
     8  
     9  // GetAllTimeoffRequests returns a list of all employee timeoff requests
    10  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#get-all-time-off-requests
    11  func (s *API) GetAllTimeoffRequests(
    12  	params map[string]zoho.Parameter,
    13  ) (data GetTimeoffsResponse, err error) {
    14  	endpoint := zoho.Endpoint{
    15  		Name: "GetAllTimeoffRequests",
    16  		URL: fmt.Sprintf(
    17  			"https://shifts.zoho.%s/api/v1/%s/%s/requests",
    18  			s.ZohoTLD,
    19  			s.OrganizationID,
    20  			TimeoffModule,
    21  		),
    22  		Method:       zoho.HTTPGet,
    23  		ResponseData: &GetTimeoffsResponse{},
    24  		URLParameters: map[string]zoho.Parameter{
    25  			"schedules":   "",
    26  			"status":      "", // pending, approved, denied, cancelled
    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 GetTimeoffsResponse{}, fmt.Errorf("failed to retrieve timeoff requests: %s", err)
    44  	}
    45  	if v, ok := endpoint.ResponseData.(*GetTimeoffsResponse); ok {
    46  		return *v, nil
    47  	}
    48  	return GetTimeoffsResponse{}, fmt.Errorf("data retrieved was not 'GetTimeoffsResponse'")
    49  }
    50  
    51  type GetTimeoffsResponse struct {
    52  	TimeOffRequests []struct {
    53  		ID            string  `json:"id,omitempty"`
    54  		StartDate     *Time   `json:"start_date,omitempty"`
    55  		EndDate       *Time   `json:"end_date,omitempty"`
    56  		EmployeeID    string  `json:"employee_id,omitempty"`
    57  		Employee      string  `json:"employee,omitempty"`
    58  		RequestedByID string  `json:"requested_by_id,omitempty"`
    59  		RequestedBy   string  `json:"requested_by,omitempty"`
    60  		TypeID        string  `json:"type_id,omitempty"`
    61  		Type          string  `json:"type,omitempty"`
    62  		DayType       string  `json:"day_type,omitempty"`
    63  		Duration      float64 `json:"duration,omitempty"`
    64  		Status        string  `json:"status,omitempty"`
    65  		CreatedAt     *Time   `json:"created_at,omitempty"`
    66  	} `json:"time_off_requests,omitempty"`
    67  	Meta struct {
    68  		Count int `json:"count,omitempty"`
    69  		Limit int `json:"limit,omitempty"`
    70  		Page  int `json:"page,omitempty"`
    71  	} `json:"meta,omitempty"`
    72  }
    73  
    74  // CreateTimeoffRequest adds a new record to the list of employee timeoff requests
    75  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#create-a-time-off-request
    76  func (s *API) CreateTimeoffRequest(
    77  	request CreateTimeoffRequest,
    78  ) (data CreateTimeoffResponse, err error) {
    79  	endpoint := zoho.Endpoint{
    80  		Name: "CreateTimeoffRequest",
    81  		URL: fmt.Sprintf(
    82  			"https://shifts.zoho.%s/api/v1/%s/%s/requests",
    83  			s.ZohoTLD,
    84  			s.OrganizationID,
    85  			TimeoffModule,
    86  		),
    87  		Method:       zoho.HTTPPost,
    88  		ResponseData: &CreateTimeoffResponse{},
    89  		RequestBody:  request,
    90  	}
    91  
    92  	if request.StartDate.IsZero() || request.EndDate.IsZero() || request.EmployeeID == "" ||
    93  		request.TypeID == "" ||
    94  		request.DayType == "" {
    95  		return CreateTimeoffResponse{}, fmt.Errorf(
    96  			"failed to create employee: start_date, end_date, employee_id, type_id, and day_type are required fields",
    97  		)
    98  	}
    99  
   100  	err = s.Zoho.HTTPRequest(&endpoint)
   101  	if err != nil {
   102  		return CreateTimeoffResponse{}, fmt.Errorf("failed to create timeoff request: %s", err)
   103  	}
   104  
   105  	if v, ok := endpoint.ResponseData.(*CreateTimeoffResponse); ok {
   106  		return *v, nil
   107  	}
   108  
   109  	return CreateTimeoffResponse{}, fmt.Errorf("data retrieved was not 'CreateTimeoffResponse'")
   110  }
   111  
   112  type CreateTimeoffRequest struct {
   113  	EmployeeID string `json:"employee_id,omitempty"`
   114  	StartDate  *Time  `json:"start_date"` // required
   115  	EndDate    *Time  `json:"end_date"`   // required
   116  	TypeID     string `json:"type_id"`    // required
   117  	DayType    string `json:"day_type"`   // required: all_day, partial
   118  	Reason     string `json:"reason,omitempty"`
   119  }
   120  
   121  type CreateTimeoffResponse struct {
   122  	ID             string  `json:"id,omitempty"`
   123  	StartDate      *Time   `json:"start_date,omitempty"`
   124  	EndDate        *Time   `json:"end_date,omitempty"`
   125  	EmployeeID     string  `json:"employee_id,omitempty"`
   126  	Employee       string  `json:"employee,omitempty"`
   127  	RequestedByID  string  `json:"requested_by_id,omitempty"`
   128  	RequestedBy    string  `json:"requested_by,omitempty"`
   129  	TypeID         string  `json:"type_id,omitempty"`
   130  	Type           string  `json:"type,omitempty"`
   131  	DayType        string  `json:"day_type,omitempty"`
   132  	Duration       float64 `json:"duration,omitempty"`
   133  	Status         string  `json:"status,omitempty"`
   134  	CreatedAt      *Time   `json:"created_at,omitempty"`
   135  	IsPaid         bool    `json:"is_paid,omitempty"`
   136  	Reason         string  `json:"reason,omitempty"`
   137  	ApproverID     string  `json:"approver_id,omitempty"`
   138  	Approver       string  `json:"approver,omitempty"`
   139  	ApprovalRespAt *Time   `json:"approval_resp_at,omitempty"`
   140  	CancelledAt    *Time   `json:"cancelled_at,omitempty"`
   141  	UpdatedAt      *Time   `json:"updated_at,omitempty"`
   142  	Comments       []struct {
   143  		CommentID   string `json:"comment_id,omitempty"`
   144  		Comment     string `json:"comment,omitempty"`
   145  		CommenterID string `json:"commenter_id,omitempty"`
   146  		Commenter   string `json:"commenter,omitempty"`
   147  		CreatedAt   *Time  `json:"created_at,omitempty"`
   148  	} `json:"comments,omitempty"`
   149  }
   150  
   151  // GetTimeoffRequest retrieves the timeoff request record with the given ID
   152  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#get-a-time-off-request
   153  func (s *API) GetTimeoffRequest(id string) (data GetTimeoffResponse, err error) {
   154  	endpoint := zoho.Endpoint{
   155  		Name: "GetTimeoffRequest",
   156  		URL: fmt.Sprintf(
   157  			"https://shifts.zoho.%s/api/v1/%s/%s/requests/%s",
   158  			s.ZohoTLD,
   159  			s.OrganizationID,
   160  			TimeoffModule,
   161  			id,
   162  		),
   163  		Method:       zoho.HTTPGet,
   164  		ResponseData: &GetTimeoffResponse{},
   165  	}
   166  
   167  	err = s.Zoho.HTTPRequest(&endpoint)
   168  	if err != nil {
   169  		return GetTimeoffResponse{}, fmt.Errorf(
   170  			"failed to retrieve timeoff request with id: %s",
   171  			err,
   172  		)
   173  	}
   174  
   175  	if v, ok := endpoint.ResponseData.(*GetTimeoffResponse); ok {
   176  		return *v, nil
   177  	}
   178  
   179  	return GetTimeoffResponse{}, fmt.Errorf("data returned was not 'GetTimeoffResponse'")
   180  }
   181  
   182  type GetTimeoffResponse struct {
   183  	ID             string  `json:"id,omitempty"`
   184  	StartDate      *Time   `json:"start_date,omitempty"`
   185  	EndDate        *Time   `json:"end_date,omitempty"`
   186  	EmployeeID     string  `json:"employee_id,omitempty"`
   187  	Employee       string  `json:"employee,omitempty"`
   188  	RequestedByID  string  `json:"requested_by_id,omitempty"`
   189  	RequestedBy    string  `json:"requested_by,omitempty"`
   190  	TypeID         string  `json:"type_id,omitempty"`
   191  	Type           string  `json:"type,omitempty"`
   192  	DayType        string  `json:"day_type,omitempty"`
   193  	Duration       float64 `json:"duration,omitempty"`
   194  	Status         string  `json:"status,omitempty"`
   195  	CreatedAt      *Time   `json:"created_at,omitempty"`
   196  	IsPaid         bool    `json:"is_paid,omitempty"`
   197  	Reason         string  `json:"reason,omitempty"`
   198  	ApproverID     string  `json:"approver_id,omitempty"`
   199  	Approver       string  `json:"approver,omitempty"`
   200  	ApprovalRespAt *Time   `json:"approval_resp_at,omitempty"`
   201  	CancelledAt    *Time   `json:"cancelled_at,omitempty"`
   202  	UpdatedAt      *Time   `json:"updated_at,omitempty"`
   203  	Comments       []struct {
   204  		CommentID   string `json:"comment_id,omitempty"`
   205  		Comment     string `json:"comment,omitempty"`
   206  		CommenterID string `json:"commenter_id,omitempty"`
   207  		Commenter   string `json:"commenter,omitempty"`
   208  		CreatedAt   *Time  `json:"created_at,omitempty"`
   209  	} `json:"comments,omitempty"`
   210  }
   211  
   212  // UpdateTimeoffRequest modifies the timeoff request with the given ID
   213  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#update-a-time-off-request
   214  func (s *API) UpdateTimeoff(
   215  	id string,
   216  	request UpdateTimeoffRequest,
   217  ) (data UpdateTimeoffResponse, err error) {
   218  	endpoint := zoho.Endpoint{
   219  		Name: "UpdateTimeoff",
   220  		URL: fmt.Sprintf(
   221  			"https://shifts.zoho.%s/api/v1/%s/%s/requests/%s",
   222  			s.ZohoTLD,
   223  			s.OrganizationID,
   224  			TimeoffModule,
   225  			id,
   226  		),
   227  		Method:       zoho.HTTPPut,
   228  		ResponseData: &UpdateTimeoffResponse{},
   229  		RequestBody:  request,
   230  	}
   231  
   232  	err = s.Zoho.HTTPRequest(&endpoint)
   233  	if err != nil {
   234  		return UpdateTimeoffResponse{}, fmt.Errorf("failed to update timeoff request: %s", err)
   235  	}
   236  
   237  	if v, ok := endpoint.ResponseData.(*UpdateTimeoffResponse); ok {
   238  		return *v, nil
   239  	}
   240  
   241  	return UpdateTimeoffResponse{}, fmt.Errorf("data retrieved was not 'UpdateTimeoffResponse'")
   242  }
   243  
   244  type UpdateTimeoffRequest struct {
   245  	StartDate *Time  `json:"start_date,omitempty"`
   246  	EndDate   *Time  `json:"end_date,omitempty"`
   247  	TypeID    string `json:"type_id,omitempty"`
   248  	DayType   string `json:"day_type,omitempty"` // all_day, partial
   249  	Reason    string `json:"reason,omitempty"`
   250  }
   251  
   252  type UpdateTimeoffResponse struct {
   253  	ID             string  `json:"id,omitempty"`
   254  	StartDate      *Time   `json:"start_date,omitempty"`
   255  	EndDate        *Time   `json:"end_date,omitempty"`
   256  	EmployeeID     string  `json:"employee_id,omitempty"`
   257  	Employee       string  `json:"employee,omitempty"`
   258  	RequestedByID  string  `json:"requested_by_id,omitempty"`
   259  	RequestedBy    string  `json:"requested_by,omitempty"`
   260  	TypeID         string  `json:"type_id,omitempty"`
   261  	Type           string  `json:"type,omitempty"`
   262  	DayType        string  `json:"day_type,omitempty"`
   263  	Duration       float64 `json:"duration,omitempty"`
   264  	Status         string  `json:"status,omitempty"`
   265  	CreatedAt      *Time   `json:"created_at,omitempty"`
   266  	IsPaid         bool    `json:"is_paid,omitempty"`
   267  	Reason         string  `json:"reason,omitempty"`
   268  	ApproverID     string  `json:"approver_id,omitempty"`
   269  	Approver       string  `json:"approver,omitempty"`
   270  	ApprovalRespAt *Time   `json:"approval_resp_at,omitempty"`
   271  	CancelledAt    *Time   `json:"cancelled_at,omitempty"`
   272  	UpdatedAt      *Time   `json:"updated_at,omitempty"`
   273  	Comments       []struct {
   274  		CommentID   string `json:"comment_id,omitempty"`
   275  		Comment     string `json:"comment,omitempty"`
   276  		CommenterID string `json:"commenter_id,omitempty"`
   277  		Commenter   string `json:"commenter,omitempty"`
   278  		CreatedAt   *Time  `json:"created_at,omitempty"`
   279  	} `json:"comments,omitempty"`
   280  }
   281  
   282  // DeleteTimeoffRequest deletes the timeoff request record with the given ID
   283  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#delete-a-time-off-request
   284  func (s *API) DeleteTimeoffRequest(id string) (data DeleteTimeoffResponse, err error) {
   285  	endpoint := zoho.Endpoint{
   286  		Name: "DeleteTimeoffRequest",
   287  		URL: fmt.Sprintf(
   288  			"https://shifts.zoho.%s/api/v1/%s/%s/requests/%s",
   289  			s.ZohoTLD,
   290  			s.OrganizationID,
   291  			TimeoffModule,
   292  			id,
   293  		),
   294  		Method:       zoho.HTTPDelete,
   295  		ResponseData: &DeleteTimeoffResponse{},
   296  	}
   297  
   298  	err = s.Zoho.HTTPRequest(&endpoint)
   299  	if err != nil {
   300  		return DeleteTimeoffResponse{}, fmt.Errorf(
   301  			"failed to delete timeoff request with id: %s",
   302  			err,
   303  		)
   304  	}
   305  
   306  	if v, ok := endpoint.ResponseData.(*DeleteTimeoffResponse); ok {
   307  		return *v, nil
   308  	}
   309  
   310  	return DeleteTimeoffResponse{}, fmt.Errorf("data returned was not 'DeleteTimeoffResponse'")
   311  }
   312  
   313  type DeleteTimeoffResponse struct {
   314  	Message string `json:"message,omitempty"`
   315  }
   316  
   317  // CancelTimeoffRequest cancels the timeoff request with the given id
   318  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#cancel-a-time-off-request
   319  func (s *API) CancelTimeoffRequest(id string) (data CancelTimeoffResponse, err error) {
   320  	endpoint := zoho.Endpoint{
   321  		Name: "CancelTimeoffRequest",
   322  		URL: fmt.Sprintf(
   323  			"https://shifts.zoho.%s/api/v1/%s/%s/requests/%s/cancel",
   324  			s.ZohoTLD,
   325  			s.OrganizationID,
   326  			TimeoffModule,
   327  			id,
   328  		),
   329  		Method:       zoho.HTTPPost,
   330  		ResponseData: &CancelTimeoffResponse{},
   331  	}
   332  
   333  	err = s.Zoho.HTTPRequest(&endpoint)
   334  	if err != nil {
   335  		return CancelTimeoffResponse{}, fmt.Errorf("failed to cancel timeoff request: %s", err)
   336  	}
   337  
   338  	if v, ok := endpoint.ResponseData.(*CancelTimeoffResponse); ok {
   339  		return *v, nil
   340  	}
   341  
   342  	return CancelTimeoffResponse{}, fmt.Errorf("data retrieved was not 'CancelTimeoffResponse'")
   343  }
   344  
   345  type CancelTimeoffResponse struct {
   346  	ID             string  `json:"id,omitempty"`
   347  	StartDate      *Time   `json:"start_date,omitempty"`
   348  	EndDate        *Time   `json:"end_date,omitempty"`
   349  	EmployeeID     string  `json:"employee_id,omitempty"`
   350  	Employee       string  `json:"employee,omitempty"`
   351  	RequestedByID  string  `json:"requested_by_id,omitempty"`
   352  	RequestedBy    string  `json:"requested_by,omitempty"`
   353  	TypeID         string  `json:"type_id,omitempty"`
   354  	Type           string  `json:"type,omitempty"`
   355  	DayType        string  `json:"day_type,omitempty"`
   356  	Duration       float64 `json:"duration,omitempty"`
   357  	Status         string  `json:"status,omitempty"`
   358  	CreatedAt      *Time   `json:"created_at,omitempty"`
   359  	IsPaid         bool    `json:"is_paid,omitempty"`
   360  	Reason         string  `json:"reason,omitempty"`
   361  	ApproverID     string  `json:"approver_id,omitempty"`
   362  	Approver       string  `json:"approver,omitempty"`
   363  	ApprovalRespAt *Time   `json:"approval_resp_at,omitempty"`
   364  	CancelledAt    *Time   `json:"cancelled_at,omitempty"`
   365  	UpdatedAt      *Time   `json:"updated_at,omitempty"`
   366  	Comments       []struct {
   367  		CommentID   string `json:"comment_id,omitempty"`
   368  		Comment     string `json:"comment,omitempty"`
   369  		CommenterID string `json:"commenter_id,omitempty"`
   370  		Commenter   string `json:"commenter,omitempty"`
   371  		CreatedAt   *Time  `json:"created_at,omitempty"`
   372  	} `json:"comments,omitempty"`
   373  }
   374  
   375  // ApproveTimeoffRequest approves the timeoff request with the given id
   376  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#approve-a-time-off-request
   377  func (s *API) ApproveTimeoffRequest(id string) (data ApproveTimeoffResponse, err error) {
   378  	endpoint := zoho.Endpoint{
   379  		Name: "ApproveTimeoffRequest",
   380  		URL: fmt.Sprintf(
   381  			"https://shifts.zoho.%s/api/v1/%s/%s/requests/%s/approve",
   382  			s.ZohoTLD,
   383  			s.OrganizationID,
   384  			TimeoffModule,
   385  			id,
   386  		),
   387  		Method:       zoho.HTTPPost,
   388  		ResponseData: &ApproveTimeoffResponse{},
   389  	}
   390  
   391  	err = s.Zoho.HTTPRequest(&endpoint)
   392  	if err != nil {
   393  		return ApproveTimeoffResponse{}, fmt.Errorf("failed to approve timeoff request: %s", err)
   394  	}
   395  
   396  	if v, ok := endpoint.ResponseData.(*ApproveTimeoffResponse); ok {
   397  		return *v, nil
   398  	}
   399  
   400  	return ApproveTimeoffResponse{}, fmt.Errorf("data retrieved was not 'ApproveTimeoffResponse'")
   401  }
   402  
   403  type ApproveTimeoffResponse struct {
   404  	ID             string  `json:"id,omitempty"`
   405  	StartDate      *Time   `json:"start_date,omitempty"`
   406  	EndDate        *Time   `json:"end_date,omitempty"`
   407  	EmployeeID     string  `json:"employee_id,omitempty"`
   408  	Employee       string  `json:"employee,omitempty"`
   409  	RequestedByID  string  `json:"requested_by_id,omitempty"`
   410  	RequestedBy    string  `json:"requested_by,omitempty"`
   411  	TypeID         string  `json:"type_id,omitempty"`
   412  	Type           string  `json:"type,omitempty"`
   413  	DayType        string  `json:"day_type,omitempty"`
   414  	Duration       float64 `json:"duration,omitempty"`
   415  	Status         string  `json:"status,omitempty"`
   416  	CreatedAt      *Time   `json:"created_at,omitempty"`
   417  	IsPaid         bool    `json:"is_paid,omitempty"`
   418  	Reason         string  `json:"reason,omitempty"`
   419  	ApproverID     string  `json:"approver_id,omitempty"`
   420  	Approver       string  `json:"approver,omitempty"`
   421  	ApprovalRespAt *Time   `json:"approval_resp_at,omitempty"`
   422  	CancelledAt    *Time   `json:"cancelled_at,omitempty"`
   423  	UpdatedAt      *Time   `json:"updated_at,omitempty"`
   424  	Comments       []struct {
   425  		CommentID   string `json:"comment_id,omitempty"`
   426  		Comment     string `json:"comment,omitempty"`
   427  		CommenterID string `json:"commenter_id,omitempty"`
   428  		Commenter   string `json:"commenter,omitempty"`
   429  		CreatedAt   *Time  `json:"created_at,omitempty"`
   430  	} `json:"comments,omitempty"`
   431  }
   432  
   433  // DenyTimeoffRequest denies the timeoff request with the given id
   434  // https://www.zoho.com/shifts/api/v1/time-off-requests-api/#deny-a-time-off-request
   435  func (s *API) DenyTimeoffRequest(id string) (data DenyTimeoffResponse, err error) {
   436  	endpoint := zoho.Endpoint{
   437  		Name: "DenyTimeoffRequest",
   438  		URL: fmt.Sprintf(
   439  			"https://shifts.zoho.%s/api/v1/%s/%s/requests/%s/deny",
   440  			s.ZohoTLD,
   441  			s.OrganizationID,
   442  			TimeoffModule,
   443  			id,
   444  		),
   445  		Method:       zoho.HTTPPost,
   446  		ResponseData: &DenyTimeoffResponse{},
   447  	}
   448  
   449  	err = s.Zoho.HTTPRequest(&endpoint)
   450  	if err != nil {
   451  		return DenyTimeoffResponse{}, fmt.Errorf("failed to deny timeoff request: %s", err)
   452  	}
   453  
   454  	if v, ok := endpoint.ResponseData.(*DenyTimeoffResponse); ok {
   455  		return *v, nil
   456  	}
   457  
   458  	return DenyTimeoffResponse{}, fmt.Errorf("data retrieved was not 'DenyTimeoffResponse'")
   459  }
   460  
   461  type DenyTimeoffResponse struct {
   462  	ID             string  `json:"id,omitempty"`
   463  	StartDate      *Time   `json:"start_date,omitempty"`
   464  	EndDate        *Time   `json:"end_date,omitempty"`
   465  	EmployeeID     string  `json:"employee_id,omitempty"`
   466  	Employee       string  `json:"employee,omitempty"`
   467  	RequestedByID  string  `json:"requested_by_id,omitempty"`
   468  	RequestedBy    string  `json:"requested_by,omitempty"`
   469  	TypeID         string  `json:"type_id,omitempty"`
   470  	Type           string  `json:"type,omitempty"`
   471  	DayType        string  `json:"day_type,omitempty"`
   472  	Duration       float64 `json:"duration,omitempty"`
   473  	Status         string  `json:"status,omitempty"`
   474  	CreatedAt      *Time   `json:"created_at,omitempty"`
   475  	IsPaid         bool    `json:"is_paid,omitempty"`
   476  	Reason         string  `json:"reason,omitempty"`
   477  	ApproverID     string  `json:"approver_id,omitempty"`
   478  	Approver       string  `json:"approver,omitempty"`
   479  	ApprovalRespAt *Time   `json:"approval_resp_at,omitempty"`
   480  	CancelledAt    *Time   `json:"cancelled_at,omitempty"`
   481  	UpdatedAt      *Time   `json:"updated_at,omitempty"`
   482  	Comments       []struct {
   483  		CommentID   string `json:"comment_id,omitempty"`
   484  		Comment     string `json:"comment,omitempty"`
   485  		CommenterID string `json:"commenter_id,omitempty"`
   486  		Commenter   string `json:"commenter,omitempty"`
   487  		CreatedAt   *Time  `json:"created_at,omitempty"`
   488  	} `json:"comments,omitempty"`
   489  }