Skip to content

Commit

Permalink
Add submission download
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinbrewer committed Nov 7, 2016
1 parent baa20bf commit 31a399e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/download/export/brand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
118 changes: 104 additions & 14 deletions src/middleware/download/export/submissions.js
Original file line number Diff line number Diff line change
@@ -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();
};
2 changes: 1 addition & 1 deletion src/middleware/download/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 31a399e

Please sign in to comment.