diff --git a/package.json b/package.json index feb8ac7..5898e85 100644 --- a/package.json +++ b/package.json @@ -83,6 +83,7 @@ "koa-compose": "^3.1.0", "koa-router": "^7.0.1", "lodash": "^4.13.1", + "moment": "^2.15.2", "qs": "^6.2.1", "redink": "^1.1.7", "request": "^2.74.0", diff --git a/src/middleware/download/export/brand.js b/src/middleware/download/export/brand.js index f831594..c058e5a 100644 --- a/src/middleware/download/export/brand.js +++ b/src/middleware/download/export/brand.js @@ -5,7 +5,7 @@ import excel from 'excel-export'; const query = (brandId, skip, conn) => ( r.table('listing') - .filter({ brandId }) + .getAll(brandId, { index: 'brandId' }) .merge(row => ({ categories: row('categories').map(cat => ( r.table('category') diff --git a/src/middleware/download/export/submissions.js b/src/middleware/download/export/submissions.js index 204c5a5..5cd38b6 100644 --- a/src/middleware/download/export/submissions.js +++ b/src/middleware/download/export/submissions.js @@ -1,15 +1,105 @@ -// import { find } from 'redink'; -// import { forEach } from 'lodash'; -// import excel from 'excel-export'; -// -// fields: -// fieldid: { -// label: -// name: -// value: -// }, - -export default id => { - console.log('ID'); - return id; +/* eslint-disable max-len */ +import redink from 'redink'; +import r from 'rethinkdb'; +import { forEach } from 'lodash'; +import excel from 'excel-export'; +import moment from 'moment'; + +// test non payment: d885906e-9a1c-4b11-b3b4-75dc37670f9b +// test payment: 36afccc8-a091-4a3e-9b3b-eec5c19136c8 + +const query = (form, conn) => ( + r.table('submission') + .getAll(form, { index: 'formId' }) + .coerceTo('array') + .run(conn) +); + +/** + * Export submission table + * + **/ +export default (form) => { + const fields = [ + 'createdOn', + 'transactionID', + 'items', + 'payment', + 'fields', + ]; + + const config = { + cols: [], + rows: [], + }; + + config.cols = fields.map(field => ({ + caption: field, + type: 'string', + })); + + const findMore = () => ( + query(form, redink().conn()) + .then(rows => { + forEach(rows, row => { + const record = []; + + forEach(fields, field => { + // let input; + if (field === 'fields' && row[field]) { + const fieldInput = []; + + Object.keys(row[field]).forEach(f => { + fieldInput.push(`${row[field][f].label} = ${row[field][f].value}`); + }); + + record.push(fieldInput.join(' | ')); + } else if (field === 'items' && row[field]) { + const itemInput = []; + + Object.keys(row[field]).forEach(item => { + if (row[field][item].quantity > 0) { + itemInput.push( + `${row[field][item].label}, ${row[field][item].quantity}, $${row[field][item].price * row[field][item].quantity}` + ); + } + }); + + record.push(itemInput.join(' | ')); + } else if (field === 'payment' && row[field] && Object.keys(row[field]).length > 1) { + const paymentInput = []; + + paymentInput.push(`firstName = ${row[field].firstName}`); + paymentInput.push(`lastName = ${row[field].lastName}`); + paymentInput.push(`email = ${row[field].email}`); + paymentInput.push(`phone = ${row[field].phone}`); + paymentInput.push(`address = ${row[field].address}`); + paymentInput.push(`city = ${row[field].city}`); + paymentInput.push(`state = ${row[field].state}`); + paymentInput.push(`zip = ${row[field].zip}`); + paymentInput.push(`country = ${row[field].country}`); + + record.push(paymentInput.join(' | ')); + } else if (field === 'createdOn') { + record.push(`${moment(row[field]).format('LLLL')}`); + } else if (row[field] && field !== 'payment') { + record.push(`${row[field]}`); + } else record.push('N/A'); + }); + + config.rows.push(record); + }); + + return rows.length; + }) + ); + + async function main() { + await findMore(); + + const exportFile = excel.execute(config); + return Promise.resolve(new Buffer(exportFile, 'binary')); + } + + return main(); }; diff --git a/src/middleware/download/index.js b/src/middleware/download/index.js index 9a8c275..53945a7 100644 --- a/src/middleware/download/index.js +++ b/src/middleware/download/index.js @@ -14,7 +14,7 @@ export default (ctx, next) => { response.set({ 'Content-Type': 'application/vnd.openxmlformats', - 'Content-Disposition': `attachment; filename=${downloadTable}_${counter}.xlsx`, + 'Content-Disposition': `attachment; filename=${downloadTable}_${counter || 0}.xlsx`, }); switch (downloadTable) {