-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from SiebeBaree/development
v4.1
- Loading branch information
Showing
59 changed files
with
2,909 additions
and
479 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Hono } from 'hono'; | ||
import vote from './vote'; | ||
import member from './member'; | ||
|
||
const app = new Hono(); | ||
|
||
app.route('/vote', vote); | ||
app.route('/member', member); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Hono } from 'hono'; | ||
import Member from '../schemas/member'; | ||
|
||
const app = new Hono(); | ||
|
||
type InvestmentBody = { | ||
type: 'BUY' | 'SELL'; | ||
userId: string; | ||
ticker: string; | ||
amount: number; | ||
price: number; | ||
}; | ||
|
||
app.put('/investment', async (c) => { | ||
const authKey = c.req.header('Authorization'); | ||
if (authKey !== process.env.TOKEN!) { | ||
return c.json({ error: 'Invalid auth key' }, 401); | ||
} | ||
|
||
const body: InvestmentBody = await c.req.json(); | ||
if (!['BUY', 'SELL'].includes(body.type)) { | ||
return c.json({ error: 'Invalid type' }, 400); | ||
} | ||
|
||
const member = await Member.findOne({ id: body.userId }); | ||
if (!member) { | ||
return c.json({ error: 'Member not found' }, 404); | ||
} | ||
|
||
if (member.premium < 2) { | ||
return c.json( | ||
{ | ||
error: 'You need Coinz Pro to use the web interface. Please upgrade your account.', | ||
}, | ||
403, | ||
); | ||
} | ||
|
||
const ownedInvestment = member.investments.find((i) => i.ticker === body.ticker); | ||
if (body.type === 'BUY') { | ||
console.log(`${body.userId} bought ${body.amount} of ${body.ticker} for ${body.price}`); | ||
if (ownedInvestment) { | ||
await Member.updateOne( | ||
{ id: member.id, 'investments.ticker': body.ticker }, | ||
{ | ||
$set: { | ||
'investments.$.amount': `${Number.parseFloat(ownedInvestment.amount) + body.amount}`, | ||
}, | ||
$inc: { | ||
'investments.$.buyPrice': Math.round(body.price), | ||
}, | ||
}, | ||
); | ||
} else { | ||
await Member.updateOne( | ||
{ id: member.id }, | ||
{ | ||
$push: { | ||
investments: { | ||
ticker: body.ticker, | ||
amount: body.amount, | ||
buyPrice: body.price, | ||
}, | ||
}, | ||
}, | ||
); | ||
} | ||
} else { | ||
if (!ownedInvestment) { | ||
return c.json({ error: 'Investment not found' }, 404); | ||
} | ||
|
||
console.log(`${body.userId} sold ${body.amount} of ${body.ticker} for ${body.price}`); | ||
const ownedAmount = Number.parseFloat(ownedInvestment.amount); | ||
if (Number.parseFloat(ownedInvestment.amount) <= body.amount || ownedAmount - body.amount < 0.00001) { | ||
await Member.updateOne( | ||
{ id: member.id }, | ||
{ | ||
$pull: { investments: { ticker: ownedInvestment.ticker } }, | ||
}, | ||
); | ||
} else { | ||
await Member.updateOne( | ||
{ id: member.id, 'investments.ticker': body.ticker }, | ||
{ | ||
$set: { | ||
'investments.$.amount': `${ownedAmount - body.amount}`, | ||
}, | ||
$inc: { | ||
'investments.$.buyPrice': -Math.floor((body.amount / ownedAmount) * ownedInvestment.buyPrice), | ||
}, | ||
}, | ||
); | ||
} | ||
} | ||
|
||
return c.json({ success: true }, 200); | ||
}); | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.