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

@@ -2,11 +2,11 @@
Meteor.methods({
// Cleans up all Date objects that don't have a time component. Removes any time component.
"cleanDates": function() {
"cleanDates": async function() {
// Update the sales dates.
let sales = Sales.find({}).fetch();
let sales = await Sales.find({}).fetchAsync();
for(let i = 0; i < sales.length; i++) {
if(sales[i].date && _.isDate(sales[i].date)) {
@@ -15,7 +15,7 @@ Meteor.methods({
console.log("Converted " + sales[i].date + " to " + date);
// Save to the database.
Sales.update(sales[i]._id, {$set: {date}}, function(err, id) {
await Sales.updateAsync(sales[i]._id, {$set: {date}}, function(err, id) {
if(err) console.log(err);
}, {bypassCollection2: true});
}
@@ -25,7 +25,7 @@ Meteor.methods({
// Update the product price effective dates.
let products = Products.find({}).fetch();
let products = await Products.find({}).fetchAsync();
for(let i = 0; i < products.length; i++) {
let product = products[i];
@@ -48,7 +48,7 @@ Meteor.methods({
// Save the changes.
if(hasChanges) {
Products.update(product._id, {$set: {prices}}, function(err, id) {
await Products.updateAsync(product._id, {$set: {prices}}, function(err, id) {
if(err) console.log(err);
}, {validate: false, bypassCollection2: true});
}

View File

@@ -11,8 +11,8 @@
Meteor.methods({
// Cleans up all Date objects that don't have a time component. Removes any time component.
"enhanceSalesDateFields": function() {
let sales = Sales.find({}).fetch();
"enhanceSalesDateFields": async function() {
let sales = await Sales.find({}).fetchAsync();
for(let i = 0; i < sales.length; i++) {
let dateString = sales[i].date.toString();
@@ -25,7 +25,7 @@ Meteor.methods({
}
// Save to the database.
Sales.update(sales[i]._id, {$set: {"timestamp": timestamp.getTime(), weekOfYear}}, {bypassCollection2: true}, function(err, id) {
await Sales.updateAsync(sales[i]._id, {$set: {"timestamp": timestamp.getTime(), weekOfYear}}, {bypassCollection2: true}, function(err, id) {
if(err) console.log(err);
});
}

View File

@@ -1,15 +1,15 @@
let fs = require('fs');
Meteor.methods({
"exportMissingSales": function() {
"exportMissingSales": async function() {
try {
let products = Products.find({}).fetch();
let venues = Venues.find({}).fetch();
let measures = Measures.find({}).fetch();
let products = await Products.find({}).fetchAsync();
let venues = await Venues.find({}).fetchAsync();
let measures = await Measures.find({}).fetchAsync();
let productNameMap = {};
let venueNameMap = {};
let measureNameMap = {};
let sales = Sales.find({createdAt: {$gte: moment.utc("2017-01-28", "YYYY-MM-DD").toDate(), $lt: moment.utc("2017-01-29", "YYYY-MM-DD").toDate()}}).fetch();
let sales = await Sales.find({createdAt: {$gte: moment.utc("2017-01-28", "YYYY-MM-DD").toDate(), $lt: moment.utc("2017-01-29", "YYYY-MM-DD").toDate()}}).fetchAsync();
let output = "";
for(let i = 0; i < products.length; i++) {

View File

@@ -1,12 +1,12 @@
Meteor.methods({
"checkDuplicateSales": function() {
"checkDuplicateSales": async function() {
try {
let sales = Sales.find({}).fetch();
let sales = await Sales.find({}).fetchAsync();
let duplicateCount = 0;
for(let i = 0; i < sales.length; i++) {
let next = missingData[i];
let count = Sales.find({date: next.date, measureId: next.measureId, productId: next.productId, venueId: next.venueId, price: next.price, amount: next.amount}).count();
let count = await Sales.countDocuments({date: next.date, measureId: next.measureId, productId: next.productId, venueId: next.venueId, price: next.price, amount: next.amount});
if(count > 1) {
console.log("This sale has a duplicate:");

View File

@@ -2,13 +2,13 @@ import readCSV from '/imports/util/csv.js';
//Returns the ID of the created or found object.
//This is needed because for some crazy reason upsert only returns the affected document if a document is inserted, otherwise it does not return the ID of the affected document.
function insertOrCreate(collection, selector, object) {
async function insertOrCreate(collection, selector, object) {
let id;
let findResult = collection.findOne(selector, {fields: {_id: 1}});
let findResult = await collection.findOneAsync(selector, {fields: {_id: 1}});
if(findResult && findResult._id) id = findResult._id;
else id = collection.insert(object);
else id = await collection.insertAsync(object);
//
// let result = collection.upsert(selector, {$setOnInsert: object}, {multi: false});
@@ -26,7 +26,7 @@ function insertOrCreate(collection, selector, object) {
}
Meteor.methods({
"importBasics": function() {
"importBasics": async function() {
const measures = [
{name: "Jar 4oz", postfix: "4oz", order: 0, createdAt: new Date()},
{name: "Jar 8oz", postfix: "8oz", order: 1, createdAt: new Date()},
@@ -55,22 +55,22 @@ Meteor.methods({
{name: "Website Order", type: "Mail", createdAt: new Date()}
];
Measures.remove({});
Venues.remove({});
await Measures.removeAsync({});
await Venues.removeAsync({});
for(let next of measures) {
Measures.insert(next, function(error, _id) {
await Measures.insertAsync(next, function(error, _id) {
if(error) console.log("Failed to insert measure: " + JSON.stringify(next) + " ERROR: " + error);
});
}
for(let next of venues) {
Venues.insert(next, function(error, _id) {
await Venues.insertAsync(next, function(error, _id) {
if(error) console.log("Failed to insert venue: " + JSON.stringify(next) + " ERROR: " + error);
});
}
},
"importItems": function() {
"importItems": async function() {
let fileName = "importItems.csv";
//The mapping of model attributes to CSV columns. The oz sizes are arrays of columns since there are multiple.
let map = {};
@@ -81,31 +81,31 @@ Meteor.methods({
{
let result;
result = Measures.findOne({name: 'Jar 4oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 4oz'}, {fields: {_id: 1}});
if(result) measureIdMap['4oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 4oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 8oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 8oz'}, {fields: {_id: 1}});
if(result) measureIdMap['8oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 8oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 12oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 12oz'}, {fields: {_id: 1}});
if(result) measureIdMap['12oz'] = result._id
else {console.log("Error: Couldn't find the _id for Jar 12oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 16oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 16oz'}, {fields: {_id: 1}});
if(result) measureIdMap['16oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 16oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 32oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 32oz'}, {fields: {_id: 1}});
if(result) measureIdMap['32oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 32oz"); hasError = true;}
result = Measures.findOne({name: 'Each'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Each'}, {fields: {_id: 1}});
if(result) measureIdMap['Each'] = result._id;
else {console.log("Error: Couldn't find the _id for Each"); hasError = true;}
result = Measures.findOne({name: 'Bags'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Bags'}, {fields: {_id: 1}});
if(result) measureIdMap['Bags'] = result._id;
else {console.log("Error: Couldn't find the _id for Bags"); hasError = true;}
}
@@ -178,7 +178,7 @@ Meteor.methods({
}
//Reads a single row of CSV data and adds it to the database.
function readRow(row) {
async function readRow(row) {
let category = row[map.category].trim();
let subcategory = row[map.subcategory].trim();
let item = row[map.item];
@@ -219,7 +219,7 @@ Meteor.methods({
}
// categoryId = insertOrCreate(Categories, {name: category}, {name: category, createdAt: new Date()});
categoryId = insertOrCreate(ProductTags, {name: category}, {name: category, createdAt: new Date()});
let categoryId = await insertOrCreate(ProductTags, {name: category}, {name: category, createdAt: new Date()});
if(categoryId) {
// subcategoryId = insertOrCreate(Subcategories, {name: subcategory}, {
@@ -227,7 +227,7 @@ Meteor.methods({
// categoryId: categoryId,
// createdAt: new Date()
// });
subcategoryId = insertOrCreate(ProductTags, {name: subcategory}, {name: subcategory, createdAt: new Date()});
let subcategoryId = await insertOrCreate(ProductTags, {name: subcategory}, {name: subcategory, createdAt: new Date()});
if(subcategoryId) {
let weightedMeasures = [];
@@ -258,7 +258,7 @@ Meteor.methods({
//Here we can just insert since we don't care about the resulting ID.
let obj = {name: item, tags: [categoryId, subcategoryId], measures: measures, createdAt: new Date()};
Products.insert(obj, function(error) {
await Products.insertAsync(obj, function(error) {
if(error) console.log("Could not insert the Product: " + JSON.stringify(obj) + "\n ERROR: " + error);
});
}
@@ -271,7 +271,7 @@ Meteor.methods({
}
}
readCSV(fileName, Meteor.bindEnvironment(function(error, data) {
readCSV(fileName, Meteor.bindEnvironment(async function(error, data) {
//csv.read(fileName, function(error, csvData) {
if(error) console.log("Unable to read the importItems.csv file:" + error);
else {
@@ -282,19 +282,19 @@ Meteor.methods({
// Items.remove({});
// Subcategories.remove({});
// Categories.remove({});
Products.remove({});
await Products.removeAsync({});
// console.log("CSV Column Mapping: " + JSON.stringify(map));
// readRow(data[1]);
for(let i = 1; i < data.length; i++) {
readRow(data[i]);
await readRow(data[i]);
}
}
}));
}
},
"importSales": function() {
"importSales": async function() {
let fileName = "importSales.csv";
//The mapping of model attributes to CSV columns. The oz sizes are arrays of columns since there are multiple.
let map = {};
@@ -543,82 +543,82 @@ Meteor.methods({
{ //Load the object ids for the measures and venues we will encounter.
let result;
result = Measures.findOne({name: 'Jar 4oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 4oz'}, {fields: {_id: 1}});
if(result) measureIdMap['4oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 4oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 8oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 8oz'}, {fields: {_id: 1}});
if(result) measureIdMap['8oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 8oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 12oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 12oz'}, {fields: {_id: 1}});
if(result) measureIdMap['12oz'] = result._id
else {console.log("Error: Couldn't find the _id for Jar 12oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 16oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 16oz'}, {fields: {_id: 1}});
if(result) measureIdMap['16oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 16oz"); hasError = true;}
result = Measures.findOne({name: 'Jar 32oz'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Jar 32oz'}, {fields: {_id: 1}});
if(result) measureIdMap['32oz'] = result._id;
else {console.log("Error: Couldn't find the _id for Jar 32oz"); hasError = true;}
result = Measures.findOne({name: 'Each'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Each'}, {fields: {_id: 1}});
if(result) measureIdMap['Each'] = result._id;
else {console.log("Error: Couldn't find the _id for Each"); hasError = true;}
result = Measures.findOne({name: 'Bags'}, {fields: {_id: 1}});
result = await Measures.findOneAsync({name: 'Bags'}, {fields: {_id: 1}});
if(result) measureIdMap['Bags'] = result._id;
else {console.log("Error: Couldn't find the _id for Bags"); hasError = true;}
result = Venues.findOne({name: 'Boonville'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Boonville'}, {fields: {_id: 1}});
if(result) venueIdMap['bv'] = result._id;
else {console.log("Error: Couldn't find the _id for Boonville"); hasError = true;}
result = Venues.findOne({name: 'Clement St'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Clement St'}, {fields: {_id: 1}});
if(result) venueIdMap['sf'] = result._id;
else {console.log("Error: Couldn't find the _id for Clement St"); hasError = true;}
result = Venues.findOne({name: 'Ukiah'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Ukiah'}, {fields: {_id: 1}});
if(result) venueIdMap['uk'] = result._id;
else {console.log("Error: Couldn't find the _id for Ukiah"); hasError = true;}
result = Venues.findOne({name: 'Mendocino'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Mendocino'}, {fields: {_id: 1}});
if(result) venueIdMap['men'] = result._id;
else {console.log("Error: Couldn't find the _id for Mendocino"); hasError = true;}
result = Venues.findOne({name: 'Ft Bragg'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Ft Bragg'}, {fields: {_id: 1}});
if(result) venueIdMap['fb'] = result._id;
else {console.log("Error: Couldn't find the _id for Ft Bragg"); hasError = true;}
result = Venues.findOne({name: 'On Farm'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'On Farm'}, {fields: {_id: 1}});
if(result) venueIdMap['of'] = result._id;
else {console.log("Error: Couldn't find the _id for On Farm"); hasError = true;}
result = Venues.findOne({name: 'Unknown Restaurant'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Unknown Restaurant'}, {fields: {_id: 1}});
if(result) {venueIdMap['res'] = result._id; venueIdMap['w'] = result._id;}
else {console.log("Error: Couldn't find the _id for Unknown Restaurant"); hasError = true;}
result = Venues.findOne({name: 'Yorkville Market'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Yorkville Market'}, {fields: {_id: 1}});
if(result) venueIdMap['ym'] = result._id;
else {console.log("Error: Couldn't find the _id for Yorkville Market"); hasError = true;}
result = Venues.findOne({name: 'Yorkville Cellars'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Yorkville Cellars'}, {fields: {_id: 1}});
if(result) venueIdMap['yc'] = result._id;
else {console.log("Error: Couldn't find the _id for Yorkville Cellars"); hasError = true;}
result = Venues.findOne({name: 'Mail Order'}, {fields: {_id: 1}});
result = await Venues.findOneAsync({name: 'Mail Order'}, {fields: {_id: 1}});
if(result) venueIdMap['mo'] = result._id;
else {console.log("Error: Couldn't find the _id for Mail Order"); hasError = true;}
// result = Items.find({}, {fields: {_id: 1, name: 1}, sort: {name: 1}}).fetch();
result = Products.find({}, {fields: {_id: 1, name: 1}, sort: {name: 1}}).fetch();
result = await Products.find({}, {fields: {_id: 1, name: 1}, sort: {name: 1}}).fetchAsync();
for(let i = 0; i < result.length; i++) itemIdMap[result[i].name.toLowerCase()] = result[i]._id;
//console.log(JSON.stringify(itemIdMap));
}
readCSV(fileName, Meteor.bindEnvironment(function(error, data) {
readCSV(fileName, Meteor.bindEnvironment(async function(error, data) {
//csv.read(fileName, function(error, csvData) {
//Data is an array of arrays. data[0] = array of headers. data[1] = first row of data.
@@ -628,14 +628,14 @@ Meteor.methods({
collectMetadata(data[0]);
//Remove everything first.
Sales.remove({"importTag": "1"});
await Sales.removeAsync({"importTag": "1"});
// console.log("CSV Column Mapping: " + JSON.stringify(map));
// readRow(data[1]);
let undefinedItems = {};
for(let i = 1; i < data.length; i++) {
readRow(data[i], undefinedItems);
await readRow(data[i], undefinedItems);
}
// let output = "";
// output += "----------------\n";
@@ -714,7 +714,7 @@ Meteor.methods({
}
//Reads a single row of CSV data and adds it to the database.
function readRow(row, undefinedItems) {
async function readRow(row, undefinedItems) {
let date = moment(row[map.date], "M/D/YYYY").toDate();
let venue = row[map.venue].toLowerCase();
let item = row[map.item].trim();
@@ -757,7 +757,7 @@ Meteor.methods({
if(price != undefined) price = price['oz32'];
if(price == undefined) price = priceMap[year]['oz32'];
if(price) insertSale({date: date, amount: oz32, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['32oz']});
if(price) await insertSale({date: date, amount: oz32, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['32oz']});
else console.log("Could not find a price in the year " + year + " for " + item + " in the size oz32");
}
@@ -766,7 +766,7 @@ Meteor.methods({
if(price != undefined) price = price['oz16'];
if(price == undefined) price = priceMap[year]['oz16'];
if(price) insertSale({date: date, amount: oz16, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['16oz']});
if(price) await insertSale({date: date, amount: oz16, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['16oz']});
else console.log("Could not find a price in the year " + year + " for " + item + " in the size oz16");
}
@@ -775,7 +775,7 @@ Meteor.methods({
if(price != undefined) price = price['oz12'];
if(price == undefined) price = priceMap[year]['oz12'];
if(price) insertSale({date: date, amount: oz12, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['12oz']});
if(price) await insertSale({date: date, amount: oz12, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['12oz']});
else console.log("Could not find a price in the year " + year + " for " + item + " in the size oz12");
}
@@ -784,7 +784,7 @@ Meteor.methods({
if(price != undefined) price = price['oz8'];
if(price == undefined) price = priceMap[year]['oz8'];
if(price) insertSale({date: date, amount: oz8, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['8oz']});
if(price) await insertSale({date: date, amount: oz8, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['8oz']});
else console.log("Could not find a price in the year " + year + " for " + item + " in the size oz8");
}
@@ -793,7 +793,7 @@ Meteor.methods({
if(price != undefined) price = price['oz4'];
if(price == undefined) price = priceMap[year]['oz4'];
if(price) insertSale({date: date, amount: oz4, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['4oz']});
if(price) await insertSale({date: date, amount: oz4, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['4oz']});
else console.log("Could not find a price in the year " + year + " for " + item + " in the size oz4");
}
@@ -802,7 +802,7 @@ Meteor.methods({
if(price != undefined) price = price['bags'];
if(price == undefined) price = priceMap[year]['bags'];
if(price) insertSale({date: date, amount: bags, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['Bags']});
if(price) await insertSale({date: date, amount: bags, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['Bags']});
else {
console.log("Could not find a price in the year " + year + " for " + item + " in the size bags");
}
@@ -813,16 +813,16 @@ Meteor.methods({
if(price != undefined) price = price['each'];
if(price == undefined) price = priceMap[year]['each'];
if(price) insertSale({date: date, amount: each, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['Each']});
if(price) await insertSale({date: date, amount: each, price: price, venueId: venueId, productId: itemId, measureId: measureIdMap['Each']});
else console.log("Could not find a price in the year " + year + " for " + item + " in the size each");
}
}
}
function insertSale(sale) {
async function insertSale(sale) {
sale.createdAt = new Date();
sale.importTag = "1";
Sales.insert(sale, function(error) {
await Sales.insertAsync(sale, function(error) {
if(error) console.log("Failed to insert the sale: " + JSON.stringify(sale) + "\n ERROR: " + error);
}, {bypassCollection2: true});
}

View File

@@ -1,10 +1,10 @@
Meteor.methods({
"importMissingSales": function() {
"importMissingSales": async function() {
console.log("Importing Missing Sales...");
try {
let products = Products.find({}).fetch();
let venues = Venues.find({}).fetch();
let measures = Measures.find({}).fetch();
let products = await Products.find({}).fetchAsync();
let venues = await Venues.find({}).fetchAsync();
let measures = await Measures.find({}).fetchAsync();
let productNameMap = {};
let venueNameMap = {};
let measureNameMap = {};