@meetelise/chat 1.15.1 → 1.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,31 +10,49 @@ import {
10
10
  TourAvailabilityResponseRankOrderedSupportedTourTypesEnum,
11
11
  } from "@meetelise/rest-sdk";
12
12
  import groupBy from "lodash/groupBy";
13
+ import { TourType } from "./WebComponent/Scheduler/tour-scheduler";
13
14
 
14
15
  const availabilitiesCache: {
15
- [buildingId: number]: TourAvailabilityResponse;
16
- } = {};
16
+ buildingId?: number | null;
17
+ availabilities: { [buildingId: number]: TourAvailabilityResponse };
18
+ } = { buildingId: null, availabilities: {} };
17
19
 
20
+ /**
21
+ * Returns the raw availabilities.
22
+ *
23
+ * If no `buildingId` is provided, it will use a cached buildingId from the previous call.
24
+ * If there is none cached, it will throw an error.
25
+ */
18
26
  export const getRawAvailabilities = async (
19
- buildingId: number
27
+ buildingId?: number
20
28
  ): Promise<TourAvailabilityResponse> => {
21
- if (availabilitiesCache[buildingId]) {
22
- return availabilitiesCache[buildingId];
29
+ if (buildingId) {
30
+ availabilitiesCache.buildingId = buildingId;
31
+ }
32
+ const buildingIdToUse = availabilitiesCache.buildingId;
33
+ if (!buildingIdToUse) {
34
+ throw new Error(
35
+ "No buildingId was provided to getRawAvailabilities and there is no buildingId cached."
36
+ );
37
+ }
38
+ const availabilities = availabilitiesCache.availabilities;
39
+ if (availabilities[buildingIdToUse]) {
40
+ return availabilities[buildingIdToUse];
23
41
  }
24
42
  const startTime = startOfToday();
25
43
  const endTime = formatISO(endOfDay(addDays(startTime, 30)));
26
- const url = `https://app.meetelise.com/api/pub/v1/buildings/${buildingId}/tour/availabilities?startTime=${formatISO(
44
+ const url = `https://app.meetelise.com/api/pub/v1/buildings/${buildingIdToUse}/tour/availabilities?startTime=${formatISO(
27
45
  startTime
28
46
  )}&endTime=${endTime}`;
29
47
  const result = await axios.get<TourAvailabilityResponse>(url);
30
- availabilitiesCache[buildingId] = result.data;
48
+ availabilitiesCache.availabilities[buildingIdToUse] = result.data;
31
49
  // The endpoint INCORRECTLY states that these are returned as dates. They are, in fact, strings.
32
50
  return result.data;
33
51
  };
34
52
 
35
53
  export const getAvailabilitiesForTourType = async (
36
- buildingId: number,
37
- tourType: TourAvailabilityResponseRankOrderedSupportedTourTypesEnum
54
+ tourType: TourAvailabilityResponseRankOrderedSupportedTourTypesEnum,
55
+ buildingId?: number
38
56
  ): Promise<{
39
57
  /**
40
58
  *
@@ -68,13 +86,46 @@ export interface DateWithTimeZoneOffset {
68
86
  offset: string;
69
87
  }
70
88
 
89
+ /**
90
+ * Returns an object that reveals whether each tour type supported by
91
+ * `tour-scheduler` has availabilities (time slots available for scheduling) in
92
+ * the time window of interest.
93
+ *
94
+ * Note that the existence of current availabilities is distinct from the
95
+ * question of whether the community supports the tour type at all. The first
96
+ * implies the second but not vice versa.
97
+ */
98
+ export const getExistenceOfAvailabilitiesByTourType = async (): Promise<{
99
+ [TourType.Guided]: boolean;
100
+ [TourType.Self]: boolean;
101
+ [TourType.Virtual]: boolean;
102
+ }> => {
103
+ return {
104
+ [TourType.Guided]: !!(
105
+ await getAvailabilitiesForTourType(
106
+ TourAvailabilityResponseRankOrderedSupportedTourTypesEnum.WithAgent
107
+ )
108
+ )?.availableTourStartTimes?.length,
109
+ [TourType.Self]: !!(
110
+ await getAvailabilitiesForTourType(
111
+ TourAvailabilityResponseRankOrderedSupportedTourTypesEnum.SelfGuided
112
+ )
113
+ )?.availableTourStartTimes?.length,
114
+ [TourType.Virtual]: !!(
115
+ await getAvailabilitiesForTourType(
116
+ TourAvailabilityResponseRankOrderedSupportedTourTypesEnum.VirtualShowing
117
+ )
118
+ )?.availableTourStartTimes?.length,
119
+ };
120
+ };
121
+
71
122
  export const getAvailabilitiesGroupedByDay = async (
72
- buildingId: number,
73
- tourType: TourAvailabilityResponseRankOrderedSupportedTourTypesEnum
123
+ tourType: TourAvailabilityResponseRankOrderedSupportedTourTypesEnum,
124
+ buildingId?: number
74
125
  ): Promise<{ [day: string]: DateWithTimeZoneOffset[] }> => {
75
126
  const availabilitiesForTourTypeRaw = await getAvailabilitiesForTourType(
76
- buildingId,
77
- tourType
127
+ tourType,
128
+ buildingId
78
129
  );
79
130
  if (!availabilitiesForTourTypeRaw) {
80
131
  return {};
@@ -97,16 +148,6 @@ export const getAvailabilitiesGroupedByDay = async (
97
148
  );
98
149
  };
99
150
 
100
- // TODO: if cache is empty, observe it and wait for it to be filled in.
101
- // TODO: alternative to this: cache the building id when getRawAvailabilities is called. Then I can call the normal methods.
102
- export const getAvailabilitiesGroupedByDayCached = async (
103
- tourType: TourAvailabilityResponseRankOrderedSupportedTourTypesEnum
104
- ): Promise<{ [day: string]: DateWithTimeZoneOffset[] }> =>
105
- getAvailabilitiesGroupedByDay(
106
- Object.keys(availabilitiesCache).map(Number)[0],
107
- tourType
108
- );
109
-
110
151
  /**
111
152
  * Takes an ISO 8601 date string with time zone offset and returns
112
153
  * an object of our custom type DateWithTimeZoneOffset.