Started adding async calls for upgrading to Meteor 3.0. Numerous other fixes.

This commit is contained in:
2025-07-02 11:18:09 -07:00
parent e1216741d6
commit 2e99ad007c
32 changed files with 549 additions and 373 deletions

View File

@@ -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:
@@ -10,12 +12,24 @@ import {SimpleSchema} from 'meteor/aldeed:simple-schema';
*/
let Batches = new Mongo.Collection('Batches');
if(Meteor.isServer) {
//Set MongoDB indexes (or remove them) here.
try {
Batches.rawCollection().createIndex({date: -1}, {unique: false})
Batches.rawCollection().createIndex({measureId: -1}, {unique: false})
Batches.rawCollection().createIndex({productId: -1}, {unique: false})
Batches.rawCollection().createIndex({cookId: -1}, {unique: false})
Batches.rawCollection().createIndex({cannerId: -1}, {unique: false})
} catch(e) {console.log("Caught exception while setting indexes in MongoDB"); console.error(e)}
}
let BatchesSchema = new SimpleSchema({
date: {
type: Number, // A number in the format of YYYYMMDD to allow for searching using greater and less than, and to prevent timezones from messing everything up.
label: "Date",
optional: false,
index: 1
//index: 1
},
timestamp: { //This is based off the date with zero for the time and set to GMT (Zulu time).
type: Date,
@@ -31,21 +45,21 @@ let BatchesSchema = new SimpleSchema({
type: Number,
label: "Amount",
optional: false,
decimal: true
//decimal: true
},
measureId: {
type: String,
label: "Measure Id",
trim: false,
regEx: SimpleSchema.RegEx.Id,
index: 1
//index: 1
},
productId: {
type: String,
label: "Product Id",
trim: false,
regEx: SimpleSchema.RegEx.Id,
index: 1,
//index: 1,
optional: false
},
cookId: {
@@ -53,14 +67,14 @@ let BatchesSchema = new SimpleSchema({
label: "Cook Worker Id",
trim: false,
regEx: SimpleSchema.RegEx.Id,
index: 1
//index: 1
},
cannerId: {
type: String,
label: "Canner Worker Id",
trim: false,
regEx: SimpleSchema.RegEx.Id,
index: 1,
//index: 1,
optional: false
},
hasLabels: {
@@ -134,11 +148,11 @@ if(Meteor.isServer) {
});
Meteor.methods({
getBatchCount: function(query) {
getBatchCount: async function(query) {
//TODO: Validate the query?
return Batches.find(query).count();
return await Batches.countDocuments(query);
},
insertBatches: function(batches) { //Insert one or more batches (if one, you can pass just the batch).
insertBatches: async function(batches) { //Insert one or more batches (if one, you can pass just the batch).
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
//Force it to be an array if it isn't.
if(!Array.isArray(batches)) batches = [batches];
@@ -170,37 +184,37 @@ if(Meteor.isServer) {
}
for(let batch of batches) {
Batches.insert(batch, function(err, id) {
await Batches.insertAsync(batch, function(err, id) {
if(err) console.log(err);
}, {bypassCollection2: true});
}
}
else throw new Meteor.Error(403, "Not authorized.");
},
deleteBatch: function(id) { //Does not actually delete the batch, but rather just marks it for deleting by applying a deletion date.
deleteBatch: async function(id) { //Does not actually delete the batch, but rather just marks it for deleting by applying a deletion date.
check(id, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
let deletedAt = new Date();
//Batches.remove(id);
Batches.update(id, {$set: {deletedAt}}, function(err, id) {
await Batches.updateAsync(id, {$set: {deletedAt}}, function(err, id) {
if(err) console.log(err);
});
}
else throw new Meteor.Error(403, "Not authorized.");
},
undeleteBatch: function(id) { //Revokes the previous deletion.
undeleteBatch: async function(id) { //Revokes the previous deletion.
check(id, String);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Batches.update(id, {$unset: {deletedAt:""}}, function(err, id) {
await Batches.updateAsync(id, {$unset: {deletedAt:""}}, function(err, id) {
if(err) console.log(err);
});
}
else throw new Meteor.Error(403, "Not authorized.");
},
//editBatchComment: function(id, comment) {
//editBatchComment: async function(id, comment) {
// check(id, String);
// check(comment, String);
// //Trim and convert empty comment to undefined.
@@ -211,19 +225,19 @@ if(Meteor.isServer) {
// console.log("Changed comment of " + id + " to: " + comment);
//
// if(comment) {
// Batches.update(id, {$set: {comment}}, function(error, count) {
// await Batches.updateAsync(id, {$set: {comment}}, function(error, count) {
// if(error) throw new Meteor.Error(400, "Unexpected database error: " + error);
// });
// }
// else {
// Batches.update(id, {$unset: {comment: ""}}, function(error, count) {
// await Batches.updateAsync(id, {$unset: {comment: ""}}, function(error, count) {
// if(error) throw new Meteor.Error(400, "Unexpected database error: " + error);
// });
// }
// }
// else throw new Meteor.Error(403, "Not authorized.");
//},
updateBatch: function(id, amount, comment) {
updateBatch: async function(id, amount, comment) {
check(id, String);
check(amount, Number);
check(comment, Match.OneOf(String, undefined));
@@ -237,13 +251,13 @@ if(Meteor.isServer) {
//let weekOfYear = timestamp.getWeek().toString();
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Batches.update(id, {$set: {comment, amount}}, function(err, id) {
await Batches.updateAsync(id, {$set: {comment, amount}}, function(err, id) {
if(err) console.log(err);
}, {bypassCollection2: true});
}
else throw new Meteor.Error(403, "Not authorized.");
},
setBatchHasLabels: function(id, hasLabels) {
setBatchHasLabels: async function(id, hasLabels) {
//console.log(id);
//console.log(hasLabels);
//check(id, Meteor.validators.ObjectID);
@@ -251,7 +265,7 @@ if(Meteor.isServer) {
check(hasLabels, Boolean);
if(Roles.userIsInRole(this.userId, [Meteor.UserRoles.ROLE_UPDATE])) {
Batches.update(id, {$set: {hasLabels}}, function(err, id) {
await Batches.updateAsync(id, {$set: {hasLabels}}, function(err, id) {
if(err) console.log(err);
}, {bypassCollection2: true});
}