diff --git a/package-lock.json b/package-lock.json index 9f7c7a6..731f732 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3175,6 +3175,7 @@ "version": "0.2.3", "resolved": "https://registry.npmjs.org/hpp/-/hpp-0.2.3.tgz", "integrity": "sha512-4zDZypjQcxK/8pfFNR7jaON7zEUpXZxz4viyFmqjb3kWNWAHsLEUmWXcdn25c5l76ISvnD6hbOGO97cXUI3Ryw==", + "license": "ISC", "dependencies": { "lodash": "^4.17.12", "type-is": "^1.6.12" diff --git a/src/controllers/wallet.js b/src/controllers/wallet.js index b1617fb..6bd15b3 100644 --- a/src/controllers/wallet.js +++ b/src/controllers/wallet.js @@ -114,3 +114,88 @@ export const createWallet = async (req, res) => { } }; +// Get all wallets +export const getAllWallets = async (req, res) => { + try { + if (!BITNOB_API_KEY) { + return res.status(500).json({ + status: false, + message: 'API key is not configured' + }); + } + + const response = await axios({ + method: 'GET', + url: `${BITNOB_API_URL}/wallets`, + headers: { + 'accept': 'application/json', + 'Authorization': `Bearer ${BITNOB_API_KEY.trim()}` + } + }); + + return res.status(200).json(response.data); + } catch (error) { + console.error('Error fetching wallets:', error.message); + + if (error.response) { + return res.status(error.response.status).json({ + status: false, + message: error.response.data?.message || 'Failed to fetch wallets', + details: error.response.data + }); + } + + return res.status(500).json({ + status: false, + message: 'Error fetching wallets' + }); + } +}; + +// Get wallet by coin +export const getWalletByCoin = async (req, res) => { + try { + const { coin } = req.params; + + if (!coin || !['trx', 'bnb'].includes(coin)) { + return res.status(400).json({ + status: false, + message: 'Invalid coin type. Must be either "trx" or "bnb"' + }); + } + + if (!BITNOB_API_KEY) { + return res.status(500).json({ + status: false, + message: 'API key is not configured' + }); + } + + const response = await axios({ + method: 'GET', + url: `${BITNOB_API_URL}/wallets/crypto-wallet/${coin}`, + headers: { + 'accept': 'application/json', + 'Authorization': `Bearer ${BITNOB_API_KEY.trim()}` + } + }); + + return res.status(200).json(response.data); + } catch (error) { + console.error('Error fetching wallet:', error.message); + + if (error.response) { + return res.status(error.response.status).json({ + status: false, + message: error.response.data?.message || 'Failed to fetch wallet', + details: error.response.data + }); + } + + return res.status(500).json({ + status: false, + message: 'Error fetching wallet' + }); + } +}; + diff --git a/src/index.js b/src/index.js index 8e782e2..c5518a5 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ import hpp from 'hpp'; import helmet from 'helmet'; import cors from 'cors'; import compression from 'compression'; -import { createWallet } from './controllers/wallet.js'; +import walletRoute from './routes/walletRoute.js'; import { config } from './configs/config.env.js'; import { customersRouter } from './routes/customersRoutes.js'; import { BaseError } from './errors/errors.js'; @@ -29,7 +29,7 @@ app.use(compression()); app.use(json({ limit: '200mb' })); app.use(urlencoded({ extended: true, limit: '200mb' })); -createWallet(); // Example usage of createWallet function +app.use('/api/wallet', walletRoute); app.use(`${BASE_URL}/customers`, customersRouter); @@ -47,7 +47,7 @@ const start = async () => { console.log('Connected to postgres DB'); // Sync all table models - await sequelize.sync({alter: true, force: true}) + await sequelize.sync({alter: true}) console.log("Models synchronized"); // Start the server diff --git a/src/routes/wallet.js b/src/routes/wallet.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/routes/walletRoute.js b/src/routes/walletRoute.js index 8a2dc81..a76aef9 100644 --- a/src/routes/walletRoute.js +++ b/src/routes/walletRoute.js @@ -1,8 +1,15 @@ import express from 'express'; -import { createWallet } from '../controllers/wallet.js'; +import { createWallet, getAllWallets, getWalletByCoin } from '../controllers/wallet.js'; const router = express.Router(); +// Create wallet router.post('/create', createWallet); +// List all wallets +router.get('/', getAllWallets); + +// Get wallet by Coin +router.get('/coin/:coin', getWalletByCoin); + export default router; \ No newline at end of file