diff --git a/package.json b/package.json index 11cb1cc..91bdc5c 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ }, "homepage": "https://github.com/ole-vi/planet-scripts#readme", "dependencies": { + "btoa": "^1.2.1", "request": "^2.88.0" } } diff --git a/update-logs.js b/update-logs.js index 4c5473c..3ec6178 100644 --- a/update-logs.js +++ b/update-logs.js @@ -1,5 +1,6 @@ #!/usr/bin/env node const request = require('request'); +const btoa = require('btoa'); const user = process.argv[2]; const pass = process.argv[3]; @@ -28,11 +29,35 @@ const updateDocs = (docs, db, callback) => { }; const addParentToConfiguration = (configurations) => { - updateDocs(configurations.map(config => config.parentCode = parentCode), () => { + updateDocs(configurations.map(config => ({ ...config, parentCode})), 'configurations', () => { + createConfigurationReplication(configurations[0]); console.log('Configurations updated'); }); } +const createConfigurationReplication = (configuration) => { + const replicationAddress = protocol + '://' + user + ':' + pass + '@localhost:5984/'; + const replicationObject = { + // Name the id always after the local database + '_id': 'configuration_push_' + Date.now(), + 'source': { + 'url': replicationAddress + 'configurations' + }, + 'target': { + 'headers': { + 'Authorization': 'Basic ' + btoa(configuration.adminName + ':' + pass) + }, + 'url': 'https://' + configuration.parentDomain + '/communityregistrationrequests' + }, + 'create_target': false, + 'owner': user, + 'continuous': false + }; + request.post({ uri: couchAddress + '_replicator', body: replicationObject, json: true }, () => { + console.log('Configuration replicating to parent'); + }); +} + const appendPlanetCodes = (array, codeField) => { return array.map(item => ({ ...item, parentCode, [codeField]: code })); };