import { RowDataPacket } from "mysql2";
import DB from "../../../config/database/db";
import CONSTANTS from "../../../config/constants";

const whatsAppTemplateDB: any = {};

whatsAppTemplateDB.getByClientAndType = async ({
  clientId,
  type,
  userType = CONSTANTS.USER_TYPE.TENANT
}: any) => {
  const query =
    "Select * from WhatsappTemplates where clientId = ? and type=? and userType=?";
  const data = [clientId, type, userType];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};

whatsAppTemplateDB.getByType = async ({
  type,
  userType = CONSTANTS.USER_TYPE.TENANT
}: any) => {
  const query =
    "Select * from WhatsappTemplates where clientId is NULL and type=? and userType=?";
  const data = [type, userType];
  const [rows] = await DB.execute<RowDataPacket[]>(query, data);
  if (rows?.length > 0) return rows[0];
  else return false;
};



export default whatsAppTemplateDB;
