-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathuseractions.js
More file actions
72 lines (49 loc) · 2.02 KB
/
useractions.js
File metadata and controls
72 lines (49 loc) · 2.02 KB
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
"use server"
import Razorpay from "razorpay"
import Payment from "@/models/Payment"
import connectDb from "@/db/connectDb"
import User from "@/models/User"
export const initiate = async (amount, to_username, paymentform) => {
await connectDb()
// fetch the secret of the user who is getting the payment
let user = await User.findOne({ username: to_username })
const secret = user.razorpaysecret
var instance = new Razorpay({ key_id: user.razorpayid, key_secret: secret })
let options = {
amount: Number.parseInt(amount),
currency: "INR",
}
let x = await instance.orders.create(options)
// create a payment object which shows a pending payment in the database
await Payment.create({ oid: x.id, amount: amount / 100, to_user: to_username, name: paymentform.name, message: paymentform.message })
return x
}
export const fetchuser = async (username) => {
await connectDb()
let u = await User.findOne({ username: username })
let user = u.toObject({ flattenObjectIds: true })
return user
}
export const fetchpayments = async (username) => {
await connectDb()
// find all payments sorted by decreasing order of amount and flatten object ids
let p = await Payment.find({ to_user: username, done: true }).sort({ amount: -1 }).limit(10).lean()
return JSON.parse(JSON.stringify(p))
}
export const updateProfile = async (data, oldusername) => {
await connectDb()
let ndata = Object.fromEntries(data)
// If the username is being updated, check if username is available
if (oldusername !== ndata.username) {
let u = await User.findOne({ username: ndata.username })
if (u) {
return { error: "Username already exists" }
}
await User.updateOne({ email: ndata.email }, ndata)
// Now update all the usernames in the Payments table
await Payment.updateMany({ to_user: oldusername }, { to_user: ndata.username })
}
else {
await User.updateOne({ email: ndata.email }, ndata)
}
}