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

     1  package shifts
     2  
     3  import (
     4  	"fmt"
     5  
     6  	zoho "github.com/schmorrison/Zoho"
     7  )
     8  
     9  // GetAllSchedules returns a list of all schedules
    10  // https://www.zoho.com/shifts/api/v1/schedules-api/#get-all-schedules
    11  func (s *API) GetAllSchedules(
    12  	params map[string]zoho.Parameter,
    13  ) (data GetSchedulesResponse, err error) {
    14  	endpoint := zoho.Endpoint{
    15  		Name: "GetAllSchedules",
    16  		URL: fmt.Sprintf(
    17  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
    18  			s.ZohoTLD,
    19  			s.OrganizationID,
    20  			SettingsModule,
    21  			schedulesModule,
    22  		),
    23  		Method:       zoho.HTTPGet,
    24  		ResponseData: &GetSchedulesResponse{},
    25  	}
    26  
    27  	err = s.Zoho.HTTPRequest(&endpoint)
    28  	if err != nil {
    29  		return GetSchedulesResponse{}, fmt.Errorf("failed to retrieve schedules: %s", err)
    30  	}
    31  	if v, ok := endpoint.ResponseData.(*GetSchedulesResponse); ok {
    32  		return *v, nil
    33  	}
    34  	return GetSchedulesResponse{}, fmt.Errorf("data retrieved was not 'GetSchedulesResponse'")
    35  }
    36  
    37  type GetSchedulesResponse struct {
    38  	Schedules []struct {
    39  		ID        string `json:"id,omitempty"`
    40  		Name      string `json:"name,omitempty"`
    41  		Timezone  string `json:"timezone,omitempty"`
    42  		Address   string `json:"address,omitempty"`
    43  		Latitude  string `json:"latitude,omitempty"`
    44  		Longitude string `json:"longitude,omitempty"`
    45  	} `json:"schedules,omitempty"`
    46  }
    47  
    48  // CreateSchedule adds a new record to the list of schedules
    49  // https://www.zoho.com/shifts/api/v1/schedules-api/#create-a-schedule
    50  func (s *API) CreateSchedule(
    51  	request CreateScheduleRequest,
    52  ) (data CreateScheduleResponse, err error) {
    53  	endpoint := zoho.Endpoint{
    54  		Name: "CreateSchedule",
    55  		URL: fmt.Sprintf(
    56  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
    57  			s.ZohoTLD,
    58  			s.OrganizationID,
    59  			SettingsModule,
    60  			schedulesModule,
    61  		),
    62  		Method:       zoho.HTTPPost,
    63  		ResponseData: &CreateScheduleResponse{},
    64  		RequestBody:  request,
    65  	}
    66  
    67  	if request.Name == "" {
    68  		return CreateScheduleResponse{}, fmt.Errorf(
    69  			"failed to create schedule: name is a required field",
    70  		)
    71  	}
    72  
    73  	err = s.Zoho.HTTPRequest(&endpoint)
    74  	if err != nil {
    75  		return CreateScheduleResponse{}, fmt.Errorf("failed to create a schedule: %s", err)
    76  	}
    77  
    78  	if v, ok := endpoint.ResponseData.(*CreateScheduleResponse); ok {
    79  		return *v, nil
    80  	}
    81  
    82  	return CreateScheduleResponse{}, fmt.Errorf("data retrieved was not 'CreateScheduleResponse'")
    83  }
    84  
    85  type CreateScheduleRequest struct {
    86  	Name      string `json:"name"` // required
    87  	Address   string `json:"address,omitempty"`
    88  	Latitude  string `json:"latitude,omitempty"`
    89  	Longitude string `json:"longitude,omitempty"`
    90  }
    91  
    92  type CreateScheduleResponse struct {
    93  	ID        string `json:"id,omitempty"`
    94  	Name      string `json:"name,omitempty"`
    95  	Timezone  string `json:"timezone,omitempty"`
    96  	Address   string `json:"address,omitempty"`
    97  	Latitude  string `json:"latitude,omitempty"`
    98  	Longitude string `json:"longitude,omitempty"`
    99  }
   100  
   101  // UpdateSchedule modifies the schedule with the given ID
   102  // https://www.zoho.com/shifts/api/v1/schedules-api/#update-a-schedule
   103  func (s *API) UpdateSchedule(
   104  	id string,
   105  	request UpdateScheduleRequest,
   106  ) (data UpdateScheduleResponse, err error) {
   107  	endpoint := zoho.Endpoint{
   108  		Name: "UpdateSchedule",
   109  		URL: fmt.Sprintf(
   110  			"https://shifts.zoho.%s/api/v1/%s/%s/%s/%s",
   111  			s.ZohoTLD,
   112  			s.OrganizationID,
   113  			SettingsModule,
   114  			schedulesModule,
   115  			id,
   116  		),
   117  		Method:       zoho.HTTPPut,
   118  		ResponseData: &UpdateScheduleResponse{},
   119  		RequestBody:  request,
   120  	}
   121  
   122  	err = s.Zoho.HTTPRequest(&endpoint)
   123  	if err != nil {
   124  		return UpdateScheduleResponse{}, fmt.Errorf("failed to update schedule: %s", err)
   125  	}
   126  
   127  	if v, ok := endpoint.ResponseData.(*UpdateScheduleResponse); ok {
   128  		return *v, nil
   129  	}
   130  
   131  	return UpdateScheduleResponse{}, fmt.Errorf("data retrieved was not 'UpdateScheduleResponse'")
   132  }
   133  
   134  type UpdateScheduleRequest struct {
   135  	Name      string `json:"name,omitempty"`
   136  	Timezone  string `json:"timezone,omitempty"`
   137  	Address   string `json:"address,omitempty"`
   138  	Latitude  string `json:"latitude,omitempty"`
   139  	Longitude string `json:"longitude,omitempty"`
   140  }
   141  
   142  type UpdateScheduleResponse struct {
   143  	ID        string `json:"id,omitempty"`
   144  	Name      string `json:"name,omitempty"`
   145  	Timezone  string `json:"timezone,omitempty"`
   146  	Address   string `json:"address,omitempty"`
   147  	Latitude  string `json:"latitude,omitempty"`
   148  	Longitude string `json:"longitude,omitempty"`
   149  }
   150  
   151  // DeleteSchedule deletes the schedule record with the given ID
   152  // https://www.zoho.com/shifts/api/v1/schedules-api/#delete-a-schedule
   153  func (s *API) DeleteSchedule(id string) (data DeleteScheduleResponse, err error) {
   154  	endpoint := zoho.Endpoint{
   155  		Name: "DeleteSchedule",
   156  		URL: fmt.Sprintf(
   157  			"https://shifts.zoho.%s/api/v1/%s/%s/%s/%s",
   158  			s.ZohoTLD,
   159  			s.OrganizationID,
   160  			SettingsModule,
   161  			schedulesModule,
   162  			id,
   163  		),
   164  		Method:       zoho.HTTPDelete,
   165  		ResponseData: &DeleteScheduleResponse{},
   166  	}
   167  
   168  	err = s.Zoho.HTTPRequest(&endpoint)
   169  	if err != nil {
   170  		return DeleteScheduleResponse{}, fmt.Errorf("failed to delete schedule with id: %s", err)
   171  	}
   172  
   173  	if v, ok := endpoint.ResponseData.(*DeleteScheduleResponse); ok {
   174  		return *v, nil
   175  	}
   176  
   177  	return DeleteScheduleResponse{}, fmt.Errorf("data returned was not 'DeleteScheduleResponse'")
   178  }
   179  
   180  type DeleteScheduleResponse struct {
   181  	Message string `json:"message,omitempty"`
   182  }
   183  
   184  // GetAllPositions returns a list of all position
   185  // https://www.zoho.com/shifts/api/v1/positions-api/#get-all-positions
   186  func (s *API) GetAllPositions(
   187  	params map[string]zoho.Parameter,
   188  ) (data GetPositionsResponse, err error) {
   189  	endpoint := zoho.Endpoint{
   190  		Name: "GetAllPositions",
   191  		URL: fmt.Sprintf(
   192  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   193  			s.ZohoTLD,
   194  			s.OrganizationID,
   195  			SettingsModule,
   196  			positionsModule,
   197  		),
   198  		Method:       zoho.HTTPGet,
   199  		ResponseData: &GetPositionsResponse{},
   200  	}
   201  
   202  	err = s.Zoho.HTTPRequest(&endpoint)
   203  	if err != nil {
   204  		return GetPositionsResponse{}, fmt.Errorf("failed to retrieve positions: %s", err)
   205  	}
   206  	if v, ok := endpoint.ResponseData.(*GetPositionsResponse); ok {
   207  		return *v, nil
   208  	}
   209  	return GetPositionsResponse{}, fmt.Errorf("data retrieved was not 'GetPositionsResponse'")
   210  }
   211  
   212  type GetPositionsResponse struct {
   213  	Positions []struct {
   214  		ID        string `json:"id,omitempty"`
   215  		Name      string `json:"name,omitempty"`
   216  		Color     string `json:"color,omitempty"`
   217  		Schedules []struct {
   218  			ID   string `json:"id,omitempty"`
   219  			Name string `json:"name,omitempty"`
   220  		} `json:"schedules,omitempty"`
   221  	} `json:"positions,omitempty"`
   222  }
   223  
   224  // CreatePosition adds a new record to the list of positions
   225  // https://www.zoho.com/shifts/api/v1/positions-api/#create-a-position
   226  func (s *API) CreatePosition(
   227  	request CreatePositionRequest,
   228  ) (data CreatePositionResponse, err error) {
   229  	endpoint := zoho.Endpoint{
   230  		Name: "CreatePosition",
   231  		URL: fmt.Sprintf(
   232  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   233  			s.ZohoTLD,
   234  			s.OrganizationID,
   235  			SettingsModule,
   236  			positionsModule,
   237  		),
   238  		Method:       zoho.HTTPPost,
   239  		ResponseData: &CreatePositionResponse{},
   240  		RequestBody:  request,
   241  	}
   242  
   243  	if request.Name == "" {
   244  		return CreatePositionResponse{}, fmt.Errorf(
   245  			"failed to create position: name is a required field",
   246  		)
   247  	}
   248  
   249  	err = s.Zoho.HTTPRequest(&endpoint)
   250  	if err != nil {
   251  		return CreatePositionResponse{}, fmt.Errorf("failed to create a position: %s", err)
   252  	}
   253  
   254  	if v, ok := endpoint.ResponseData.(*CreatePositionResponse); ok {
   255  		return *v, nil
   256  	}
   257  
   258  	return CreatePositionResponse{}, fmt.Errorf("data retrieved was not 'CreatePositionResponse'")
   259  }
   260  
   261  type CreatePositionRequest struct {
   262  	Name      string `json:"name"`            // required
   263  	Color     string `json:"color,omitempty"` // red, pink, magenta, purple, deep-purple, indigo, light-violet, blue, light-blue, cyan, muted-green, teal, green, light-green, lime, yellow, amber, orange, deep-orange, brown, grey, blue-grey
   264  	Schedules []struct {
   265  		ID string `json:"id,omitempty"`
   266  	} `json:"schedules,omitempty"`
   267  }
   268  
   269  type CreatePositionResponse struct {
   270  	ID        string `json:"id,omitempty"`
   271  	Name      string `json:"name,omitempty"`
   272  	Color     string `json:"color,omitempty"`
   273  	Schedules []struct {
   274  		ID   string `json:"id,omitempty"`
   275  		Name string `json:"name,omitempty"`
   276  	} `json:"schedules,omitempty"`
   277  }
   278  
   279  // UpdatePosition modifies the position with the given ID
   280  // https://www.zoho.com/shifts/api/v1/positions-api/#update-a-position
   281  func (s *API) UpdatePosition(
   282  	id string,
   283  	request UpdatePositionRequest,
   284  ) (data UpdatePositionResponse, err error) {
   285  	endpoint := zoho.Endpoint{
   286  		Name: "UpdatePosition",
   287  		URL: fmt.Sprintf(
   288  			"https://shifts.zoho.%s/api/v1/%s/%s/%s/%s",
   289  			s.ZohoTLD,
   290  			s.OrganizationID,
   291  			SettingsModule,
   292  			positionsModule,
   293  			id,
   294  		),
   295  		Method:       zoho.HTTPPut,
   296  		ResponseData: &UpdatePositionResponse{},
   297  		RequestBody:  request,
   298  	}
   299  
   300  	err = s.Zoho.HTTPRequest(&endpoint)
   301  	if err != nil {
   302  		return UpdatePositionResponse{}, fmt.Errorf("failed to update position: %s", err)
   303  	}
   304  
   305  	if v, ok := endpoint.ResponseData.(*UpdatePositionResponse); ok {
   306  		return *v, nil
   307  	}
   308  
   309  	return UpdatePositionResponse{}, fmt.Errorf("data retrieved was not 'UpdatePositionResponse'")
   310  }
   311  
   312  type UpdatePositionRequest struct {
   313  	Name      string `json:"name,omitempty"`
   314  	Color     string `json:"color,omitempty"` // red, pink, magenta, purple, deep-purple, indigo, light-violet, blue, light-blue, cyan, muted-green, teal, green, light-green, lime, yellow, amber, orange, deep-orange, brown, grey, blue-grey
   315  	Schedules []struct {
   316  		ID string `json:"id,omitempty"`
   317  	} `json:"schedules,omitempty"`
   318  }
   319  
   320  type UpdatePositionResponse struct {
   321  	ID        string `json:"id,omitempty"`
   322  	Name      string `json:"name,omitempty"`
   323  	Color     string `json:"color,omitempty"`
   324  	Schedules []struct {
   325  		ID   string `json:"id,omitempty"`
   326  		Name string `json:"name,omitempty"`
   327  	} `json:"schedules,omitempty"`
   328  }
   329  
   330  // DeletePosition deletes the schedule record with the given ID
   331  // https://www.zoho.com/shifts/api/v1/positions-api/#delete-a-position
   332  func (s *API) DeletePosition(id string) (data DeletePositionResponse, err error) {
   333  	endpoint := zoho.Endpoint{
   334  		Name: "DeletePosition",
   335  		URL: fmt.Sprintf(
   336  			"https://shifts.zoho.%s/api/v1/%s/%s/%s/%s",
   337  			s.ZohoTLD,
   338  			s.OrganizationID,
   339  			SettingsModule,
   340  			positionsModule,
   341  			id,
   342  		),
   343  		Method:       zoho.HTTPDelete,
   344  		ResponseData: &DeletePositionResponse{},
   345  	}
   346  
   347  	err = s.Zoho.HTTPRequest(&endpoint)
   348  	if err != nil {
   349  		return DeletePositionResponse{}, fmt.Errorf("failed to delete position with id: %s", err)
   350  	}
   351  
   352  	if v, ok := endpoint.ResponseData.(*DeletePositionResponse); ok {
   353  		return *v, nil
   354  	}
   355  
   356  	return DeletePositionResponse{}, fmt.Errorf("data returned was not 'DeletePositionResponse'")
   357  }
   358  
   359  type DeletePositionResponse struct {
   360  	Message string `json:"message,omitempty"`
   361  }
   362  
   363  // GetAllJobsites returns a list of all job sites
   364  // https://www.zoho.com/shifts/api/v1/job-sites-api/#get-all-job-sites
   365  func (s *API) GetAllJobsites(
   366  	params map[string]zoho.Parameter,
   367  ) (data GetJobsitesResponse, err error) {
   368  	endpoint := zoho.Endpoint{
   369  		Name: "GetAllJobsites",
   370  		URL: fmt.Sprintf(
   371  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   372  			s.ZohoTLD,
   373  			s.OrganizationID,
   374  			SettingsModule,
   375  			jobSitesModule,
   376  		),
   377  		Method:       zoho.HTTPGet,
   378  		ResponseData: &GetJobsitesResponse{},
   379  		URLParameters: map[string]zoho.Parameter{
   380  			"page":  "",
   381  			"limit": "",
   382  		},
   383  	}
   384  
   385  	err = s.Zoho.HTTPRequest(&endpoint)
   386  	if err != nil {
   387  		return GetJobsitesResponse{}, fmt.Errorf("failed to retrieve job sites: %s", err)
   388  	}
   389  	if v, ok := endpoint.ResponseData.(*GetJobsitesResponse); ok {
   390  		return *v, nil
   391  	}
   392  	return GetJobsitesResponse{}, fmt.Errorf("data retrieved was not 'GetJobsitesResponse'")
   393  }
   394  
   395  type GetJobsitesResponse struct {
   396  	JobSites []struct {
   397  		ID        string `json:"id,omitempty"`
   398  		Name      string `json:"name,omitempty"`
   399  		Schedules []struct {
   400  			ID   string `json:"id,omitempty"`
   401  			Name string `json:"name,omitempty"`
   402  		} `json:"schedules,omitempty"`
   403  		Address   string `json:"address,omitempty"`
   404  		Latitude  string `json:"latitude,omitempty"`
   405  		Longitude string `json:"longitude,omitempty"`
   406  		Notes     string `json:"notes,omitempty"`
   407  	} `json:"job_sites,omitempty"`
   408  	Meta struct {
   409  		Count int `json:"count,omitempty"`
   410  		Limit int `json:"limit,omitempty"`
   411  		Page  int `json:"page,omitempty"`
   412  	} `json:"meta,omitempty"`
   413  }
   414  
   415  // CreateJobsite adds a new record to the list of job sites
   416  // https://www.zoho.com/shifts/api/v1/job-sites-api/#create-a-job-site
   417  func (s *API) CreateJobsite(request CreateJobsiteRequest) (data CreateJobsiteResponse, err error) {
   418  	endpoint := zoho.Endpoint{
   419  		Name: "CreateJobsite",
   420  		URL: fmt.Sprintf(
   421  			"https://shifts.zoho.%s/api/v1/%s/%s/%s",
   422  			s.ZohoTLD,
   423  			s.OrganizationID,
   424  			SettingsModule,
   425  			jobSitesModule,
   426  		),
   427  		Method:       zoho.HTTPPost,
   428  		ResponseData: &CreateJobsiteResponse{},
   429  		RequestBody:  request,
   430  	}
   431  
   432  	if request.Name == "" {
   433  		return CreateJobsiteResponse{}, fmt.Errorf(
   434  			"failed to create job site: name is a required field",
   435  		)
   436  	}
   437  
   438  	err = s.Zoho.HTTPRequest(&endpoint)
   439  	if err != nil {
   440  		return CreateJobsiteResponse{}, fmt.Errorf("failed to create a job site: %s", err)
   441  	}
   442  
   443  	if v, ok := endpoint.ResponseData.(*CreateJobsiteResponse); ok {
   444  		return *v, nil
   445  	}
   446  
   447  	return CreateJobsiteResponse{}, fmt.Errorf("data retrieved was not 'CreateJobsiteResponse'")
   448  }
   449  
   450  type CreateJobsiteRequest struct {
   451  	Name      string `json:"name"` // required
   452  	Schedules []struct {
   453  		ID string `json:"id,omitempty"`
   454  	} `json:"schedules,omitempty"`
   455  	Address   string `json:"address,omitempty"`
   456  	Latitude  string `json:"latitude,omitempty"`
   457  	Longitude string `json:"longitude,omitempty"`
   458  	Notes     string `json:"notes,omitempty"`
   459  }
   460  
   461  type CreateJobsiteResponse struct {
   462  	ID        string `json:"id,omitempty"`
   463  	Name      string `json:"name,omitempty"`
   464  	Schedules []struct {
   465  		ID   string `json:"id,omitempty"`
   466  		Name string `json:"name,omitempty"`
   467  	} `json:"schedules,omitempty"`
   468  	Address   string `json:"address,omitempty"`
   469  	Latitude  string `json:"latitude,omitempty"`
   470  	Longitude string `json:"longitude,omitempty"`
   471  	Notes     string `json:"notes,omitempty"`
   472  }
   473  
   474  // UpdateJobsite modifies the job site with the given ID
   475  // https://www.zoho.com/shifts/api/v1/job-sites-api/#update-a-job-site
   476  func (s *API) UpdateJobsite(
   477  	id string,
   478  	request UpdateJobsiteRequest,
   479  ) (data UpdateJobsiteResponse, err error) {
   480  	endpoint := zoho.Endpoint{
   481  		Name: "UpdateJobsite",
   482  		URL: fmt.Sprintf(
   483  			"https://shifts.zoho.%s/api/v1/%s/%s/%s/%s",
   484  			s.ZohoTLD,
   485  			s.OrganizationID,
   486  			SettingsModule,
   487  			jobSitesModule,
   488  			id,
   489  		),
   490  		Method:       zoho.HTTPPut,
   491  		ResponseData: &UpdateJobsiteResponse{},
   492  		RequestBody:  request,
   493  	}
   494  
   495  	err = s.Zoho.HTTPRequest(&endpoint)
   496  	if err != nil {
   497  		return UpdateJobsiteResponse{}, fmt.Errorf("failed to update job site: %s", err)
   498  	}
   499  
   500  	if v, ok := endpoint.ResponseData.(*UpdateJobsiteResponse); ok {
   501  		return *v, nil
   502  	}
   503  
   504  	return UpdateJobsiteResponse{}, fmt.Errorf("data retrieved was not 'UpdateJobsiteResponse'")
   505  }
   506  
   507  type UpdateJobsiteRequest struct {
   508  	Name      string `json:"name,omitempty"`
   509  	Schedules []struct {
   510  		ID string `json:"id,omitempty"`
   511  	} `json:"schedules,omitempty"`
   512  	Address   string `json:"address,omitempty"`
   513  	Latitude  string `json:"latitude,omitempty"`
   514  	Longitude string `json:"longitude,omitempty"`
   515  	Notes     string `json:"notes,omitempty"`
   516  }
   517  
   518  type UpdateJobsiteResponse struct {
   519  	ID        string `json:"id,omitempty"`
   520  	Name      string `json:"name,omitempty"`
   521  	Schedules []struct {
   522  		ID   string `json:"id,omitempty"`
   523  		Name string `json:"name,omitempty"`
   524  	} `json:"schedules,omitempty"`
   525  	Address   string `json:"address,omitempty"`
   526  	Latitude  string `json:"latitude,omitempty"`
   527  	Longitude string `json:"longitude,omitempty"`
   528  	Notes     string `json:"notes,omitempty"`
   529  }
   530  
   531  // DeleteJobsite deletes the job site record with the given ID
   532  // https://www.zoho.com/shifts/api/v1/job-sites-api/#delete-a-job-site
   533  func (s *API) DeleteJobsite(id string) (data DeleteJobsiteResponse, err error) {
   534  	endpoint := zoho.Endpoint{
   535  		Name: "DeleteJobsite",
   536  		URL: fmt.Sprintf(
   537  			"https://shifts.zoho.%s/api/v1/%s/%s/%s/%s",
   538  			s.ZohoTLD,
   539  			s.OrganizationID,
   540  			SettingsModule,
   541  			jobSitesModule,
   542  			id,
   543  		),
   544  		Method:       zoho.HTTPDelete,
   545  		ResponseData: &DeleteJobsiteResponse{},
   546  	}
   547  
   548  	err = s.Zoho.HTTPRequest(&endpoint)
   549  	if err != nil {
   550  		return DeleteJobsiteResponse{}, fmt.Errorf("failed to delete job site with id: %s", err)
   551  	}
   552  
   553  	if v, ok := endpoint.ResponseData.(*DeleteJobsiteResponse); ok {
   554  		return *v, nil
   555  	}
   556  
   557  	return DeleteJobsiteResponse{}, fmt.Errorf("data returned was not 'DeleteJobsiteResponse'")
   558  }
   559  
   560  type DeleteJobsiteResponse struct {
   561  	Message string `json:"message,omitempty"`
   562  }