-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSquareHours.java
171 lines (148 loc) · 5.81 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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.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 {
private static volatile Location cachedLocation; // Cache for the last successful location data
@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 Location loc = 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 {
}
/**
* Gets the location data, using cache if the Square API call fails.
*
* @return the Location object
* @throws Exception if an error occurs and no cached data is available
*/
private Location getLocation() throws Exception {
try {
Location loc = getSquareClient().getLocationsApi().retrieveLocation(System.getenv("SQUARE_LOCATION_ID")).getLocation();
cachedLocation = loc;
return loc;
} catch (Exception ex) {
log.error("Failed to retrieve location from Square API, using cached data if available", ex);
if (cachedLocation != null) {
return cachedLocation;
} else {
throw new Exception("No cached data available and failed to retrieve from Square API", ex);
}
}
}
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() {
// The current time in the TZ
final var tz = ZoneId.of(loc.getTimezone());
final var now = ZonedDateTime.now(tz);
final var today = now.toLocalDate();
return stream()
.filter(p -> p.getDow().equals(now.getDayOfWeek()))
.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);
});
}
}
@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();
}
}