Started adding async calls for upgrading to Meteor 3.0. Numerous other fixes.
This commit is contained in:
@@ -1,9 +1,20 @@
|
||||
import {Mongo} from "meteor/mongo";
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
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 Products from "./Product";
|
||||
import Measures from "./Measure";
|
||||
|
||||
Barcodes = new Mongo.Collection('Barcodes');
|
||||
let Barcodes = new Mongo.Collection('Barcodes');
|
||||
|
||||
if(Meteor.isServer) {
|
||||
//Set MongoDB indexes (or remove them) here.
|
||||
try {
|
||||
Barcodes.rawCollection().createIndex({barcodeId: -1}, {unique: true})
|
||||
Barcodes.rawCollection().createIndex({productAndMeasureId: -1}, {unique: true})
|
||||
} catch(e) {console.log("Caught exception while setting indexes in MongoDB"); console.error(e)}
|
||||
}
|
||||
|
||||
// A simple mapping between a concatenation of the product & measure ID and a unique sequential number for the barcode. This allows us to have a small number to keep our barcodes simple, while maintaining the more traditional MongoDB ID's for the Product and Measure.
|
||||
const BarcodesSchema = new SimpleSchema({
|
||||
@@ -11,15 +22,15 @@ const BarcodesSchema = new SimpleSchema({
|
||||
type: Number,
|
||||
label: "Barcode ID",
|
||||
optional: false,
|
||||
index: 1,
|
||||
unique: true
|
||||
//index: 1,
|
||||
//unique: true
|
||||
},
|
||||
productAndMeasureId: { //Just the two ids jammed together with a single space between them.
|
||||
type: String,
|
||||
label: "Product And Measure ID",
|
||||
optional: false,
|
||||
index: 1,
|
||||
unique: true
|
||||
//index: 1,
|
||||
//unique: true
|
||||
}
|
||||
});
|
||||
|
||||
@@ -29,15 +40,15 @@ if(Meteor.isServer) {
|
||||
//});
|
||||
|
||||
Meteor.methods({
|
||||
getBarcodeId: function(productId, measureId) {
|
||||
getBarcodeId: async function(productId, measureId) {
|
||||
check(productId, String);
|
||||
check(measureId, String);
|
||||
|
||||
let hasProduct = Meteor.collections.Products.findOne({_id: productId}, {fields: {}});
|
||||
let hasMeasure = Meteor.collections.Measures.findOne({_id: measureId}, {fields: {}});
|
||||
let hasProduct = await Meteor.collections.Products.findOneAsync({_id: productId}, {fields: {}});
|
||||
let hasMeasure = await Meteor.collections.Measures.findOneAsync({_id: measureId}, {fields: {}});
|
||||
|
||||
if(hasProduct && hasMeasure) {
|
||||
let existing = Barcodes.findOne({productAndMeasureId: productId + ' ' + measureId});
|
||||
let existing = await Barcodes.findOneAsync({productAndMeasureId: productId + ' ' + measureId});
|
||||
|
||||
if(existing) {
|
||||
return existing.barcodeId;
|
||||
@@ -48,10 +59,12 @@ if(Meteor.isServer) {
|
||||
//Try a thousand times before failing. Should never fail, should also not ever need to try a thousand times (unless we somehow automate label generation to the point where a 1000 processes at once are requesting labels that have never been generated before - highly unlikely).
|
||||
while(c++ < 1000) {
|
||||
//Lookup the most likely next barcode id from the db, then attempt to insert with it. If it fails due to duplication, then increment and repeat.
|
||||
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 : 1;
|
||||
//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 : 1;
|
||||
let product = await Products.findOneAsync({barcodeId: {$exists: true}}, {sort: {barcodeId: -1}})
|
||||
let barcodeId = product ? product.barcodeId : 1;
|
||||
|
||||
Barcodes.insert({productAndMeasureId: productId + ' ' + measureId, barcodeId}, function(err, id) {
|
||||
await Barcodes.insertAsync({productAndMeasureId: productId + ' ' + measureId, barcodeId}, function(err, id) {
|
||||
if(err) console.log(err);
|
||||
else return barcodeId;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user