const Component = require(`vn-print/core/component`); const emailBody = new Component('email-body'); const name = 'backup-printer-selected'; module.exports = { name: name, async serverPrefetch() { const notifications = await this.rawSqlFromDef('previousNotifications', [name]); if (!notifications.length || checkDuplicates(notifications, this.labelerId, this.sectorId)) throw new Error('Previous notification sended with the same parameters'); this.sector = await this.findOneFromDef('sector', [this.sectorId]); if (!this.sector) throw new Error('Something went wrong'); this.labeler = await this.findOneFromDef('printer', [this.labelerId]); this.mainPrinter = await this.findOneFromDef('printer', [this.sector.backupPrinterFk]); this.worker = await this.findOneFromDef('worker', [this.workerId]); }, components: { 'email-body': emailBody.build(), }, props: { labelerId: { type: Number, required: true }, sectorId: { type: Number, required: true }, workerId: { type: Number, required: true } } }; function checkDuplicates(notifications, labelerFk, printerFk) { const criteria = { labelerId: labelerFk, sectorId: printerFk }; const filteredNotifications = notifications.filter(notification => { const paramsObj = JSON.parse(notification.params); return Object.keys(criteria).every(key => criteria[key] === paramsObj[key]); }); return filteredNotifications.length > 1; }