const Component = require(`${appPath}/core/component`); const db = require(`${appPath}/core/database`); const reportHeader = new Component('report-header'); const reportFooter = new Component('report-footer'); module.exports = { name: 'campaign-metrics', async serverPrefetch() { this.client = await this.fetchClient(this.recipientId); this.sales = await this.fetchSales(this.recipientId, this.from, this.to); if (!this.client) throw new Error('Something went wrong'); }, methods: { fetchClient(clientId) { return db.findOne( `SELECT c.street, c.socialName, c.city, c.postcode, c.id, c.name AS clientName, p.name AS province, co.country FROM client c JOIN province p ON c.provinceFk = p.id JOIN country co ON c.countryFk = co.id WHERE c.id = ?`, [clientId]); }, fetchSales(clientId, from, to) { return db.rawSql( `SELECT SUM(s.quantity) AS subtotal, s.itemFk, s.concept, i.subName, i.tag5, i.value5, i.tag6, i.value6, i.tag7, i.value7 FROM sale s JOIN ticket t ON t.id = s.ticketFk JOIN item i ON i.id = s.itemFk JOIN itemType it ON it.id = i.typeFk WHERE t.clientFk = ? AND it.isPackaging = FALSE AND DATE(t.shipped) BETWEEN ? AND ? GROUP BY s.itemFk ORDER BY i.typeFk , i.name`, [clientId, from, to]); }, }, components: { 'report-header': reportHeader.build(), 'report-footer': reportFooter.build() }, props: { recipientId: { required: true }, from: { required: true }, to: { required: true } } };