-
Notifications
You must be signed in to change notification settings - Fork 3
/
SquareHours.java
157 lines (134 loc) · 5.31 KB
/
SquareHours.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cloud.cleo.squareup.functions;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.squareup.square.models.BusinessHoursPeriod;
import com.squareup.square.models.Location;
import java.time.DayOfWeek;
import static java.time.DayOfWeek.FRIDAY;
import static java.time.DayOfWeek.MONDAY;
import static java.time.DayOfWeek.SATURDAY;
import static java.time.DayOfWeek.SUNDAY;
import static java.time.DayOfWeek.THURSDAY;
import static java.time.DayOfWeek.TUESDAY;
import static java.time.DayOfWeek.WEDNESDAY;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.TextStyle;
import java.util.ArrayList;
import java.util.Locale;
import java.util.function.Function;
import lombok.AccessLevel;
import lombok.Getter;
/**
* Return the store hours from Square API
*
* @author sjensen
* @param <Request>
*/
public class SquareHours<Request> extends AbstractFunction {
@Override
public String getName() {
return "store_hours";
}
@Override
public String getDescription() {
return "Return the open store hours by day of week along with current open/closed status and current date/time. Any day of week not returned means the store is closed that day.";
}
@Override
public Class getRequestClass() {
return Request.class;
}
/**
*
* @return
*/
@Override
public Function<Request, Object> getExecutor() {
return (var r) -> {
try {
final var loc = getSquareClient().getLocationsApi().retrieveLocation(System.getenv("SQUARE_LOCATION_ID")).getLocation();
final var bh = new BusinessHours(loc);
final var tz = ZoneId.of(loc.getTimezone());
final var now = ZonedDateTime.now(tz);
final var dow = now.getDayOfWeek();
/**
* GPT gives wrong information sometimes saying its open when store is closed.
* Giving it the concrete status of OPEN or CLOSED seems to help with a timestamp.
* Sometimes even though it knows the date, it says the wrong day of week too, so added that as well
* returning all this info vs just the periods seems to fix everything and I can't get it to return wrong answer anymore
*/
final ObjectNode json = mapper.createObjectNode();
json.put("open_closed_status", bh.isOpen() ? "OPEN" : "CLOSED");
json.put("current_date_time", now.toString());
json.put("current_day_of_week", now.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US).toUpperCase());
json.putPOJO("open_hours", loc.getBusinessHours().getPeriods());
return json;
} catch (Exception ex) {
log.error("Unhandled Error",ex);
return mapper.createObjectNode().put("error_message", ex.getLocalizedMessage());
}
};
}
private static class Request {
}
private static class BusinessHours extends ArrayList<OpenPeriod> {
private final Location loc;
public BusinessHours(Location loc) {
this.loc = loc;
loc.getBusinessHours().getPeriods().forEach(p -> add(new OpenPeriod(p)));
}
public boolean isOpen() {
final var tz = ZoneId.of(loc.getTimezone());
// The current time in the TZ
final var now = ZonedDateTime.now(tz);
final var today = LocalDate.now(tz);
// The Day of Week
final var dow = now.getDayOfWeek();
final var matched = stream().filter(p -> p.getDow().equals(dow)).toList();
if (!matched.isEmpty()) {
// There is something matched for this dow of week
return matched.stream().anyMatch(p -> {
final var start = LocalDateTime.of(today, p.getStart()).atZone(tz);
final var end = LocalDateTime.of(today, p.getEnd()).atZone(tz);
return now.isAfter(start) && now.isBefore(end);
});
}
// We don't have any entries for today, so definitely closed
return false;
}
}
@Getter(AccessLevel.PRIVATE)
private static class OpenPeriod {
final DayOfWeek dow;
final LocalTime start;
final LocalTime end;
public OpenPeriod(BusinessHoursPeriod bhp) {
dow = switch (bhp.getDayOfWeek()) {
case "SUN" ->
SUNDAY;
case "MON" ->
MONDAY;
case "TUE" ->
TUESDAY;
case "WED" ->
WEDNESDAY;
case "THU" ->
THURSDAY;
case "FRI" ->
FRIDAY;
case "SAT" ->
SATURDAY;
default ->
throw new RuntimeException("Day of Week Cannot be matched " + bhp.getDayOfWeek());
};
start = LocalTime.parse(bhp.getStartLocalTime());
end = LocalTime.parse(bhp.getEndLocalTime());
}
}
@Override
protected boolean isEnabled() {
return isSquareEnabled();
}
}