text: import { performQueryWithParams, checkAndReturnData } from "./";
  
  export const getJobStatus = async (shop) => {
    const query = `SELECT sitemapName, sitemapId, jobId, createdAt, importStatus FROM job_queue WHERE shopDomain = ?;`;
    const rows = await performQueryWithParams(query, [shop]);
    return rows;
  };
  
  export const getAutoImportState = async (shop) => {
    const query = `SELECT toggleButtonId, allowAutoImport, sitemapId, sitemapName FROM auto_import_state WHERE shopDomain = ?;`;
    const rows = await performQueryWithParams(query, [shop]);
    return checkAndReturnData(rows);
  };
  
  export const getSitemapSettings = async (shop, sitemapid) => {
    const query = `SELECT prodInBothDs, prodInNewDs, prodInOldDs FROM sitemap_settings WHERE shopDomain = ? AND sitemapId = ?;`;
    const rows = await performQueryWithParams(query, [shop, sitemapid]);
    return rows;
  };
  
  export const failedJobs = async (shop) => {
    const query = `SELECT * FROM job_queue WHERE shopDomain = '${shop}' AND importStatus = '${"Failed"}'`;
    const rows = await performQueryWithParams(query, [shop, "Failed"]);
    return rows;
  };
  
  export const jobQueue = async (shop, data) => {
    let query;
    const dateTime = (new Date().getTime() / 1000).toFixed(0);
    const existingJob = await performQueryWithParams(
      `SELECT * FROM job_queue WHERE shopDomain = ? AND jobId = ?`,
      [shop, data.jobId]
    );
    if (!existingJob.length) {
      query = `INSERT INTO job_queue(shopDomain, sitemapName, sitemapId, jobId, serializedData, importStatus, createdAt)
          VALUES(?, ?, ?, ?, ?, ?, ?);`;
      await performQueryWithParams(query, [
        shop,
        data.sitemapName,
        data.sitemapId,
        data.jobId,
        data.serializedData,
        data.importStatus,
        dateTime,
      ]);
      return "inserted";
    } else {
      query = `UPDATE job_queue SET sitemapName = ?, serializedData = ?, importStatus = ?, createdAt = ? WHERE
          shopDomain = ? AND jobId = ?;`;
      await performQueryWithParams(query, [
        data.sitemapName,
        data.serializedData,
        data.importStatus,
        dateTime,
        shop,
        data.jobId,
      ]);
      return "updated";
    }
  };
  
  export const rescheduleJob = async (shop, data) => {
    let query;
    const dateTime = (new Date().getTime() / 1000).toFixed(0);
    query = `UPDATE job_queue SET importStatus = ?, createdAt = ? WHERE shopDomain = ? AND jobId = ?;`;
    await performQueryWithParams(query, [
      data.importStatus,
      dateTime,
      shop,
      data.jobId,
    ]);
    query = `SELECT * FROM job_queue WHERE shopDomain = ? AND importStatus = ?`;
    const failedJobs = await performQueryWithParams(query, [shop, "Failed"]);
    return { updatedStatus: "success", failedJobs: failedJobs };
  };
  