- Jan 2, 2006
- 10,455
- 35
- 91
I'm making a web store in Meteor and I've got a collection called Discounts in MongoDB and each document contains info for a different discount / coupon code.
I came to realize that applying a discount code to someone's order isn't that straightforward - there needs to be a way to determine if the discount is valid or not, and it would be amazing if the ability to check validity was stored right in each separate discount object itself. There could be a huge range of different discounts, each with a different "rule" for determining validity, and it doesn't always make sense to store the rules separate from the discounts.
I want each discount object to contain a function that takes the customer's order form data (things like order date, total price, customer's email, etc) and checks it all to see if the conditions are valid for applying the coupon code.
If so, it'll return a discount. If not valid, it won't return any discount.
I know that Mongo is primarily a data store and should not be used for storing and calling functions for application logic:
http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/
However, using system.js outlined in the docs above seems really, really cumbersome, especially since the functions won't be able to utilize this like in my examples above to reference the objects that they're currently inside.
I'm hoping that there's an easier way to store self-referencing functions inside objects and then run them when I need them?
I came to realize that applying a discount code to someone's order isn't that straightforward - there needs to be a way to determine if the discount is valid or not, and it would be amazing if the ability to check validity was stored right in each separate discount object itself. There could be a huge range of different discounts, each with a different "rule" for determining validity, and it doesn't always make sense to store the rules separate from the discounts.
I want each discount object to contain a function that takes the customer's order form data (things like order date, total price, customer's email, etc) and checks it all to see if the conditions are valid for applying the coupon code.
If so, it'll return a discount. If not valid, it won't return any discount.
Code:
// if the customer's order contains RAM and is in 2015, they get $100 off
{
sku: 'RAM-50-2015',
discountAmount: -100,
discountCode: 'SAVEONRAM'
check: function(orderFormContents) {
if (orderFormContents.ram && orderFormContents.year === 2015) {
return this.discountAmount;
} else {
return "order not valid for this coupon"
};
}
}, {
// If the customer's order is in Oct 2015 with at least $100 in total price, they get 20% off their order
sku: 'OCT-2015-Promo',
discountAmount: -0.20,
discountCode: 'OCTSAVINGS',
check: function(orderFormContents) {
if (orderFormContents.totalPrice >= 100 && orderFormContents.month === 10 && orderFormContents.year === 2015) {
return orderFormContents.totalPrice * this.discountAmount;
} else {
return "order not valid for this coupon"
};
},
}, {
// If the customer has any credits in their account, they can be applied via a coupon code
sku: 'CustomerCredit',
discountAmount: function(email) {
return db.Customers.findOne({
email: email
}).creditAmount;
},
discountCode: 'CUSTOMERCREDIT',
check: function(orderFormContents) {
var customerCredit = this.discountAmount(orderFormContents.customerInfo.email);
if (customerCredit > 0) {
return orderFormContents.totalPrice - customerCredit;
} else {
return "You have no credits in your account."
};
}
}
I know that Mongo is primarily a data store and should not be used for storing and calling functions for application logic:
http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/
However, using system.js outlined in the docs above seems really, really cumbersome, especially since the functions won't be able to utilize this like in my examples above to reference the objects that they're currently inside.
I'm hoping that there's an easier way to store self-referencing functions inside objects and then run them when I need them?
Last edited: