- 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(
|
const BidSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
|
eventId: {
|
||||||
|
type: String,
|
||||||
|
required: true,
|
||||||
|
trim: true,
|
||||||
|
},
|
||||||
itemId: {
|
itemId: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
const config = require('../config.js');
|
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const timestamps = require('mongoose-timestamp');
|
const timestamps = require('mongoose-timestamp');
|
||||||
|
|
||||||
|
const config = require('../config.js');
|
||||||
|
const Item = require('./item');
|
||||||
|
|
||||||
const PostSchema = new mongoose.Schema(
|
const PostSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
author: String,
|
author: String,
|
||||||
@@ -133,7 +135,11 @@ const EventSchema = new mongoose.Schema(
|
|||||||
|
|
||||||
EventSchema.plugin(timestamps);
|
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);
|
const Event = mongoose.model('Event', EventSchema);
|
||||||
module.exports = Event;
|
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);
|
const Item = mongoose.model('Item', ItemSchema);
|
||||||
module.exports = Item;
|
module.exports = Item;
|
||||||
|
|||||||
@@ -244,6 +244,7 @@ UserSchema.methods.toAuthJSON = function () {
|
|||||||
email: this.email,
|
email: this.email,
|
||||||
token: this.generateJWT(),
|
token: this.generateJWT(),
|
||||||
user: {
|
user: {
|
||||||
|
id: this._id,
|
||||||
nomDeBid: nomDeBid,
|
nomDeBid: nomDeBid,
|
||||||
email: this.email,
|
email: this.email,
|
||||||
firstName: this.firstName,
|
firstName: this.firstName,
|
||||||
@@ -266,6 +267,11 @@ UserSchema.methods.toProfileJSON = function () {
|
|||||||
email: this.email,
|
email: this.email,
|
||||||
firstName: this.firstName,
|
firstName: this.firstName,
|
||||||
generatedNomDeBid: !hasNomDeBid,
|
generatedNomDeBid: !hasNomDeBid,
|
||||||
|
hasLinkedApple: !!this.getAuthStrategy('apple'),
|
||||||
|
hasLinkedFacebook: !!this.getAuthStrategy('facebook'),
|
||||||
|
hasLinkedGoogle: !!this.getAuthStrategy('google'),
|
||||||
|
hasLocalAccount: !!this.getAuthStrategy('local'),
|
||||||
|
_id: this.id,
|
||||||
isAllowedToBid: this.isAllowedToBid,
|
isAllowedToBid: this.isAllowedToBid,
|
||||||
isOrganizationEmployee: this.isOrganizationEmployee,
|
isOrganizationEmployee: this.isOrganizationEmployee,
|
||||||
isVerified: this.isVerified,
|
isVerified: this.isVerified,
|
||||||
|
|||||||
43
routes/auction.js
Normal file
43
routes/auction.js
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
const errors = require('restify-errors');
|
||||||
|
|
||||||
|
const config = require('../config');
|
||||||
|
const Auction = require('../models/auction');
|
||||||
|
const Bid = require('../models/bid');
|
||||||
|
const User = require('../models/user');
|
||||||
|
|
||||||
|
module.exports = function (server, auth) {
|
||||||
|
|
||||||
|
server.get('/auction', auth.basic, function (req, res, next) {
|
||||||
|
Auction.getAuctionStatus(null, req.user.bidderId, (err, doc) => {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(doc);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
server.get('/auction/:event_id', auth.basic, function (req, res, next) {
|
||||||
|
Auction.getAuctionStatus(req.params.event_id, req.user.bidderId, (err, doc) => {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(doc);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
server.get('/auction/:event_id/:item_id', auth.basic, function (req, res, next) {
|
||||||
|
const { event_id: eventId, item_id: itemId } = req.params;
|
||||||
|
Auction.getAuctionItemStatus(eventId, itemId, req.user.bidderId, (err, doc) => {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
res.send(doc);
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
module.exports = function(server, auth) {
|
module.exports = function(server, auth) {
|
||||||
|
require('./auction.js')(server, auth);
|
||||||
require('./auth.js')(server, auth);
|
require('./auth.js')(server, auth);
|
||||||
require('./bids.js')(server, auth);
|
require('./bids.js')(server, auth);
|
||||||
require('./demo.js')(server, auth);
|
require('./demo.js')(server, auth);
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ module.exports = function (server, auth) {
|
|||||||
|
|
||||||
server.get('/items', auth.basic, (req, res, next) => {
|
server.get('/items', auth.basic, (req, res, next) => {
|
||||||
const select = req.user.isManager ? STAFF : PUBLIC;
|
const select = req.user.isManager ? STAFF : PUBLIC;
|
||||||
|
|
||||||
Item.find(req.params, select, function(err, docs) {
|
Item.find(req.params, select, function(err, docs) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ module.exports = function (server, auth) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send(doc);
|
res.send(req.user.isManager ? doc : doc.toProfileJSON());
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const createRequestUserObject = (req, user) => ({
|
const createRequestUserObject = (req, user) => ({
|
||||||
|
bidderId: user.id || null,
|
||||||
isGuest: !(user && user.id),
|
isGuest: !(user && user.id),
|
||||||
isManager: user && user.isEventManager(),
|
isManager: user && user.isEventManager(),
|
||||||
isSelf: user && user.id === req.params.user_id,
|
isSelf: user && user.id === req.params.user_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user