mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-10 20:38:32 +03:00
New export route working to query all blueprints and add to one data object; need to write to file
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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}`,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user