2016-05-03 22:50:21 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2014-02-19 19:44:16 +00:00
|
|
|
var loopback = require('../../');
|
|
|
|
|
2014-06-05 07:45:09 +00:00
|
|
|
var CartItem = exports.CartItem = loopback.PersistedModel.extend('CartItem', {
|
2016-11-15 21:46:23 +00:00
|
|
|
tax: {type: Number, default: 0.1},
|
2014-02-19 19:44:16 +00:00
|
|
|
price: Number,
|
|
|
|
item: String,
|
2016-11-15 21:46:23 +00:00
|
|
|
qty: {type: Number, default: 0},
|
2016-04-01 09:14:26 +00:00
|
|
|
cartId: Number,
|
2014-02-19 19:44:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
CartItem.sum = function(cartId, callback) {
|
2016-11-15 21:46:23 +00:00
|
|
|
this.find({where: {cartId: 1}}, function(err, items) {
|
2014-02-19 19:44:16 +00:00
|
|
|
var total = items
|
|
|
|
.map(function(item) {
|
|
|
|
return item.total();
|
|
|
|
})
|
|
|
|
.reduce(function(cur, prev) {
|
|
|
|
return prev + cur;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
callback(null, total);
|
|
|
|
});
|
2016-04-01 09:14:26 +00:00
|
|
|
};
|
2014-02-19 19:44:16 +00:00
|
|
|
|
2014-06-19 19:15:46 +00:00
|
|
|
CartItem.remoteMethod('sum',
|
2014-02-19 19:44:16 +00:00
|
|
|
{
|
2016-11-15 21:46:23 +00:00
|
|
|
accepts: {arg: 'cartId', type: 'number'},
|
|
|
|
returns: {arg: 'total', type: 'number'},
|
2014-02-19 19:44:16 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
CartItem.prototype.total = function() {
|
2016-11-21 16:49:18 +00:00
|
|
|
return this.price * this.qty * (1 + this.tax);
|
2016-04-01 09:14:26 +00:00
|
|
|
};
|