77 lines
1.8 KiB
JavaScript
77 lines
1.8 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const timestamps = require('mongoose-timestamp');
|
|
|
|
const ItemAuctionStatusSchema = new mongoose.Schema(
|
|
{
|
|
bidCount: Number,
|
|
bidders: [ mongoose.Schema.Types.ObjectId ],
|
|
currentBid: Number,
|
|
currentMax: Number,
|
|
winners: [ mongoose.Schema.Types.ObjectId ],
|
|
},
|
|
{ minimize: false },
|
|
);
|
|
|
|
const AuctionSchema = new mongoose.Schema(
|
|
{
|
|
eventId: {
|
|
type: String,
|
|
required: true,
|
|
trim: true,
|
|
},
|
|
scoreboard: [ ItemAuctionStatusSchema ],
|
|
},
|
|
{ minimize: false },
|
|
);
|
|
|
|
AuctionSchema.plugin(timestamps);
|
|
|
|
AuctionSchema.statics.getAuctionStatus = function(eventId, bidderId, callback = () => {}) {
|
|
this.findOne({ _id: eventId }, (err, event) => {
|
|
if (err) {
|
|
return next(err);
|
|
}
|
|
|
|
const statusObject = [];
|
|
|
|
if (event) {
|
|
statusObject.push(event.scoreboard.map((item) => ({
|
|
id: item._id,
|
|
bidCount: item.bidCount,
|
|
currentPrice: item.currentBid,
|
|
isBidding: item.bidders.indexOf(bidderId) > -1,
|
|
isWinning: item.winners.indexOf(bidderId) > -1,
|
|
})));
|
|
}
|
|
|
|
return callback(null, statusObject);
|
|
});
|
|
};
|
|
|
|
AuctionSchema.statics.getAuctionItemStatus = function(eventId, itemId, bidderId, callback = () => {}) {
|
|
this.findOne({ _id: eventId }, (err, event) => {
|
|
if (err) {
|
|
return next(err);
|
|
}
|
|
|
|
const itemStatus = {};
|
|
|
|
if (event) {
|
|
const item = event.scoreboard.id(itemId);
|
|
if (item) {
|
|
itemStatus._id = item._id;
|
|
itemStatus.bidCount = item.bidCount;
|
|
itemStatus.currentPrice = item.currentBid;
|
|
itemStatus.isBidding = item.bidders.indexOf(bidderId) > -1;
|
|
itemStatus.isWinning = item.winners.indexOf(bidderId) > -1;
|
|
}
|
|
}
|
|
|
|
return callback(null, itemStatus);
|
|
});
|
|
};
|
|
|
|
|
|
const Auction = mongoose.model('Auction', AuctionSchema);
|
|
module.exports = Auction;
|