findSlots function
The bookable slots starting at or after from and ending at or before
to: the openings of the provider's week and exceptions, cut into slots
of duration, minus what is busy (with buffers), too soon
(minNotice), too far (maxAdvanceDays) or on a full date
(maxPerDay). Bad input never throws: it is in the problems.
Implementation
SlotsResult findSlots(SlotRules rules, {required Instant from, required Instant to}) {
final problems = <Problem>[];
try {
final p = _plan(rules, from, to, null, problems);
if (p == null) return SlotsResult(const [], problems);
final slots = <Slot>[];
final cursor = [0];
var seen = 0;
for (final t in _candidates(p)) {
if (t + p.dur > p.to) break;
if (++seen > maxCandidates || slots.length >= maxSlots) {
problems.add(
const Problem(
ProblemCode.tooManyOccurrences,
Severity.warning,
'step',
'more than $maxSlots slots or $maxCandidates starts: narrow the range',
),
);
break;
}
final slot = _slotAt(p, t);
if (_refusal(p, t, slot.start, cursor) == null) slots.add(slot);
}
return SlotsResult(List.unmodifiable(slots), problems);
} catch (e) {
// Last resort: a value no check foresaw is still an answer, not a throw.
problems.add(Problem(ProblemCode.invalidSlotQuery, Severity.error, 'query', '$e'));
return SlotsResult(const [], problems);
}
}