arky-sdk 0.3.82 → 0.4.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.
- package/dist/index.cjs +1 -309
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -59
- package/dist/index.d.ts +4 -59
- package/dist/index.js +1 -309
- package/dist/index.js.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var nanostores = require('nanostores');
|
|
4
|
-
|
|
5
3
|
// src/types/index.ts
|
|
6
4
|
var PaymentMethodType = /* @__PURE__ */ ((PaymentMethodType2) => {
|
|
7
5
|
PaymentMethodType2["Cash"] = "CASH";
|
|
@@ -1713,310 +1711,9 @@ async function injectSvgIntoElement(mediaObject, targetElement, className) {
|
|
|
1713
1711
|
console.error("Error injecting SVG:", error);
|
|
1714
1712
|
}
|
|
1715
1713
|
}
|
|
1716
|
-
function formatTime(ts, tz) {
|
|
1717
|
-
return new Date(ts * 1e3).toLocaleTimeString([], {
|
|
1718
|
-
hour: "2-digit",
|
|
1719
|
-
minute: "2-digit",
|
|
1720
|
-
timeZone: tz
|
|
1721
|
-
});
|
|
1722
|
-
}
|
|
1723
|
-
function formatSlotTime(from, to, tz) {
|
|
1724
|
-
return `${formatTime(from, tz)} \u2013 ${formatTime(to, tz)}`;
|
|
1725
|
-
}
|
|
1726
|
-
function getTzOffset(date, tz) {
|
|
1727
|
-
const utc = new Date(date.toLocaleString("en-US", { timeZone: "UTC" }));
|
|
1728
|
-
const local = new Date(date.toLocaleString("en-US", { timeZone: tz }));
|
|
1729
|
-
return (utc.getTime() - local.getTime()) / 6e4;
|
|
1730
|
-
}
|
|
1731
|
-
function toUtcTimestamp(year, month, day, mins, tz) {
|
|
1732
|
-
const midnight = new Date(Date.UTC(year, month - 1, day));
|
|
1733
|
-
const offset = getTzOffset(midnight, tz);
|
|
1734
|
-
return Math.floor(midnight.getTime() / 1e3) + (mins + offset) * 60;
|
|
1735
|
-
}
|
|
1736
|
-
function isBlocked(from, to, timeline, limit) {
|
|
1737
|
-
const before = timeline.filter((p) => p.timestamp <= from).sort((a, b) => b.timestamp - a.timestamp);
|
|
1738
|
-
if (before.length > 0 && before[0].concurrent >= limit) return true;
|
|
1739
|
-
for (const p of timeline) {
|
|
1740
|
-
if (p.timestamp >= from && p.timestamp < to && p.concurrent >= limit) return true;
|
|
1741
|
-
}
|
|
1742
|
-
return false;
|
|
1743
|
-
}
|
|
1744
|
-
function getTotalDuration(durations) {
|
|
1745
|
-
return durations.reduce((sum, d) => sum + d.duration, 0);
|
|
1746
|
-
}
|
|
1747
|
-
function getWorkingHoursForDate(wt, date, tz) {
|
|
1748
|
-
if (!wt) return [];
|
|
1749
|
-
const dayName = date.toLocaleDateString("en-US", { weekday: "long", timeZone: tz }).toLowerCase();
|
|
1750
|
-
const m = date.getMonth() + 1;
|
|
1751
|
-
const d = date.getDate();
|
|
1752
|
-
const ts = Math.floor(date.getTime() / 1e3);
|
|
1753
|
-
const specific = wt.specificDates?.find((s) => s.date === ts);
|
|
1754
|
-
if (specific) return specific.workingHours || [];
|
|
1755
|
-
const outcast = wt.outcastDates?.find((o) => o.month === m && o.day === d);
|
|
1756
|
-
if (outcast) return outcast.workingHours || [];
|
|
1757
|
-
return wt.workingDays?.find((w) => w.day === dayName)?.workingHours || [];
|
|
1758
|
-
}
|
|
1759
|
-
function computeSlotsForDate(opts) {
|
|
1760
|
-
const { providers, date, durations, timezone, slotInterval } = opts;
|
|
1761
|
-
const total = getTotalDuration(durations);
|
|
1762
|
-
const interval = slotInterval || total;
|
|
1763
|
-
const slots = [];
|
|
1764
|
-
const nowTs = Math.floor(Date.now() / 1e3);
|
|
1765
|
-
const today = /* @__PURE__ */ new Date();
|
|
1766
|
-
today.setHours(0, 0, 0, 0);
|
|
1767
|
-
if (date < today) return [];
|
|
1768
|
-
const [year, month, day] = date.toLocaleDateString("en-CA", { timeZone: timezone }).split("-").map(Number);
|
|
1769
|
-
for (const p of providers) {
|
|
1770
|
-
for (const wh of getWorkingHoursForDate(p.workingTime, date, timezone)) {
|
|
1771
|
-
for (let m = wh.from; m + total <= wh.to; m += interval) {
|
|
1772
|
-
const from = toUtcTimestamp(year, month, day, m, timezone);
|
|
1773
|
-
const to = from + total * 60;
|
|
1774
|
-
if (from < nowTs) continue;
|
|
1775
|
-
if (!isBlocked(from, to, p.timeline, p.concurrentLimit)) {
|
|
1776
|
-
slots.push({ from, to, providerId: p.id });
|
|
1777
|
-
}
|
|
1778
|
-
}
|
|
1779
|
-
}
|
|
1780
|
-
}
|
|
1781
|
-
return slots.sort((a, b) => a.from - b.from);
|
|
1782
|
-
}
|
|
1783
|
-
function hasAvailableSlots(opts) {
|
|
1784
|
-
return computeSlotsForDate(opts).length > 0;
|
|
1785
|
-
}
|
|
1786
|
-
var createInitialState = (timezone) => ({
|
|
1787
|
-
service: null,
|
|
1788
|
-
providers: [],
|
|
1789
|
-
selectedProvider: null,
|
|
1790
|
-
currentMonth: new Date((/* @__PURE__ */ new Date()).getFullYear(), (/* @__PURE__ */ new Date()).getMonth(), 1),
|
|
1791
|
-
calendar: [],
|
|
1792
|
-
selectedDate: null,
|
|
1793
|
-
startDate: null,
|
|
1794
|
-
endDate: null,
|
|
1795
|
-
slots: [],
|
|
1796
|
-
selectedSlot: null,
|
|
1797
|
-
cart: [],
|
|
1798
|
-
timezone,
|
|
1799
|
-
loading: false
|
|
1800
|
-
});
|
|
1801
|
-
var createReservationEngine = (api, config = {}) => {
|
|
1802
|
-
const timezone = config.timezone || Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
1803
|
-
const store = nanostores.map(createInitialState(timezone));
|
|
1804
|
-
const getServiceDurations = () => {
|
|
1805
|
-
const state = store.get();
|
|
1806
|
-
if (!state.service?.durations?.length) return [{ duration: 60, isPause: false }];
|
|
1807
|
-
return state.service.durations.map((d) => ({
|
|
1808
|
-
duration: d.duration,
|
|
1809
|
-
isPause: d.isPause || d.is_pause || false
|
|
1810
|
-
}));
|
|
1811
|
-
};
|
|
1812
|
-
const buildCalendar = () => {
|
|
1813
|
-
const state = store.get();
|
|
1814
|
-
const { currentMonth, selectedDate, startDate, endDate, providers, selectedProvider, timezone: timezone2 } = state;
|
|
1815
|
-
const year = currentMonth.getFullYear();
|
|
1816
|
-
const month = currentMonth.getMonth();
|
|
1817
|
-
const first = new Date(year, month, 1);
|
|
1818
|
-
const last = new Date(year, month + 1, 0);
|
|
1819
|
-
const today = /* @__PURE__ */ new Date();
|
|
1820
|
-
today.setHours(0, 0, 0, 0);
|
|
1821
|
-
const cells = [];
|
|
1822
|
-
const pad = (first.getDay() + 6) % 7;
|
|
1823
|
-
for (let i = 0; i < pad; i++) {
|
|
1824
|
-
cells.push({ date: /* @__PURE__ */ new Date(0), iso: "", available: false, isSelected: false, isInRange: false, isToday: false, blank: true });
|
|
1825
|
-
}
|
|
1826
|
-
const activeProviders = selectedProvider ? providers.filter((p) => p.id === selectedProvider.id) : providers;
|
|
1827
|
-
const durations = getServiceDurations();
|
|
1828
|
-
for (let d = 1; d <= last.getDate(); d++) {
|
|
1829
|
-
const date = new Date(year, month, d);
|
|
1830
|
-
const iso = `${year}-${String(month + 1).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
|
|
1831
|
-
const available = activeProviders.length > 0 && hasAvailableSlots({ providers: activeProviders, date, durations, timezone: timezone2 });
|
|
1832
|
-
const isToday = date.getTime() === today.getTime();
|
|
1833
|
-
const isSelected = iso === selectedDate || iso === startDate || iso === endDate;
|
|
1834
|
-
let isInRange = false;
|
|
1835
|
-
if (startDate && endDate) {
|
|
1836
|
-
const t = date.getTime();
|
|
1837
|
-
isInRange = t > new Date(startDate).getTime() && t < new Date(endDate).getTime();
|
|
1838
|
-
}
|
|
1839
|
-
cells.push({ date, iso, available, isSelected, isInRange, isToday, blank: false });
|
|
1840
|
-
}
|
|
1841
|
-
const suffix = (7 - cells.length % 7) % 7;
|
|
1842
|
-
for (let i = 0; i < suffix; i++) {
|
|
1843
|
-
cells.push({ date: /* @__PURE__ */ new Date(0), iso: "", available: false, isSelected: false, isInRange: false, isToday: false, blank: true });
|
|
1844
|
-
}
|
|
1845
|
-
return cells;
|
|
1846
|
-
};
|
|
1847
|
-
const computeSlots = (dateStr) => {
|
|
1848
|
-
const state = store.get();
|
|
1849
|
-
const { providers, selectedProvider, timezone: timezone2, service } = state;
|
|
1850
|
-
const date = /* @__PURE__ */ new Date(dateStr + "T00:00:00");
|
|
1851
|
-
const activeProviders = selectedProvider ? providers.filter((p) => p.id === selectedProvider.id) : providers;
|
|
1852
|
-
const raw = computeSlotsForDate({ providers: activeProviders, date, durations: getServiceDurations(), timezone: timezone2 });
|
|
1853
|
-
return raw.map((s, i) => ({
|
|
1854
|
-
id: `${service?.id}-${s.from}-${i}`,
|
|
1855
|
-
serviceId: service?.id || "",
|
|
1856
|
-
providerId: s.providerId,
|
|
1857
|
-
from: s.from,
|
|
1858
|
-
to: s.to,
|
|
1859
|
-
timeText: formatSlotTime(s.from, s.to, timezone2),
|
|
1860
|
-
dateText: new Date(s.from * 1e3).toLocaleDateString([], { weekday: "short", month: "short", day: "numeric", timeZone: timezone2 })
|
|
1861
|
-
}));
|
|
1862
|
-
};
|
|
1863
|
-
const actions = {
|
|
1864
|
-
setTimezone(tz) {
|
|
1865
|
-
store.setKey("timezone", tz);
|
|
1866
|
-
store.setKey("calendar", buildCalendar());
|
|
1867
|
-
const state = store.get();
|
|
1868
|
-
if (state.selectedDate) {
|
|
1869
|
-
store.setKey("slots", computeSlots(state.selectedDate));
|
|
1870
|
-
store.setKey("selectedSlot", null);
|
|
1871
|
-
}
|
|
1872
|
-
},
|
|
1873
|
-
async setService(serviceId) {
|
|
1874
|
-
store.setKey("loading", true);
|
|
1875
|
-
try {
|
|
1876
|
-
const service = await api.getService({ id: serviceId });
|
|
1877
|
-
store.set({
|
|
1878
|
-
...store.get(),
|
|
1879
|
-
service,
|
|
1880
|
-
selectedProvider: null,
|
|
1881
|
-
providers: [],
|
|
1882
|
-
selectedDate: null,
|
|
1883
|
-
startDate: null,
|
|
1884
|
-
endDate: null,
|
|
1885
|
-
slots: [],
|
|
1886
|
-
selectedSlot: null,
|
|
1887
|
-
currentMonth: new Date((/* @__PURE__ */ new Date()).getFullYear(), (/* @__PURE__ */ new Date()).getMonth(), 1),
|
|
1888
|
-
loading: false
|
|
1889
|
-
});
|
|
1890
|
-
await actions.loadMonth();
|
|
1891
|
-
} catch (e) {
|
|
1892
|
-
store.setKey("loading", false);
|
|
1893
|
-
throw e;
|
|
1894
|
-
}
|
|
1895
|
-
},
|
|
1896
|
-
async loadMonth() {
|
|
1897
|
-
const state = store.get();
|
|
1898
|
-
if (!state.service) return;
|
|
1899
|
-
store.setKey("loading", true);
|
|
1900
|
-
try {
|
|
1901
|
-
const { currentMonth, service } = state;
|
|
1902
|
-
const year = currentMonth.getFullYear();
|
|
1903
|
-
const month = currentMonth.getMonth();
|
|
1904
|
-
const from = Math.floor(new Date(year, month, 1).getTime() / 1e3);
|
|
1905
|
-
const to = Math.floor(new Date(year, month + 1, 0, 23, 59, 59).getTime() / 1e3);
|
|
1906
|
-
const providers = await api.getServiceProviders({ serviceId: service.id, from, to });
|
|
1907
|
-
store.setKey("providers", providers || []);
|
|
1908
|
-
store.setKey("calendar", buildCalendar());
|
|
1909
|
-
} finally {
|
|
1910
|
-
store.setKey("loading", false);
|
|
1911
|
-
}
|
|
1912
|
-
},
|
|
1913
|
-
prevMonth() {
|
|
1914
|
-
const { currentMonth } = store.get();
|
|
1915
|
-
store.setKey("currentMonth", new Date(currentMonth.getFullYear(), currentMonth.getMonth() - 1, 1));
|
|
1916
|
-
actions.loadMonth();
|
|
1917
|
-
},
|
|
1918
|
-
nextMonth() {
|
|
1919
|
-
const { currentMonth } = store.get();
|
|
1920
|
-
store.setKey("currentMonth", new Date(currentMonth.getFullYear(), currentMonth.getMonth() + 1, 1));
|
|
1921
|
-
actions.loadMonth();
|
|
1922
|
-
},
|
|
1923
|
-
selectProvider(provider) {
|
|
1924
|
-
store.set({
|
|
1925
|
-
...store.get(),
|
|
1926
|
-
selectedProvider: provider,
|
|
1927
|
-
selectedDate: null,
|
|
1928
|
-
startDate: null,
|
|
1929
|
-
endDate: null,
|
|
1930
|
-
slots: [],
|
|
1931
|
-
selectedSlot: null
|
|
1932
|
-
});
|
|
1933
|
-
store.setKey("calendar", buildCalendar());
|
|
1934
|
-
},
|
|
1935
|
-
selectDate(day) {
|
|
1936
|
-
if (day.blank || !day.available) return;
|
|
1937
|
-
const state = store.get();
|
|
1938
|
-
const slots = computeSlots(day.iso);
|
|
1939
|
-
store.set({ ...state, selectedDate: day.iso, slots, selectedSlot: null });
|
|
1940
|
-
store.setKey("calendar", buildCalendar());
|
|
1941
|
-
},
|
|
1942
|
-
selectSlot(slot) {
|
|
1943
|
-
store.setKey("selectedSlot", slot);
|
|
1944
|
-
},
|
|
1945
|
-
findFirstAvailable() {
|
|
1946
|
-
const state = store.get();
|
|
1947
|
-
for (const day of state.calendar) {
|
|
1948
|
-
if (!day.blank && day.available) {
|
|
1949
|
-
actions.selectDate(day);
|
|
1950
|
-
return;
|
|
1951
|
-
}
|
|
1952
|
-
}
|
|
1953
|
-
},
|
|
1954
|
-
updateCalendar() {
|
|
1955
|
-
store.setKey("calendar", buildCalendar());
|
|
1956
|
-
},
|
|
1957
|
-
addToCart() {
|
|
1958
|
-
const state = store.get();
|
|
1959
|
-
if (!state.selectedSlot) return;
|
|
1960
|
-
store.set({
|
|
1961
|
-
...state,
|
|
1962
|
-
cart: [...state.cart, state.selectedSlot],
|
|
1963
|
-
selectedDate: null,
|
|
1964
|
-
startDate: null,
|
|
1965
|
-
endDate: null,
|
|
1966
|
-
slots: [],
|
|
1967
|
-
selectedSlot: null
|
|
1968
|
-
});
|
|
1969
|
-
store.setKey("calendar", buildCalendar());
|
|
1970
|
-
},
|
|
1971
|
-
removeFromCart(slotId) {
|
|
1972
|
-
const state = store.get();
|
|
1973
|
-
store.setKey("cart", state.cart.filter((s) => s.id !== slotId));
|
|
1974
|
-
},
|
|
1975
|
-
clearCart() {
|
|
1976
|
-
store.setKey("cart", []);
|
|
1977
|
-
},
|
|
1978
|
-
async checkout(options = {}) {
|
|
1979
|
-
const state = store.get();
|
|
1980
|
-
if (!state.cart.length) throw new Error("Cart is empty");
|
|
1981
|
-
store.setKey("loading", true);
|
|
1982
|
-
try {
|
|
1983
|
-
return api.checkout({
|
|
1984
|
-
items: state.cart.map((s) => ({
|
|
1985
|
-
serviceId: s.serviceId,
|
|
1986
|
-
providerId: s.providerId,
|
|
1987
|
-
from: s.from,
|
|
1988
|
-
to: s.to,
|
|
1989
|
-
blocks: s.serviceBlocks || []
|
|
1990
|
-
})),
|
|
1991
|
-
paymentMethod: options.paymentMethod,
|
|
1992
|
-
promoCode: options.promoCode ?? null,
|
|
1993
|
-
blocks: options.blocks || []
|
|
1994
|
-
});
|
|
1995
|
-
} finally {
|
|
1996
|
-
store.setKey("loading", false);
|
|
1997
|
-
}
|
|
1998
|
-
},
|
|
1999
|
-
async getQuote(options = {}) {
|
|
2000
|
-
const state = store.get();
|
|
2001
|
-
if (!state.cart.length) return null;
|
|
2002
|
-
return api.getQuote({
|
|
2003
|
-
items: state.cart.map((s) => ({ serviceId: s.serviceId })),
|
|
2004
|
-
paymentMethod: options.paymentMethod || "CASH",
|
|
2005
|
-
promoCode: options.promoCode
|
|
2006
|
-
});
|
|
2007
|
-
},
|
|
2008
|
-
async getProvidersList() {
|
|
2009
|
-
const state = store.get();
|
|
2010
|
-
if (!state.service) return [];
|
|
2011
|
-
const response = await api.getProviders({ serviceId: state.service.id, limit: 100 });
|
|
2012
|
-
return response?.items || [];
|
|
2013
|
-
}
|
|
2014
|
-
};
|
|
2015
|
-
return { store, ...actions };
|
|
2016
|
-
};
|
|
2017
1714
|
|
|
2018
1715
|
// src/index.ts
|
|
2019
|
-
var SDK_VERSION = "0.
|
|
1716
|
+
var SDK_VERSION = "0.4.0";
|
|
2020
1717
|
var SUPPORTED_FRAMEWORKS = [
|
|
2021
1718
|
"astro",
|
|
2022
1719
|
"react",
|
|
@@ -2067,11 +1764,6 @@ async function createArkySDK(config) {
|
|
|
2067
1764
|
database: createDatabaseApi(apiConfig),
|
|
2068
1765
|
featureFlags: createFeatureFlagsApi(apiConfig),
|
|
2069
1766
|
location: createLocationApi(apiConfig),
|
|
2070
|
-
// High-level reservation engine
|
|
2071
|
-
reservationEngine: (engineConfig) => {
|
|
2072
|
-
const reservationApi = createReservationApi(apiConfig);
|
|
2073
|
-
return createReservationEngine(reservationApi, engineConfig);
|
|
2074
|
-
},
|
|
2075
1767
|
setBusinessId: (businessId) => {
|
|
2076
1768
|
apiConfig.businessId = businessId;
|
|
2077
1769
|
},
|