generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathwallets.js
73 lines (64 loc) · 1.89 KB
/
wallets.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
const { fetchWallet, createWallet } = require("../models/wallets");
const userUtils = require("../utils/users");
const walletConstants = require("../constants/wallets");
const { SOMETHING_WENT_WRONG } = require("../constants/errorMessages");
/**
* Get the wallet for userId, or create default one for
* existing members
* @param {string} userId
*/
const getWallet = async (userId) => {
try {
let wallet = await fetchWallet(userId);
if (!wallet) {
// #TODO Log which users didn't have a wallet
wallet = await createWallet(userId, walletConstants.INITIAL_WALLET);
logger.info("Created new wallet for user");
}
return wallet;
} catch (err) {
logger.error(`Error in getWallet ${err}`);
return null;
}
};
/**
* Get the wallet details of user
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
const getOwnWallet = async (req, res) => {
const { id: userId } = req.userData;
try {
const wallet = await getWallet(userId);
return res.json({
message: "Wallet returned successfully for user",
wallet,
});
} catch (err) {
logger.error(`Error while retriving wallet data ${err}`);
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
}
};
/**
* Get the wallet details of user, if a username is provided
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
const getUserWallet = async (req, res) => {
const { params: { username } = {} } = req;
const userId = await userUtils.getUserId(username);
try {
const wallet = await getWallet(userId);
return res.json({
message: "Wallet returned successfully",
wallet,
});
} catch (err) {
logger.error(`Error while retriving wallet data ${err}`);
return res.boom.badImplementation(SOMETHING_WENT_WRONG);
}
};
module.exports = {
getOwnWallet,
getUserWallet,
};