Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.1/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.1/auctionController.js

const db = require("../models");
const { col, Op, sequelize } = require("sequelize");

/* 
/* const getVehiclesByAuctionId = async (req, res) => {
  try {
    const { user_id, week_id, auction_id } = req.body;

    const result = await db.tblOpportunity.findAll({
      attributes: [],
      where: {
        user_id: user_id,
        week_id: week_id,
      },
      include: [
        {
          model: db.tblVehicle,
          attributes: [
            "year",
            "make",
            "model",
            "imageUrl",
            "color_name",
            "details",
          ],
          raw: true,
          required: true,
          include: [
            {
              model: db.tblVehicleRun,
              attributes: ["vehicle_id", "condition_light", "sale_status"],
              raw: true,
              required: false,
              include: [
                {
                  model: db.tblAuctionLane,
                  attributes: ["lane_id", "name"],
                  raw: true,
                  include: [
                    {
                      model: db.tblAuction,
                      attributes: ["auction_id", "name"],
                      raw: true,
                      where: {
                        auction_id: auction_id,
                      },
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    });

    // Format the result as per the required format
    const formattedResult = result.map((item) =>
      item.tblVehicles.map((vehicle) => ({
        year: vehicle.year,
        make: vehicle.make,
        model: vehicle.model,
        imageUrl: vehicle.imageUrl,
        color_name: vehicle.color_name,
        details: vehicle.details,
        vehicle_id: vehicle.tblVehicleRuns[0].vehicle_id,
        condition_light: vehicle.tblVehicleRuns[0].condition_light,
        sale_status: vehicle.tblVehicleRuns[0].sale_status,
        lane_id: vehicle.tblVehicleRuns[0].tblAuctionLane.lane_id,
        lane_name: vehicle.tblVehicleRuns[0].tblAuctionLane.name,
        auction_id:
          vehicle.tblVehicleRuns[0].tblAuctionLane.tblAuction.auction_id,
        auction_name: vehicle.tblVehicleRuns[0].tblAuctionLane.tblAuction.name,
      }))
    );

    return res.json({ data: formattedResult[0] });
  } catch (error) {
    console.error(error);
    return res.status(500).json({
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    });
  }
}; */
/* const getVehiclesByAuctionId = async (req, res) => {
  try {
    const { user_id, week_id, auction_id } = req.body;

    const result = await db.tblOpportunity.findAll({
      attributes: [],
      where: { user_id, week_id },
      include: [
        {
          model: db.tblVehicle,
          attributes: [
            "year",
            "make",
            "model",
            "imageUrl",
            "color_name",
            "details",
          ],
          raw: true,
          //   required: true,
          include: [
            {
              model: db.tblVehicleRun,
              attributes: ["vehicle_id", "condition_light", "sale_status"],
              raw: true,
              //   required: false,
              include: [
                {
                  model: db.tblAuctionLane,
                  attributes: ["lane_id", "name"],
                  raw: true,
                  include: [
                    {
                      model: db.tblAuction,
                      attributes: ["auction_id", "name"],
                      raw: true,
                      where: { auction_id },
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    });

    // Format the result as per the required format
    const formattedResult = result.flatMap((item) =>
      (item.tblVehicles || []).map((vehicle) => {
        const vehicleRun = vehicle.tblVehicleRuns?.[0] || {};
        const auctionLane = vehicleRun.tblAuctionLane || {};
        const auction = auctionLane.tblAuction || {};

        return {
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          imageUrl: vehicle.imageUrl,
          color_name: vehicle.color_name,
          details: vehicle.details,
          vehicle_id: vehicleRun?.vehicle_id,
          condition_light: vehicleRun?.condition_light,
          sale_status: vehicleRun?.sale_status,
          lane_id: auctionLane?.lane_id,
          lane_name: auctionLane?.name,
          auction_id: auction?.auction_id,
          auction_name: auction?.name,
        };
      })
    );

    return res.json({ data: formattedResult });
  } catch (error) {
    console.error(error);
    return res.status(500).json({
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    });
  }
};
 */
/* const getVehiclesByAuctionId = async (req, res) => {
  try {
    const { user_id, week_id, auction_id } = req.body;

    const result = await db.tblOpportunity.findAll({
      attributes: [],
      where: { user_id, week_id },
      include: [
        {
          model: db.tblVehicle,
          attributes: [
            "year",
            "make",
            "model",
            "imageUrl",
            "color_name",
            "details",
          ],
          raw: true,
          include: [
            {
              model: db.tblVehicleRun,
              attributes: ["vehicle_id", "condition_light", "sale_status"],
              raw: true,
              where: {
                vehicle_id: col("tblVehicles.vehicle_id"),
              },
              include: [
                {
                  model: db.tblAuctionLane,
                  attributes: ["lane_id", "name"],
                  raw: true,
                  include: [
                    {
                      model: db.tblAuction,
                      attributes: ["auction_id", "name"],
                      raw: true,
                      where: { auction_id },
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    });

    // console.log(result[0].tblVehicles[0]?.tblVehicleRuns?.[0].tblAuctionLane);
    // return res.json({ data: result });
    // Check if result is empty or if vehicleRun is not present

    // return res.json({ data: result });
    if (!result.length || !result[0].tblVehicles[0]?.tblVehicleRuns?.[0]) {
      return res.json({ data: [] });
    }
    // console.log(result);
    // return res.json({ data: result });
    // // Format the result as per the required format
    const formattedResult = result.flatMap((item) =>
      item.tblVehicles.map((vehicle) => {
        const vehicleRun = vehicle.tblVehicleRuns?.[0] || {};
        const auctionLane = vehicleRun.tblAuctionLane || {};
        const auction = auctionLane.tblAuction || {};

        return {
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          imageUrl: vehicle.imageUrl,
          color_name: vehicle.color_name,
          details: vehicle.details,
          vehicle_id: vehicleRun?.vehicle_id,
          condition_light: vehicleRun?.condition_light,
          sale_status: vehicleRun?.sale_status,
          lane_id: auctionLane?.lane_id,
          lane_name: auctionLane?.name,
          auction_id: auction?.auction_id,
          auction_name: auction?.name,
        };
      })
    );

    return res.json({ data: formattedResult });
  } catch (error) {
    console.error(error);
    return res.status(500).json({
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    });
  }
};
 

 */
const getVehiclesByAuctionId = async (req, res) => {
  try {
    const { user_id, week_id, auction_id } = req.body;

    const result = await db.tblOpportunity.findAll({
      attributes: [],
      where: { user_id, week_id },
      include: [
        {
          model: db.tblVehicle,
          attributes: [
            "year",
            "make",
            "model",
            "imageUrl",
            "color_name",
            "details",
          ],
          raw: true,
          include: [
            {
              model: db.tblVehicleRun,
              attributes: ["vehicle_id", "condition_light", "sale_status"],
              raw: true,
              where: {
                vehicle_id: col("tblVehicles.vehicle_id"),
                lane_id: col("tblVehicles.tblVehicleRuns.lane_id"),
              },
              include: [
                {
                  model: db.tblAuctionLane,
                  attributes: ["lane_id", "name"],
                  raw: true,
                  include: [
                    {
                      model: db.tblAuction,
                      attributes: ["auction_id", "name"],
                      raw: true,
                      where: { auction_id },
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    });
    // return res.json({ data: result });
    // Check if result is empty or if vehicleRun is not present
    if (
      !result.length ||
      !result[0].tblVehicles[0]?.tblVehicleRuns?.[0].tblAuctionLane
    ) {
      return res.json({ status: 200, message: "Success", data: [] });
    }
    // // Format the result as per the required format
    const formattedResult = result.flatMap((item) =>
      item.tblVehicles.map((vehicle) => {
        const vehicleRun = vehicle.tblVehicleRuns?.[0] || {};
        const auctionLane = vehicleRun.tblAuctionLane || {};
        const auction = auctionLane.tblAuction || {};

        return {
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          imageUrl: vehicle.imageUrl,
          color_name: vehicle.color_name,
          details: vehicle.details,
          vehicle_id: vehicleRun?.vehicle_id,
          condition_light: vehicleRun?.condition_light,
          sale_status: vehicleRun?.sale_status,
          lane_id: auctionLane?.lane_id,
          lane_name: auctionLane?.name,
          auction_id: auction?.auction_id,
          auction_name: auction?.name,
        };
      })
    );

    return res.json({ data: formattedResult });
  } catch (error) {
    console.error(error);
    return res.status(500).json({
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    });
  }
};
//auctioneer side filter
const getAuctioneerVehiclesByAuctionId = async (req, res) => {
  try {
    const { auctioneer_id, week_id, auction_id } = req.body;

    const result = await db.tblOpportunity.findAll({
      attributes: [],
      where: { auctioneer_id, week_id },
      include: [
        {
          model: db.tblVehicle,
          attributes: [
            "year",
            "make",
            "model",
            "imageUrl",
            "color_name",
            "details",
          ],
          raw: true,
          include: [
            {
              model: db.tblVehicleRun,
              attributes: ["vehicle_id", "condition_light", "sale_status"],
              raw: true,
              where: {
                vehicle_id: col("tblVehicles.vehicle_id"),
                lane_id: col("tblVehicles.tblVehicleRuns.lane_id"),
              },
              include: [
                {
                  model: db.tblAuctionLane,
                  attributes: ["lane_id", "name"],
                  raw: true,
                  include: [
                    {
                      model: db.tblAuction,
                      attributes: ["auction_id", "name"],
                      raw: true,
                      where: { auction_id },
                    },
                  ],
                },
              ],
            },
          ],
        },
      ],
    });
    // return res.json({ data: result });
    // Check if result is empty or if vehicleRun is not present
    if (
      !result.length ||
      !result[0].tblVehicles[0]?.tblVehicleRuns?.[0].tblAuctionLane
    ) {
      return res.json({ status: 200, message: "Success", data: [] });
    }
    // // Format the result as per the required format
    const formattedResult = result.flatMap((item) =>
      item.tblVehicles.map((vehicle) => {
        const vehicleRun = vehicle.tblVehicleRuns?.[0] || {};
        const auctionLane = vehicleRun.tblAuctionLane || {};
        const auction = auctionLane.tblAuction || {};

        return {
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          imageUrl: vehicle.imageUrl,
          color_name: vehicle.color_name,
          details: vehicle.details,
          vehicle_id: vehicleRun?.vehicle_id,
          condition_light: vehicleRun?.condition_light,
          sale_status: vehicleRun?.sale_status,
          lane_id: auctionLane?.lane_id,
          lane_name: auctionLane?.name,
          auction_id: auction?.auction_id,
          auction_name: auction?.name,
        };
      })
    );

    return res.json({ data: formattedResult });
  } catch (error) {
    console.error(error);
    return res.status(500).json({
      status: 500,
      message: "Internal Server Error",
      error: error.message,
    });
  }
};

module.exports = { getVehiclesByAuctionId, getAuctioneerVehiclesByAuctionId };

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