mirror of
https://github.com/bellingcat/datasheet-server.git
synced 2026-06-12 05:18:31 +03:00
add _buildBlueprintsAsnc in Fetcher
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
// FetcherTwo class interfaces with Google Sheet, and saves to a specified db
|
// FetcherTwo class interfaces with Google Sheet, and saves to a specified db
|
||||||
import { google } from 'googleapis'
|
import { google } from 'googleapis'
|
||||||
|
import { buildDesaturated } from './blueprinters'
|
||||||
import {
|
import {
|
||||||
fmtName,
|
fmtName,
|
||||||
fmtBlueprinterTitles,
|
fmtBlueprinterTitles,
|
||||||
@@ -46,18 +47,8 @@ class Fetcher {
|
|||||||
* data structure updates as well. It is the model layer that determines the
|
* data structure updates as well. It is the model layer that determines the
|
||||||
* performance of indexing the blueprints.
|
* performance of indexing the blueprints.
|
||||||
*/
|
*/
|
||||||
this.blueprints = this._indexDbForBlueprints()
|
this.blueprints = null
|
||||||
.then(allUrls => {
|
this._buildBlueprintsAsync() // NB: modifies this.blueprints on completion
|
||||||
const allParts = allUrls.reduce((acc, url) => {
|
|
||||||
if (url.startsWith(this.id)) {
|
|
||||||
const parts = url.split('/')
|
|
||||||
acc.push([ parts[1], parts[2] ])
|
|
||||||
return acc
|
|
||||||
}
|
|
||||||
}, [])
|
|
||||||
console.log(allParts)
|
|
||||||
return {}
|
|
||||||
})
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Google API setup
|
* Google API setup
|
||||||
@@ -69,6 +60,29 @@ class Fetcher {
|
|||||||
this._saveViaBlueprinter = R.curry(this._saveViaBlueprinter)
|
this._saveViaBlueprinter = R.curry(this._saveViaBlueprinter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_buildBlueprintsAsync () {
|
||||||
|
return this.db.index()
|
||||||
|
.then(allUrls => {
|
||||||
|
const allParts = allUrls.reduce((acc, url) => {
|
||||||
|
if (url.startsWith(this.id)) {
|
||||||
|
const parts = url.split('/')
|
||||||
|
acc.push([ parts[1], parts[2] ])
|
||||||
|
return acc
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
return allParts
|
||||||
|
.map(parts => buildDesaturated(
|
||||||
|
this.sheetId,
|
||||||
|
this.sheetName,
|
||||||
|
parts[0],
|
||||||
|
parts[1]
|
||||||
|
))
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
this.blueprints = res
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/** save data under a given tab name via its blueprinter, which generates
|
/** save data under a given tab name via its blueprinter, which generates
|
||||||
* its resource name. Note that this is curried in the constructor.
|
* its resource name. Note that this is curried in the constructor.
|
||||||
*/
|
*/
|
||||||
@@ -87,14 +101,6 @@ class Fetcher {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** index the db and produce appropriate blueprints structure **/
|
|
||||||
_indexDbForBlueprints () {
|
|
||||||
return this.db.index()
|
|
||||||
.then(res => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/** returns a Promise that resolves if access is granted to the account, and rejects otherwise. */
|
/** returns a Promise that resolves if access is granted to the account, and rejects otherwise. */
|
||||||
authenticate (clientEmail, privateKey) {
|
authenticate (clientEmail, privateKey) {
|
||||||
const googleAuth = new google.auth.JWT(clientEmail, null, privateKey, [
|
const googleAuth = new google.auth.JWT(clientEmail, null, privateKey, [
|
||||||
@@ -155,6 +161,7 @@ class Fetcher {
|
|||||||
|
|
||||||
if (Object.keys(this.blueprinters).indexOf(tab) > -1) {
|
if (Object.keys(this.blueprinters).indexOf(tab) > -1) {
|
||||||
const bpConfig = this.blueprinters[tab]
|
const bpConfig = this.blueprinters[tab]
|
||||||
|
|
||||||
if (isFunction(bpConfig)) {
|
if (isFunction(bpConfig)) {
|
||||||
// if bpConfig specifies a single blueprinter
|
// if bpConfig specifies a single blueprinter
|
||||||
return this._saveViaBlueprinter(tab, data, bpConfig)
|
return this._saveViaBlueprinter(tab, data, bpConfig)
|
||||||
|
|||||||
@@ -16,8 +16,12 @@ export const defaultResource = {
|
|||||||
data: []
|
data: []
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildDesaturated (tab, resource) {
|
export function buildDesaturated (sheetId, sheetName, tab, resource) {
|
||||||
const bp = R.clone(defaultBlueprint)
|
const bp = R.clone(defaultBlueprint)
|
||||||
|
bp.name = sheetName
|
||||||
|
bp.id = sheetId
|
||||||
|
bp.resources[tab] = resource
|
||||||
|
return bp
|
||||||
}
|
}
|
||||||
|
|
||||||
// import all default exports from 'blueprinters' folder
|
// import all default exports from 'blueprinters' folder
|
||||||
@@ -33,5 +37,6 @@ fs.readdirSync(normalizedPath).forEach(file => {
|
|||||||
// each file in blueprinters folder available for granular import from here.
|
// each file in blueprinters folder available for granular import from here.
|
||||||
module.exports = Object.assign({
|
module.exports = Object.assign({
|
||||||
defaultBlueprint,
|
defaultBlueprint,
|
||||||
defaultResource
|
defaultResource,
|
||||||
|
buildDesaturated
|
||||||
}, allBps)
|
}, allBps)
|
||||||
|
|||||||
Reference in New Issue
Block a user