Sindbad~EG File Manager

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

const db = require("../models");

const getVehiclesByAuctionId = async (req, res) => {
  try {
    let {
      user_id,
      week_id,
      auction_id,
      filter_type,
      sortBy = "vehicle_id",
      orderDirection = "ASC",
    } = req.body;

    if (auction_id === 0) {
      filter_type = "Uncategorized";
      auction_id = null;
    }

    let result;
    result = await db.tblOpportunity.findAll({
      attributes: ["opportunity_id", "auctioneer_id"],
      where: { user_id, week_id },
      include: [
        {
          model: db.tblVehicleRun,
          required: !filter_type, // Adjust based on your logic needs
          where: filter_type ? { lane_id: null } : undefined, // Add condition only if filter_type exists
          include: [
            {
              model: db.tblVehicle,
              required: true,
            },
            ...(auction_id
              ? [
                  {
                    model: db.tblAuctionLane,
                    attributes: ["lane_id", "name"],
                    required: true,
                    include: [
                      {
                        model: db.tblAuction,
                        attributes: ["auction_id", "name"],
                        required: true,
                        where: { auction_id },
                      },
                    ],
                  },
                ]
              : []),
          ],
        },
      ],
      order: [[db.tblVehicleRun, db.tblVehicle, sortBy, orderDirection]],
    });

    const formattedResult = result.flatMap((item) =>
      item.tblVehicleRuns.map((vehicleRun) => {
        const vehicle = vehicleRun.tblVehicle || {};
        const auctionLane = vehicleRun.tblAuctionLane || {};
        const auction = auctionLane.tblAuction || {};

        return {
          auctioneer_id: item?.auctioneer_id || null,
          vehicle_id: vehicle.vehicle_id,
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          vin: vehicle.vin,
          mileage: vehicle.mileage,
          trim: vehicle.trim,
          color: vehicle.color,
          color_name: vehicle.color_name,
          details: vehicle.details,
          imageUrl: vehicle.imageUrl || "preview.png",
          condition_light: vehicleRun?.condition_light || 4,
          sale_price: vehicleRun?.sale_price || 0,
          run_no: vehicleRun?.run_no || null,
          sale_status: vehicleRun?.sale_status || false,
          lane_id: vehicleRun?.lane_id || null,
          auction_fee: vehicleRun?.auction_fee || 300,
          net_proceeds: vehicleRun?.net_proceeds || null,
          vehicle_total_fee: vehicleRun?.vehicle_total_fee || 0,
          reserve: vehicleRun?.reserve || 0,
          lane_name: auctionLane?.name || null,
          lane_id: auctionLane?.lane_id || null,
          auction_id: auction?.auction_id || null,
          auction_name: auction?.name || null,
        };
      })
    );

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

const getAuctioneerVehiclesByAuctionId = async (req, res) => {
  try {
    let {
      auctioneer_id,
      week_id,
      auction_id,
      filter_type,
      sortBy = "vehicle_id",
      orderDirection = "ASC",
    } = req.body;

    if (auction_id === 0) {
      filter_type = "Uncategorized";
      auction_id = null;
    }

    let result;
    result = await db.tblOpportunity.findAll({
      attributes: ["opportunity_id", "auctioneer_id"],
      where: { auctioneer_id, week_id },
      include: [
        {
          model: db.tblVehicleRun,
          required: !filter_type, // Adjust based on your logic needs
          where: filter_type ? { lane_id: null } : undefined, // Add condition only if filter_type exists
          include: [
            {
              model: db.tblVehicle,
              required: true,
            },
            ...(auction_id
              ? [
                  {
                    model: db.tblAuctionLane,
                    attributes: ["lane_id", "name"],
                    required: true,
                    include: [
                      {
                        model: db.tblAuction,
                        attributes: ["auction_id", "name"],
                        required: true,
                        where: { auction_id },
                      },
                    ],
                  },
                ]
              : []),
          ],
        },
      ],
      order: [[db.tblVehicleRun, db.tblVehicle, sortBy, orderDirection]],
    });

    const formattedResult = result.flatMap((item) =>
      item.tblVehicleRuns.map((vehicleRun) => {
        const vehicle = vehicleRun.tblVehicle || {};
        const auctionLane = vehicleRun.tblAuctionLane || {};
        const auction = auctionLane.tblAuction || {};

        return {
          auctioneer_id: item?.auctioneer_id || null,
          vehicle_id: vehicle.vehicle_id,
          year: vehicle.year,
          make: vehicle.make,
          model: vehicle.model,
          vin: vehicle.vin,
          mileage: vehicle.mileage,
          trim: vehicle.trim,
          color: vehicle.color,
          color_name: vehicle.color_name,
          details: vehicle.details,
          condition_light: vehicleRun?.condition_light || 4,
          sale_price: vehicleRun?.sale_price || 0,
          run_no: vehicleRun?.run_no || null,
          sale_status: vehicleRun?.sale_status || false,
          lane_id: vehicleRun?.lane_id || null,
          auction_fee: vehicleRun?.auction_fee || 300,
          net_proceeds: vehicleRun?.net_proceeds || null,
          vehicle_total_fee: vehicleRun?.vehicle_total_fee || 0,
          reserve: vehicleRun?.reserve || 0,
          lane_name: auctionLane?.name || null,
          lane_id: auctionLane?.lane_id || null,
          auction_id: auction?.auction_id || null,
          auction_name: auction?.name || null,
        };
      })
    );

    return res.json({ status: 200, message: "Success", 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