-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUltimate Booking Scheduler
More file actions
executable file
·270 lines (210 loc) · 13.1 KB
/
Ultimate Booking Scheduler
File metadata and controls
executable file
·270 lines (210 loc) · 13.1 KB
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
/*
Script: Ultimate Booking Scheduler
Authors: Kamille Parks, Jeremy Oglesby
This script combines Jeremy Oglesby's "Check for Speaker Schedule Conflicts" script and
Kamille Parks's "No-Conflict Asset Reservations" script to create the ultimate scheduling
aid for event planners!
*/
// Begin BASE SPECIFIC NAMES Section (adjust these values to fit your base!)
const BaseSpecificNames = {
// Reservations Table
reservationsTable: "Reservations", // name of the [RESERVATIONS] table
assetField: "Room", // name of the link-type field connecting to the [ASSETS] table
startField: "Start Date",
endField: "End Date",
personField: "Customer", // name of the link-type field connection to the [PEOPLE] table
// Assets Table
assetsTable: "Rooms", // name of the [ASSETS] table
assetName: "Room #", // name of the primary field in the [ASSETS] table
reservationsField: "Reservations",
// People Table
peopleTable: "People", // name of the [PEOPLE] table
peopleName: "Name" // name of the primary field in the [PEOPLE] table
}
// End BASE SPECIFIC NAMES Section (everything below should work without the need for further adjustment.)
// Begin VARIABLE DECLARATIONS Section
const peopleTable = base.getTable(BaseSpecificNames.peopleTable);
const reservationsTable = base.getTable(BaseSpecificNames.reservationsTable);
const reservationsQuery = await reservationsTable.selectRecordsAsync();
const allReservations = reservationsQuery.records;
const assetsTable = base.getTable(BaseSpecificNames.assetsTable);
const assetsQuery = await assetsTable.selectRecordsAsync({sorts: [{field: BaseSpecificNames.assetName}]});
const allAssets = assetsQuery.records;
let conflicts, unavailableAssets, availableAssets;
let overlaps = [];
let altered = [];
let unaltered = [];
// End VARIABLE DECLARATIONS Section
// Begin FUNCTIONS Section
async function findConflictingReservations(startDate, endDate) {
conflicts = [];
for (let reservation of allReservations) {
let compareStart = new Date(reservation.getCellValue(BaseSpecificNames.startField)).toISOString();
let compareEnd = new Date(reservation.getCellValue(BaseSpecificNames.endField)).toISOString();
if ((compareStart >= startDate && compareStart <= endDate) || startDate >= compareStart && startDate <= compareEnd || (compareStart <= startDate && compareEnd >= endDate)) {
conflicts.push(reservation.id);
};
}
}
async function setUnavailableAssets() {
unavailableAssets = [];
for (var i = 0; i < conflicts.length; i++) {
let reservation = reservationsQuery.getRecord(conflicts[i]);
let asset = reservation.getCellValueAsString(BaseSpecificNames.assetField);
unavailableAssets.push(asset);
};
}
async function setAvailableAssets() {
availableAssets = [];
availableAssets = allAssets.filter(record => {
let assetName = record.getCellValue(BaseSpecificNames.assetName);
return assetName !== null && ! unavailableAssets.includes(assetName);
});
}
// End FUNCTIONS Section
output.markdown(`# Ultimate Booking Scheduler`)
let mode = await input.buttonsAsync('What would you like to do?',[
{label: 'Book a New Reservation', value: 'book', variant: 'primary'},
{label: 'Resolve Scheduling Conflicts', value: 'resolve', variant: 'danger'}
]);
output.markdown('---');
if (mode == 'book') {
output.markdown(`## Schedule a New ${BaseSpecificNames.assetField} Reservation`);
let person = await input.recordAsync("Reserve a " + BaseSpecificNames.assetField + " For:", peopleTable, {shouldAllowCreatingRecord: true});
let startInput = await input.textAsync("Start Date (YYYY-MM-DD):");
let startDate = new Date(startInput).toISOString();
let endInput = await input.textAsync("End Date (YYYY-MM-DD):");
let endDate = new Date(endInput).toISOString();
findConflictingReservations(startDate, endDate);
setUnavailableAssets();
setAvailableAssets();
if (availableAssets.length > 0) {
let selectedAsset = await input.recordAsync("Requested " + BaseSpecificNames.assetField + ":", availableAssets);
if (selectedAsset) {
output.markdown(`You are about to reserve **${selectedAsset.name}** for **${person.name}** from **${startInput}** to **${endInput}**.`);
let confirmed = await input.buttonsAsync('',[{label: 'Confirm Reservation', value: 'true', variant: 'primary'}]);
if (confirmed) {
await reservationsTable.createRecordAsync({
[BaseSpecificNames.assetField]: [{id: selectedAsset.id}],
[BaseSpecificNames.personField]: [{id: person.id}],
[BaseSpecificNames.startField]: startDate,
[BaseSpecificNames.endField]: endDate
})
output.markdown(`*Your reservation was booked successfully. Please run the script again to book another reservation or to check for scheduling conflicts.*`)
}
} else {
output.markdown(`#### No ${BaseSpecificNames.assetField} was selected. Please run the script again and select a ${BaseSpecificNames.assetField}.`)
}
}
else {
output.markdown(`#### Unfortunately, there are no available ${BaseSpecificNames.assetsTable} for this date range. Please run the script again and select new dates.`)
}
} else {
output.markdown(`## Check for Conflicting ${BaseSpecificNames.assetField} ${BaseSpecificNames.reservationsTable}`);
let reservationsChecked = [];
for (let asset of allAssets) {
let conflictingRecords = new Set();
let conflictingReservationsNames = new Set();
let linkedReservations = allReservations.filter(record => {
let assetFieldValues = record.getCellValueAsString(BaseSpecificNames.assetField);
return assetFieldValues !== null && assetFieldValues.includes(asset.name);
});
for (let reservation of linkedReservations) {
let startDate = new Date(reservation.getCellValue(BaseSpecificNames.startField)).toISOString();
let endDate= new Date(reservation.getCellValue(BaseSpecificNames.endField)).toISOString();
reservationsChecked.push(reservation.id);
for (let currentReservation of linkedReservations) {
if (!reservationsChecked.includes(currentReservation.id)) {
let compareStart = new Date(currentReservation.getCellValue(BaseSpecificNames.startField)).toISOString();
let compareEnd = new Date(currentReservation.getCellValue(BaseSpecificNames.endField)).toISOString();
if ((startDate >= compareStart && startDate <= compareEnd) || compareStart >= startDate && compareStart <= endDate || (startDate <= compareStart && endDate >= compareEnd)) {
conflictingRecords.add(reservation);
conflictingRecords.add(currentReservation);
conflictingReservationsNames.add(reservation.name);
conflictingReservationsNames.add(currentReservation.name);
}
}
}
}
if (conflictingRecords.size > 0) {
overlaps.push({Asset: asset, ConflictingRecords: conflictingRecords, ConflictingRecordsNames: conflictingReservationsNames});
}
}
if (overlaps.length > 0) {
output.markdown(`#### The following (${overlaps.length}) ${BaseSpecificNames.assetsTable} have scheduling conflicts:`);
output.table(overlaps.map( conflict => ({
[BaseSpecificNames.assetField]: conflict.Asset.getCellValue(BaseSpecificNames.assetName),
['Conflicting ' + BaseSpecificNames.reservationsTable]: [...conflict.ConflictingRecordsNames].sort().join(', '),
'Count': conflict.ConflictingRecords.size
})))
output.markdown(`*You will now be asked to resolve each of the scheduling conflicts shown above, one at a time. Conflicts will be grouped by ${BaseSpecificNames.assetField}. For each conflict you will be given the choice to change the ${BaseSpecificNames.assetField}, change the dates, or skip to the next conflict.*`);
let beginResolve = await input.buttonsAsync('',[{label: 'Begin', variant: 'primary'}])
for (let overlap of overlaps) {
output.clear();
output.markdown(`#### Conflicting ${BaseSpecificNames.assetField} ${BaseSpecificNames.reservationsTable} for ${overlap.Asset.name}`)
let conflictingRecords = overlap.ConflictingRecords;
output.table(Array.from(conflictingRecords).map(record => ({
Name: record.name,
Start: record.getCellValueAsString(BaseSpecificNames.startField),
End: record.getCellValueAsString(BaseSpecificNames.endField),
[BaseSpecificNames.personField]: record.getCellValueAsString(BaseSpecificNames.personField)
})));
output.markdown('---');
let index = 0;
for (let currentRecord of conflictingRecords) {
index ++;
output.markdown(`**${index}. ${currentRecord.name}**`)
let method = await input.buttonsAsync('What would you like to do to this record?',[
{label: 'Change the ' + BaseSpecificNames.assetField, value: 'reassign', variant: 'primary'},
{label: 'Change the Dates', value: 'reschedule', variant: 'primary'},
{label: 'Skip', value: 'skip', variant: 'default'}
])
if (method == 'reassign') {
let startDate = new Date(currentRecord.getCellValue(BaseSpecificNames.startField)).toISOString();
let endDate = new Date(currentRecord.getCellValue(BaseSpecificNames.endField)).toISOString();
findConflictingReservations(startDate, endDate)
setUnavailableAssets();
setAvailableAssets();
if (availableAssets.length > 0) {
let selectedAsset = await input.recordAsync("New " + BaseSpecificNames.assetField + ":", availableAssets);
if (selectedAsset) {
await reservationsTable.updateRecordAsync(currentRecord.id, {[BaseSpecificNames.assetField]: [{id: selectedAsset.id}]});
output.markdown(`**Success!** *New Details: ${selectedAsset.name} from ${startDate} to ${endDate}*`);
altered.push(currentRecord);
} else {
output.markdown(`**WARNING:** *No changes were made to this record because no ${BaseSpecificNames.assetField} was selected.*`);
unaltered.push(currentRecord);
}
} else {
output.markdown(`*Unfortunately, there are no available ${BaseSpecificNames.assetsTable} for this date range. Please run the script again and select new dates.*`);
unaltered.push(currentRecord);
}
} else if (method == 'reschedule') {
let newStart = await input.textAsync("New Start Date (YYYY-MM-DD):");
let startDate = new Date(newStart).toISOString();
let newEnd = await input.textAsync("New End Date (YYYY-MM-DD):");
let endDate = new Date(newEnd).toISOString();
await reservationsTable.updateRecordAsync(currentRecord.id, {[BaseSpecificNames.startField]: startDate,[BaseSpecificNames.endField]: endDate});
output.markdown(`**Success!** *New Details: ${currentRecord.getCellValueAsString(BaseSpecificNames.assetField)} from ${startDate} to ${endDate}*`);
altered.push(currentRecord);
} else if (method == 'skip') {
output.markdown(`*No changes were made for ${currentRecord.name}.*`);
unaltered.push(currentRecord);
}
output.markdown('---');
}
output.markdown(`**End of shceduling conflicts for ${overlap.Asset.name}**`);
let next = await input.buttonsAsync('',['Continue']);
}
output.clear();
output.markdown(`#### Done! All conflicting ${BaseSpecificNames.assetField} ${BaseSpecificNames.reservationsTable} have been dealt with.`);
output.markdown(`##### Altered ${BaseSpecificNames.assetField} ${BaseSpecificNames.reservationsTable}: ${altered.length}`)
output.table(altered);
output.markdown(`##### Unaltered ${BaseSpecificNames.assetField} ${BaseSpecificNames.reservationsTable}: ${unaltered.length}`)
output.table(unaltered);
output.markdown(`*Please run the script again to book a new reservation or to re-check for scheduling conflicts.*`);
} else {
output.markdown(`#### Good News! There are no conflicting ${BaseSpecificNames.assetField} ${BaseSpecificNames.reservationsTable}.`);
output.markdown(`*Please run the script again to book a new reservation.*`);
}
}