@velocitycareerlabs/data-loader 1.19.0-dev-build.11b7cc7b6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +248 -0
- package/jest.config.js +7 -0
- package/package.json +43 -0
- package/src/batch-issuing/README.md +95 -0
- package/src/batch-issuing/constants.js +8 -0
- package/src/batch-issuing/fetchers.js +100 -0
- package/src/batch-issuing/file-readers.js +15 -0
- package/src/batch-issuing/file-writers.js +37 -0
- package/src/batch-issuing/index.js +115 -0
- package/src/batch-issuing/orchestrators.js +258 -0
- package/src/batch-issuing/prepare-data.js +208 -0
- package/src/batch-issuing/prompts.js +47 -0
- package/src/batch-issuing/validate-directory-exists.js +11 -0
- package/src/data-loader.js +14 -0
- package/src/helpers/common.js +30 -0
- package/src/helpers/compute-activation-date.js +12 -0
- package/src/helpers/index.js +6 -0
- package/src/helpers/load-csv.js +29 -0
- package/src/helpers/load-handlebars-template.js +9 -0
- package/src/index.js +3 -0
- package/src/vendor-credentials/README.md +32 -0
- package/src/vendor-credentials/execute-update.js +32 -0
- package/src/vendor-credentials/index.js +53 -0
- package/src/vendor-credentials/prepare-data.js +40 -0
- package/test/batch-issuing.test.js +916 -0
- package/test/data/badge-offer.template.json +22 -0
- package/test/data/batch-vars-offerids.csv +3 -0
- package/test/data/batch-vars.csv +3 -0
- package/test/data/batchissuing-variables.csv +3 -0
- package/test/data/driver-license-offer.template.json +10 -0
- package/test/data/driver-license-variables.csv +3 -0
- package/test/data/email-offer.template.json +10 -0
- package/test/data/email-variables.csv +3 -0
- package/test/data/id-document-offer.template.json +10 -0
- package/test/data/id-document-variables.csv +3 -0
- package/test/data/national-id-card-offer.template.json +10 -0
- package/test/data/national-id-card-variables.csv +3 -0
- package/test/data/offer.template.json +22 -0
- package/test/data/passport-offer.template.json +10 -0
- package/test/data/passport-variables.csv +3 -0
- package/test/data/person.template.json +15 -0
- package/test/data/phone-offer.template.json +10 -0
- package/test/data/phone-variables.csv +2 -0
- package/test/data/phones-batch-vars-offerids.csv +3 -0
- package/test/data/proof-of-age-offer.template.json +10 -0
- package/test/data/proof-of-age-variables.csv +3 -0
- package/test/data/resident-permit-offer.template.json +10 -0
- package/test/data/resident-permit-variables.csv +3 -0
- package/test/data/variables-no-did.csv +3 -0
- package/test/data/variables.csv +3 -0
- package/test/vendor-credentials.test.js +225 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
const fs = require('fs');
|
2
|
+
const csv = require('csv-parser');
|
3
|
+
const { map } = require('lodash/fp');
|
4
|
+
|
5
|
+
const loadCsv = (fileName) => {
|
6
|
+
return new Promise((resolve, reject) => {
|
7
|
+
const results = [];
|
8
|
+
fs.createReadStream(fileName)
|
9
|
+
.pipe(csv())
|
10
|
+
.on('data', (data) => {
|
11
|
+
// eslint-disable-next-line better-mutation/no-mutating-methods
|
12
|
+
results.push(data);
|
13
|
+
})
|
14
|
+
.on('err', (err) => reject(err))
|
15
|
+
.on('end', () => {
|
16
|
+
resolve(results);
|
17
|
+
});
|
18
|
+
});
|
19
|
+
};
|
20
|
+
|
21
|
+
const mergeRowsWithDefaults = (defaultVars) =>
|
22
|
+
map((csvRow) => ({ ...csvRow, ...defaultVars }));
|
23
|
+
|
24
|
+
const loadCsvVariableSets = async (fileName, defaultVars) => {
|
25
|
+
const csvRows = await loadCsv(fileName);
|
26
|
+
return mergeRowsWithDefaults(defaultVars)(csvRows);
|
27
|
+
};
|
28
|
+
|
29
|
+
module.exports = { loadCsv, loadCsvVariableSets, mergeRowsWithDefaults };
|
@@ -0,0 +1,9 @@
|
|
1
|
+
const Handlebars = require('handlebars');
|
2
|
+
const { readFile } = require('./common');
|
3
|
+
|
4
|
+
const loadHandlebarsTemplate = (filename) => {
|
5
|
+
const templateString = readFile(filename);
|
6
|
+
return Handlebars.compile(templateString);
|
7
|
+
};
|
8
|
+
|
9
|
+
module.exports = { loadHandlebarsTemplate };
|
package/src/index.js
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# vendor-credentials command
|
2
|
+
|
3
|
+
Used for creating users and then attaching them to specific credentials.
|
4
|
+
|
5
|
+
- Supports new users only, not matching to existing users
|
6
|
+
- Credentials & People must be specified as a handlebars templates. The data is populated from two sources
|
7
|
+
- the csv specified with variable names the same as the headers
|
8
|
+
- any `-v` arg parsed in
|
9
|
+
|
10
|
+
Checkout the [test data](../test/data).
|
11
|
+
|
12
|
+
## How to Use
|
13
|
+
`data-loader vendorcreds [options]`
|
14
|
+
### Options
|
15
|
+
|
16
|
+
`-c [CSV_FILENAME]` required parameter containing the csv file
|
17
|
+
|
18
|
+
`-o [OFFER_TEMPLATE_FILENAME]` offer handlebars template. Use moustaches around variables such as `{{did}}`
|
19
|
+
|
20
|
+
`-p [PERSON_TEMPLATE_FILENAME]` user handlebars template. Use moustaches around variables such as `{{email}}`.
|
21
|
+
If -p is left out, no users will be created; only offers will be created.
|
22
|
+
|
23
|
+
`--var=[VAR_NAME]=[VAR_VALUE]` variables used in the templates can be specified on the command line. They override any csv values
|
24
|
+
|
25
|
+
`-e [URL]` the endpoint of the mockvendor server
|
26
|
+
|
27
|
+
`-t [AUTH_TOKEN]` the bearer token to use when calling the mockvendor server
|
28
|
+
|
29
|
+
### Dry Run Example
|
30
|
+
Dry runs that print out what updates will be issued should omit the `-e` prop
|
31
|
+
|
32
|
+
`./data-loader vendorcreds -c ./test/data/variables.csv -o ./test/data/offer.template.json -p ./test/data/person.template.json --var=did=did:ion:sap456`
|
@@ -0,0 +1,32 @@
|
|
1
|
+
const got = require('got');
|
2
|
+
const { printInfo } = require('../helpers/common');
|
3
|
+
|
4
|
+
const setupGot = ({ endpoint, authToken }) => {
|
5
|
+
const options = {
|
6
|
+
prefixUrl: endpoint,
|
7
|
+
};
|
8
|
+
if (authToken != null) {
|
9
|
+
options.headers = { Authorization: `Bearer ${authToken}` };
|
10
|
+
}
|
11
|
+
return got.extend(options);
|
12
|
+
};
|
13
|
+
|
14
|
+
const initExecuteUpdate = (options) => {
|
15
|
+
const vendorGot = setupGot(options);
|
16
|
+
return async ({ person, offer }) => {
|
17
|
+
if (person) {
|
18
|
+
printInfo({
|
19
|
+
createdPerson: await vendorGot
|
20
|
+
.post('api/users', { json: person })
|
21
|
+
.json(),
|
22
|
+
});
|
23
|
+
}
|
24
|
+
printInfo({
|
25
|
+
createdOffer: await vendorGot.post('api/offers', { json: offer }).json(),
|
26
|
+
});
|
27
|
+
};
|
28
|
+
};
|
29
|
+
|
30
|
+
module.exports = {
|
31
|
+
initExecuteUpdate,
|
32
|
+
};
|
@@ -0,0 +1,53 @@
|
|
1
|
+
/* eslint-disable no-await-in-loop */
|
2
|
+
|
3
|
+
const { program } = require('commander');
|
4
|
+
const { reduce } = require('lodash/fp');
|
5
|
+
const { prepareData } = require('./prepare-data');
|
6
|
+
const { printInfo } = require('../helpers/common');
|
7
|
+
const { initExecuteUpdate } = require('./execute-update');
|
8
|
+
|
9
|
+
const parseVar = reduce((acc, pair) => {
|
10
|
+
const [key, value] = pair.split('=');
|
11
|
+
acc[key] = value;
|
12
|
+
return acc;
|
13
|
+
}, {});
|
14
|
+
|
15
|
+
program
|
16
|
+
.name('data-loader vendorcreds')
|
17
|
+
.description('Loads data into a db')
|
18
|
+
.usage('[options]')
|
19
|
+
.requiredOption(
|
20
|
+
'-c, --csv-filename <filename>',
|
21
|
+
'File name containing variables'
|
22
|
+
)
|
23
|
+
.requiredOption(
|
24
|
+
'-o, --offer-template-filename <filename>',
|
25
|
+
'File name containing the credential template file'
|
26
|
+
)
|
27
|
+
.option(
|
28
|
+
'-p, --person-template-filename <filename>',
|
29
|
+
'File name containing the credential template file'
|
30
|
+
)
|
31
|
+
.option(
|
32
|
+
'-e, --endpoint <url>',
|
33
|
+
'Endpoint to call to upload the people and credentials to'
|
34
|
+
)
|
35
|
+
.option('-t, --auth-token <url>', 'Bearer Auth Token to use')
|
36
|
+
.option('-l, --label <label>', 'A label to attach to the documents inserted')
|
37
|
+
.option('-v, --var <var...>', 'A variable to add. use name=value')
|
38
|
+
.action(async () => {
|
39
|
+
const options = program.opts();
|
40
|
+
// eslint-disable-next-line better-mutation/no-mutation
|
41
|
+
options.vars = parseVar(options.var);
|
42
|
+
printInfo(options);
|
43
|
+
const executeUpdate = initExecuteUpdate(options);
|
44
|
+
const updates = await prepareData(options);
|
45
|
+
if (options.endpoint != null) {
|
46
|
+
for (const update of updates) {
|
47
|
+
await executeUpdate(update);
|
48
|
+
}
|
49
|
+
} else {
|
50
|
+
printInfo(JSON.stringify({ updates }));
|
51
|
+
}
|
52
|
+
})
|
53
|
+
.parseAsync(process.argv);
|
@@ -0,0 +1,40 @@
|
|
1
|
+
const { map } = require('lodash/fp');
|
2
|
+
const {
|
3
|
+
loadHandlebarsTemplate,
|
4
|
+
} = require('../helpers/load-handlebars-template');
|
5
|
+
const { loadCsvVariableSets } = require('../helpers/load-csv');
|
6
|
+
|
7
|
+
const prepareData = async ({
|
8
|
+
csvFilename,
|
9
|
+
offerTemplateFilename,
|
10
|
+
personTemplateFilename,
|
11
|
+
label,
|
12
|
+
vars: defaultVars,
|
13
|
+
}) => {
|
14
|
+
const variableSets = await loadCsvVariableSets(csvFilename, defaultVars);
|
15
|
+
const offerTemplate = loadHandlebarsTemplate(offerTemplateFilename);
|
16
|
+
const personTemplate = personTemplateFilename
|
17
|
+
? loadHandlebarsTemplate(personTemplateFilename)
|
18
|
+
: undefined;
|
19
|
+
return map((variableSet) => {
|
20
|
+
return {
|
21
|
+
offer: prepareDocument({ template: offerTemplate, variableSet, label }),
|
22
|
+
person: personTemplate
|
23
|
+
? prepareDocument({ template: personTemplate, variableSet, label })
|
24
|
+
: undefined,
|
25
|
+
};
|
26
|
+
}, variableSets);
|
27
|
+
};
|
28
|
+
|
29
|
+
const prepareDocument = ({ template, variableSet, label }) => {
|
30
|
+
const offerString = template(variableSet);
|
31
|
+
const json = JSON.parse(offerString);
|
32
|
+
return {
|
33
|
+
...json,
|
34
|
+
label,
|
35
|
+
};
|
36
|
+
};
|
37
|
+
|
38
|
+
module.exports = {
|
39
|
+
prepareData,
|
40
|
+
};
|