Sindbad~EG File Manager
const db = require("../models");
// const getVehiclesByAuctionId = async (req, res) => {
// try {
// const {
// user_id,
// week_id,
// auction_id,
// sortBy = "vehicle_id",
// orderDirection = "ASC",
// } = req.body;
// let result;
// result = await db.tblOpportunity.findAll({
// attributes: ["opportunity_id","auctioneer_id"],
// where: { user_id, week_id },
// include: [
// {
// model: db.tblVehicleRun,
// required: true, // Use this instead of raw to ensure an INNER JOIN
// include: [
// {
// model: db.tblVehicle,
// required: true, // Use this instead of raw to ensure an INNER JOIN
// },
// {
// model: db.tblAuctionLane,
// attributes: ["lane_id", "name"],
// required: true, // Use this instead of raw to ensure an INNER JOIN
// include: [
// {
// model: db.tblAuction,
// attributes: ["auction_id", "name"],
// required: true, // Use this instead of raw to ensure an INNER JOIN
// where: { auction_id },
// },
// ],
// },
// ],
// },
// ],
// order: [
// // Include the nested order here instead
// [db.tblVehicleRun, db.tblVehicle, sortBy, orderDirection],
// ],
// });
// if (!result.length || !result[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.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,
// imageUrl: vehicle.imageUrl,
// color: vehicle.color,
// color_name: vehicle.color_name,
// details: vehicle.details,
// condition_light: vehicleRun?.condition_light || 0,
// 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_proceed: vehicleRun?.net_proceed || 0,
// 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 getVehiclesByAuctionId = async (req, res) => {
try {
const {
user_id,
week_id,
auction_id,
filter_type,
sortBy = "vehicle_id",
orderDirection = "ASC",
} = req.body;
let result;
/* result = await db.tblOpportunity.findAll({
attributes: ["opportunity_id", "auctioneer_id"],
where: { user_id, week_id },
include: [
{
model: db.tblVehicleRun,
required: filter_type ? false : true, // Adjust this 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, // Use this instead of raw to ensure an INNER JOIN
},
auction_id
? {
model: db.tblAuctionLane,
attributes: ["lane_id", "name"],
required: true, // Use this instead of raw to ensure an INNER JOIN
include: [
{
model: db.tblAuction,
attributes: ["auction_id", "name"],
required: true, // Use this instead of raw to ensure an INNER JOIN
where: { auction_id },
},
],
}
: undefined,
],
},
],
order: [
// Include the nested order here instead
[db.tblVehicleRun, db.tblVehicle, sortBy, orderDirection],
],
}); */
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]],
});
// return res.json(result);
// if (!result.length || !result[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.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,
imageUrl: vehicle.imageUrl,
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_proceed: vehicleRun?.net_proceed || 0,
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 {
const {
auctioneer_id,
week_id,
auction_id,
sortBy = "vehicle_id",
orderDirection = "ASC",
} = req.body;
let result;
result = await db.tblOpportunity.findAll({
attributes: ["opportunity_id"],
where: { auctioneer_id, week_id },
include: [
{
model: db.tblVehicleRun,
required: true, // Use this instead of raw to ensure an INNER JOIN
include: [
{
model: db.tblVehicle,
required: true, // Use this instead of raw to ensure an INNER JOIN
},
{
model: db.tblAuctionLane,
attributes: ["lane_id", "name"],
where: { auctioneer_id },
required: true, // Use this instead of raw to ensure an INNER JOIN
include: [
{
model: db.tblAuction,
attributes: ["auction_id", "name"],
required: true, // Use this instead of raw to ensure an INNER JOIN
where: { auction_id },
},
],
},
],
},
],
order: [
// Include the nested order here instead
[db.tblVehicleRun, db.tblVehicle, sortBy, orderDirection],
],
});
if (!result.length || !result[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.tblVehicleRuns.map((vehicleRun) => {
const vehicle = vehicleRun.tblVehicle || {};
const auctionLane = vehicleRun.tblAuctionLane || {};
const auction = auctionLane.tblAuction || {};
return {
vehicle_id: vehicle.vehicle_id,
year: vehicle.year,
make: vehicle.make,
model: vehicle.model,
vin: vehicle.vin,
mileage: vehicle.mileage,
imageUrl: vehicle.imageUrl,
color: vehicle.color,
color_name: vehicle.color_name,
details: vehicle.details,
condition_light: vehicleRun?.condition_light || 0,
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_proceed: vehicleRun?.net_proceed || 0,
vehicle_total_fee: vehicleRun?.vehicle_total_fee || 0,
reserve: vehicleRun?.reserve || 0,
lane_name: auctionLane?.name || 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