DEV Community

Susheel kumar
Susheel kumar

Posted on

Building a Simple Wallet Management System with Mongoose and Saksh Easy Wallet

Managing User Wallets with Mongoose and Saksh Easy Wallet

In today's digital age, managing user transactions and balances efficiently is crucial for any application that deals with financial data. In this blog post, we will explore how to implement a simple wallet management system using Mongoose for MongoDB and the Saksh Easy Wallet library. This example will guide you through connecting to a MongoDB database, crediting and debiting user wallets, and retrieving balance information.

Prerequisites

Before we dive into the code, ensure you have the following set up:

  1. Node.js: Make sure you have Node.js installed on your machine.
  2. MongoDB: You should have a MongoDB instance running locally or remotely.
  3. NPM Packages: You will need to install mongoose and saksh-easy-wallet. You can do this by running:
   npm install mongoose saksh-easy-wallet
Enter fullscreen mode Exit fullscreen mode

Setting Up the Wallet Management System

Let's break down the code step by step.

1. Import Required Libraries

First, we need to import Mongoose and the Saksh Easy Wallet library.

const mongoose = require('mongoose');
const SakshWallet = require('saksh-easy-wallet');
Enter fullscreen mode Exit fullscreen mode

2. Connect to MongoDB

Next, we establish a connection to our MongoDB database. In this example, we are connecting to a database named saksh2323Cart.

await mongoose.connect('mongodb://localhost:27017/saksh2323Cart', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});
console.log('Database connected');
Enter fullscreen mode Exit fullscreen mode

3. Initialize the Wallet

We create an instance of the SakshWallet class, which will allow us to manage user wallets.

const wallet = new SakshWallet();
Enter fullscreen mode Exit fullscreen mode

4. Set Admin Email (Optional)

You can set an admin email for the wallet management system. This is optional but can be useful for tracking purposes.

wallet.setAdmin('admin@example.com');
Enter fullscreen mode Exit fullscreen mode

5. Credit an Amount to the User's Wallet

To credit an amount to a user's wallet, we use the sakshCredit method. In this example, we are crediting $500 to the user's wallet.

console.log(await wallet.sakshCredit(userEmail, 500, 'USD', 'ref123', 'Salary payment'));
Enter fullscreen mode Exit fullscreen mode

6. Debit an Amount from the User's Wallet

Similarly, we can debit an amount from the user's wallet using the sakshDebit method. Here, we are debiting $200 for grocery shopping.

console.log(await wallet.sakshDebit(userEmail, 200, 'USD', 'ref124', 'Grocery shopping', 5));
Enter fullscreen mode Exit fullscreen mode

7. Get User Balance

To retrieve the current balance of a user, we can use the sakshGetBalance method.

const balance = await wallet.sakshGetBalance(userEmail, 'USD');
console.log('User Balance:', balance);
Enter fullscreen mode Exit fullscreen mode

8. Get Balance Summary

For a more detailed overview, we can get a balance summary using the sakshGetBalanceSummary method.

const balanceSummary = await wallet.sakshGetBalanceSummary(userEmail);
console.log('Balance Summary:', balanceSummary);
Enter fullscreen mode Exit fullscreen mode

9. Close the MongoDB Connection

Finally, we should close the MongoDB connection to free up resources.

await mongoose.connection.close();
Enter fullscreen mode Exit fullscreen mode

Complete Code Example

Here’s the complete code wrapped in an asynchronous IIFE (Immediately Invoked Function Expression):

const mongoose = require('mongoose');
const SakshWallet = require('saksh-easy-wallet');

(async () => {
    try {
        const userEmail = 'user@example.com';

        await mongoose.connect('mongodb://localhost:27017/saksh2323Cart', {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
        console.log('Database connected');

        const wallet = new SakshWallet();
        wallet.setAdmin('admin@example.com');

        console.log(await wallet.sakshCredit(userEmail, 500, 'USD', 'ref123', 'Salary payment'));
        console.log(await wallet.sakshDebit(userEmail, 200, 'USD', 'ref124', 'Grocery shopping', 5));

        const balance = await wallet.sakshGetBalance(userEmail, 'USD');
        console.log('User Balance:', balance);

        const balanceSummary = await wallet.sakshGetBalanceSummary(userEmail);
        console.log('Balance Summary:', balanceSummary);

        await mongoose.connection.close();
    } catch (error) {
        console.error('Error:', error);
    }
})();
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this blog post, we demonstrated how to create a simple wallet management system using Mongoose

Top comments (0)