-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailable-feed.js
277 lines (265 loc) · 8.73 KB
/
available-feed.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const {
input,
div,
text,
script,
domReady,
style,
button,
h3,
ul,
li,
form,
a,
b,
} = require("@saltcorn/markup/tags");
const View = require("@saltcorn/data/models/view");
const Workflow = require("@saltcorn/data/models/workflow");
const Table = require("@saltcorn/data/models/table");
const Form = require("@saltcorn/data/models/form");
const Field = require("@saltcorn/data/models/field");
const {
InvalidConfiguration,
isNode,
isWeb,
mergeConnectedObjects,
hashState,
} = require("@saltcorn/data/utils");
const {
link_view,
stateToQueryString,
stateFieldsToWhere,
stateFieldsToQuery,
readState,
} = require("@saltcorn/data/plugin-helper");
const get_state_fields = async (table_id, viewname, { show_view }) => {
const table = Table.findOne(table_id);
const table_fields = table.fields;
return table_fields
.filter((f) => !f.primary_key)
.map((f) => {
const sf = new Field(f);
sf.required = false;
return sf;
});
};
const configuration_workflow = (req) =>
new Workflow({
steps: [
{
name: req.__("Views"),
form: async (context) => {
const table = Table.findOne(context.table_id);
const fields = table.fields;
const reservable_entity_fields = fields.filter((f) => f.is_fkey);
const show_view_opts = {};
const slots_available_field = {};
const distinct_slot_fields = new Set();
for (const rfield of reservable_entity_fields) {
const retable = Table.findOne(rfield.reftable_name);
const show_views = await View.find_table_views_where(
retable.id,
({ state_fields, viewtemplate, viewrow }) =>
viewtemplate.runMany &&
viewrow.name !== context.viewname &&
state_fields.some((sf) => sf.name === "id")
);
show_view_opts[rfield.name] = show_views.map((v) => v.name);
slots_available_field[rfield.name] = retable.fields
.filter((f) => f.type?.name === "Integer")
.map((f) => f.name);
slots_available_field[rfield.name].forEach((v) =>
distinct_slot_fields.add(v)
);
slots_available_field[rfield.name].unshift("");
}
return new Form({
fields: [
{
name: "reservable_entity_key",
label: "Key to reservable entity",
type: "String",
required: true,
attributes: {
options: reservable_entity_fields.map((f) => f.name),
},
},
{
name: "valid_field",
label: "Valid reservation field",
sublabel: "Only consider reservations with this field valid",
type: "String",
attributes: {
options: fields
.filter((f) => f.type.name === "Bool")
.map((f) => f.name),
},
},
{
name: "start_field",
label: "Start date field",
type: "String",
required: true,
attributes: {
options: fields
.filter((f) => f.type.name === "Date")
.map((f) => f.name),
},
},
{
name: "end_field",
label: "End date field",
type: "String",
required: true,
attributes: {
options: fields
.filter((f) => f.type.name === "Date")
.map((f) => f.name),
},
},
/*{
name: "duration_field",
label: "Duration field",
sublabel: "Integer field holding booked duration in minutes",
type: "String",
attributes: {
options: fields
.filter((f) => f.type.name === "Integer")
.map((f) => f.name),
},
}, */
{
name: "show_view",
label: req.__("Single item view"),
type: "String",
sublabel:
req.__("The underlying individual view of each table row") +
". " +
a(
{
"data-dyn-href": `\`/viewedit/config/\${show_view}\``,
target: "_blank",
},
req.__("Configure")
),
required: true,
attributes: {
calcOptions: ["reservable_entity_key", show_view_opts],
},
},
{
name: "slots_available_field",
label: "Slots available field",
sublabel:
"Field with a number of available instances of the reservable entity. If blank, treat as one per entity.",
type: "String",
attributes: {
calcOptions: ["reservable_entity_key", slots_available_field],
},
},
{
name: "slot_count_field",
label: "Slots taken field",
sublabel:
"Field with the number of entities reserved. If blank, treat as one per entity.",
type: "String",
showIf: { slots_available_field: [...distinct_slot_fields] },
attributes: {
options: fields
.filter((f) => f.type.name === "Integer")
.map((f) => f.name),
},
},
],
});
},
},
],
});
const first = (xs) => (Array.isArray(xs) ? xs[0] : xs);
const run = async (
table_id,
viewname,
{
reservable_entity_key,
valid_field,
slot_count_field,
slots_available_field,
show_view,
start_field,
end_field,
},
state,
extraArgs
) => {
const restable = Table.findOne({ id: table_id });
const resfields = restable.getFields();
const refield = restable.getField(reservable_entity_key);
const retable = Table.findOne(refield.reftable_name);
const state_res = { ...state };
readState(state_res, restable.fields);
//get reservations
const reswhere = await stateFieldsToWhere({
fields: resfields,
state: state_res,
table: restable,
});
if (valid_field) reswhere[valid_field] = true;
const reservations = await restable.getRows(reswhere);
const use_slots = slot_count_field || slots_available_field;
const sview = await View.findOne({ name: show_view });
if (!sview)
throw new InvalidConfiguration(
`View ${viewname} incorrectly configured: cannot find view ${show_view}`
);
const srespAll = await sview.runMany(state, extraArgs);
const srespsAvailable = [];
if (!use_slots) {
const resEnts = new Set(reservations.map((r) => r[reservable_entity_key]));
for (const sresp of srespAll) {
if (!resEnts.has(sresp.row[retable.pk_name])) srespsAvailable.push(sresp);
}
} else {
//console.log("state_res", state_res);
//console.log("reswhere", reswhere);
const to = new Date(first(reswhere[start_field])?.lt);
const from = new Date(first(reswhere[end_field])?.gt);
if (!from || !to) srespsAvailable.push(...srespAll);
else
for (const sresp of srespAll) {
const myreservations = reservations.filter(
(r) => r[reservable_entity_key] === sresp.row[retable.pk_name]
);
/*console.log({
taken: resEnts[sresp.row[retable.pk_name]] || 0,
available: sresp.row[slots_available_field],
});*/
const to = new Date(first(reswhere[start_field])?.lt);
const from = new Date(first(reswhere[end_field])?.gt);
let maxAvailable = sresp.row[slots_available_field];
for (let day = from; day <= to; day.setDate(day.getDate() + 1)) {
const active = myreservations.filter(
(r) => r[start_field] <= day && r[end_field] >= day
);
const taken = active
.map((r) => r[slot_count_field] || 1)
.reduce((a, b) => a + b, 0);
maxAvailable = Math.min(
maxAvailable,
sresp.row[slots_available_field] - taken
);
//console.log({ car: sresp.row.name, day, maxAvailable });
}
if (maxAvailable > 0) srespsAvailable.push(sresp);
}
}
const showRow = (r) => r.html;
return div(srespsAvailable.map(showRow));
};
module.exports = {
name: "Available Resources Feed",
display_state_form: false,
get_state_fields,
configuration_workflow,
run,
};