- Tweaks
This commit is contained in:
76
models/auction.js
Normal file
76
models/auction.js
Normal file
@@ -0,0 +1,76 @@
|
||||
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;
|
||||
@@ -3,6 +3,11 @@ const timestamps = require('mongoose-timestamp');
|
||||
|
||||
const BidSchema = new mongoose.Schema(
|
||||
{
|
||||
eventId: {
|
||||
type: String,
|
||||
required: true,
|
||||
trim: true,
|
||||
},
|
||||
itemId: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
const config = require('../config.js');
|
||||
const mongoose = require('mongoose');
|
||||
const timestamps = require('mongoose-timestamp');
|
||||
|
||||
const config = require('../config.js');
|
||||
const Item = require('./item');
|
||||
|
||||
const PostSchema = new mongoose.Schema(
|
||||
{
|
||||
author: String,
|
||||
@@ -133,7 +135,11 @@ const EventSchema = new mongoose.Schema(
|
||||
|
||||
EventSchema.plugin(timestamps);
|
||||
|
||||
EventSchema.path('images').get(v => `${config.assetStoreUrl}${v.url}`)
|
||||
EventSchema.path('images').get(v => `${config.assetStoreUrl}${v.url}`);
|
||||
|
||||
EventSchema.methods.getEventAuctionItems = function(callback = () => {}) {
|
||||
return Item.getAuctionItemsByEvent(this.id, callback);
|
||||
};
|
||||
|
||||
const Event = mongoose.model('Event', EventSchema);
|
||||
module.exports = Event;
|
||||
|
||||
@@ -131,5 +131,9 @@ ItemSchema.statics.addBatch = function(data = [], callback = () => {}) {
|
||||
|
||||
};
|
||||
|
||||
ItemSchema.statics.getAuctionItemsByEvent = function(eventId, callback = () => {}) {
|
||||
return this.find({ eventId, type: 'auction' }, callback);
|
||||
};
|
||||
|
||||
const Item = mongoose.model('Item', ItemSchema);
|
||||
module.exports = Item;
|
||||
|
||||
@@ -244,6 +244,7 @@ UserSchema.methods.toAuthJSON = function () {
|
||||
email: this.email,
|
||||
token: this.generateJWT(),
|
||||
user: {
|
||||
id: this._id,
|
||||
nomDeBid: nomDeBid,
|
||||
email: this.email,
|
||||
firstName: this.firstName,
|
||||
@@ -266,6 +267,11 @@ UserSchema.methods.toProfileJSON = function () {
|
||||
email: this.email,
|
||||
firstName: this.firstName,
|
||||
generatedNomDeBid: !hasNomDeBid,
|
||||
hasLinkedApple: !!this.getAuthStrategy('apple'),
|
||||
hasLinkedFacebook: !!this.getAuthStrategy('facebook'),
|
||||
hasLinkedGoogle: !!this.getAuthStrategy('google'),
|
||||
hasLocalAccount: !!this.getAuthStrategy('local'),
|
||||
_id: this.id,
|
||||
isAllowedToBid: this.isAllowedToBid,
|
||||
isOrganizationEmployee: this.isOrganizationEmployee,
|
||||
isVerified: this.isVerified,
|
||||
|
||||
Reference in New Issue
Block a user