server/lib/conflicts.js Repository wynajem_motorowek Original path server/lib/conflicts.jsRole SOURCE Size 1151 bytes Lines 40 SHA-256 1030fd2fc70c322fd4cfbe82baab7b1faf54ff36c42bcb000b3deeb87cc3bd05Displayed range 1–40 Previous file/page · Project index · Next file/page
const { DAY_START_MIN, DAY_END_MIN, minutesToTimeText } = require("./time");
function hasOverlap(startMs, endMs, existingBlocks) {
return existingBlocks.some((block) => startMs < block.endMs && endMs > block.startMs);
}
function findSuggestion({ startMin, endMin, blocksInDay }) {
const duration = endMin - startMin;
const normalized = blocksInDay
.map((item) => ({ startMin: item.startMin, endMin: item.endMin }))
.filter((item) => item.endMin > DAY_START_MIN && item.startMin < DAY_END_MIN)
.sort((a, b) => a.startMin - b.startMin);
let candidate = startMin;
for (const item of normalized) {
if (candidate + duration <= item.startMin) {
return {
start: minutesToTimeText(candidate),
end: minutesToTimeText(candidate + duration),
};
}
if (candidate < item.endMin) {
candidate = item.endMin;
}
}
if (candidate + duration <= DAY_END_MIN) {
return {
start: minutesToTimeText(candidate),
end: minutesToTimeText(candidate + duration),
};
}
return null;
}
module.exports = {
hasOverlap,
findSuggestion,
};