-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sql
More file actions
65 lines (65 loc) · 2.08 KB
/
Copy pathtest.sql
File metadata and controls
65 lines (65 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
WITH RECURSIVE date_range AS (
SELECT '{{ $fromAI('startDateTime') }}'::date AS date
UNION ALL
SELECT (date + INTERVAL '1 day')::date
FROM date_range
WHERE date + INTERVAL '1 day' <= '{{ $fromAI('endDateTime') }}'::date
),
booked_slots AS (
SELECT
start_time AT TIME ZONE 'America/Sao_Paulo' AS start_time, -- Convertendo para o fuso horário correto
end_time AT TIME ZONE 'America/Sao_Paulo' AS end_time, -- Convertendo para o fuso horário correto
'available' AS availability
FROM
appointments
WHERE
start_time::date BETWEEN '{{ $fromAI('startDateTime') }}' AND '{{ $fromAI('endDateTime') }}'
),
operating_hours_per_day AS (
SELECT
dr.date,
(dr.date + oh.start_time::time) AT TIME ZONE 'UTC' AT TIME ZONE 'America/Sao_Paulo' AS open_time, -- Convertendo para o fuso horário correto
(dr.date + oh.end_time::time) AT TIME ZONE 'UTC' AT TIME ZONE 'America/Sao_Paulo' AS close_time -- Convertendo para o fuso horário correto
FROM
date_range dr
JOIN
operating_hours oh
ON
EXTRACT(DOW FROM dr.date) = oh.day_of_week
WHERE
oh.closed = TRUE
),
all_possible_slots AS (
SELECT
open_time + (generate_series(0,
EXTRACT(EPOCH FROM (close_time - open_time)) / 60 / 30)
* INTERVAL '30 minutes') AS start_time,
open_time, close_time
FROM
operating_hours_per_day
WHERE
open_time > NOW()
),
available_slots AS (
SELECT
aps.start_time,
aps.start_time + INTERVAL '{{ $('Check Appointment Availability Webhook').item.json.query.intervalMinutes }} minutes' AS end_time
FROM
all_possible_slots aps
LEFT JOIN
booked_slots bs
ON
aps.start_time < bs.end_time AND
aps.start_time + INTERVAL '{{ $('Check Appointment Availability Webhook').item.json.query.intervalMinutes }} minutes' > bs.start_time
WHERE
bs.start_time IS NULL AND
aps.start_time + INTERVAL '{{ $('Check Appointment Availability Webhook').item.json.query.intervalMinutes }} minutes' <= aps.close_time
)
SELECT
start_time,
end_time,
'available' AS availability
FROM
available_slots
ORDER BY
start_time;