npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saksh-car-auction

v1.0.3

Published

A Node.js service for managing car auctions, including placing bids, ending auctions, selecting winners, and generating reports.

Readme

saksh-car-auction

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.

Features

  • 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.

Benefits

  • 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.

Installation

npm i saksh-car-auction

Usage

Example Usage

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();

Functions

  • 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.
  • 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.
  • 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.
  • 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.
  • 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.
  • 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.

Server Code

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}`);
});

Usage

  1. Start the Server:

    node server.js
  2. Place a Bid:

    curl -X POST http://localhost:3000/placeBid -H "Content-Type: application/json" -d '{"carId": "66ebd87c0cab7b7737ecda1a", "bidAmount": 5000, "userId": "user123"}'
  3. Update a Bid:

    curl -X PUT http://localhost:3000/updateBid -H "Content-Type: application/json" -d '{"bidId": "bid123", "newBidAmount": 6000, "userId": "user123"}'
  4. End the Auction:

    curl -X POST http://localhost:3000/endAuction -H "Content-Type: application/json" -d '{"carId": "66ebd87c0cab7b7737ecda1a", "userId": "user123"}'
  5. 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!

Contact Us

For any inquiries or support, please reach out to us at: [email protected]

License

This project is licensed under the MIT License.