github.com/schmorrison/Zoho@v1.1.4/shifts/employees.go (about) 1 package shifts 2 3 import ( 4 "fmt" 5 6 zoho "github.com/schmorrison/Zoho" 7 ) 8 9 // GetAllEmployees returns a list of all employees 10 // https://www.zoho.com/shifts/api/v1/employees-api/#get-all-employees 11 func (s *API) GetAllEmployees( 12 params map[string]zoho.Parameter, 13 ) (data GetEmployeesResponse, err error) { 14 endpoint := zoho.Endpoint{ 15 Name: "GetAllEmployees", 16 URL: fmt.Sprintf( 17 "https://shifts.zoho.%s/api/v1/%s/%s", 18 s.ZohoTLD, 19 s.OrganizationID, 20 EmployeesModule, 21 ), 22 Method: zoho.HTTPGet, 23 ResponseData: &GetEmployeesResponse{}, 24 URLParameters: map[string]zoho.Parameter{ 25 "schedules": "", 26 "status": "", // active, inactive 27 "invite_status": "", // not-sent, sent, accepted 28 "limit": "50", 29 "page": "1", 30 }, 31 } 32 33 if len(params) > 0 { 34 for k, v := range params { 35 endpoint.URLParameters[k] = v 36 } 37 } 38 39 err = s.Zoho.HTTPRequest(&endpoint) 40 if err != nil { 41 return GetEmployeesResponse{}, fmt.Errorf("failed to retrieve empmloyees: %s", err) 42 } 43 if v, ok := endpoint.ResponseData.(*GetEmployeesResponse); ok { 44 return *v, nil 45 } 46 return GetEmployeesResponse{}, fmt.Errorf("data retrieved was not 'GetEmployeesResponse'") 47 } 48 49 type GetEmployeesResponse struct { 50 Employees []struct { 51 ID string `json:"id,omitempty"` 52 FirstName string `json:"first_name,omitempty"` 53 LastName string `json:"last_name,omitempty"` 54 WorkEmail string `json:"work_email,omitempty"` 55 Mobile string `json:"mobile,omitempty"` 56 MobileCountryCode string `json:"mobile_country_code,omitempty"` 57 AccessLevelID string `json:"access_level_id,omitempty"` 58 Status string `json:"status,omitempty"` 59 InviteStatus string `json:"invite_status,omitempty"` 60 Schedules []struct { 61 ID string `json:"id,omitempty"` 62 } `json:"schedules,omitempty"` 63 Positions []struct { 64 ID string `json:"id,omitempty"` 65 } `json:"positions,omitempty"` 66 } `json:"employees,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 // CreateEmployee adds a new record to the list of employees 75 // https://www.zoho.com/shifts/api/v1/employees-api/#create-an-employee 76 func (s *API) CreateEmployee( 77 request CreateEmployeeRequest, 78 ) (data CreateEmployeeResponse, err error) { 79 endpoint := zoho.Endpoint{ 80 Name: "CreateEmployee", 81 URL: fmt.Sprintf( 82 "https://shifts.zoho.%s/api/v1/%s/%s", 83 s.ZohoTLD, 84 s.OrganizationID, 85 EmployeesModule, 86 ), 87 Method: zoho.HTTPPost, 88 ResponseData: &CreateEmployeeResponse{}, 89 RequestBody: request, 90 } 91 92 if request.FirstName == "" || len(request.Schedules) == 0 || request.Timezone == "" { 93 return CreateEmployeeResponse{}, fmt.Errorf( 94 "failed to create employee: first_name, schedules, and timezone are required fields", 95 ) 96 } 97 98 err = s.Zoho.HTTPRequest(&endpoint) 99 if err != nil { 100 return CreateEmployeeResponse{}, fmt.Errorf("failed to create employee: %s", err) 101 } 102 103 if v, ok := endpoint.ResponseData.(*CreateEmployeeResponse); ok { 104 return *v, nil 105 } 106 107 return CreateEmployeeResponse{}, fmt.Errorf("data retrieved was not 'CreateEmployeeResponse'") 108 } 109 110 type CreateEmployeeRequest struct { 111 FirstName string `json:"first_name"` // required 112 LastName string `json:"last_name,omitempty"` 113 WorkEmail string `json:"work_email,omitempty"` 114 Mobile string `json:"mobile,omitempty"` 115 MobileCountryCode string `json:"mobile_country_code,omitempty"` 116 Schedules []struct { 117 ID string `json:"id,omitempty"` 118 } `json:"schedules"` // required 119 Positions []struct { 120 ID string `json:"id,omitempty"` 121 } `json:"positions,omitempty"` 122 Timezone string `json:"timezone"` // required 123 AccessLevelID string `json:"access_level_id,omitempty"` 124 SendInvitation bool `json:"send_invitation,omitempty"` 125 HourlyRate int `json:"hourly_rate,omitempty"` 126 HireDate *Date `json:"hire_date,omitempty"` 127 ExternalEmployeeID string `json:"external_employee_id,omitempty"` 128 HideFromSchedule bool `json:"hide_from_schedule,omitempty"` 129 OvertimeRuleID string `json:"overtime_rule_id,omitempty"` 130 EmploymentType string `json:"employment_type,omitempty"` 131 MaxHrsWeek int `json:"max_hrs_week,omitempty"` 132 MinHrsWeek int `json:"min_hrs_week,omitempty"` 133 MaxHrsDay int `json:"max_hrs_day,omitempty"` 134 MaxDaysWeek int `json:"max_days_week,omitempty"` 135 MaxShiftsDay int `json:"max_shifts_day,omitempty"` 136 } 137 138 type CreateEmployeeResponse struct { 139 ID string `json:"id,omitempty"` 140 FirstName string `json:"first_name,omitempty"` 141 LastName string `json:"last_name,omitempty"` 142 WorkEmail string `json:"work_email,omitempty"` 143 Mobile string `json:"mobile,omitempty"` 144 MobileCountryCode string `json:"mobile_country_code,omitempty"` 145 Schedules []struct { 146 ID string `json:"id,omitempty"` 147 } `json:"schedules,omitempty"` 148 Positions []struct { 149 ID string `json:"id,omitempty"` 150 } `json:"positions,omitempty"` 151 Timezone string `json:"timezone,omitempty"` 152 AccessLevelID string `json:"access_level_id,omitempty"` 153 Status string `json:"status,omitempty"` 154 InviteStatus string `json:"invite_status,omitempty"` 155 HireDate *Date `json:"hire_date,omitempty"` 156 ExternalEmployeeID string `json:"external_employee_id,omitempty"` 157 HideFromSchedule bool `json:"hide_from_schedule,omitempty"` 158 OvertimeRuleID string `json:"overtime_rule_id,omitempty"` 159 EmploymentType string `json:"employment_type,omitempty"` 160 MaxHrsWeek int `json:"max_hrs_week,omitempty"` 161 MinHrsWeek int `json:"min_hrs_week,omitempty"` 162 MaxHrsDay int `json:"max_hrs_day,omitempty"` 163 MaxDaysWeek int `json:"max_days_week,omitempty"` 164 MaxShiftsDay int `json:"max_shifts_day,omitempty"` 165 } 166 167 // GetEmployee retrieves the employee record with the given ID 168 // https://www.zoho.com/shifts/api/v1/employees-api/#get-an-employee 169 func (s *API) GetEmployee(id string) (data GetEmployeeResponse, err error) { 170 endpoint := zoho.Endpoint{ 171 Name: "GetEmployee", 172 URL: fmt.Sprintf( 173 "https://shifts.zoho.%s/api/v1/%s/%s/%s", 174 s.ZohoTLD, 175 s.OrganizationID, 176 EmployeesModule, 177 id, 178 ), 179 Method: zoho.HTTPGet, 180 ResponseData: &GetEmployeeResponse{}, 181 } 182 183 err = s.Zoho.HTTPRequest(&endpoint) 184 if err != nil { 185 return GetEmployeeResponse{}, fmt.Errorf("failed to retrieve Employee with id: %s", err) 186 } 187 188 if v, ok := endpoint.ResponseData.(*GetEmployeeResponse); ok { 189 return *v, nil 190 } 191 192 return GetEmployeeResponse{}, fmt.Errorf("data returned was not 'GetEmployeeResponse'") 193 } 194 195 type GetEmployeeResponse struct { 196 ID string `json:"id,omitempty"` 197 FirstName string `json:"first_name,omitempty"` 198 LastName string `json:"last_name,omitempty"` 199 WorkEmail string `json:"work_email,omitempty"` 200 Mobile string `json:"mobile,omitempty"` 201 MobileCountryCode string `json:"mobile_country_code,omitempty"` 202 AccessLevelID string `json:"access_level_id,omitempty"` 203 Status string `json:"status,omitempty"` 204 InviteStatus string `json:"invite_status,omitempty"` 205 Schedules []struct { 206 ID string `json:"id,omitempty"` 207 } `json:"schedules,omitempty"` 208 Positions []struct { 209 ID string `json:"id,omitempty"` 210 } `json:"positions,omitempty"` 211 Timezone string `json:"timezone,omitempty"` 212 HireDate *Date `json:"hire_date,omitempty"` 213 ExternalEmployeeID string `json:"external_employee_id,omitempty"` 214 HideFromSchedule bool `json:"hide_from_schedule,omitempty"` 215 OvertimeRuleID string `json:"overtime_rule_id,omitempty"` 216 EmploymentType string `json:"employment_type,omitempty"` 217 MaxHrsWeek int `json:"max_hrs_week,omitempty"` 218 MinHrsWeek int `json:"min_hrs_week,omitempty"` 219 MaxHrsDay int `json:"max_hrs_day,omitempty"` 220 MaxDaysWeek int `json:"max_days_week,omitempty"` 221 MaxShiftsDay int `json:"max_shifts_day,omitempty"` 222 } 223 224 // UpdateEmployee modifies the employee with the given ID 225 // https://www.zoho.com/shifts/api/v1/employees-api/#update-an-employee 226 func (s *API) UpdateEmployee( 227 id string, 228 request UpdateEmployeeRequest, 229 ) (data UpdateEmployeeResponse, err error) { 230 endpoint := zoho.Endpoint{ 231 Name: "UpdateEmployee", 232 URL: fmt.Sprintf( 233 "https://shifts.zoho.%s/api/v1/%s/%s/%s", 234 s.ZohoTLD, 235 s.OrganizationID, 236 EmployeesModule, 237 id, 238 ), 239 Method: zoho.HTTPPut, 240 ResponseData: &UpdateEmployeeResponse{}, 241 RequestBody: request, 242 } 243 244 err = s.Zoho.HTTPRequest(&endpoint) 245 if err != nil { 246 return UpdateEmployeeResponse{}, fmt.Errorf("failed to update employee: %s", err) 247 } 248 249 if v, ok := endpoint.ResponseData.(*UpdateEmployeeResponse); ok { 250 return *v, nil 251 } 252 253 return UpdateEmployeeResponse{}, fmt.Errorf("data retrieved was not 'UpdateEmployeeResponse'") 254 } 255 256 type UpdateEmployeeRequest struct { 257 FirstName string `json:"first_name,omitempty"` 258 LastName string `json:"last_name,omitempty"` 259 WorkEmail string `json:"work_email,omitempty"` 260 Mobile string `json:"mobile,omitempty"` 261 MobileCountryCode string `json:"mobile_country_code,omitempty"` 262 Schedules []struct { 263 ID string `json:"id,omitempty"` 264 } `json:"schedules,omitempty"` 265 Positions []struct { 266 ID string `json:"id,omitempty"` 267 } `json:"positions,omitempty"` 268 Timezone string `json:"timezone,omitempty"` 269 AccessLevelID string `json:"access_level_id,omitempty"` 270 HourlyRate int `json:"hourly_rate,omitempty"` 271 HireDate *Date `json:"hire_date,omitempty"` 272 ExternalEmployeeID string `json:"external_employee_id,omitempty"` 273 HideFromSchedule bool `json:"hide_from_schedule,omitempty"` 274 OvertimeRuleID string `json:"overtime_rule_id,omitempty"` 275 EmploymentType string `json:"employment_type,omitempty"` 276 MaxHrsWeek int `json:"max_hrs_week,omitempty"` 277 MinHrsWeek int `json:"min_hrs_week,omitempty"` 278 MaxHrsDay int `json:"max_hrs_day,omitempty"` 279 MaxDaysWeek int `json:"max_days_week,omitempty"` 280 MaxShiftsDay int `json:"max_shifts_day,omitempty"` 281 } 282 283 type UpdateEmployeeResponse struct { 284 ID string `json:"id,omitempty"` 285 FirstName string `json:"first_name,omitempty"` 286 LastName string `json:"last_name,omitempty"` 287 WorkEmail string `json:"work_email,omitempty"` 288 Mobile string `json:"mobile,omitempty"` 289 MobileCountryCode string `json:"mobile_country_code,omitempty"` 290 AccessLevelID string `json:"access_level_id,omitempty"` 291 Status string `json:"status,omitempty"` 292 InviteStatus string `json:"invite_status,omitempty"` 293 Schedules []struct { 294 ID string `json:"id,omitempty"` 295 } `json:"schedules,omitempty"` 296 Positions []struct { 297 ID string `json:"id,omitempty"` 298 } `json:"positions,omitempty"` 299 Timezone string `json:"timezone,omitempty"` 300 HireDate *Date `json:"hire_date,omitempty"` 301 ExternalEmployeeID string `json:"external_employee_id,omitempty"` 302 HideFromSchedule bool `json:"hide_from_schedule,omitempty"` 303 OvertimeRuleID string `json:"overtime_rule_id,omitempty"` 304 EmploymentType string `json:"employment_type,omitempty"` 305 MaxHrsWeek int `json:"max_hrs_week,omitempty"` 306 MinHrsWeek int `json:"min_hrs_week,omitempty"` 307 MaxHrsDay int `json:"max_hrs_day,omitempty"` 308 MaxDaysWeek int `json:"max_days_week,omitempty"` 309 MaxShiftsDay int `json:"max_shifts_day,omitempty"` 310 } 311 312 // ActivateEmployee activates the employee accounts of the employee IDs provided 313 // https://www.zoho.com/shifts/api/v1/employees-api/#activate-employees 314 func (s *API) ActivateEmployee( 315 request ActivateEmployeeRequest, 316 ) (data ActivateEmployeeResponse, err error) { 317 endpoint := zoho.Endpoint{ 318 Name: "ActivateEmployee", 319 URL: fmt.Sprintf( 320 "https://shifts.zoho.%s/api/v1/%s/%s/activate", 321 s.ZohoTLD, 322 s.OrganizationID, 323 EmployeesModule, 324 ), 325 Method: zoho.HTTPPost, 326 ResponseData: &ActivateEmployeeResponse{}, 327 RequestBody: request, 328 } 329 330 err = s.Zoho.HTTPRequest(&endpoint) 331 if err != nil { 332 return ActivateEmployeeResponse{}, fmt.Errorf("failed to activate employees: %s", err) 333 } 334 335 if v, ok := endpoint.ResponseData.(*ActivateEmployeeResponse); ok { 336 return *v, nil 337 } 338 339 return ActivateEmployeeResponse{}, fmt.Errorf( 340 "data retrieved was not 'ActivateEmployeeResponse'", 341 ) 342 } 343 344 type ActivateEmployeeRequest struct { 345 Employees []string `json:"employees"` // required 346 } 347 348 type ActivateEmployeeResponse struct { 349 Message string `json:"message,omitempty"` 350 } 351 352 // DeactivateEmployee deactivates the employee accounts of the employee IDs provided 353 // https://www.zoho.com/shifts/api/v1/employees-api/#deactivate-employees 354 func (s *API) DeactivateEmployee( 355 request DeactivateEmployeeRequest, 356 ) (data DeactivateEmployeeResponse, err error) { 357 endpoint := zoho.Endpoint{ 358 Name: "DeactivateEmployee", 359 URL: fmt.Sprintf( 360 "https://shifts.zoho.%s/api/v1/%s/%s/deactivate", 361 s.ZohoTLD, 362 s.OrganizationID, 363 EmployeesModule, 364 ), 365 Method: zoho.HTTPPost, 366 ResponseData: &DeactivateEmployeeResponse{}, 367 RequestBody: request, 368 } 369 370 err = s.Zoho.HTTPRequest(&endpoint) 371 if err != nil { 372 return DeactivateEmployeeResponse{}, fmt.Errorf("failed to deactivate employees: %s", err) 373 } 374 375 if v, ok := endpoint.ResponseData.(*DeactivateEmployeeResponse); ok { 376 return *v, nil 377 } 378 379 return DeactivateEmployeeResponse{}, fmt.Errorf( 380 "data retrieved was not 'DeactivateEmployeeResponse'", 381 ) 382 } 383 384 type DeactivateEmployeeRequest struct { 385 Employees []string `json:"employees"` // required 386 } 387 388 type DeactivateEmployeeResponse struct { 389 Message string `json:"message,omitempty"` 390 } 391 392 // InviteEmployee sends an invite to the employee accounts of the employee IDs provided 393 // https://www.zoho.com/shifts/api/v1/employees-api/#invite-employees 394 func (s *API) InviteEmployee( 395 request InviteEmployeeRequest, 396 ) (data InviteEmployeeResponse, err error) { 397 endpoint := zoho.Endpoint{ 398 Name: "InviteEmployee", 399 URL: fmt.Sprintf( 400 "https://shifts.zoho.%s/api/v1/%s/%s/invite", 401 s.ZohoTLD, 402 s.OrganizationID, 403 EmployeesModule, 404 ), 405 Method: zoho.HTTPPost, 406 ResponseData: &InviteEmployeeResponse{}, 407 RequestBody: request, 408 } 409 410 err = s.Zoho.HTTPRequest(&endpoint) 411 if err != nil { 412 return InviteEmployeeResponse{}, fmt.Errorf("failed to invite employees: %s", err) 413 } 414 415 if v, ok := endpoint.ResponseData.(*InviteEmployeeResponse); ok { 416 return *v, nil 417 } 418 419 return InviteEmployeeResponse{}, fmt.Errorf("data retrieved was not 'InviteEmployeeResponse'") 420 } 421 422 type InviteEmployeeRequest struct { 423 Employees []string `json:"employees"` // required 424 AccessLevelID string `json:"access_level_id,omitempty"` 425 } 426 427 type InviteEmployeeResponse struct { 428 Message string `json:"message,omitempty"` 429 }