The saksh-car-auction
class provides a comprehensive solution for managing car auctions, including placing bids, updating bids, fetching bids, ending auctions, and selecting winners. This class is built using Node.js and MongoDB, and it leverages event-driven programming with the EventEmitter
module.
- Place Bids: Users can place bids on cars.
- Update Bids: Users can update their bids.
- Fetch Bids: Retrieve all bids for a specific car or user.
- End Auction: End the auction for a car.
- Select Winner: Select the winning bid for a car.
- Permission Validation: Ensure only authorized users can perform certain actions.
- Scalable: Built with Node.js and MongoDB, making it scalable for large applications.
-
Event-Driven: Uses
EventEmitter
for handling events, making it easy to extend and customize. - Secure: Includes permission validation to ensure only authorized users can perform actions.
- Flexible: Easily adaptable to different auction scenarios and requirements.
npm i saksh-car-auction
Here's an example demonstrating the entire flow of placing a bid, updating a bid, placing more bids from different users, selecting the highest bid, ending the auction, and selecting the winning bidder.
const mongoose = require('mongoose');
const SakshCarAuction = require('./index');
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/auctionDB', { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));
// Example user IDs
const userId1 = 'user123';
const userId2 = 'user456';
const userId3 = 'user789';
// Create instances of SakshCarAuction for different users
const auction1 = new SakshCarAuction(userId1);
const auction2 = new SakshCarAuction(userId2);
const auction3 = new SakshCarAuction(userId3);
// Example callback function for bid validation
const validateBid = (carId, bidAmount) => {
return bidAmount > 0; // Example validation: bid amount must be greater than 0
};
// Example callback function for permission validation
const validatePermission = (userId, resourceId, action) => {
// Add your permission validation logic here
if (action === "updatebid" || action === "selectwinner" || action === "closebidding") {
return userId === 'user123'; // Example validation: only user123 has permission for these actions
}
return true;
};
// Function to get car ID
async function getCarId() {
return "66ebd87c0cab7b7737ecda1a"; // Example car ID
}
// Event listeners
auction1.on('bidPlaced', (data) => {
console.log('Event: bidPlaced', data);
});
auction1.on('bidUpdated', (data) => {
console.log('Event: bidUpdated', data);
});
auction1.on('bidDeleted', (data) => {
console.log('Event: bidDeleted', data);
});
auction1.on('auctionEnded', (data) => {
console.log('Event: auctionEnded', data);
});
auction1.on('winnerSelected', (data) => {
console.log('Event: winnerSelected', data);
});
// Consolidated function to demonstrate the entire flow
async function runAuctionFlow() {
const carId = await getCarId();
// Step 1: Place a Bid
const bidAmount1 = 5000;
const placeBidResult = await auction1.sakshPlaceBid(carId, bidAmount1, validateBid);
console.log('Place Bid:', placeBidResult);
const bidId = placeBidResult.data ? placeBidResult.data.bidId : null;
// Step 2: Update the Bid
if (bidId) {
const newBidAmount = 6000;
const updateBidResult = await auction1.sakshUpdateBid(bidId, newBidAmount, validatePermission);
console.log('Update Bid:', updateBidResult);
}
// Step 3: Place More Bids from Different Users
const bidAmount2 = 7000;
const bidAmount3 = 8000;
const placeBidResult2 = await auction2.sakshPlaceBid(carId, bidAmount2, validateBid);
console.log('Place Bid from User 2:', placeBidResult2);
const placeBidResult3 = await auction3.sakshPlaceBid(carId, bidAmount3, validateBid);
console.log('Place Bid from User 3:', placeBidResult3);
// Step 4: Select the Highest Bid
const highestBidResult = await auction1.sakshGetHighestBid(carId);
console.log('Highest Bid:', highestBidResult);
// Step 5: End the Auction
const endAuctionResult = await auction1.sakshEndAuction(carId, validatePermission);
console.log('End Auction:', endAuctionResult);
// Step 6: Select the Winning Bidder
const selectWinnerResult = await auction1.sakshSelectWinner(carId, validatePermission);
console.log('Select Winner:', selectWinnerResult);
// Close the MongoDB connection
mongoose.connection.close();
}
runAuctionFlow();
-
sakshPlaceBid(carId, bidAmount, validateBidCallback): Places a bid on a car.
-
Parameters:
-
carId
: The ID of the car. -
bidAmount
: The amount of the bid. -
validateBidCallback
: A callback function to validate the bid.
-
- Returns: An object containing the success status, message, and bid data.
-
Parameters:
-
sakshUpdateBid(bidId, newBidAmount, validatePermissionCallback): Updates an existing bid.
-
Parameters:
-
bidId
: The ID of the bid. -
newBidAmount
: The new bid amount. -
validatePermissionCallback
: A callback function to validate user permissions.
-
- Returns: An object containing the success status, message, and updated bid data.
-
Parameters:
-
sakshDeleteBid(bidId, validatePermissionCallback): Deletes an existing bid.
-
Parameters:
-
bidId
: The ID of the bid. -
validatePermissionCallback
: A callback function to validate user permissions.
-
- Returns: An object containing the success status, message, and deleted bid data.
-
Parameters:
-
sakshEndAuction(carId, validatePermissionCallback): Ends the auction for a car.
-
Parameters:
-
carId
: The ID of the car. -
validatePermissionCallback
: A callback function to validate user permissions.
-
- Returns: An object containing the success status, message, and auction data.
-
Parameters:
-
sakshSelectWinner(carId, validatePermissionCallback, userId = null): Selects the winning bid for a car.
-
Parameters:
-
carId
: The ID of the car. -
validatePermissionCallback
: A callback function to validate user permissions. -
userId
: (Optional) The ID of the user to select as the winner.
-
- Returns: An object containing the success status, message, and winner data.
-
Parameters:
-
sakshGetHighestBid(carId): Fetches the highest bid for a car.
-
Parameters:
-
carId
: The ID of the car.
-
- Returns: An object containing the success status, message, and highest bid data.
-
Parameters:
const express = require('express');
const mongoose = require('mongoose');
const SakshCarAuction = require('./index');
const app = express();
const port = 3000;
// Middleware to parse JSON
app.use(express.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/auctionDB', {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('MongoDB connected'))
.catch(err => console.error('MongoDB connection error:', err));
// Example user IDs
const userId1 = 'user123';
const userId2 = 'user456';
const userId3 = 'user789';
// Create instances of SakshCarAuction for different users
const auction1 = new SakshCarAuction(userId1);
const auction2 = new SakshCarAuction(userId2);
const auction3 = new SakshCarAuction(userId3);
// Example callback function for bid validation
const validateBid = (carId, bidAmount) => {
return bidAmount > 0; // Example validation: bid amount must be greater than 0
};
// Example callback function for permission validation
const validatePermission = (userId, resourceId, action) => {
// Add your permission validation logic here
if (action === "updatebid" || action === "selectwinner" || action === "closebidding") {
return userId === 'user123'; // Example validation: only user123 has permission for these actions
}
return true;
};
// Route to place a bid
app.post('/placeBid', async (req, res) => {
const { carId, bidAmount, userId } = req.body;
const auction = new SakshCarAuction(userId);
const result = await auction.sakshPlaceBid(carId, bidAmount, validateBid);
res.json(result);
});
// Route to update a bid
app.put('/updateBid', async (req, res) => {
const { bidId, newBidAmount, userId } = req.body;
const auction = new SakshCarAuction(userId);
const result = await auction.sakshUpdateBid(bidId, newBidAmount, validatePermission);
res.json(result);
});
// Route to end the auction
app.post('/endAuction', async (req, res) => {
const { carId, userId } = req.body;
const auction = new SakshCarAuction(userId);
const result = await auction.sakshEndAuction(carId, validatePermission);
res.json(result);
});
// Route to select the winner
app.post('/selectWinner', async (req, res) => {
const { carId, userId } = req.body;
const auction = new SakshCarAuction(userId);
const result = await auction.sakshSelectWinner(carId, validatePermission);
res.json(result);
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
-
Start the Server:
node server.js
-
Place a Bid:
curl -X POST http://localhost:3000/placeBid -H "Content-Type: application/json" -d '{"carId": "66ebd87c0cab7b7737ecda1a", "bidAmount": 5000, "userId": "user123"}'
-
Update a Bid:
curl -X PUT http://localhost:3000/updateBid -H "Content-Type: application/json" -d '{"bidId": "bid123", "newBidAmount": 6000, "userId": "user123"}'
-
End the Auction:
curl -X POST http://localhost:3000/endAuction -H "Content-Type: application/json" -d '{"carId": "66ebd87c0cab7b7737ecda1a", "userId": "user123"}'
-
Select the Winner:
curl -X POST http://localhost:3000/selectWinner -H "Content-Type: application/json" -d '{"carId": "66ebd87c0cab7b7737ecda1a", "userId": "user123"}'
This example demonstrates how to integrate the SakshCarAuction
class into an Express.js application, providing routes for the main auction functionalities. Let me know if you need any further adjustments or additional features!
For any inquiries or support, please reach out to us at: susheel2339@gmail.com
This project is licensed under the MIT License.