在Sequelize中计算有条件的活动注册

时间:2018-03-04 09:58:18

标签: javascript mysql express orm sequelize.js

我正在尝试计算有效的注册人数,并且我能够获得该值。

我需要添加一个条件到我的计数。我怎样才能做到这一点? ie)只计算to_date is >= today

的注册人数

这是我的架构:

mysql> desc coaching_class_entries;
+-------------------+------------------+------+-----+-------------------+-----------------------------+
| Field             | Type             | Null | Key | Default           | Extra                       |
+-------------------+------------------+------+-----+-------------------+-----------------------------+
| id                | int(10) unsigned | NO   | PRI | NULL              | auto_increment              |
| user_id           | int(10) unsigned | NO   | MUL | NULL              |                             |
| coaching_class_id | int(10) unsigned | NO   | MUL | NULL              |                             |
| fees              | double           | NO   |     | NULL              |                             |
| from_date         | date             | NO   |     | NULL              |                             |
| to_date           | date             | YES  |     | NULL              |                             |
| tax_group_id      | int(10) unsigned | YES  | MUL | NULL              |                             |
| last_bill_date    | date             | YES  |     | NULL              |                             |
| next_bill_date    | date             | YES  |     | NULL              |                             |
| comments          | varchar(100)     | YES  |     | NULL              |                             |
| created_at        | datetime         | NO   |     | CURRENT_TIMESTAMP |                             |
| updated_at        | datetime         | YES  |     | NULL              | on update CURRENT_TIMESTAMP |
| deleted_at        | datetime         | YES  |     | NULL              |                             |
+-------------------+------------------+------+-----+-------------------+-----------------------------+


here is my code,  

  const get = (type, req, res, next) => {
      let condition = {};
      if (req.query) {
        condition = Utils.searchCondition(req.query, ['name'], true);
        if (req.query.venue) {
          condition.arenaId = req.query.venue;
        }
      }
      condition.type = type;
      condition.organizationId = req.ORG_ID;

      db.coachingClasses
        .findAll({
          attributes: [
            'id',
            'name',
            'sports',
            'gender',
            'trainerId',
            [
              db.sequelize.fn(
                'COUNT',
                db.sequelize.col('coachingClassEntries.user_id')
              ),
              'entries'
            ],
            'maxEntries',
            'fees',
            'fromDate',
            'toDate',
            'fromTime',
            'toTime',
            'billCycle',
            'dueAfter',
            'daySlot'
          ],
          where: condition,
          include: [
            {
              model: db.users,
              attributes: ['id', 'name']
            },
            {
              model: db.arenas,
              attributes: ['id', 'name'],
              include: { model: db.addresses }
            },
            {
              model: db.coachingClassEntries,
              attributes: []
            }
          ],
          group: ['id']
        })
        .then(coachings => {
          let output = coachings.map(c => {
            let arenaAddr = addressController.prepareAddressText(c.arena.address);
            let item = {
              id: c.id,
              name: c.name,
              sports: Utils.getKey(SportType, c.sports),
              gender: c.gender,
              entries: c.dataValues.entries,
              max_entries: c.maxEntries,
              fees: c.fees,
              from_date: c.fromDate,
              to_date: c.toDate,
              from_time: c.fromTime,
              to_time: c.toTime,
              bill_cycle: c.billCycle,
              due_after: c.dueAfter,
              days: Utils.getDaySlot(c.daySlot),
              arena_id: c.arena.id,
              arena: c.arena.name + ', ' + arenaAddr,
              venue: c.arena.name + ', ' + arenaAddr
            };
            if (type === 'C') {
              (item.trainer_id = c.user.id), (item.trainer_name = c.user.name);
            }
            return item;
          });
          res.send(output);
        })**strong text**
        .catch(next);
    };

任何帮助都将不胜感激。

0 个答案:

没有答案