Added a lot of functionality; Fixed a large number of bugs; Removed Bootstrap from the mix and replaced it with SimpleGrid and some choice bits from the bootstrap system; Pricing, Sales, and Product management all now function at basic levels.
This commit is contained in:
@@ -44,8 +44,9 @@ const ProductsSchema = new SimpleSchema({
|
||||
type: Object,
|
||||
//blackbox: true,
|
||||
custom: function() {
|
||||
console.log("In custom validation for prices");
|
||||
return true;
|
||||
//console.log("In custom validation for prices");
|
||||
//return true;
|
||||
|
||||
// if(this.value != undefined) {
|
||||
// console.log(this.value);
|
||||
// //check(this, Object);
|
||||
@@ -120,24 +121,14 @@ const ProductsSchema = new SimpleSchema({
|
||||
label: "Updated On",
|
||||
optional: true
|
||||
},
|
||||
deletedAt: {
|
||||
type: Date,
|
||||
label: "Deleted On",
|
||||
deactivated: {
|
||||
type: Boolean,
|
||||
label: "Deactivated",
|
||||
optional: true
|
||||
},
|
||||
deletedBy: {
|
||||
type: String,
|
||||
label: "Deleted By",
|
||||
optional: true
|
||||
},
|
||||
restoredAt: {
|
||||
type: Date,
|
||||
label: "Restored On",
|
||||
optional: true
|
||||
},
|
||||
restoredBy: {
|
||||
type: String,
|
||||
label: "Restored By",
|
||||
hidden: {
|
||||
type: Boolean,
|
||||
label: "Hidden",
|
||||
optional: true
|
||||
}
|
||||
});
|
||||
@@ -165,74 +156,93 @@ const ProductsSchema = new SimpleSchema({
|
||||
//Products.attachSchema(ProductsSchema);
|
||||
|
||||
//https://github.com/zimme/meteor-collection-softremovable
|
||||
Products.attachBehaviour("softRemovable", {
|
||||
removed: 'deleted',
|
||||
removedAt: 'deletedAt',
|
||||
removedBy: 'removedBy',
|
||||
restoredAt: 'restoredAt',
|
||||
restoredBy: 'restoredBy'
|
||||
});
|
||||
// Products.attachBehaviour("softRemovable", {
|
||||
// removed: 'deleted',
|
||||
// removedAt: 'deletedAt',
|
||||
// removedBy: 'removedBy',
|
||||
// restoredAt: 'restoredAt',
|
||||
// restoredBy: 'restoredBy'
|
||||
// });
|
||||
|
||||
if(Meteor.isServer) {
|
||||
Meteor.publish('products', function() {
|
||||
// let dbQuery = {};
|
||||
//
|
||||
// if(query) {
|
||||
// _.each(_.keys(query), function(key) {
|
||||
// if(_.isObject(query[key])) dbQuery[key] = query[key];
|
||||
// else if(_.isNumber(query[key])) dbQuery[key] = query[key];
|
||||
// else dbQuery[key] = {$regex: RegExp.escape(query[key]), $options: 'i'};
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// return Products.find(dbQuery, {sort: {date: -1}});
|
||||
return Products.find({}, {sort: {name: 1}});
|
||||
});
|
||||
|
||||
Meteor.methods({
|
||||
insertProduct: function(product) {
|
||||
check(product, {
|
||||
name: String
|
||||
});
|
||||
|
||||
product.createdAt = new Date();
|
||||
createProduct: function(name, tags, aliases, measures) {
|
||||
check(name, String);
|
||||
if(tags) check(tags, [String]);
|
||||
if(aliases) check(aliases, [String]);
|
||||
if(measures) check(measures, [String]);
|
||||
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.insert(product, function(err, id) {
|
||||
Products.insert({name, tags, aliases, measures, createdAt: new Date()}, {bypassCollection2: true}, function(err, id) {
|
||||
if(err) console.log(err);
|
||||
});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
deleteProduct: function(id) {
|
||||
deactivateProduct: function(id) {
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.remove(id);
|
||||
//Products.remove(id);
|
||||
Products.update(id, {$set: {deactivated: true}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
updateProduct: function(product) {
|
||||
check(product, {
|
||||
_id: String,
|
||||
name: String
|
||||
});
|
||||
reactivateProduct: function(id) {
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.update(id, {$set: {deactivated: false}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
hideProduct: function(id) { //One step past deactivated - will only show in the products list if hidden products are enabled.
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
//Products.remove(id);
|
||||
Products.update(id, {$set: {hidden: true}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
showProduct: function(id) { //Returns the product to being simply deactivated. Will again show in lists.
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.update(id, {$set: {hidden: false}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
updateProduct: function(id, name, tags, aliases, measures) {
|
||||
check(id, String);
|
||||
check(name, String);
|
||||
if(tags) check(tags, [String]);
|
||||
if(aliases) check(aliases, [String]);
|
||||
if(measures) check(measures, [String]);
|
||||
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.update(id, {$set: {name: product.name, updateAt: new Date()}});
|
||||
Products.update(id, {$set: {name: name, tags: tags, aliases: aliases, measures: measures, updateAt: new Date()}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
setProductPrice: function(productId, measureId, price, setPrevious, effectiveDate) {
|
||||
check(productId, String);
|
||||
clearProductPrice: function(productIds, measureId) {
|
||||
check(productIds, [String]);
|
||||
check(measureId, String);
|
||||
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
let attr = "prices." + measureId;
|
||||
|
||||
Products.update({_id: {$in: productIds}}, {$unset: {[attr]: true}}, {validate: false, bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
setProductPrice: function(productIds, measureId, price, setPrevious, effectiveDate) {
|
||||
check(productIds, [String]);
|
||||
check(measureId, String);
|
||||
check(price, Number);
|
||||
check(setPrevious, Boolean);
|
||||
check(effectiveDate, Date);
|
||||
if(setPrevious) check(setPrevious, Boolean);
|
||||
if(effectiveDate) check(effectiveDate, Date);
|
||||
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
let product = Products.findOne(productId, {fields: {prices: 1}});
|
||||
let products = Products.find({_id: {$in: productIds}}, {fields: {prices: 1}}).fetch();
|
||||
|
||||
if(product) {
|
||||
console.log("Product: " + JSON.stringify(product));
|
||||
for(let product of products) {
|
||||
let prices = product.prices ? product.prices : {};
|
||||
let measurePriceData = prices[measureId];
|
||||
|
||||
@@ -240,7 +250,6 @@ if(Meteor.isServer) {
|
||||
measurePriceData = {};
|
||||
prices[measureId] = measurePriceData;
|
||||
}
|
||||
console.log("Old Price Data: " + JSON.stringify(prices));
|
||||
|
||||
if(setPrevious && measurePriceData.price) {
|
||||
measurePriceData.previousPrice = measurePriceData.price;
|
||||
@@ -248,17 +257,29 @@ if(Meteor.isServer) {
|
||||
}
|
||||
|
||||
measurePriceData.price = price;
|
||||
console.log("New Price Data: " + JSON.stringify(prices));
|
||||
//console.log(ProductsSchema.validate(product));
|
||||
//
|
||||
// check(prices, ProductsSchema);
|
||||
|
||||
if(ProductsSchema.newContext().isValid()) {
|
||||
console.log("Valid schema for product");
|
||||
Products.update(productId, {$set: {prices: prices, updateAt: new Date()}}, {validate: false, bypassCollection2: true});
|
||||
Products.update(product._id, {$set: {prices: prices, updateAt: new Date()}}, {validate: false, bypassCollection2: true});
|
||||
}
|
||||
else console.log("Invalid schema for product");
|
||||
}
|
||||
else throw new Meteor.ERROR(400, "Could not find the requested product: " + productId);
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
tagProducts: function(productIds, tagId) {
|
||||
//Tags the products if any products don't have the tag, otherwise removes the tag from all products.
|
||||
check(productIds, [String]);
|
||||
check(tagId, String);
|
||||
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
let productsWithTag = Products.find({_id: {$in: productIds}, tags: {$all: [tagId]}}).count();
|
||||
|
||||
if(productsWithTag == productIds.length) {
|
||||
Products.update({_id: {$in: productIds}}, {$pullAll: {tags: [tagId]}}, {bypassCollection2: true, multi: true});
|
||||
}
|
||||
else {
|
||||
Products.update({_id: {$in: productIds}}, {$addToSet: {tags: tagId}}, {bypassCollection2: true, multi: true});
|
||||
}
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user