Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.2/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.2/auctionLaneController.js

const db = require("../models");
const { col, Op, sequelize } = require("sequelize");
const CircularJSON = require("circular-json");
const flatted = require("flatted");

const getAuctionsAndLanesByAuctioneerId = async (req, res) => {
  try {
    const { auctioneer_id } = req.body;
    let result = await db.tblAuctionLane.findAll({
      attributes: ["name", "lane_id"], // Select the columns you want to retrieve
      include: [
        {
          model: db.tblAuction,
          attributes: ["name"],
          raw: true,
        },
      ],
      where: {
        auctioneer_id: auctioneer_id,
      },
    });

    const transformedData = result.map((item) => ({
      lane_id: item.lane_id,
      name: item.name,
      auction_name: item.tblAuction.name,
    }));

    const data = transformedData.reduce(
      (acc, { lane_id, name, auction_name }) => {
        const auctionEntry = acc.find(
          (entry) => entry.auction_name === auction_name
        );

        if (auctionEntry) {
          auctionEntry.lane_names.push({ lane_id, name });
        } else {
          acc.push({ auction_name, lane_names: [{ lane_id, name }] });
        }

        return acc;
      },
      []
    );

    return res.json({ status: 200, data: data });
  } catch (error) {
    return res.json(error.message);
  }
};

function transformFormat(originalData) {
  const transformedData = { data: [] };

  originalData.forEach((opportunity) => {
    const auctionData = {
      auction_name:
        opportunity.tblVehicles.length > 0 &&
        opportunity.tblVehicles[0][0].lane_details
          ? opportunity.tblVehicles[0][0].lane_details.auction_details.name
          : null,
      auction_id:
        opportunity.tblVehicles.length > 0 &&
        opportunity.tblVehicles[0][0].lane_details
          ? opportunity.tblVehicles[0][0].lane_details.auction_details
              .auction_id
          : null,
      units: opportunity.tblVehicles.length,
      vehicles_list: [],
    };

    opportunity.tblVehicles.forEach((vehicleArray) => {
      vehicleArray.forEach((vehicle) => {
        const vehicleData = {
          vehicle_run_id:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].vehicle_run_id
              : null,
          vehicle_id: vehicle.vehicle_id,
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          trim: vehicle.trim,
          mileage: vehicle.mileage,
          imageUrl: vehicle.imageUrl,
          condition_light:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].condition_light
              : null,
          announcements:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].announcements
              : null,
          reserve:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].reserve
              : null,
          sale_price:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].sale_price
              : null,
          auction_fee:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].auction_fee
              : null,
          net_profit:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].net_profit
              : null,
          sale_status:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].sale_status
              : null,
          run_no:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].run_no
              : null,
          lane_id:
            vehicle.tblVehicleRuns.length > 0
              ? vehicle.tblVehicleRuns[0].lane_id
              : null,
          lane_name:
            vehicle.tblVehicleRuns.length > 0 &&
            vehicle.tblVehicleRuns[0].lane_details
              ? vehicle.tblVehicleRuns[0].lane_details.name
              : null,
        };

        auctionData.vehicles_list.push(vehicleData);
      });
    });

    transformedData.data.push(auctionData);
  });

  return transformedData;
}


const getAuctionsByAuctioneerId = async (req, res) => {
  try {
    const { user_id, week_id } = req.body;
    let result;
    [result] = await db.sequelize.query(`SELECT
    o.opportunity_id,
    a.auctioneer_id,
    au.auction_id,
    au.name as auction_name,
    IFNULL(COUNT(vr.lane_id), 0) AS units 
FROM tblOpportunities AS o
JOIN tblAuctioneers AS a ON o.auctioneer_id = a.auctioneer_id
JOIN tblAuctionLanes AS al ON a.auctioneer_id = al.auctioneer_id
JOIN tblAuctions AS au ON al.auction_id = au.auction_id
LEFT JOIN tblVehicleRuns AS vr ON al.lane_id = vr.lane_id AND vr.opportunity_id = o.opportunity_id 
WHERE o.user_id=${user_id} AND o.week_id=${week_id}
GROUP BY o.opportunity_id, au.auction_id;
    `);
    
    
    let [unCatogrizedResult] = await db.sequelize.query(
     `
SELECT 
            0 as opportunity_id, 
            0 as auctioneer_id, 
  0 as auction_id,
    COALESCE(vr.lane_id, 'Uncategorized') AS auction_name,
    COUNT(vr.vehicle_run_id) AS units
FROM tblOpportunities AS o
LEFT JOIN tblVehicleRuns AS vr ON vr.opportunity_id = o.opportunity_id 
WHERE o.user_id=${user_id} AND o.week_id=${week_id}  AND vr.lane_id IS NULL
GROUP BY  COALESCE(vr.lane_id, 'Uncategorized');
     `
    );
    if (!unCatogrizedResult[0]) {
      unCatogrizedResult[0] = {
        auction_id: 0,
        auction_name: "Uncategorized",
        units: 0,
      };
    }
    result.push(unCatogrizedResult[0]);
    

    // Returning the result as a response
    return res.json({ status: 200, message: "Success", data: result });
  } catch (error) {
    console.error("Error fetching the data: ", error);
    return res.status(500).json({
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    });
  }
};

module.exports = {
  getAuctionsAndLanesByAuctioneerId,
  getAuctionsByAuctioneerId,
};

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists