Skip to content

[Chore] Convert thenable to yield #161

@Evilscaught

Description

@Evilscaught

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    choreRefactoring, cleanup, tasks which wouldn't really have any effects on program behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions