Started adding async calls for upgrading to Meteor 3.0. Numerous other fixes.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Mongo } from 'meteor/mongo';
|
||||
import { check } from 'meteor/check';
|
||||
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
|
||||
import 'meteor/aldeed:collection2/static'
|
||||
import SimpleSchema from 'meteor/aldeed:simple-schema';
|
||||
import Measures from "./Measure";
|
||||
|
||||
/**
|
||||
* Notes:
|
||||
@@ -16,21 +18,31 @@ import {SimpleSchema} from 'meteor/aldeed:simple-schema';
|
||||
|
||||
let Products = new Mongo.Collection('Products');
|
||||
|
||||
if(Meteor.isServer) {
|
||||
//Set MongoDB indexes (or remove them) here.
|
||||
try {
|
||||
Products.rawCollection().createIndex({name: -1}, {unique: true})
|
||||
} catch(e) {console.log("Caught exception while setting indexes in MongoDB"); console.error(e)}
|
||||
}
|
||||
|
||||
const ProductsSchema = new SimpleSchema({
|
||||
name: {
|
||||
type: String,
|
||||
label: "Name",
|
||||
optional: false,
|
||||
trim: true,
|
||||
index: 1,
|
||||
unique: true
|
||||
//index: 1,
|
||||
//unique: true
|
||||
},
|
||||
tags: { //An array of ProductTag names. Note that we are not using the ProductTag ID's because I want a looser connection (if a ProductTag is deleted, it isn't a big deal if it isn't maintained in the Product records).
|
||||
type: [String],
|
||||
type: Array, //[String],
|
||||
label: "Tags",
|
||||
optional: false,
|
||||
defaultValue: []
|
||||
},
|
||||
'tags.$': {
|
||||
type: String,
|
||||
},
|
||||
measures: { //A JSON array of Measure ID's.
|
||||
type: Array,
|
||||
label: "Measures",
|
||||
@@ -181,7 +193,7 @@ if(Meteor.isServer) {
|
||||
});
|
||||
|
||||
Meteor.methods({
|
||||
createProduct: function(name, tags, aliases, measures) {
|
||||
createProduct: async function(name, tags, aliases, measures) {
|
||||
check(name, String);
|
||||
if(tags) check(tags, [String]);
|
||||
if(aliases) check(aliases, [String]);
|
||||
@@ -192,48 +204,48 @@ if(Meteor.isServer) {
|
||||
//let cursor = Products.find({}, {barCodeId: 1}).sort({barCodeId: -1}).limit(1); //Since currently products are never removed, we shouldn't need to detect sequence gaps and fill them in (odds are we will never use more than 10k numbers anyway).
|
||||
//let barCodeId = cursor.hasNext() ? cursor.next().barCodeId : 1;
|
||||
//
|
||||
Products.insert({name, tags, aliases, measures, createdAt: new Date()}, {bypassCollection2: true}, function(err, id) {
|
||||
await Products.insertAsync({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.");
|
||||
},
|
||||
convertProduct: function(productId, alternateProductId) {
|
||||
convertProduct: async function(productId, alternateProductId) {
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
check(productId, String);
|
||||
check(alternateProductId, String);
|
||||
// Replace all sale references to the given ID with the provided alternate product ID.
|
||||
Meteor.collections.Sales.update({productId: productId}, {$set: {productId: alternateProductId}}, {multi: true});
|
||||
await Meteor.collections.Sales.updateAsync({productId: productId}, {$set: {productId: alternateProductId}}, {multi: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
deactivateProduct: function(id) {
|
||||
deactivateProduct: async function(id) {
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
//Products.remove(id);
|
||||
Products.update(id, {$set: {deactivated: true}}, {bypassCollection2: true});
|
||||
await Products.updateAsync(id, {$set: {deactivated: true}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
reactivateProduct: function(id) {
|
||||
reactivateProduct: async function(id) {
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.update(id, {$set: {deactivated: false}}, {bypassCollection2: true});
|
||||
await Products.updateAsync(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.
|
||||
hideProduct: async 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});
|
||||
await Products.updateAsync(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.
|
||||
showProduct: async 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});
|
||||
await Products.updateAsync(id, {$set: {hidden: false}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
updateProduct: function(id, name, tags, aliases, measures) {
|
||||
updateProduct: async function(id, name, tags, aliases, measures) {
|
||||
check(id, String);
|
||||
check(name, String);
|
||||
if(tags) check(tags, [String]);
|
||||
@@ -241,22 +253,22 @@ if(Meteor.isServer) {
|
||||
if(measures) check(measures, [String]);
|
||||
|
||||
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
|
||||
Products.update(id, {$set: {name: name, tags: tags, aliases: aliases, measures: measures, updatedAt: new Date()}}, {bypassCollection2: true});
|
||||
await Products.updateAsync(id, {$set: {name: name, tags: tags, aliases: aliases, measures: measures, updatedAt: new Date()}}, {bypassCollection2: true});
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
clearProductPrice: function(productIds, measureId) {
|
||||
clearProductPrice: async 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});
|
||||
|
||||
await Products.updateAsync({_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) {
|
||||
setProductPrice: async function(productIds, measureId, price, setPrevious, effectiveDate) {
|
||||
check(productIds, [String]);
|
||||
check(measureId, String);
|
||||
check(price, Number);
|
||||
@@ -283,14 +295,14 @@ if(Meteor.isServer) {
|
||||
measurePriceData.price = price;
|
||||
|
||||
if(ProductsSchema.newContext().isValid()) {
|
||||
Products.update(product._id, {$set: {prices: prices, updateAt: new Date()}}, {validate: false, bypassCollection2: true});
|
||||
await Products.updateAsync(product._id, {$set: {prices: prices, updateAt: new Date()}}, {validate: false, bypassCollection2: true});
|
||||
}
|
||||
else console.log("Invalid schema for product");
|
||||
}
|
||||
}
|
||||
else throw new Meteor.Error(403, "Not authorized.");
|
||||
},
|
||||
tagProducts: function(productIds, tagId) {
|
||||
tagProducts: async 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);
|
||||
@@ -299,10 +311,10 @@ if(Meteor.isServer) {
|
||||
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});
|
||||
await Products.updateAsync({_id: {$in: productIds}}, {$pullAll: {tags: [tagId]}}, {bypassCollection2: true, multi: true});
|
||||
}
|
||||
else {
|
||||
Products.update({_id: {$in: productIds}}, {$addToSet: {tags: tagId}}, {bypassCollection2: true, multi: true});
|
||||
await Products.updateAsync({_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