Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Created September 1, 2020 18:55
Show Gist options
  • Save joshuakfarrar/88670c313208b32092fcb65427e58873 to your computer and use it in GitHub Desktop.
Save joshuakfarrar/88670c313208b32092fcb65427e58873 to your computer and use it in GitHub Desktop.
const http = require('./http');
const API_URL = 'http://localhost:3001';
const API_CURRENT_VERSION = 'v0';
const CURRENT_API_URL = `${API_URL}/api/${API_CURRENT_VERSION}`;
const API = {
entities: {
all: () => http.get({
url: `${CURRENT_API_URL}/entities`
}),
create: (name) => http.post({
url: `${CURRENT_API_URL}/entities`,
data: { name: name }
}),
},
aggregate: aggregateId => {
const aggregateUrl = `${CURRENT_API_URL}/aggregates/${aggregateId}`;
const commandUrl = `${aggregateUrl}/commands`;
const updateValue = command => (path, value) => http.post({
url: `${commandUrl}/${command}`,
data: { payload: { path: path, value: value }}
});
return {
fetch: () => http.get({
url: aggregateUrl
}),
events: (path) => http.get({
url: `${aggregateUrl}/events`,
params: { path: path }
}),
commands: {
setValue: updateValue('set_value'),
addValue: updateValue('add_value'),
removeValue: updateValue('remove_value')
}
};
},
entity: entityId => {
const entityUrl = `${CURRENT_API_URL}/entities/${entityId}`;
const aggregatesUrl = `${entityUrl}/aggregates`;
const commandUrl = `${entityUrl}/commands`;
return {
fetch: () => http.get({ url: entityUrl }),
events: (path) => http.get({
url: `${entityUrl}/events`,
params: { path: path }
}),
aggregates: {
all: () => http.get({ url: aggregatesUrl }),
create: () => http.post({ url: aggregatesUrl})
},
commands: {
createField: (path, type) => http.post({
url: `${commandUrl}/create_field`,
data: { payload: { path: path, type: type }}
}),
destroyField: (path) => http.post({
url: `${commandUrl}/destroy_field`,
data: { payload: { path: path }}
})
}
};
}
};
module.exports = API;
#!/usr/bin/env node
'use strict';
const _ = require('lodash');
const inquirer = require('inquirer');
const ttys = require('ttys');
const { Client } = require('@elastic/elasticsearch');
const API = require('./lib/api');
const util = require('./lib/util');
const client = new Client({ node: 'http://localhost:9200' });
const Administrations = {
VHA: 'vha',
VBA: 'vba'
}
const FACILITY_UNAVAILABLE = 'facility_unavailable';
function processData(json) {
return util.processAsync(json, (results, district) => {
return util.processAsync(_.get(district, 'facilities'), (result, facility) => {
return client.search({
index: 'facilities',
body: { query: { match: { name: facility }}}
}).then(response => inquirer.createPromptModule({ input: ttys.stdin })([{
type: 'list',
name: 'facility',
message: `What facility is ${facility}?`,
choices: _.concat(
_.map(_.get(response, 'body.hits.hits'), hit => {
return {
name: `(${_.get(hit, '_source.unique_id')}) ${_.get(hit, '_source.name')}`,
value: {
id: _.get(hit, '_id'),
unique_id: _.get(hit, '_source.unique_id'),
tags: [`administration:${Administrations.VHA}`, `district:${_.get(district, 'name').toLowerCase()}`]
},
short: _.get(hit, '_source.name')
};
}),
{ name: 'Facility not listed', value: FACILITY_UNAVAILABLE }
)
}]).then(response => {
return new Promise((resolve, reject) => {
if (_.get(response, 'facility') == FACILITY_UNAVAILABLE) return resolve();
else return resolve(Promise.all(_.map(_.get(response, 'facility.tags'), tag => {
return API.aggregate(_.get(response, 'facility.id')).commands.addValue('tags', tag);
})));
});
}));
}).then(util.curry(Array.prototype.concat)(results));
});
};
util.pipe(process.stdin.fd)(data => processData(JSON.parse(data)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment