Skip to content

Initialiese balances

Course: PayTM project - chapter 11

Update the signup endpoint to give the user a random balance between 1 and 10000.

This is so we don’t have to integrate with banks and give them random balances to start with.

Solution
router.post("/signup", async (req, res) => {
const { success } = signupBody.safeParse(req.body)
if (!success) {
return res.status(411).json({
message: "Email already taken / Incorrect inputs"
})
}
const existingUser = await User.findOne({
username: req.body.username
})
if (existingUser) {
return res.status(411).json({
message: "Email already taken/Incorrect inputs"
})
}
const user = await User.create({
username: req.body.username,
password: req.body.password,
firstName: req.body.firstName,
lastName: req.body.lastName,
})
const userId = user._id;
/// ----- Create new account ------
await Account.create({
userId,
balance: 1 + Math.random() * 10000
})
/// ----- ------
const token = jwt.sign({
userId
}, JWT_SECRET);
res.json({
message: "User created successfully",
token: token
})
})