generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 201
/
auctions.js
193 lines (174 loc) · 5.68 KB
/
auctions.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const firestore = require("../utils/firestore");
const auctionsModel = firestore.collection("auctions");
const bidsModel = firestore.collection("bids");
const usersUtils = require("../utils/users");
const walletModels = require("./wallets");
/**
* Fetches auction details by auctionId
*
* @param auctionsId { String }: Id for the auction to be fetched
* @return {Promise<{AuctionData|Object}>}
*/
const fetchAuctionById = async (auctionId) => {
try {
const auctionRef = await auctionsModel.doc(auctionId).get();
if (!auctionRef.data()) {
return false;
}
const auctionMetadata = auctionRef.data();
const biddersAndBidsRef = await bidsModel.where("auction_id", "==", auctionId).get();
const biddersAndBidsArray = [];
biddersAndBidsRef.forEach((bidData) => {
biddersAndBidsArray.push(bidData.data());
});
const promises = biddersAndBidsArray.map(async (bidData) => {
const bidderUsername = await usersUtils.getUsername(bidData.bidder);
return { ...bidData, bidder: bidderUsername };
});
const biddersAndBids = await Promise.all(promises);
const seller = usersUtils.getUsername(auctionMetadata.seller);
const highestBidder = usersUtils.getUsername(auctionMetadata.highest_bidder);
const promise = await Promise.all([seller, highestBidder]);
const [sellerUsername, highestBidderUsername] = promise;
return {
...auctionMetadata,
bidders_and_bids: biddersAndBids,
seller: sellerUsername,
highest_bidder: highestBidderUsername,
};
} catch (error) {
logger.error(`Error fetching auction ${auctionId}: ${error}`);
throw error;
}
};
/**
* Fetches auction bidders usernames for a given auction
*
* @param auctionsId { String }: Id for the auction
* @return {Promise<{BiddersUsernames|Array}>}
*/
const fetchAuctionBidders = async (auctionId) => {
try {
const biddersRef = await bidsModel.where("auction_id", "==", auctionId).get();
const biddersSet = new Set([]);
biddersRef.forEach((bid) => {
const { bidder } = bid.data();
biddersSet.add(bidder);
});
const biddersArray = [...biddersSet];
const promises = biddersArray.map(async (bidder) => {
const bidderUsername = await usersUtils.getUsername(bidder);
return bidderUsername;
});
const bidders = await Promise.all(promises);
return bidders;
} catch (error) {
logger.error(`Error fetching bidders: ${error}`);
throw error;
}
};
/**
* Fetches all active (ongoing) auctions
*
* @return {Promise<{AuctionData|Array}>}
*/
const fetchAvailableAuctions = async () => {
try {
const now = new Date().getTime();
const auctionsRef = await auctionsModel.where("end_time", ">=", now).get();
const auctions = [];
auctionsRef.forEach((auction) => {
auctions.push({
id: auction.id,
...auction.data(),
});
});
const promises = auctions.map(async (auction) => {
try {
const seller = usersUtils.getUsername(auction.seller);
const highestBidder = usersUtils.getUsername(auction.highest_bidder);
const biddersArray = fetchAuctionBidders(auction.id);
const promise = await Promise.all([seller, highestBidder, biddersArray]);
const [sellerUsername, highestBidderUsername, bidders] = promise;
return { ...auction, seller: sellerUsername, highest_bidder: highestBidderUsername, bidders };
} catch (error) {
logger.error(`Error fetching bidders for auction - ${auction.id}: ${error}`);
throw error;
}
});
const auctionsAndBidders = await Promise.all(promises);
return auctionsAndBidders;
} catch (error) {
logger.error(`Error fetching auction: ${error}`);
throw error;
}
};
/**
* Creates new auction
*
* @param { Object }: Details required to create an auction
* @return id {string}
*/
const createNewAuction = async ({ seller, initialPrice, endTime, itemType, quantity }) => {
try {
const auctionRef = await auctionsModel.add({
seller: seller,
item: itemType,
quantity: parseInt(quantity),
highest_bidder: null,
highest_bid: parseInt(initialPrice),
start_time: new Date().getTime(),
end_time: parseInt(endTime),
});
return auctionRef.id;
} catch (error) {
logger.error(`Error creating new auction: ${error}`);
throw error;
}
};
/**
* Makes new bid for a given auction
*
* @param { Object }: Data required for placing a bid
* @return id { string }
*/
const makeNewBid = async ({ bidder, auctionId, bid }) => {
try {
const auctionRef = await auctionsModel.doc(auctionId);
const auctionDataRef = await auctionRef.get();
if (!auctionDataRef.data()) return { auctionNotFound: true };
const wallet = (await walletModels.fetchWallet(bidder)) || {};
let usersMoney = 0;
if (Object.keys(wallet).length === 0) return { noWallet: true };
// todo: make the currency adaptive, dinero wont be default currency
else usersMoney = wallet.currencies.dinero;
const { highest_bid: highestBid } = await auctionDataRef.data();
if (parseInt(bid) <= parseInt(highestBid)) {
return { lowBid: true };
}
if (parseInt(bid) > parseInt(usersMoney)) {
return { insufficientMoney: true };
}
await auctionRef.update({
highest_bidder: bidder,
highest_bid: parseInt(bid),
});
const bidRef = await bidsModel.add({
auction_id: auctionId,
bidder: bidder,
bid: parseInt(bid),
time: new Date().getTime(),
});
return bidRef.id;
} catch (error) {
logger.error(`Error making bid: ${error}`);
throw error;
}
};
module.exports = {
fetchAuctionById,
fetchAvailableAuctions,
fetchAuctionBidders,
createNewAuction,
makeNewBid,
};