diff --git a/src/api/index.js b/src/api/index.js index 8bd5d43..b2d48a3 100755 --- a/src/api/index.js +++ b/src/api/index.js @@ -5,6 +5,7 @@ import copy from '../copy/en' export default ({ config, controller }) => { let api = Router() + const fileDest = config.EXPORT_FILE_DEST || "" api.get('/', (req, res) => { res.json({ @@ -24,15 +25,8 @@ export default ({ config, controller }) => { }) api.get('/export', (req, res) => { - const bps = controller.blueprints() - const bpsParsed = bps.map(bp => ({ - sheet: bp.sheet.name, - tab: bp.name, - resources: bp.resources, - url: bp.urls[0] - })) controller - .retrieveAll(bpsParsed) + .retrieveAll(fileDest) .then(msg => res.json({ success: msg diff --git a/src/copy/en.js b/src/copy/en.js index 8c3350b..2795462 100644 --- a/src/copy/en.js +++ b/src/copy/en.js @@ -1,6 +1,7 @@ export default { errors: { update: 'The server could not update. Check your API credentials and internet connection and try again.', + export: 'The server could not export. Check that you provided a file path to export to and try again.', onlySheet: 'You cannot query a sheet directly. The URL needs to be in the format /:sheet/:tab/:resource.', onlyTab: 'You cannot query a tab directly. The URL needs to be in the format /:sheet/:tab/:resource.', noSheet: sheet => `The sheet ${sheet} is not available in this server.`, @@ -9,6 +10,7 @@ export default { modelLayer: prts => `Something went wrong at the model layer` }, success: { - update: 'All sheets updated' + update: 'All sheets updated', + export: dest => `All resources exported to the file: ${dest}`, } } diff --git a/src/lib/Controller.js b/src/lib/Controller.js index c6b8468..3f98270 100644 --- a/src/lib/Controller.js +++ b/src/lib/Controller.js @@ -39,8 +39,29 @@ class Controller { }) } - retrieveAll (blueprints) { - // index through bps, grab data and add to existing data object with value for url name and data points for data object, write to file and return success msg; catch errors appropriately + retrieveAll (fileDest) { + if (fileDest === '') throw new Error(copy.errors.export) + + const indexedData = {} + const urls = [] + + const bps = this.blueprints() + return Promise.all( + bps.map(bp => { + const resource = Object.keys(bp.resources)[0] + urls.push(bp.urls[0]) + return this.retrieve(bp.sheet.name, bp.name, resource) + }) + ).then(results => { + if (results.every(res => res)) { + urls.forEach((item, idx) => { + indexedData[item] = results[idx] + }) + return 'Success' + } else { + throw new Error(copy.errors.export) + } + }) } retrieve (sheet, tab, resource) {