const UserError = require('vn-loopback/util/user-error'); const moment = require('moment'); const qrcode = require('qrcode'); module.exports = { name: 'item-label-qr', async serverPrefetch() { this.company = await this.findOneFromDef('company', [this.warehouseId]); if (!this.company) throw new UserError(`There is no company associated with that warehouse`); this.date = Date.vnNew(); this.lastBuy = await this.findOneFromDef('lastBuy', [ this.id, this.warehouseId, this.date ]); this.items = await this.rawSqlFromDef('item', [this.copies || 1, this.lastBuy.id]); if (!this.items.length) throw new UserError(`Empty data source`); this.qr = await this.getQr(this.items[0].buyFk); this.date = moment(this.date).format('WW/E'); }, methods: { getQr(data) { data = { company: this.company, user: this.userId, created: this.date, table: 'buy', id: data }; return qrcode.toDataURL(JSON.stringify(data), { margin: 0, errorCorrectionLevel: 'L' }); } }, props: { id: { type: Number, required: true, description: 'The item id' }, warehouseId: { type: Number }, packing: { type: Number }, copies: { type: Number }, userId: { type: Number } } };