-
Notifications
You must be signed in to change notification settings - Fork 0
/
userRoute.js
168 lines (127 loc) · 5.6 KB
/
userRoute.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const moment = require('moment')
const ObjectId = require('mongodb').ObjectId;
module.exports = routes = async (fastify, options) => {
const collection = fastify.mongo.db.collection("users")
const transcollection = fastify.mongo.db.collection("transactions")
fastify.get('/user/all', async (request, reply) => {
const result = await collection.find({}).toArray()
if (result.length == 0) {
throw new Error('No documents found')
}
return result
})
fastify.post('/user/transaction/:id', async (request, reply) => {
const { id } = request.params;
const result = await transcollection.find({ user_id: ObjectId(id) }).toArray()
if (!result) {
throw new Error('Invalid value')
}
console.log(result)
return result
})
const userBodyJsonSchema = {
type: "object",
required: ['email', 'password'],
properties: {
firstname: { type: "string" },
lastname: { type: "string" },
username: { type: "string" },
email: { type: "string" },
password: { type: "string" }
},
}
const schema = {
body: userBodyJsonSchema
}
// btc_wallet: password.current.value,
// eth_wallet: eth_wallet.current.value,
// crt_wallet: crt_wallet.current.value,
// ltc_wallet: ltc_wallet.current.value,
// bch_wallet: bch_wallet.current.value,
// dash_wallet: dash_wallet.current.value,
// doge_wallet: doge_wallet.current.value,
// zcash_wallet: zcash_wallet.current.value,
// monero_wallet: monero_wallet.current.value,
// etc_wallet: etc_wallet.current.value,
fastify.post('/sign-up', { schema }, async (request, reply) => {
const { firstname, lastname, username, email, password } = request.body
const oldOne = await collection.findOne({ email: email })
if (oldOne) {
throw new Error('Signup-Error: account already exists')
}
const result = await collection.insertOne({ firstname, lastname, username, email, password })
const token = fastify.jwt.sign({ "id": result.insertedId, "email": email, "username": username })
return reply.send({ token });
})
fastify.post('/update-profile/:id', async (request, reply) => {
const { id } = request.params;
const { firstname, lastname, username, email, password, btc_wallet, eth_wallet, crt_wallet, ltc_wallet, bch_wallet, dash_wallet, doge_wallet, zcash_wallet, monero_wallet, etc_wallet } = request.body
const oldOne = await collection.findOne({ _id: ObjectId(id) })
if (!oldOne) {
throw new Error('update-profile: account not exist')
}
const result = await collection.updateOne({ _id: ObjectId(id) }, { $set: { firstname, lastname, username, email, password, btc_wallet, eth_wallet, crt_wallet, ltc_wallet, bch_wallet, dash_wallet, doge_wallet, zcash_wallet, monero_wallet, etc_wallet } })
return reply.send({ success: true });
})
fastify.post('/user/profile/:id', async (request, reply) => {
const { id } = request.params;
console.log(id)
const oldOne = await collection.findOne({ _id: ObjectId(id) })
if (!oldOne) {
throw new Error('Profile-Error: profile not found')
}
return reply.send(oldOne);
})
fastify.post('/sign-in', { schema }, async (request, reply) => {
const { email, password } = request.body
const result = await collection.findOne({ email: email })
if (!result) {
throw new Error("ERROR-Login: No user with that credential found!")
}
console.log(result._id);
if (result.password != password) {
throw new Error("ERROR-Login: Password not matched!")
}
const token = fastify.jwt.sign({ "id": result._id, "email": email, "username": result.username })
console.log(token)
reply.send({ token })
})
fastify.post('/verify-token', async (request, reply) => {
try {
await request.jwtVerify()
reply.send({ login: "success" })
} catch (err) {
throw new Error('Error: Invalid Authentication Token')
}
})
fastify.post('/user/create-order', async (request, reply) => {
const { email, payType, payAmount } = request.body;
const user = await collection.findOne({ email: email })
if (!user) {
throw new Error('Invalid user')
}
const result = await transcollection.insertOne({ user_id: user._id, payType, payAmount, confirmed: false, active: false, created_at: moment().format('YYYY-MM-D h:mm:s'), confirmed_at: null, status: true, power: 660.07 })
if (!result) {
throw new Error('Invalid transaction')
}
return reply.send({ ordered: "success", id: result.insertedId })
})
fastify.post('/transaction/confirm/:id', async (request, reply) => {
const { id } = request.params;
const result = await transcollection.updateOne({ "_id": ObjectId(id) }, { $set: { confirmed: true, confirmed_at: moment().format('YYYY-MM-D h:mm:s') } })
console.log(result)
if (!result) {
throw new Error('TR-Update: Database Error')
}
return result
})
fastify.get('/transaction/:id', async (request, reply) => {
const { id } = request.params;
const result = await transcollection.findOne({ "_id": ObjectId(id) })
console.log(result)
if (!result) {
throw new Error('Invalid value')
}
return result
})
}