find_slots( [ 'meeting_id' => (int) $meeting_id, 'date_from' => (string) $date_from, 'date_to' => (string) $date_to, 'staff_id' => (int) $staff_id, 'location' => $location, 'time_from' => (string) $time_from, ] ); } catch ( \Throwable $e ) { Log::error( 'Timetics slot search failed: ' . $e->getMessage() ); return [ 'ok' => false, 'error' => 'Free times could not be loaded. Tell the visitor to open the booking page.', ]; } if ( empty( $found['ok'] ) ) { return $found; } $timezone = self::timezone_label( $this->gateway->visitor_timezone() ); $days = []; $cards = []; $total = 0; $truncated = false; foreach ( $found['days'] as $date => $slots ) { // Each start time once, with every staff member free then: for the // model, so a big team cannot push afternoon times out of what it // sees, and for the card, so 5 free staff are one button, not 5. $unique = []; $buttons = []; foreach ( $slots as $slot ) { $label = $this->time_label( $slot['start'] ); $unique[ $slot['start'] ] ??= [ 'start' => $slot['start'], 'label' => $label, 'staff_ids' => [], ]; $unique[ $slot['start'] ]['staff_ids'][] = $slot['staff_id']; $buttons[ $slot['start'] ] ??= [ 'start' => $slot['start'], 'label' => $label, 'staff' => [], ]; $buttons[ $slot['start'] ]['staff'][] = [ 'id' => $slot['staff_id'], 'name' => $slot['staff_name'], ]; } $unique = array_values( $unique ); $total += count( $unique ); $day = [ 'date' => $date, 'date_label' => $this->date_label( $date ), 'count' => count( $unique ), 'times' => array_slice( $unique, 0, self::MAX_TIMES ), ]; if ( count( $unique ) > self::MAX_TIMES ) { $day['more_after'] = $unique[ self::MAX_TIMES - 1 ]['start']; $truncated = true; } $days[] = $day; if ( $slots ) { $cards[] = [ 'date' => $date, 'date_label' => $this->date_label( $date ), // Same cut as the model's list, so the card stays a size a phone can show. 'slots' => array_slice( array_values( $buttons ), 0, self::MAX_TIMES ), ]; } } $result = [ 'ok' => true, 'timezone' => $timezone, 'staff' => $found['staff'], 'days' => $days, ]; $notes = []; if ( $found['staff_total'] > count( $found['staff'] ) ) { $notes[] = 'Only ' . count( $found['staff'] ) . ' of ' . $found['staff_total'] . ' staff were searched. A time not listed may be free with someone else: ask whether the visitor has a staff member in mind and search for that person.'; } if ( $truncated ) { $notes[] = 'Some days list only their first ' . self::MAX_TIMES . ' times (see more_after). A time not listed is not proof it is taken: call timetics-available-slots again with time_from set to the visitor\'s time.'; } if ( 0 === $total ) { $notes[] = 'No free times in this range. Try the next 7 days once.'; } if ( $notes ) { $result['note'] = implode( ' ', $notes ); } if ( 0 === $total ) { return $result; } $this->set_card( 'slot_picker', [ 'service_id' => $found['meeting']['id'], 'service_name' => $found['meeting']['name'], 'timezone_label' => $timezone, 'days' => $cards, // The times were found for these. A tap sends them back, so an // older card still books what it showed after the chat moved on. // Index 0 is a real location, so no array_filter here. 'choices' => null === $found['location'] ? [] : [ 'location_index' => $found['location'] ], ] ); return $result; } /** * Check a visitor's choice and collect free slots, merged across staff. * * @param array $choice meeting_id, date_from, date_to, staff_id, location (int|null), time_from. * @param bool $fresh Skip the one-minute cache, for checking a time right before a booking link. * @return array [ 'ok' => true, 'meeting' => array, 'location' => int|null, 'staff' => [ [ id, name ] ], * 'staff_total' => int, 'days' => [ 'Y-m-d' => [ [ start, staff_id, staff_name ] ] ] ] * or [ 'ok' => false, 'error' => string ]. */ public function find_slots( array $choice, bool $fresh = false ): array { $inactive = $this->inactive(); if ( null !== $inactive ) { return $inactive; } $meeting = $this->gateway->meeting( (int) ( $choice['meeting_id'] ?? 0 ) ); if ( null === $meeting ) { return $this->error( 'That service is not available. Call timetics-service-search again.' ); } if ( ! $meeting['staff'] ) { return $this->error( 'This service has no staff assigned. Suggest another service.' ); } $visitor_tz = new \DateTimeZone( $this->gateway->visitor_timezone() ); $from = $this->parse_date( (string) ( $choice['date_from'] ?? '' ), $visitor_tz ); $to = '' !== (string) ( $choice['date_to'] ?? '' ) ? $this->parse_date( (string) $choice['date_to'], $visitor_tz ) : $from; if ( null === $from || null === $to ) { return $this->error( 'Dates must be YYYY-MM-DD.' ); } if ( $from->format( 'Y-m-d' ) < $this->visitor_today() ) { return $this->error( 'That date has passed. The visitor\'s today is ' . $this->visitor_today() . '. Ask for a date from then on.' ); } if ( $to < $from || $to > $from->modify( '+' . ( self::MAX_DAYS - 1 ) . ' days' ) ) { return $this->error( 'Search at most 7 days at a time.' ); } $location = null === ( $choice['location'] ?? null ) ? null : (int) $choice['location']; if ( null !== $location && ! in_array( $location, array_column( $meeting['locations'], 'index' ), true ) ) { return $this->error( 'That location is not offered for this service. Options: ' . implode( ', ', array_map( static fn( array $l ): string => $l['index'] . ' = ' . $l['label'], $meeting['locations'] ) ) . '.' ); } $time_from = (string) ( $choice['time_from'] ?? '' ); if ( '' !== $time_from && 1 !== preg_match( '/^([01]\d|2[0-3]):[0-5]\d$/', $time_from ) ) { return $this->error( 'time_from must be HH:MM, 24 hour.' ); } $staff_id = (int) ( $choice['staff_id'] ?? 0 ); $all_staff = $staff_id > 0 ? array_values( array_filter( $meeting['staff'], static fn( array $m ): bool => $m['id'] === $staff_id ) ) : $meeting['staff']; if ( ! $all_staff ) { return $this->error( 'That staff member does not offer this service.' ); } $room = self::MAX_LOOKUPS - $this->lookups; $staff = array_slice( $all_staff, 0, min( self::MAX_STAFF, $room ) ); if ( ! $staff ) { return $this->error( 'Enough times were searched for this message. Show the visitor what was found, or ask for one day or one staff member and search again on the next message.' ); } $days = []; for ( $day = $from; $day <= $to; $day = $day->modify( '+1 day' ) ) { $days[ $day->format( 'Y-m-d' ) ] = []; } $blocked = $this->gateway->blocked_days(); $now = $this->visitor_now(); $cache_key = 'aisentic_tt_slots_' . md5( (string) wp_json_encode( [ $meeting['id'], array_column( $staff, 'id' ), $from->format( 'Y-m-d' ), $to->format( 'Y-m-d' ), $visitor_tz->getName() ] ) ); $by_staff = $fresh ? false : get_transient( $cache_key ); if ( ! is_array( $by_staff ) ) { $by_staff = []; foreach ( $staff as $member ) { $by_staff[ $member['id'] ] = $this->gateway->slots( $meeting['id'], $member['id'], $from->format( 'Y-m-d' ), $to->format( 'Y-m-d' ), $visitor_tz->getName() ); } $this->lookups += count( $staff ); set_transient( $cache_key, $by_staff, self::CACHE_TTL ); } foreach ( $staff as $member ) { $raw = $by_staff[ $member['id'] ] ?? []; foreach ( Timetics_Public_Gateway::filter_slots( $raw, $meeting, $blocked, $now ) as $day ) { if ( ! array_key_exists( $day['date'], $days ) ) { continue; } foreach ( $day['slots'] as $start ) { if ( $start < $time_from ) { continue; } $days[ $day['date'] ][] = [ 'start' => $start, 'staff_id' => $member['id'], 'staff_name' => $member['name'], ]; } } } foreach ( $days as $date => $slots ) { usort( $slots, static fn( array $a, array $b ): int => [ $a['start'], $a['staff_name'] ] <=> [ $b['start'], $b['staff_name'] ] ); $days[ $date ] = $slots; } return [ 'ok' => true, 'meeting' => $meeting, 'location' => $location, 'staff' => array_map( static fn( array $m ): array => [ 'id' => $m['id'], 'name' => $m['name'], ], $staff ), 'staff_total' => count( $all_staff ), 'days' => $days, ]; } /** * Parse a strict YYYY-MM-DD date. * * @param string $value Date. * @param \DateTimeZone $tz Visitor timezone. * @return \DateTimeImmutable|null */ private function parse_date( string $value, \DateTimeZone $tz ): ?\DateTimeImmutable { $date = \DateTimeImmutable::createFromFormat( '!Y-m-d', $value, $tz ); return $date && $date->format( 'Y-m-d' ) === $value ? $date : null; } /** * Failure result. * * @param string $message Message for the model. * @return array */ private function error( string $message ): array { return [ 'ok' => false, 'error' => $message, ]; } }