From 0bab9a5bd1f9d3302cfd98ef52f91b4a3470a034 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Thu, 7 Jul 2022 19:45:56 -0300 Subject: [PATCH 1/2] API endpoint for available dates --- application/config/routes.php | 2 + .../api/v1/Availabilities_api_v1.php | 51 +++++++++++++++++++ openapi.yml | 42 +++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/application/config/routes.php b/application/config/routes.php index 5249eee484..acd14592b6 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -132,6 +132,8 @@ $route['api/v1/availabilities']['get'] = 'api/v1/availabilities_api_v1/get'; +$route['api/v1/availabilities/dates']['get'] = 'api/v1/availabilities_api_v1/dates'; + /* | ------------------------------------------------------------------------- | CUSTOM ROUTING diff --git a/application/controllers/api/v1/Availabilities_api_v1.php b/application/controllers/api/v1/Availabilities_api_v1.php index 60a7e98387..d8521c7144 100644 --- a/application/controllers/api/v1/Availabilities_api_v1.php +++ b/application/controllers/api/v1/Availabilities_api_v1.php @@ -77,4 +77,55 @@ public function get() json_exception($e); } } + + /** + * Generate the available dates based on the selected month, service and provider. + * + * This resource requires the following query parameters: + * + * - serviceId + * - providerId + * - month + * + * If no month parameter is provided then the current month will be used. + */ + public function dates() + { + try + { + $provider_id = request('providerId'); + + $service_id = request('serviceId'); + + $month = request('month'); + $month = new DateTime($month); + + $provider = $this->providers_model->find($provider_id); + + $service = $this->services_model->find($service_id); + + $number_of_days_in_month = (int)$month->format('t'); + $available_dates = []; + + for ($i = 1; $i <= $number_of_days_in_month; $i++) + { + $current_date = new DateTime($month->format('Y-m') . '-' . $i); + + $available_hours = $this->availability->get_available_hours( + $current_date->format('Y-m-d'), + $service, + $provider + ); + + if ( !empty($available_hours) ) + $available_dates[] = $current_date->format('Y-m-d'); + } + + json_response($available_dates); + } + catch (Throwable $e) + { + json_exception($e); + } + } } diff --git a/openapi.yml b/openapi.yml index ab11dbe432..5676480044 100644 --- a/openapi.yml +++ b/openapi.yml @@ -65,6 +65,44 @@ paths: security: - BearerToken: [ ] - BasicAuth: [ ] + /availabilities/dates: + get: + tags: + - availabilities + summary: Gets availability dates + parameters: + - name: providerId + in: query + schema: + type: integer + required: true + - name: serviceId + in: query + schema: + type: integer + required: true + - name: month + in: query + schema: + type: string + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/AvailabilityDates' + '401': + description: Unauthorized + '500': + description: Internal Server Error + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + security: + - BearerToken: [ ] + - BasicAuth: [ ] /appointments: get: tags: @@ -1722,6 +1760,10 @@ components: type: array items: type: string + AvailabilityDates: + type: array + items: + type: string AppointmentRecord: type: object properties: From be2f84db19731985f159fc0a9d9f31643282c2ee Mon Sep 17 00:00:00 2001 From: Gabriel Date: Mon, 5 Sep 2022 17:04:01 -0300 Subject: [PATCH 2/2] Add dates to /availabilities --- application/config/routes.php | 2 - .../api/v1/Availabilities_api_v1.php | 54 +++---------------- openapi.yml | 46 ++-------------- 3 files changed, 11 insertions(+), 91 deletions(-) diff --git a/application/config/routes.php b/application/config/routes.php index acd14592b6..5249eee484 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -132,8 +132,6 @@ $route['api/v1/availabilities']['get'] = 'api/v1/availabilities_api_v1/get'; -$route['api/v1/availabilities/dates']['get'] = 'api/v1/availabilities_api_v1/dates'; - /* | ------------------------------------------------------------------------- | CUSTOM ROUTING diff --git a/application/controllers/api/v1/Availabilities_api_v1.php b/application/controllers/api/v1/Availabilities_api_v1.php index d8521c7144..53a5231a08 100644 --- a/application/controllers/api/v1/Availabilities_api_v1.php +++ b/application/controllers/api/v1/Availabilities_api_v1.php @@ -33,12 +33,12 @@ public function __construct() } /** - * Generate the available hours based on the selected date, service and provider. + * Generate the available dates and hours based on the selected date, service and provider. * * This resource requires the following query parameters: * * - serviceId - * - providerI + * - providerId * - date * * Based on those values it will generate the available hours, just like how the booking page works. @@ -58,58 +58,20 @@ public function get() $service_id = request('serviceId'); $date = request('date'); - - if ( ! $date) - { - $date = date('Y-m-d'); - } - - $provider = $this->providers_model->find($provider_id); - - $service = $this->services_model->find($service_id); - - $available_hours = $this->availability->get_available_hours($date, $service, $provider); - - json_response($available_hours); - } - catch (Throwable $e) - { - json_exception($e); - } - } - - /** - * Generate the available dates based on the selected month, service and provider. - * - * This resource requires the following query parameters: - * - * - serviceId - * - providerId - * - month - * - * If no month parameter is provided then the current month will be used. - */ - public function dates() - { - try - { - $provider_id = request('providerId'); - - $service_id = request('serviceId'); - - $month = request('month'); - $month = new DateTime($month); + if(substr_count($date, '-') == 1) + $date .= '-01'; + $date = new DateTime($date); $provider = $this->providers_model->find($provider_id); $service = $this->services_model->find($service_id); - $number_of_days_in_month = (int)$month->format('t'); + $number_of_days_in_month = (int)$date->format('t'); $available_dates = []; for ($i = 1; $i <= $number_of_days_in_month; $i++) { - $current_date = new DateTime($month->format('Y-m') . '-' . $i); + $current_date = new DateTime($date->format('Y-m') . '-' . $i); $available_hours = $this->availability->get_available_hours( $current_date->format('Y-m-d'), @@ -118,7 +80,7 @@ public function dates() ); if ( !empty($available_hours) ) - $available_dates[] = $current_date->format('Y-m-d'); + $available_dates[$current_date->format('Y-m-d')] = $available_hours; } json_response($available_dates); diff --git a/openapi.yml b/openapi.yml index 5676480044..99e649ba3e 100644 --- a/openapi.yml +++ b/openapi.yml @@ -30,42 +30,6 @@ tags: - name: webhooks paths: /availabilities: - get: - tags: - - availabilities - summary: Gets availability - parameters: - - name: providerId - in: query - schema: - type: integer - - name: serviceId - in: query - schema: - type: integer - - name: date - in: query - schema: - type: string - responses: - '200': - description: OK - content: - application/json: - schema: - $ref: '#/components/schemas/Availabilities' - '401': - description: Unauthorized - '500': - description: Internal Server Error - content: - application/json: - schema: - $ref: '#/components/schemas/ErrorResponse' - security: - - BearerToken: [ ] - - BasicAuth: [ ] - /availabilities/dates: get: tags: - availabilities @@ -81,7 +45,7 @@ paths: schema: type: integer required: true - - name: month + - name: date in: query schema: type: string @@ -91,7 +55,7 @@ paths: content: application/json: schema: - $ref: '#/components/schemas/AvailabilityDates' + $ref: '#/components/schemas/Availabilities' '401': description: Unauthorized '500': @@ -1759,11 +1723,7 @@ components: Availabilities: type: array items: - type: string - AvailabilityDates: - type: array - items: - type: string + type: object AppointmentRecord: type: object properties: