|
| 1 | +import { Request, Response } from "express"; |
| 2 | +import Auction from "../models/auction.model"; |
| 3 | +import Bid from "../models/bid.model"; |
| 4 | +import Product from "../models/product.model"; |
| 5 | +import mongoose from "mongoose"; |
| 6 | + |
| 7 | +//new auction |
| 8 | +export const createAuction = async (req: Request, res: Response): Promise<void> => { |
| 9 | + try { |
| 10 | + const userId = req.user?.id; |
| 11 | + if (!userId) { |
| 12 | + res.status(401).json({ message: "Unauthorized" }); |
| 13 | + return; |
| 14 | + } |
| 15 | + const { productId, startPrice, minIncrement, durationHours } = req.body; |
| 16 | + if (!productId || !startPrice) { |
| 17 | + res.status(400).json({ message: "Product ID and start price are required" }); |
| 18 | + return; |
| 19 | + } |
| 20 | + //if product exists |
| 21 | + const product = await Product.findById(productId); |
| 22 | + if (!product) { |
| 23 | + res.status(404).json({ message: "Product not found" }); |
| 24 | + return; |
| 25 | + } |
| 26 | + const existingAuction = await Auction.findOne({ productId: new mongoose.Types.ObjectId(productId), status: "active" }); |
| 27 | + if (existingAuction) { |
| 28 | + res.status(400).json({ message: "An active auction already exists for this product" }); |
| 29 | + return; |
| 30 | + } |
| 31 | + const expiresAt = new Date(); |
| 32 | + expiresAt.setHours(expiresAt.getHours() + (durationHours || 48)); |
| 33 | + |
| 34 | + const auction = new Auction({ |
| 35 | + productId: new mongoose.Types.ObjectId(productId), |
| 36 | + itemName: product.name, |
| 37 | + itemDescription: product.description, |
| 38 | + startPrice, |
| 39 | + currentHighestBid: startPrice, |
| 40 | + minIncrement: minIncrement || 100, |
| 41 | + seller: new mongoose.Types.ObjectId(userId), |
| 42 | + expiresAt, |
| 43 | + status: "active", |
| 44 | + }); |
| 45 | + await auction.save(); |
| 46 | + res.status(201).json(auction); |
| 47 | + } catch (error: any) { |
| 48 | + console.error("Create auction error:", error); |
| 49 | + res.status(500).json({ message: error.message || "Server error" }); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +export const placeBid = async (req: Request, res: Response): Promise<void> => { |
| 54 | + try { |
| 55 | + const userId = req.user?.id; |
| 56 | + if (!userId) { |
| 57 | + res.status(401).json({ message: "Unauthorized" }); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const { id } = req.params; |
| 62 | + const { amount } = req.body; |
| 63 | + |
| 64 | + if (!amount || amount <= 0) { |
| 65 | + res.status(400).json({ message: "Valid bid amount is required" }); |
| 66 | + return; |
| 67 | + } |
| 68 | + let auction = await Auction.findById(id); |
| 69 | + if (!auction) { |
| 70 | + auction = await Auction.findOne({ productId: new mongoose.Types.ObjectId(id), status: "active" }); |
| 71 | + } |
| 72 | + |
| 73 | + if (!auction) { |
| 74 | + res.status(404).json({ message: "Auction not found or not active" }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + if (auction.status !== "active") { |
| 79 | + res.status(400).json({ message: "Auction is not active" }); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Check if expired |
| 84 | + if (new Date() > auction.expiresAt) { |
| 85 | + auction.status = "expired"; |
| 86 | + await auction.save(); |
| 87 | + res.status(400).json({ message: "Auction has expired" }); |
| 88 | + return; |
| 89 | + } |
| 90 | + if (auction.seller.toString() === userId) { |
| 91 | + res.status(400).json({ message: "You cannot bid on your own auction" }); |
| 92 | + return; |
| 93 | + } |
| 94 | + const minBid = auction.currentHighestBid + (auction.minIncrement || 100); |
| 95 | + if (amount < minBid) { |
| 96 | + res.status(400).json({ message: `Bid must be at least ₹${minBid}` }); |
| 97 | + return; |
| 98 | + } |
| 99 | + const bid = new Bid({ |
| 100 | + auctionId: auction._id, |
| 101 | + bidder: new mongoose.Types.ObjectId(userId), |
| 102 | + amount, |
| 103 | + }); |
| 104 | + |
| 105 | + await bid.save(); |
| 106 | + auction.currentHighestBid = amount; |
| 107 | + auction.highestBidder = new mongoose.Types.ObjectId(userId); |
| 108 | + await auction.save(); |
| 109 | + |
| 110 | + res.status(201).json({ |
| 111 | + message: "Bid placed successfully", |
| 112 | + bid, |
| 113 | + auction: { |
| 114 | + currentHighestBid: auction.currentHighestBid, |
| 115 | + highestBidder: auction.highestBidder, |
| 116 | + }, |
| 117 | + }); |
| 118 | + } catch (error: any) { |
| 119 | + console.error("Place bid error:", error); |
| 120 | + res.status(500).json({ message: error.message || "Server error" }); |
| 121 | + } |
| 122 | +}; |
| 123 | +export const acceptHighestBid = async (req: Request, res: Response): Promise<void> => { |
| 124 | + try { |
| 125 | + const userId = req.user?.id; |
| 126 | + if (!userId) { |
| 127 | + res.status(401).json({ message: "Unauthorized" }); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + const { id } = req.params; |
| 132 | + let auction = await Auction.findById(id); |
| 133 | + if (!auction) { |
| 134 | + auction = await Auction.findOne({ productId: new mongoose.Types.ObjectId(id), status: "active" }); |
| 135 | + } |
| 136 | + |
| 137 | + if (!auction) { |
| 138 | + res.status(404).json({ message: "Auction not found" }); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + if (auction.seller.toString() !== userId) { |
| 143 | + res.status(403).json({ message: "Only the seller can accept bids" }); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + if (!auction.highestBidder) { |
| 148 | + res.status(400).json({ message: "No bids to accept" }); |
| 149 | + return; |
| 150 | + } |
| 151 | + // Mark auction as sold |
| 152 | + auction.status = "sold"; |
| 153 | + auction.soldTo = auction.highestBidder; |
| 154 | + auction.soldPrice = auction.currentHighestBid; |
| 155 | + await auction.save(); |
| 156 | + |
| 157 | + res.status(200).json({ |
| 158 | + message: "Bid accepted successfully", |
| 159 | + auction, |
| 160 | + }); |
| 161 | + } catch (error: any) { |
| 162 | + console.error("Accept bid error:", error); |
| 163 | + res.status(500).json({ message: error.message || "Server error" }); |
| 164 | + } |
| 165 | +}; |
| 166 | + |
| 167 | +// Get auction by product ID |
| 168 | +export const getAuctionByProductId = async (req: Request, res: Response): Promise<void> => { |
| 169 | + try { |
| 170 | + const { productId } = req.params; |
| 171 | + |
| 172 | + const auction = await Auction.findOne({ |
| 173 | + productId: new mongoose.Types.ObjectId(productId), |
| 174 | + status: "active", |
| 175 | + }).populate("seller", "name email").populate("highestBidder", "name email"); |
| 176 | + |
| 177 | + if (!auction) { |
| 178 | + res.status(404).json({ message: "No active auction found for this product" }); |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + //time left |
| 183 | + const now = new Date(); |
| 184 | + const expiresAt = new Date(auction.expiresAt); |
| 185 | + const timeLeftMs = expiresAt.getTime() - now.getTime(); |
| 186 | + const timeLeftHours = Math.max(0, Math.floor(timeLeftMs / (1000 * 60 * 60))); |
| 187 | + const timeLeftMinutes = Math.max(0, Math.floor((timeLeftMs % (1000 * 60 * 60)) / (1000 * 60))); |
| 188 | + |
| 189 | + res.status(200).json({ |
| 190 | + ...auction.toObject(), |
| 191 | + timeLeft: timeLeftMs > 0 ? `${timeLeftHours}h ${timeLeftMinutes}m` : "Expired", |
| 192 | + isExpired: timeLeftMs <= 0, |
| 193 | + }); |
| 194 | + } catch (error: any) { |
| 195 | + console.error("Get auction error:", error); |
| 196 | + res.status(500).json({ message: error.message || "Server error" }); |
| 197 | + } |
| 198 | +}; |
| 199 | + |
| 200 | +// Get all bids for an auction |
| 201 | +export const getAuctionBids = async (req: Request, res: Response): Promise<void> => { |
| 202 | + try { |
| 203 | + const { id } = req.params; |
| 204 | + |
| 205 | + let auction = await Auction.findById(id); |
| 206 | + if (!auction) { |
| 207 | + auction = await Auction.findOne({ productId: new mongoose.Types.ObjectId(id) }); |
| 208 | + } |
| 209 | + |
| 210 | + if (!auction) { |
| 211 | + res.status(404).json({ message: "Auction not found" }); |
| 212 | + return; |
| 213 | + } |
| 214 | + |
| 215 | + const bids = await Bid.find({ auctionId: auction._id }) |
| 216 | + .populate("bidder", "name email") |
| 217 | + .sort({ createdAt: -1 }); |
| 218 | + |
| 219 | + res.status(200).json(bids); |
| 220 | + } catch (error: any) { |
| 221 | + console.error("Get bids error:", error); |
| 222 | + res.status(500).json({ message: error.message || "Server error" }); |
| 223 | + } |
| 224 | +}; |
| 225 | + |
0 commit comments