refactor: don't use env for ofac sources
This commit is contained in:
parent
575a411ea3
commit
74a026dc1b
7 changed files with 18 additions and 52 deletions
|
|
@ -11,13 +11,14 @@ const logger = require('../logger')
|
|||
const DOWNLOAD_DIR = path.resolve('/tmp')
|
||||
|
||||
const OFAC_DATA_DIR = process.env.OFAC_DATA_DIR
|
||||
const OFAC_SOURCES_NAMES = process.env.OFAC_SOURCES_NAMES.split(',')
|
||||
const OFAC_SOURCES_URLS = process.env.OFAC_SOURCES_URLS.split(',')
|
||||
|
||||
const ofacSources = _.map(
|
||||
([name, url]) => ({ name, url }),
|
||||
_.zip(OFAC_SOURCES_NAMES, OFAC_SOURCES_URLS)
|
||||
)
|
||||
const OFAC_SOURCES = [{
|
||||
name: 'sdn_advanced',
|
||||
url: 'https://sanctionslistservice.ofac.treas.gov/api/download/sdn_advanced.xml'
|
||||
}, {
|
||||
name: 'cons_advanced',
|
||||
url: 'https://sanctionslistservice.ofac.treas.gov/api/download/cons_advanced.xml'
|
||||
}]
|
||||
|
||||
const mkdir = path =>
|
||||
fs.mkdir(path)
|
||||
|
|
@ -92,39 +93,35 @@ function update () {
|
|||
throw new Error('ofacDataDir must be defined in the environment')
|
||||
}
|
||||
|
||||
if (!ofacSources) {
|
||||
logger.error('ofacSources must be defined in the environment')
|
||||
}
|
||||
|
||||
const OFAC_SOURCES_DIR = path.join(OFAC_DATA_DIR, 'sources')
|
||||
const OFAC_ETAGS_FILE = path.join(OFAC_DATA_DIR, 'etags.json')
|
||||
|
||||
return mkdir(OFAC_DATA_DIR)
|
||||
.then(() => mkdir(OFAC_SOURCES_DIR))
|
||||
.then(() => writeFile(OFAC_ETAGS_FILE, '{}', {encoding: 'utf-8', flag: 'wx'}))
|
||||
.then(() => writeFile(OFAC_ETAGS_FILE, '{}', { encoding: 'utf-8', flag: 'wx' }))
|
||||
.catch(err => {
|
||||
if (err.code === 'EEXIST') return
|
||||
throw err
|
||||
})
|
||||
.then(() => {
|
||||
const promiseOldEtags = readFile(OFAC_ETAGS_FILE, {encoding: 'utf-8'})
|
||||
.then(json => JSON.parse(json))
|
||||
.catch(_ => {
|
||||
logger.error('Can\'t parse etags.json, getting new data...')
|
||||
return {}
|
||||
})
|
||||
const promiseOldEtags = readFile(OFAC_ETAGS_FILE, { encoding: 'utf-8' })
|
||||
.then(json => JSON.parse(json))
|
||||
.catch(_ => {
|
||||
logger.error('Can\'t parse etags.json, getting new data...')
|
||||
return {}
|
||||
})
|
||||
|
||||
const promiseNewEtags = Promise.resolve(ofacSources || [])
|
||||
const promiseNewEtags = Promise.resolve(OFAC_SOURCES || [])
|
||||
.then(sources => Promise.all(_.map(promiseGetEtag, sources))
|
||||
.then(etags => _.map(
|
||||
([source, etag]) => _.set('etag', etag, source),
|
||||
([source, etag]) => _.set('etag', etag, source),
|
||||
_.zip(sources, etags)
|
||||
))
|
||||
)
|
||||
|
||||
return Promise.all([promiseOldEtags, promiseNewEtags])
|
||||
.then(([oldEtags, newEtags]) => {
|
||||
const hasNotChanged = ({name, etag}) => oldEtags[name] === etag
|
||||
const hasNotChanged = ({ name, etag }) => oldEtags[name] === etag
|
||||
|
||||
const downloads = _.flow(
|
||||
_.reject(hasNotChanged),
|
||||
|
|
@ -155,4 +152,4 @@ function update () {
|
|||
})
|
||||
}
|
||||
|
||||
module.exports = {update}
|
||||
module.exports = { update }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue