Home > Software design >  Sequelize Validation with condition object
Sequelize Validation with condition object

Time:12-02

const db = require("../models");
const Meet = db.meet;

checkDuplicateTime = (req, res, next) => {
  Meet.findAll({
      where: {
          tanggal: req.body.date,
          waktu: req.body.time                 
      }
  }).then(time => {
          if(time --- (1000 * 60 * 60) || time     (2000 * 60 * 60)) {
              res.status(400).send({
                  message: "Failed! Time already use!!"
              });
              return;
          }
          next();
      });
  });
}

How to inspection data from already in database with condition isBetween before and after time??

in Frontend, I succeed to do it like this :

timedi() {
        const timede = this.dateCheck.filter((item) => {
          return moment(item.dates).isSame(new Date(this.dates), "day")
        }).some((item)=> {
            const format = 'hh:mm:ss'
            const time = moment(this.time, format) // from input user
            const beforeTime = moment(item.time, format).add(-1, 'hours') // from already database 
            const afterTime = moment(item.time, format).add(2, 'hours'); // from already database

            if(time.isBetween(beforeTime, afterTime)) {
                console.log('Ignore')
            } else {
                console.log('Allow')
            }
        })
    },

Hope there is an answer. Thanks.

CodePudding user response:

Source code :

const db = require("../models");
const Meet = db.meet;

checkDuplicateDate = (req, res, next) => {
        const dates = req.body.dated;
        const times = req.body.timed;
        const date = moment(dates).tz('Asia/Jakarta').format('YYYY-MM-DD');
        const time = moment(times, [moment.ISO_8601, 'HH:mm']).tz('Asia/Jakarta').format('HH:mm:ss');
        
        const startTime = moment(time, [moment.ISO_8601, 'HH:mm']).add(-2, 'hours').tz('Asia/Jakarta').format('HH:mm:ss');
        const beforeTime = moment(time, [moment.ISO_8601, 'HH:mm']).add(2, 'hours').tz('Asia/Jakarta').format('HH:mm:ss');
        Meet.findOne({
            where: {
                tanggal: date,
                waktu: {
                    [Op.between]: [startTime, beforeTime]
                }
            }
        }).then(meet => {
            if(meet) {
                res.status(400).send({
                    message: "Failed! Time is booked!!"
                })
                return
            }
            next();
        });
    });
};

I hope in future, this case can help anyone. Thanks.

  • Related