Files
PetitTetonMeteor/imports/api/Label.js
2020-01-16 09:31:12 -08:00

82 lines
3.9 KiB
JavaScript

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { check } from 'meteor/check';
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
import LabelFormats from '/imports/LabelFormats.js';
if(Meteor.isServer) {
const puppeteer = require('puppeteer');
Meteor.methods({
async printLabels(format, title1, title2, ingredients, date) {
let data = {format, title1, title2, ingredients, date};
let params = "";
let url = Meteor.absoluteUrl("/PrintLabel");
//Switch to the static page - for some reason the Meteor page is not loading correctly (it just appears blank).
//url = Meteor.absoluteUrl("/LabelPrint.html");
params = Object.keys(data).map(function(k) {
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
}).join('&');
url += "?" + params;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url, {waitUntil: 'networkidle0'}); //, {waitUntil: 'networkidle0'}
const pdf = await page.pdf({width: LabelFormats[format].width, height: LabelFormats[format].height}); //path: 'C:\\Users\\Grumpy\\label.pdf',
//const pdf = await page.pdf({format: 'A4'});
//await page.pdf({path: 'C:\\Users\\Grumpy\\label.pdf', width: '6in', height: '4in'});
await browser.close();
return new Uint8Array(pdf, 0, pdf.length);
}
});
//Returns a JSON containing a denormalized list of products {product_id, measure_id, product_name, measure_name, price, }
WebApp.connectHandlers.use("/labels/GetBarCodeData", (req, res, next) => {
try {
let barcodes = Meteor.collections.Barcodes.find({}, {fields: {_id: 0, barcodeId: 1, productAndMeasureId: 1}});
let measures = Meteor.collections.Measures.find({}, {fields: {_id: 1, name: 1}, sort: {order: 1}}).fetch();
//Note: Price data looks like this: {XZ5Z3CM49NDrJNADA /* MeasureID */: {price: 10.5, effectiveDate: ISODate("2017-01-12T13:14:18.876-08:00"), previousPrice: 9}, ...}
//Measures is an array of MeasureIDs valid for this product.
let products = Meteor.collections.Products.find({}, {fields: {_id: 1, name: 1, measures: 1, prices: 1}, sort: {order: 1}}).fetch();
let measuresById = {};
let barcodesByProductAndMeasureIds = {};
let result = {};
let today = new Date();
for(measure of measures) measuresById[measure._id] = measure;
for(barcode of barcodes) barcodesByProductAndMeasureIds[barcode.productAndMeasureId] = barcode.barcodeId;
for(let product of products) {
for(let measureId of product.measures) {
let measureName = measuresById[measureId] ? measuresById[measureId].name : undefined;
let priceData = product.prices ? product.prices[measureId] : undefined;
let price = (priceData ? (priceData.effectiveDate && moment(priceData.effectiveDate).isAfter(today) ? priceData.previousPrice : priceData.price) : 0); //Get the price based on the effective date - whether we should use the new price or the old.
let barcodeId = barcodesByProductAndMeasureIds[productId + " " + measureId];
//Ignore any product/measure combinations that don't have barcodes.
if(barcodeId) {
result[barcodeId] = {productId: product._id, measureId: measureId, productName: product.name, measureName, price};
}
//TODO: Pass the product & measure data separately from the barcodes also to handle the missing barcode scenario. When a user types in a product name and picks a measure, we can record in the sale the product ID and measure ID, and we can lookup any pricing data.
//Log any errors so we can figure out what is going on.
if(measureName === undefined) {
//Note: We will pass a price of zero if the price is unknown. This should be fine for now.
console.log(product._id + " " + product.name + " references a measure (" + measureId + ") which is not in the measures array.");
}
}
}
res.end(JSON.stringify(result), "JSON");
} catch(err) {
console.log(err);
res.end();
}
});
}