2019-10-07 15:51:50 -07:00
import { Meteor } from 'meteor/meteor' ;
import { Mongo } from 'meteor/mongo' ;
import { check } from 'meteor/check' ;
2025-07-02 11:18:09 -07:00
import 'meteor/aldeed:collection2/static'
import SimpleSchema from 'meteor/aldeed:simple-schema' ;
2020-01-16 09:31:12 -08:00
import LabelFormats from '/imports/LabelFormats.js' ;
2019-10-07 15:51:50 -07:00
if ( Meteor . isServer ) {
const puppeteer = require ( 'puppeteer' ) ;
Meteor . methods ( {
2020-01-16 09:31:12 -08:00
async printLabels ( format , title1 , title2 , ingredients , date ) {
let data = { format , title1 , title2 , ingredients , date } ;
2019-10-07 15:51:50 -07:00
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'}
2020-01-16 09:31:12 -08:00
const pdf = await page . pdf ( { width : LabelFormats [ format ] . width , height : LabelFormats [ format ] . height } ) ; //path: 'C:\\Users\\Grumpy\\label.pdf',
2019-10-07 15:51:50 -07:00
//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, }
2025-07-02 11:18:09 -07:00
WebApp . connectHandlers . use ( "/labels/GetBarCodeData" , async ( req , res , next ) => {
2019-10-07 15:51:50 -07:00
try {
let barcodes = Meteor . collections . Barcodes . find ( { } , { fields : { _id : 0 , barcodeId : 1 , productAndMeasureId : 1 } } ) ;
2025-07-02 11:18:09 -07:00
let measures = await Meteor . collections . Measures . find ( { } , { fields : { _id : 1 , name : 1 } , sort : { order : 1 } } ) . fetchAsync ( ) ;
2019-10-07 15:51:50 -07:00
//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.
2025-07-02 11:18:09 -07:00
let products = await Meteor . collections . Products . find ( { } , { fields : { _id : 1 , name : 1 , measures : 1 , prices : 1 } , sort : { order : 1 } } ) . fetchAsync ( ) ;
2019-10-07 15:51:50 -07:00
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 ( ) ;
}
} ) ;
}