import validator from 'validator';
import { v4 as uuidv4 } from 'uuid';
import { Sequelize, Op } from 'sequelize';

class Utils {
  static uuid(value: string): string {
    let id = value;

    if (!validator.isUUID(id)) {
      id = uuidv4();
    }

    return id;
  }

  static ilike(model: string, column: string, value: string) {
    return Sequelize.where(
      Sequelize.fn('lower', Sequelize.col(`${model}.${column}`)),
      {
        [Op.like]: `%${value}%`.toLowerCase(),
      },
    );
  }
}

export default Utils;
