DEV Community

Susheel kumar
Susheel kumar

Posted on

Introducing SakshCart: A Comprehensive Node.js Shopping Cart Solution

SakshCart

SakshCart is a Node.js package that provides a comprehensive shopping cart service with various functionalities such as adding items to the cart, viewing the cart, removing items, clearing the cart, checking out, validating coupons, and generating various reports. It also includes wishlist functionality.

Features

  • Add items to the cart
  • View cart items
  • Remove items from the cart
  • Clear the cart
  • Checkout with check payment
  • Validate coupons
  • Generate sales reports
  • Generate user reports
  • Generate monthly sales reports
  • Generate top-selling products reports
  • Generate user order count reports
  • Add items to the wishlist
  • View wishlist items
  • Remove items from the wishlist

Installation

Install the package via npm:

npm install saksh-cart
Enter fullscreen mode Exit fullscreen mode

Usage

Setting Up

Import the package:


const express = require('express');
const mongoose = require('mongoose');

// Import services from the local index file
const { SakshCartService, SakshCheckoutService, SakshCouponService, SakshOrderService, SakshWishlistService, SakshReportingService } = require('saksh-cart');

const app = express();
const PORT = process.env.PORT || 5000;

// Middleware
app.use(express.json());

// Simulate logged-in user
const loggedInUserId = 'user123';

// Connect to the database
mongoose.connect('mongodb://localhost:27017/sakshCart', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
}).then(() => {
    console.log('Database connected');
}).catch(err => {
    console.error('Database connection error', err);
});

// Initialize services
const cartService = new SakshCartService();
const checkoutService = new SakshCheckoutService();
const couponService = new SakshCouponService();
const orderService = new SakshOrderService();
const reportingService = new SakshReportingService();
const wishlistService = new SakshWishlistService();
// Routes

// Add item to cart
app.post('/api/cart/add', async (req, res) => {
    try {
        const { cartData } = req.body;
        const items = await cartService.addToCart(loggedInUserId, cartData);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// View cart
app.get('/api/cart', async (req, res) => {
    try {
        const items = await cartService.viewCart(loggedInUserId);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Remove item from cart
app.delete('/api/cart/remove', async (req, res) => {
    try {
        const { itemId } = req.body;
        const items = await cartService.removeFromCart(loggedInUserId, itemId);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Clear cart
app.delete('/api/cart/clear', async (req, res) => {
    try {
        const items = await cartService.clearCart(loggedInUserId);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Checkout with check payment
app.post('/api/checkout', async (req, res) => {
    try {
        const { couponCode } = req.body;
        const result = await checkoutService.sakshProcessCheckoutWithCheck(loggedInUserId, couponCode);
        res.status(200).json(result);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Validate coupon
app.post('/api/coupon/validate', async (req, res) => {
    try {
        const { couponCode } = req.body;
        const coupon = await couponService.sakshValidateCoupon(couponCode);
        res.status(200).json(coupon);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Generate user report
app.get('/api/reporting/user', async (req, res) => {
    try {
        const report = await reportingService.sakshGenerateUserReport(loggedInUserId);
        res.status(200).json(report);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Generate monthly sales report
app.get('/api/reporting/monthly-sales', async (req, res) => {
    try {
        const report = await reportingService.sakshGenerateMonthlySalesReport();
        res.status(200).json(report);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Generate top selling products report
app.get('/api/reporting/top-selling-products', async (req, res) => {
    try {
        const report = await reportingService.sakshGenerateTopSellingProductsReport();
        res.status(200).json(report);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Generate user order count report
app.get('/api/reporting/user-order-count', async (req, res) => {
    try {
        const report = await reportingService.sakshGenerateUserOrderCountReport();
        res.status(200).json(report);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Wishlist routes

// Add item to wishlist
app.post('/api/wishlist/add', async (req, res) => {
    try {
        const { itemId } = req.body;
        const items = await wishlistService.addToWishlist(loggedInUserId, itemId);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// View wishlist
app.get('/api/wishlist', async (req, res) => {
    try {
        const items = await wishlistService.viewWishlist(loggedInUserId);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

// Remove item from wishlist
app.delete('/api/wishlist/remove', async (req, res) => {
    try {
        const { itemId } = req.body;
        const items = await wishlistService.removeFromWishlist(loggedInUserId, itemId);
        res.status(200).json(items);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});


// Error handling middleware
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

// Start the server
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

Enter fullscreen mode Exit fullscreen mode

API Endpoints

Add Item to Cart

curl -X POST http://localhost:5000/api/cart/add \
-H "Content-Type: application/json" \
-d '{
    "cartData": {
        "itemId": "item456",
        "quantity": 2,
        "price": 100,
        "sellerId": "seller789",
        "shippingPrice": 10
    }
}'
Enter fullscreen mode Exit fullscreen mode

View Cart

curl -X GET http://localhost:5000/api/cart
Enter fullscreen mode Exit fullscreen mode

Remove Item from Cart

curl -X DELETE http://localhost:5000/api/cart/remove \
-H "Content-Type: application/json" \
-d '{
    "itemId": "item456"
}'
Enter fullscreen mode Exit fullscreen mode

Clear Cart

curl -X DELETE http://localhost:5000/api/cart/clear
Enter fullscreen mode Exit fullscreen mode

Checkout with Check Payment

curl -X POST http://localhost:5000/api/checkout \
-H "Content-Type: application/json" \
-d '{
    "couponCode": "DISCOUNT10"
}'
Enter fullscreen mode Exit fullscreen mode

Validate Coupon

curl -X POST http://localhost:5000/api/coupon/validate \
-H "Content-Type: application/json" \
-d '{
    "couponCode": "DISCOUNT10"
}'
Enter fullscreen mode Exit fullscreen mode

Generate User Report

curl -X GET http://localhost:5000/api/reporting/user
Enter fullscreen mode Exit fullscreen mode

Generate Monthly Sales Report

curl -X GET http://localhost:5000/api/reporting/monthly-sales
Enter fullscreen mode Exit fullscreen mode

Generate Top Selling Products Report

curl -X GET http://localhost:5000/api/reporting/top-selling-products
Enter fullscreen mode Exit fullscreen mode

Generate User Order Count Report

curl -X GET http://localhost:5000/api/reporting/user-order-count
Enter fullscreen mode Exit fullscreen mode

Wishlist Endpoints

Add Item to Wishlist

curl -X POST http://localhost:5000/api/wishlist/add \
-H "Content-Type: application/json" \
-d '{
    "itemId": "item456"
}'
Enter fullscreen mode Exit fullscreen mode

View Wishlist

curl -X GET http://localhost:5000/api/wishlist
Enter fullscreen mode Exit fullscreen mode

Remove Item from Wishlist

curl -X DELETE http://localhost:5000/api/wishlist/remove \
-H "Content-Type: application/json" \
-d '{
    "itemId": "item456"
}'
Enter fullscreen mode Exit fullscreen mode

License

This project is licensed under the MIT License. See the LICENSE file for details.


Top comments (0)