-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
150 lines (104 loc) · 3.79 KB
/
handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const serverless = require("serverless-http");
const express = require("express");
const mongoose = require("mongoose");
const { v4: uuidv4 } = require('uuid');
const casinoID = "000000000000000000"
const Handler = require('./mongo/mongo-handler');
const handler = new Handler()
const Verifier = require('./src/security')
const verifier = new Verifier()
const Logger = require('./src/logger')
const logger = new Logger()
const MONGO_URL = process.env.MONGO_URL
const app = express();
async function dbConnect() {
mongoose.connect(MONGO_URL, {
"auth": { "authSource": "cookies" },
"user": process.env.MONGO_USER,
"pass": process.env.MONGO_PASS
})
.then(() => {
console.log('Connected to remote DB')
})
}
dbConnect().then(async () => {
app.get("/", (req, res) => {
return res.status(200).json({
message: "whats up :)",
});
});
app.post("/payuser/:id&:amount&:key", async (req, res) => {
let userID = req.params.id
let amount = Number(req.params.amount)
let key = req.params.key
let userExists = await handler.checkExist(userID)
let keyExists = await verifier.verifySecretKey(key)
if (!keyExists) return res.status(403).json({ result: false, message: "bad key" })
if (!userExists) return res.status(404).json({ result: false, message: "user not found" })
if (isNaN(amount)) return res.status(400).json({ result: false, message: "amount value should be a number" })
let userData = await handler.fetchData(userID)
try {
handler.payUser(casinoID, userID, amount).then((result) => {
const transactionID = uuidv4();
logger.log("casino", amount, userID, transactionID, Math.floor(Date.now() / 100))
if (result === true) return res.status(200).json({ result: true, from: "casino", to: userData.userTag, amount: amount, transactionID: transactionID });
})
}
catch (e) {
console.error(e)
}
});
app.post("/chargeuser/:id&:amount&:key", async (req, res) => {
let userID = req.params.id
let amount = Number(req.params.amount)
let key = req.params.key
let userExists = await handler.checkExist(userID)
let keyExists = await verifier.verifySecretKey(key)
if (!keyExists) return res.status(403).json({ result: false, message: "bad key" })
if (!userExists) return res.status(404).json({ result: false, message: "user not found" })
if (isNaN(amount)) return res.status(400).json({ result: false, message: "amount value should be a number" })
let userData = await handler.fetchData(userID)
try {
handler.payUser(userID, casinoID, amount).then((result) => {
const transactionID = uuidv4();
logger.log(userID, amount, "casino", transactionID, Math.floor(Date.now() / 100))
if (result === true) return res.status(200).json({ result: true, from: "casino", to: userData.userTag, amount: amount, transactionID: transactionID });
})
}
catch (e) {
console.error(e)
}
});
app.get("/balance/:id", async (req, res) => {
if (!handler.checkExist(req.params.id)) return res.status(404).json({
message: "user not found in database"
})
try {
await handler.fetchData(req.params.id).then((resObject) => {
console.log(resObject)
try {
return res.status(200).json({
balance: resObject.balance,
userTag: resObject.userTag,
isBanned: resObject.isBanned
});
}
catch (e) {
console.error(e)
return res.status(500).json({
message: "sorry i fucked up :("
})
}
})
}
catch (e) {
console.error(e)
}
});
app.use((req, res) => {
return res.status(404).json({
error: "Not Found",
});
});
})
module.exports.handler = serverless(app);