@ancon/wildcat-utils 1.32.0 → 1.32.1
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const
|
|
1
|
+
"use strict";const F=require("moment"),r=require("@ancon/wildcat-types"),g=require("../shared/isWithinFlag.js"),I=require("../shared/generateId.js"),c=[{id:r.OrderFormat.EatIn,name:"eatIn"},{id:r.OrderFormat.TakeAway,name:"takeaway"},{id:r.OrderFormat.TableOrder,name:"tableOrder"},{id:r.OrderFormat.Delivery,name:"delivery"}];function v(e){const o=F.utc(e).subtract(1,"day").startOf("day"),t=[];for(let s=0,d=o;s<7;s+=1)d=d.clone().add(1,"day").startOf("day"),t.push(d.clone());return t}function W(e){return[...e].sort((o,t)=>o.isoWeekday()-t.isoWeekday())}function k(e){return e.split(":").map(Number)}function T(e,o,t){const[s,d]=k(e.start),[p,m]=k(e.duration),[n,y]=k(t||"00:00:00");if(e.type===r.IntervalType.Interval){let i=o.clone().add(s,"hour").add(d,"minute");const a=i.clone().add(n,"hour").add(y,"minute").date()-o.date();a!==0&&(i=i.subtract(a,"day"));const f=i.clone().add(p,"hour").add(m,"minute");return{id:e.id,start:i.toISOString(),end:f.toISOString()}}if(e.type===r.IntervalType.Exception){const i=F(e.fromDate),u=F(e.toDate);if(o.isBetween(i,u,void 0,"[]")){const a=o.clone().add(s,"hour").add(d,"minute"),f=a.clone().add(p,"hour").add(m,"minute");return{id:e.id,start:a.toISOString(),end:f.toISOString(),reason:e.reason}}return null}throw new Error("Invalid interval type")}function S(e,o,t,s){const d={[r.DayOfWeek.Monday]:1,[r.DayOfWeek.Tuesday]:2,[r.DayOfWeek.Wednesday]:3,[r.DayOfWeek.Thursday]:4,[r.DayOfWeek.Friday]:5,[r.DayOfWeek.Saturday]:6,[r.DayOfWeek.Sunday]:7,[r.DayOfWeek.Everyday]:null};for(let p=0;p<o.length;p+=1){const m=o[p],n=m.isoWeekday(),y=e.dayOfWeek,u=(d[y]??null)===n||y===r.DayOfWeek.Everyday;if(t[n]||(t[n]={isoWeekday:n,key:"",open:[],adjusted:[],closed:[]}),u){const a=T(e,m,s);a&&(t[n].key+=a.id,e.type===r.IntervalType.Interval&&e.state===r.IntervalState.Open?t[n].open.push(a):e.type===r.IntervalType.Interval&&e.state===r.IntervalState.Closed?t[n].closed.push(a):e.type===r.IntervalType.Exception&&e.state===r.IntervalState.Open?t[n].adjusted.push(a):e.type===r.IntervalType.Exception&&e.state===r.IntervalState.Closed&&t[n].closed.push(a))}}}function h(e){switch(e.type){case r.IntervalType.Exception:return I("interval-exception");case r.IntervalType.Interval:return I("interval-normal");default:return I("interval-unknown")}}function D(e){const o=e.reduce((t,s)=>(c.forEach(d=>{g(d.id,s.orderFormats)&&(t[d.id]||(t[d.id]={orderFormat:d,intervals:[]}),t[d.id].intervals.push(...s.intervals.sort((p,m)=>p.dayOfWeek-m.dayOfWeek)))}),t),{});return Object.values(o)}function M(e,o){const t=o.reduce((s,d)=>d<=e?s+1:s,0);return e-t}function H(e){const{mergedTables:o}=e.reduce((t,s,d)=>{const p=t.mergedTables.findIndex(m=>m!==s&&!m.orderFormats.some(n=>s.orderFormats.some(y=>y.id===n.id))&&m.openingHours.every(n=>s.openingHours.some(y=>y.key===n.key))&&s.openingHours.every(n=>m.openingHours.some(y=>y.key===n.key)));if(p>=0){if(t.skipOrderFormats.some(u=>s.orderFormats.every(a=>a.id===u.id)))return t;const n=t.mergedTables[p];t.mergedTables.splice(p,1,{...n,orderFormats:[...s.orderFormats,...n.orderFormats]});const y=n.orderFormats.filter(u=>!t.skipOrderFormats.some(a=>a.id===u.id));y.length>0&&t.skipOrderFormats.push(...y);const i=M(d,[...t.removedIndexes]);return t.mergedTables.splice(i,1),t.removedIndexes.push(d),t}return t},{mergedTables:[...e],removedIndexes:[],skipOrderFormats:[]});return o}function x(e){return e.map(o=>({...o,intervals:o.intervals.map(t=>({...t,id:h(t)}))}))}function E(e,o){const t=D(x(e.serviceDateTimes)),s=v(o),d=W(s),p=t.reduce((i,u)=>(i[u.orderFormat.id]||(i[u.orderFormat.id]={}),u.intervals.reduce((a,f)=>(S(f,d,a,e.timeZoneOffset),a),i[u.orderFormat.id]),i),{}),m={[r.OrderFormat.EatIn]:c[0],[r.OrderFormat.TakeAway]:c[1],[r.OrderFormat.TableOrder]:c[2],[r.OrderFormat.Delivery]:c[3]},n=Object.entries(p).map(([i,u])=>({orderFormats:[m[Number(i)]],openingHours:Object.entries(u).map(([a,f])=>({isoWeekday:Number(a),key:f.key,open:f.open.sort((l,O)=>l.start.localeCompare(O.start)),adjusted:f.adjusted.sort((l,O)=>l.start.localeCompare(O.start)),closed:f.closed.sort((l,O)=>l.start.localeCompare(O.start))}))}));return H(n).sort((i,u)=>i.orderFormats[0].id-u.orderFormats[0].id)}module.exports=E;
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { OrderFormat as
|
|
1
|
+
import S from "moment";
|
|
2
|
+
import { OrderFormat as l, DayOfWeek as y, IntervalType as c, IntervalState as g } from "@ancon/wildcat-types";
|
|
3
3
|
import h from "../shared/isWithinFlag.mjs";
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
{ id:
|
|
7
|
-
{ id:
|
|
8
|
-
{ id:
|
|
9
|
-
{ id:
|
|
4
|
+
import F from "../shared/generateId.mjs";
|
|
5
|
+
const I = [
|
|
6
|
+
{ id: l.EatIn, name: "eatIn" },
|
|
7
|
+
{ id: l.TakeAway, name: "takeaway" },
|
|
8
|
+
{ id: l.TableOrder, name: "tableOrder" },
|
|
9
|
+
{ id: l.Delivery, name: "delivery" }
|
|
10
10
|
];
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
for (let
|
|
14
|
-
|
|
11
|
+
function v(e) {
|
|
12
|
+
const o = S.utc(e).subtract(1, "day").startOf("day"), t = [];
|
|
13
|
+
for (let n = 0, s = o; n < 7; n += 1)
|
|
14
|
+
s = s.clone().add(1, "day").startOf("day"), t.push(s.clone());
|
|
15
15
|
return t;
|
|
16
16
|
}
|
|
17
|
-
function
|
|
18
|
-
return [...e].sort((
|
|
17
|
+
function T(e) {
|
|
18
|
+
return [...e].sort((o, t) => o.isoWeekday() - t.isoWeekday());
|
|
19
19
|
}
|
|
20
|
-
function
|
|
20
|
+
function W(e) {
|
|
21
21
|
return e.split(":").map(Number);
|
|
22
22
|
}
|
|
23
|
-
function
|
|
24
|
-
const [
|
|
23
|
+
function M(e, o, t) {
|
|
24
|
+
const [n, s] = W(e.start), [m, u] = W(
|
|
25
25
|
e.duration
|
|
26
|
-
), [r,
|
|
26
|
+
), [r, p] = W(t || "00:00:00");
|
|
27
27
|
if (e.type === c.Interval) {
|
|
28
|
-
let
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
const
|
|
28
|
+
let a = o.clone().add(n, "hour").add(s, "minute");
|
|
29
|
+
const d = a.clone().add(r, "hour").add(p, "minute").date() - o.date();
|
|
30
|
+
d !== 0 && (a = a.subtract(d, "day"));
|
|
31
|
+
const f = a.clone().add(m, "hour").add(u, "minute");
|
|
32
32
|
return {
|
|
33
33
|
id: e.id,
|
|
34
|
-
start:
|
|
35
|
-
end:
|
|
34
|
+
start: a.toISOString(),
|
|
35
|
+
end: f.toISOString()
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
if (e.type === c.Exception) {
|
|
39
|
-
const
|
|
40
|
-
if (
|
|
41
|
-
const
|
|
39
|
+
const a = S(e.fromDate), i = S(e.toDate);
|
|
40
|
+
if (o.isBetween(a, i, void 0, "[]")) {
|
|
41
|
+
const d = o.clone().add(n, "hour").add(s, "minute"), f = d.clone().add(m, "hour").add(u, "minute");
|
|
42
42
|
return {
|
|
43
43
|
id: e.id,
|
|
44
|
-
start:
|
|
45
|
-
end:
|
|
44
|
+
start: d.toISOString(),
|
|
45
|
+
end: f.toISOString(),
|
|
46
46
|
reason: e.reason
|
|
47
47
|
};
|
|
48
48
|
}
|
|
@@ -50,8 +50,8 @@ function H(e, n, t) {
|
|
|
50
50
|
}
|
|
51
51
|
throw new Error("Invalid interval type");
|
|
52
52
|
}
|
|
53
|
-
function
|
|
54
|
-
const
|
|
53
|
+
function H(e, o, t, n) {
|
|
54
|
+
const s = {
|
|
55
55
|
[y.Monday]: 1,
|
|
56
56
|
[y.Tuesday]: 2,
|
|
57
57
|
[y.Wednesday]: 3,
|
|
@@ -61,123 +61,138 @@ function E(e, n, t, u) {
|
|
|
61
61
|
[y.Sunday]: 7,
|
|
62
62
|
[y.Everyday]: null
|
|
63
63
|
};
|
|
64
|
-
for (let
|
|
65
|
-
const
|
|
64
|
+
for (let m = 0; m < o.length; m += 1) {
|
|
65
|
+
const u = o[m], r = u.isoWeekday(), p = e.dayOfWeek, i = (s[p] ?? null) === r || p === y.Everyday;
|
|
66
66
|
if (t[r] || (t[r] = {
|
|
67
67
|
isoWeekday: r,
|
|
68
68
|
key: "",
|
|
69
69
|
open: [],
|
|
70
70
|
adjusted: [],
|
|
71
71
|
closed: []
|
|
72
|
-
}),
|
|
73
|
-
const
|
|
72
|
+
}), i) {
|
|
73
|
+
const d = M(
|
|
74
74
|
e,
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
u,
|
|
76
|
+
n
|
|
77
77
|
);
|
|
78
|
-
|
|
78
|
+
d && (t[r].key += d.id, e.type === c.Interval && e.state === g.Open ? t[r].open.push(d) : e.type === c.Interval && e.state === g.Closed ? t[r].closed.push(d) : e.type === c.Exception && e.state === g.Open ? t[r].adjusted.push(d) : e.type === c.Exception && e.state === g.Closed && t[r].closed.push(d));
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
|
-
function
|
|
82
|
+
function x(e) {
|
|
83
83
|
switch (e.type) {
|
|
84
84
|
case c.Exception:
|
|
85
|
-
return
|
|
85
|
+
return F("interval-exception");
|
|
86
86
|
case c.Interval:
|
|
87
|
-
return
|
|
87
|
+
return F("interval-normal");
|
|
88
88
|
default:
|
|
89
|
-
return
|
|
89
|
+
return F("interval-unknown");
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
function
|
|
93
|
-
const
|
|
94
|
-
(t,
|
|
95
|
-
h(
|
|
96
|
-
orderFormat:
|
|
92
|
+
function E(e) {
|
|
93
|
+
const o = e.reduce(
|
|
94
|
+
(t, n) => (I.forEach((s) => {
|
|
95
|
+
h(s.id, n.orderFormats) && (t[s.id] || (t[s.id] = {
|
|
96
|
+
orderFormat: s,
|
|
97
97
|
intervals: []
|
|
98
|
-
}), t[
|
|
99
|
-
...
|
|
100
|
-
(
|
|
98
|
+
}), t[s.id].intervals.push(
|
|
99
|
+
...n.intervals.sort(
|
|
100
|
+
(m, u) => m.dayOfWeek - u.dayOfWeek
|
|
101
101
|
)
|
|
102
102
|
));
|
|
103
103
|
}), t),
|
|
104
104
|
{}
|
|
105
105
|
);
|
|
106
|
-
return Object.values(
|
|
106
|
+
return Object.values(o);
|
|
107
|
+
}
|
|
108
|
+
function D(e, o) {
|
|
109
|
+
const t = o.reduce((n, s) => s <= e ? n + 1 : n, 0);
|
|
110
|
+
return e - t;
|
|
107
111
|
}
|
|
108
|
-
function
|
|
109
|
-
|
|
110
|
-
(
|
|
111
|
-
const
|
|
112
|
-
(
|
|
113
|
-
(
|
|
114
|
-
(
|
|
112
|
+
function b(e) {
|
|
113
|
+
const { mergedTables: o } = e.reduce(
|
|
114
|
+
(t, n, s) => {
|
|
115
|
+
const m = t.mergedTables.findIndex(
|
|
116
|
+
(u) => u !== n && !u.orderFormats.some(
|
|
117
|
+
(r) => n.orderFormats.some(
|
|
118
|
+
(p) => p.id === r.id
|
|
115
119
|
)
|
|
116
|
-
) &&
|
|
117
|
-
(
|
|
118
|
-
) &&
|
|
119
|
-
(
|
|
120
|
+
) && u.openingHours.every(
|
|
121
|
+
(r) => n.openingHours.some((p) => p.key === r.key)
|
|
122
|
+
) && n.openingHours.every(
|
|
123
|
+
(r) => u.openingHours.some((p) => p.key === r.key)
|
|
120
124
|
)
|
|
121
125
|
);
|
|
122
|
-
if (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
126
|
+
if (m >= 0) {
|
|
127
|
+
if (t.skipOrderFormats.some(
|
|
128
|
+
(i) => n.orderFormats.every((d) => d.id === i.id)
|
|
129
|
+
))
|
|
130
|
+
return t;
|
|
131
|
+
const r = t.mergedTables[m];
|
|
132
|
+
t.mergedTables.splice(m, 1, {
|
|
133
|
+
...r,
|
|
134
|
+
orderFormats: [...n.orderFormats, ...r.orderFormats]
|
|
135
|
+
});
|
|
136
|
+
const p = r.orderFormats.filter(
|
|
137
|
+
(i) => !t.skipOrderFormats.some((d) => d.id === i.id)
|
|
138
|
+
);
|
|
139
|
+
p.length > 0 && t.skipOrderFormats.push(...p);
|
|
140
|
+
const a = D(s, [...t.removedIndexes]);
|
|
141
|
+
return t.mergedTables.splice(a, 1), t.removedIndexes.push(s), t;
|
|
128
142
|
}
|
|
129
|
-
return
|
|
143
|
+
return t;
|
|
130
144
|
},
|
|
131
|
-
[...e]
|
|
145
|
+
{ mergedTables: [...e], removedIndexes: [], skipOrderFormats: [] }
|
|
132
146
|
);
|
|
147
|
+
return o;
|
|
133
148
|
}
|
|
134
149
|
function j(e) {
|
|
135
|
-
return e.map((
|
|
136
|
-
...
|
|
137
|
-
intervals:
|
|
150
|
+
return e.map((o) => ({
|
|
151
|
+
...o,
|
|
152
|
+
intervals: o.intervals.map((t) => ({
|
|
138
153
|
...t,
|
|
139
|
-
id:
|
|
154
|
+
id: x(t)
|
|
140
155
|
}))
|
|
141
156
|
}));
|
|
142
157
|
}
|
|
143
|
-
function R(e,
|
|
144
|
-
const t =
|
|
158
|
+
function R(e, o) {
|
|
159
|
+
const t = E(
|
|
145
160
|
j(e.serviceDateTimes)
|
|
146
|
-
),
|
|
147
|
-
(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
161
|
+
), n = v(o), s = T(n), m = t.reduce(
|
|
162
|
+
(a, i) => (a[i.orderFormat.id] || (a[i.orderFormat.id] = {}), i.intervals.reduce((d, f) => (H(
|
|
163
|
+
f,
|
|
164
|
+
s,
|
|
165
|
+
d,
|
|
151
166
|
e.timeZoneOffset
|
|
152
|
-
),
|
|
167
|
+
), d), a[i.orderFormat.id]), a),
|
|
153
168
|
{}
|
|
154
|
-
),
|
|
155
|
-
[
|
|
156
|
-
[
|
|
157
|
-
[
|
|
158
|
-
[
|
|
169
|
+
), u = {
|
|
170
|
+
[l.EatIn]: I[0],
|
|
171
|
+
[l.TakeAway]: I[1],
|
|
172
|
+
[l.TableOrder]: I[2],
|
|
173
|
+
[l.Delivery]: I[3]
|
|
159
174
|
}, r = Object.entries(
|
|
160
|
-
|
|
161
|
-
).map(([
|
|
162
|
-
orderFormats: [
|
|
163
|
-
openingHours: Object.entries(
|
|
164
|
-
([
|
|
165
|
-
isoWeekday: Number(
|
|
166
|
-
key:
|
|
167
|
-
open:
|
|
168
|
-
(
|
|
175
|
+
m
|
|
176
|
+
).map(([a, i]) => ({
|
|
177
|
+
orderFormats: [u[Number(a)]],
|
|
178
|
+
openingHours: Object.entries(i).map(
|
|
179
|
+
([d, f]) => ({
|
|
180
|
+
isoWeekday: Number(d),
|
|
181
|
+
key: f.key,
|
|
182
|
+
open: f.open.sort(
|
|
183
|
+
(O, k) => O.start.localeCompare(k.start)
|
|
169
184
|
),
|
|
170
|
-
adjusted:
|
|
171
|
-
(
|
|
185
|
+
adjusted: f.adjusted.sort(
|
|
186
|
+
(O, k) => O.start.localeCompare(k.start)
|
|
172
187
|
),
|
|
173
|
-
closed:
|
|
174
|
-
(
|
|
188
|
+
closed: f.closed.sort(
|
|
189
|
+
(O, k) => O.start.localeCompare(k.start)
|
|
175
190
|
)
|
|
176
191
|
})
|
|
177
192
|
)
|
|
178
193
|
}));
|
|
179
|
-
return
|
|
180
|
-
(
|
|
194
|
+
return b(r).sort(
|
|
195
|
+
(a, i) => a.orderFormats[0].id - i.orderFormats[0].id
|
|
181
196
|
);
|
|
182
197
|
}
|
|
183
198
|
export {
|