-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
choreRefactoring, cleanup, tasks which wouldn't really have any effects on program behaviorRefactoring, cleanup, tasks which wouldn't really have any effects on program behavior
Description
Conversion provided by Co-Pilot
File path: safely-api/api/availability/index.js
// Converted to a coroutine so callers can keep using promise-style chaining
// while we use `yield` internally for clarity. We preserve parallelism by
// using `Promise.map` with a coroutine mapping function.
let filterBlocks = Promise.coroutine(function* (day, blocks) {
const mapped = yield Promise.map(blocks, Promise.coroutine(function* (block) {
let s = moment(day).hour(block[0]).startOf('hour').toDate();
if (!showBackdate && moment().isAfter(s)) {
return null;
}
if (find(totalClassBlackouts, blackout => day.hour(block[0]).isBetween(blackout.start, blackout.end, null, '[]'))) {
return null;
}
let end = moment(s).add(1, 'hour').endOf('hour');
const seats = yield Seat.countAvailableSeats(s, end);
const result = yield findClassBlackout(s, block);
let _block = block.concat([{ seats }]);
if (result.exceptions.length > 0) {
const reduction = find(result.classBlackouts, b => !!b.seats);
if (reduction) {
const registrations = yield Registration.count({
$or: [{
cancelledOn: null
}, {
cancelledOn: { $exists: false }
}],
'times.start': { $lte: s },
'times.end': { $gte: end },
classes: { $in: map(reduction.classExceptions, '_id') }
});
Object.assign(_block[_block.length - 1], {
registrations,
onlyClasses: result.exceptions,
reduceSeats: reduction.seats - registrations
});
return _block;
} else {
Object.assign(_block[_block.length - 1], {
onlyClasses: result.exceptions
});
}
} else if (result.explicit.length > 0) {
const reduction = find(result.classBlackouts, b => !!b.seats);
if (reduction) {
const registrations = yield Registration.count({
$or: [{
cancelledOn: null
}, {
cancelledOn: { $exists: false }
}],
'times.start': { $lte: s },
'times.end': { $gte: end },
classes: { $in: map(reduction.classExplicit, '_id') }
});
Object.assign(_block[_block.length - 1], {
registrations,
blockOutExplicit: result.explicit,
reduceSeats: reduction.seats - registrations
});
return _block;
} else {
Object.assign(_block[_block.length - 1], {
blockOutExplicit: result.explicit
});
}
}
return _block;
}));
return mapped.filter(b => b !== null);
});Pasting of code snippet above ends at:
const body = {
availability: yield range(getWeekNums(startFrom) + 1).map(w => {
...Covered by these tests:
safely-api/api/availability/test.js
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
choreRefactoring, cleanup, tasks which wouldn't really have any effects on program behaviorRefactoring, cleanup, tasks which wouldn't really have any effects on program behavior