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:
Wynne Crisman
2017-01-17 22:31:43 -08:00
parent b757595cd6
commit 55337521f6
49 changed files with 16017 additions and 1547 deletions

View File

@@ -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.");
}

View File

@@ -54,6 +54,25 @@ if(Meteor.isServer) {
check(id, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
//Remove all references to the tag in the products.
Meteor.collections.Products.update({tags: {$all: [id]}}, {$pullAll: {tags: [id]}}, {bypassCollection2: true, multi: true});
// let products = Meteor.collections.Products.find({tags: {$all: [id]}}, {_id: 1, tags: 1}).fetch();
//
// //Try to remove all tags from products. Log/Ignore any errors.
// for(let product of products) {
// try {
// let index = product.tags.indexOf(id);
//
// if(index >= 0) {
// product.tags.splice(index, 1);
// Meteor.collections.Products.update(products._id, {$set: {tags: product.tags}}, {bypassCollection2: true, multi: true});
// }
// }
// catch(err) {
// console.log(err);
// }
// }
ProductTags.remove(id);
}
else throw new Meteor.Error(403, "Not authorized.");

View File

@@ -62,23 +62,42 @@ let SalesSchema = new SimpleSchema({
Sales.attachSchema(SalesSchema);
if(Meteor.isServer) {
Meteor.publish('sales', function(query, limit = 100) {
let dbQuery = {};
Meteor.publish('sales', function(query, limit = 100, skipCount) {
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'};
// });
_.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'};
})
if(_.isObject(query[key])) dbQuery.push({[key]: query[key]});
else if(_.isNumber(query[key])) dbQuery.push({[key]: query[key]});
else {
let searchValue = query[key];
let searches = searchValue && searchValue.length > 0 ? searchValue.split(/\s+/) : undefined;
for(let search of searches) {
dbQuery.push({[key]: {$regex: '\\b' + search, $options: 'i'}});
}
}
});
}
if(!_.isNumber(limit)) limit = 100;
if(!_.isNumber(skipCount) || skipCount < 0) skipCount = 0;
return Meteor.collections.Sales.find(dbQuery, {limit: limit, sort: {date: -1}});
dbQuery = dbQuery.length > 0 ? {$and: dbQuery} : {};
return Meteor.collections.Sales.find(dbQuery, {limit: limit, sort: {date: -1, createdAt: -1}, skip: skipCount});
});
Meteor.methods({
getSalesCount: function(query) {
//TODO: Validate the query?
return Sales.find(query).count();
},
insertSale: function(sale) {
//TODO: Check the structure of sale. Use: check(sale, {name: String, ...});
sale.createdAt = new Date();