UNPKG

auction

Version:
172 lines (136 loc) 4.17 kB
'use strict'; /** * Module dependencies. */ Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var _errors = require('./errors'); var _predefine = require('predefine'); var _predefine2 = _interopRequireDefault(_predefine); var _cuid = require('cuid'); var _cuid2 = _interopRequireDefault(_cuid); var _debug = require('debug'); var _debug2 = _interopRequireDefault(_debug); var debug = (0, _debug2['default'])('auction:bid'); var Bid = (function () { function Bid() { var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0]; _classCallCheck(this, Bid); this.status = Bid.REJECTED; debug('creating bid with options %j', options); if ('number' !== typeof options.price && !options.maxPrice) { throw new _errors.BidError('Invalid bid price.'); } if (!options.auctionId) { throw new _errors.BidError('Invalid auction id.'); } if ('string' !== typeof options.agentId) { throw new _errors.BidError('Invalid agent.'); } if (this.placed) { throw new _errors.BidError('Bid already placed.'); } this.id = (0, _cuid2['default'])(); this.agentId = options.agentId; this.price = options.price || 0; this.saleId = options.saleId; this.auctionId = options.auctionId; this.status = Bid.INITIALIZED; debug('initialized bid %j', this.data); } /** * Bid states. * * @type {Number} * @api protected */ /** * Accept `bid`. * * @return {Object} data * @api public */ _createClass(Bid, [{ key: 'accept', value: function accept() { if (!this.place()) throw new _errors.BidError('Bid already placed.'); this.status = Bid.ACCEPTED; debug('bid %s accepted', this.id); return this.data; } /** * Reject `bid`. * * @return {Object} data * @api public */ }, { key: 'reject', value: function reject() { if (!this.place()) throw new _errors.BidError('Bid already placed.'); this.status = Bid.REJECTED; debug('bid %s rejected', this.id); return this.data; } /** * Place `bid`. * * @return {Bid|Null} * @api private */ }, { key: 'place', value: function place() { if (this.placed) return null; this.placed = true; this.timestamp = Date.now(); debug('bid %s placed', this.id); return this; } /** * Method to extend object. * * @param {Function} fn * @param {Object} options * @return {Bid} this * @api public */ }, { key: 'use', value: function use(fn, options) { fn(this, options); return this; } /** * Lazy get `bid` data. * * @type {Object} * @api public */ }, { key: 'data', get: function get() { var data = { id: this.id, price: this.price, placed: this.placed, status: this.status, agentId: this.agentId, timestamp: this.timestamp, auctionId: this.auctionId }; if (this.saleId) data.saleId = this.saleId; return data; } }]); return Bid; })(); exports['default'] = Bid; Bid.INITIALIZED = 'initialized'; Bid.ACCEPTED = 'accepted'; Bid.REJECTED = 'rejected'; module.exports = exports['default'];