alextselegidis / easyappointments

:date: Easy!Appointments - Self Hosted Appointment Scheduler
https://easyappointments.org
GNU General Public License v3.0
3.35k stars 1.28k forks source link

Question: REST API receive next available date / time #1262

Open drscream opened 2 years ago

drscream commented 2 years ago

Is it somehow possible to receive the next available date for a service (and provider)? Basically the idea it to receive the next available slot but the date could be in the future.

alextselegidis commented 2 years ago

Hello!

Currently you would need to query the /availabilities endpoint to figure this out yourself, but I see that your question introduces a great feature enhancement: the creation of a new /availabilities/next endpoint that will give you the next available slot.

This way any integration will be able to get the next available time for a service and a provider.

I'll mark this for as a feature enhancement in one of the upcoming releases.

Alex Tselegidis, Easy!Appointments Creator
Need a customization? Get a free quote!

fliot commented 1 year ago

👍

drscream commented 1 year ago

I've created a somehow workaround for me which is not optimal and I'm sure there might be better PHP code available. But maybe someone looking for exactly this:

<?php

$begin = new DateTime();
$tmp   = new DateTime();
$end   = $tmp->modify('+14 days');

$url   = 'https://XXXXXXXX/api/v1/availabilities';
$token = 'XXXXXXX';

$services = [
    3 => 'SERVICE_NAME',
];
$providers = [
    7 => 'PROVIDER_NAME1',
    8 => 'PROVIDER_NAME2'
];

$monthFormat   = new IntlDateFormatter('de_DE', IntlDateFormatter::MEDIUM, IntlDateFormatter::MEDIUM);
$monthFormat->setPattern('MMM');

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$found = [];
for($i = $begin; $i <= $end; $i->modify('+1 day')){
    foreach($providers as $providerId => $provider) {
        foreach($services as $serviceId => $service) {
            $requestUri = $url . '?' . http_build_query(
                array(
                    'providerId' => $providerId,
                    'serviceId'  => $serviceId,
                    'date'       => $i->format("Y-m-d")
                )
            );
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Authorization: Bearer '.$token
            ));
            curl_setopt($ch, CURLOPT_URL, $requestUri);
            $r = json_decode(curl_exec($ch));
            if(!empty($r)) {
        $month = $monthFormat->format($i);
                //list($hours, $minutes) = explode(':', $r[0]);
        //$i->add(new DateInterval('PT'.$hours.'H'.$minutes.'M'));
                $found[] = array(
                    'service'  => $service,
                    'provider' => $provider,
                    'timestamp'  => $i->format('Y-m-d').' '.$r[0],
            'date_year'  => $i->format('Y'),
            'date_month' => $month,
            'date_day'   => $i->format('d'),
                    'date_time'  => $r[0],
                );
        break;
            }
        }
    }
    if(count($found) > 1) { break; }
}

usort($found, function($a, $b) {
  $ad = new DateTime($a['timestamp']);
  $bd = new DateTime($b['timestamp']);

  if ($ad == $bd) {
    return 0;
  }

  return $ad < $bd ? -1 : 1;
});

header('Content-Type: application/json');
echo json_encode($found);
alextselegidis commented 1 year ago

Hello!

Thanks for sharing this!

Alex Tselegidis, Easy!Appointments Creator
Need a customization? Get a free quote!