Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: add type to transactions #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions brisk-api/controllers/TransactionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class TransactionController {
console.error('Error fetching:', error);
}
data.userId = user._id;
data.type = 'Received BTC';
const invoice = await dbClient.addInvoice(data);
return res.status(201).json(invoice);
}
Expand All @@ -130,16 +131,16 @@ class TransactionController {
if (satAmount > user.balanceSat) { return res.status(400).send({ error: 'Unsufiscient BTC balance' }); }
const update1 = await dbClient.updateUser({ _id: ObjectId(user._id) }, { $inc : { balanceFiat: +fiatAmount } });
const update2 = await dbClient.updateUser({ _id: ObjectId(user._id) }, { $inc : { balanceSat: -satAmount } });
if (update1.matchedCount && update2.matchedCount) { return res.status(200).send({ message: 'Update Successful' }); }
await dbClient.addInvoice({ id: 234, amount: satAmount, createdTime: 12345567, status: 'settled', checkoutLink: 'none' });
return res.status(400).send({ message: 'Update Failed' });
await dbClient.addInvoice({ id: 234, userId: user._id, type: 'Conversion Sat to Fiat', amount: satAmount, createdTime: 12345567, status: 'settled', checkoutLink: 'none' });
if (update1.matchedCount && update2.matchedCount) { return res.status(200).send({ message: 'Conversion Successful' }); }
return res.status(400).send({ message: 'Conversion Failed' });
} else if ( type === 'fiattosat') {
if (fiatAmount > user.balanceFiat) { return res.status(400).send({ message: 'Unsufiscient Fiat balance' }); }
const update1 = await dbClient.updateUser({ _id: ObjectId(user._id) }, { $inc : { balanceFiat: -fiatAmount } });
const update2 = await dbClient.updateUser({ _id: ObjectId(user._id) }, { $inc : { balanceSat: +satAmount } });
if (update1.matchedCount && update2.matchedCount) { return res.status(200).send({ message: 'Update Successful' }); }
await dbClient.addInvoice({ id: 234, amount: satAmount, createdTime: 12345567, status: 'settled', checkoutLink: 'none' });
return res.status(400).send({ message: 'Update Failed' });
await dbClient.addInvoice({ id: 234, userId: user._id, type: 'Conversion Fiat to Sat', amount: satAmount, createdTime: 12345567, status: 'settled', checkoutLink: 'none' });
if (update1.matchedCount && update2.matchedCount) { return res.status(200).send({ message: 'Conversion Successful' }); }
return res.status(400).send({ message: 'Conversion Failed' });
}
return res.status(400).send({ error: 'Bad Operation' });
}
Expand Down

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions brisk-api/routes/transctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ transactionsRouter.post('/transactions', (req, res) => {
* description: Invoice created successfully
* 400:
* description: Invoice not created
* 401:
* description: Unthorized
*/
transactionsRouter.post('/invoices', (req, res) => {
TransactionController.createInvoice(req, res);
Expand Down Expand Up @@ -115,6 +117,8 @@ transactionsRouter.get('/rates', (req, res) => {
* description: Invoice list retrived successfully
* 400:
* description: Failed
* 401:
* description: Unthorized
*/
transactionsRouter.get('/invoices', (req, res) => {
TransactionController.getInvoices(req, res);
Expand All @@ -134,6 +138,8 @@ transactionsRouter.get('/invoices', (req, res) => {
* description: Invoice retrived successfully
* 400:
* description: Failed
* 401:
* description: Unthorized
*/
transactionsRouter.get('/invoices/:id', (req, res) => {
TransactionController.getInvoice(req, res);
Expand Down
2 changes: 2 additions & 0 deletions brisk-api/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ usersRouter.get('/users/me', (req, res) => {
* description: The user's username
* 400:
* description: Bad request
* 401:
* description: Unthorized
*/
usersRouter.post('/users', (req, res) => {
UserController.postNew(req, res);
Expand Down