DEV Community

Susheel kumar
Susheel kumar

Posted on

Full example of saksh-wallet class

You can download saksh-wallet [ https://www.npmjs.com/package/saksh-wallet
] class and use in your node js package

npm i saksh-wallet
Enter fullscreen mode Exit fullscreen mode

here is a complete use of this class. how to use the SakshWallet class, including user management, transaction management, budget management, recurring payments, standard reporting, and AI-enhanced reporting functionalities.

Full Example

const SakshWallet = require('./SakshWallet');
const wallet = new SakshWallet();

async function fullExample() {
    // User Management
    const adminUserId = 'admin123';
    await wallet.sakshSetAdmin(adminUserId);
    console.log('Admin set successfully.');

    const userId = 'user123';
    await wallet.sakshSetLimit(false);
    console.log('Limit set successfully.');

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

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

    // Transaction Management
    const creditTransaction = await wallet.sakshCredit(userId, 100, 'USD', 'Salary', 'ref123', 'Income');
    console.log('Credit Transaction:', creditTransaction);

    const debitTransaction = await wallet.sakshDebit(userId, 50, 'USD', 'Grocery Shopping', 'ref124', 'Expense');
    console.log('Debit Transaction:', debitTransaction);

    const transferTransaction = await wallet.sakshTransferFunds(userId, 'user456', 20, 'USD', 'Gift', 'ref125', 'Transfer');
    console.log('Transfer Transaction:', transferTransaction);

    const transactionReport = await wallet.sakshGetTransactionReport(userId);
    console.log('Transaction Report:', transactionReport);

    const reversedTransaction = await wallet.sakshReverseTransaction(creditTransaction._id, null);
    console.log('Reversed Transaction:', reversedTransaction);

    const reversedMultipleTransactions = await wallet.sakshReverseMultipleTransactions([debitTransaction._id, transferTransaction._id]);
    console.log('Reversed Multiple Transactions:', reversedMultipleTransactions);

    // Budget Management
    const budget = await wallet.sakshSetBudget(userId, 'Food', 500, 'Monthly');
    console.log('Budget Set:', budget);

    const adjustedBudget = await wallet.sakshAdjustBudget(userId, 'Food', 600);
    console.log('Budget Adjusted:', adjustedBudget);

    const isBudgetAllowed = await wallet.sakshCheckBudget(userId, 'Food', 50);
    console.log('Is Budget Allowed:', isBudgetAllowed);

    const budgetStatus = await wallet.sakshGetBudgetStatus(userId, 'Food');
    console.log('Budget Status:', budgetStatus);

    // Recurring Payments
    const recurringPayment = await wallet.sakshCreateRecurringPayment(userId, 'user456', 100, 'USD', 'Subscription', 'ref126', 'Monthly');
    console.log('Recurring Payment Created:', recurringPayment);

    const updatedRecurringPayment = await wallet.sakshUpdateRecurringPayment(recurringPayment._id, { amount: 120 });
    console.log('Recurring Payment Updated:', updatedRecurringPayment);

    const deletedRecurringPayment = await wallet.sakshDeleteRecurringPayment(recurringPayment._id);
    console.log('Recurring Payment Deleted:', deletedRecurringPayment);

    await wallet.sakshProcessDuePayments();
    console.log('Due Payments Processed.');

    // Standard Reporting
    const monthlyReport = await wallet.sakshGetMonthlyTransactionReport(userId, 2024, 8);
    console.log('Monthly Transaction Report:', monthlyReport);

    const dailyReport = await wallet.sakshGetDailyTransactionReport(userId, new Date('2024-08-15'));
    console.log('Daily Transaction Report:', dailyReport);

    const yearlyReport = await wallet.sakshGetYearlyTransactionReport(userId, 2024);
    console.log('Yearly Transaction Report:', yearlyReport);

    const transactionsByCategory = await wallet.sakshGetTransactionsByCategory(userId, 'Food');
    console.log('Transactions by Category:', transactionsByCategory);

    const spendingSummary = await wallet.sakshGetSpendingSummary(userId, new Date('2024-08-01'), new Date('2024-08-31'));
    console.log('Spending Summary:', spendingSummary);

    const transactionCount = await wallet.sakshGetTransactionCount(userId);
    console.log('Transaction Count:', transactionCount);

    const averageTransactionAmount = await wallet.sakshGetAverageTransactionAmount(userId, new Date('2024-08-01'), new Date('2024-08-31'));
    console.log('Average Transaction Amount:', averageTransactionAmount);

    const largestTransaction = await wallet.sakshGetLargestTransaction(userId);
    console.log('Largest Transaction:', largestTransaction);

    const smallestTransaction = await wallet.sakshGetSmallestTransaction(userId);
    console.log('Smallest Transaction:', smallestTransaction);

    const transactionsByPaymentMethod = await wallet.sakshGetTransactionsByPaymentMethod(userId, 'Credit Card');
    console.log('Transactions by Payment Method:', transactionsByPaymentMethod);

    const balanceByCurrency = await wallet.sakshGetBalanceByCurrency(userId);
    console.log('Balance by Currency:', balanceByCurrency);

    const allUsersBalanceByCurrency = await wallet.sakshGetAllUsersBalanceByCurrency();
    console.log('All Users Balance by Currency:', allUsersBalanceByCurrency);

    const allUsersBalanceByCategory = await wallet.sakshGetAllUsersBalanceByCategory();
    console.log('All Users Balance by Category:', allUsersBalanceByCategory);

    const allUsersBalanceAtDate = await wallet.sakshGetAllUsersBalanceAtDate(new Date('2024-08-31'));
    console.log('All Users Balance at Date:', allUsersBalanceAtDate);

    // AI Reporting
    const geminiAIKey = 'your-gemini-ai-key';
    const aiModel = 'text-davinci-003';
    const maxTokens = 150;
    await wallet.sakshGeminiAIKey(geminiAIKey, aiModel, maxTokens);
    console.log('AI key set successfully.');

    const aiReportSummary = await wallet.sakshGenerateReportSummary(transactionsByCategory);
    console.log('AI Report Summary:', aiReportSummary);

    const userQueryResult = await wallet.sakshHandleUserQuery('Show me all transactions for user123 in August 2024');
    console.log('User Query Result:', userQueryResult);

    const aiReport = await wallet.sakshGenerateReport('Generate a monthly transaction report for user123 for August 2024', userId);
    console.log('AI Generated Report:', aiReport);
}

fullExample().catch(console.error);
Enter fullscreen mode Exit fullscreen mode

Explanation

  • User Management:

    • Set an admin user.
    • Set a limit for the wallet.
    • Get the balance and balance summary for a user.
  • Transaction Management:

    • Credit and debit transactions.
    • Transfer funds between users.
    • Get a transaction report.
    • Reverse transactions.
  • Budget Management:

    • Set and adjust budgets.
    • Check if a budget allows for a specific amount.
    • Get the status of a budget.
  • Recurring Payments:

    • Create, update, and delete recurring payments.
    • Process due payments.
  • Standard Reporting:

    • Generate various types of transaction reports (monthly, daily, yearly).
    • Get transactions by category, spending summary, transaction count, average transaction amount, largest and smallest transactions, transactions by payment method, balance by currency, and all users' balance by currency and category.
  • AI Reporting:

    • Set the Gemini AI key.
    • Generate a report summary using AI.
    • Handle user queries using AI.
    • Generate comprehensive reports using AI.

This example covers all the methods provided by the SakshWallet class, demonstrating how to use each functionality. Let me know if you need any further adjustments or additional information!

Top comments (0)