github.com/schmorrison/Zoho@v1.1.4/bookings/availability.go (about)

     1  package bookings
     2  
     3  import (
     4  	"fmt"
     5  
     6  	zoho "github.com/schmorrison/Zoho"
     7  )
     8  
     9  func (c *API) FetchAvailability(
    10  	serviceID zoho.Parameter,
    11  	staffID zoho.Parameter,
    12  	resourceID zoho.Parameter,
    13  	date zoho.Parameter,
    14  ) (data AvailabilityResponse, err error) {
    15  	endpoint := zoho.Endpoint{
    16  		Name: FetchServicesModule,
    17  		URL: fmt.Sprintf(
    18  			"https://www.zohoapis.%s/bookings/v1/json/%s",
    19  			c.ZohoTLD,
    20  			GetAvailabilityModule,
    21  		),
    22  		Method:       zoho.HTTPGet,
    23  		ResponseData: &AvailabilityResponse{},
    24  		URLParameters: map[string]zoho.Parameter{
    25  			"filter_by": "",
    26  		},
    27  	}
    28  	if serviceID == "" {
    29  		return AvailabilityResponse{}, fmt.Errorf(
    30  			"Failed to execute FetchAvailability due to non-availability of service_id",
    31  		)
    32  	}
    33  	endpoint.URLParameters["service_id"] = serviceID
    34  
    35  	if staffID == "" && resourceID == "" {
    36  		return AvailabilityResponse{}, fmt.Errorf(
    37  			"Failed to execute FetchAvailability due to non-availability of both staff_id and resource_id(atleast one is required)",
    38  		)
    39  	}
    40  	if resourceID != "" {
    41  		endpoint.URLParameters["resource_id"] = resourceID
    42  	}
    43  	if staffID != "" {
    44  		endpoint.URLParameters["staff_id"] = staffID
    45  	}
    46  
    47  	if date == "" {
    48  		return AvailabilityResponse{}, fmt.Errorf(
    49  			"Failed to execute FetchAvailability due to non-availability of date",
    50  		)
    51  	}
    52  	endpoint.URLParameters["selected_date"] = date
    53  
    54  	err = c.Zoho.HTTPRequest(&endpoint)
    55  	if err != nil {
    56  		return AvailabilityResponse{}, fmt.Errorf("Failed to retrieve services: %s", err)
    57  	}
    58  
    59  	if v, ok := endpoint.ResponseData.(*AvailabilityResponse); ok {
    60  		return *v, nil
    61  	}
    62  	return AvailabilityResponse{}, fmt.Errorf("Data retrieved was not 'Service Response'")
    63  }
    64  
    65  type AvailabilityResponse struct {
    66  	Response struct {
    67  		ReturnValue struct {
    68  			Response bool     `json:"response"`
    69  			Data     []string `json:"data"`
    70  			TimeZone string   `json:"time_zone"`
    71  		} `json:"returnvalue"`
    72  		Status string `json:"status"`
    73  	} `json:"response"`
    74  }