Skip to content

Commit

Permalink
Merge pull request #2 from Emmanuel-Develops/validate-inputs
Browse files Browse the repository at this point in the history
feat: validate frequency and amount inputs
  • Loading branch information
bigmarh authored Aug 15, 2023
2 parents 15e0c16 + 67fbb3a commit 156d989
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
14 changes: 5 additions & 9 deletions activity-generator/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { program } from 'commander';
import { select, input, confirm } from '@inquirer/prompts';
import { v4 } from 'uuid';
import { parse } from 'json2csv';
import { getFrequency, getAmountInSats } from './validation/inputGetters.js';
program.requiredOption('--config <file>');
program.option('--csv');
program.parse();
Expand Down Expand Up @@ -58,11 +59,7 @@ async function buildControlNodes() {
nodeObj[current_node.identity_pubkey] = current_node;
nodeObj[current_node.identity_pubkey].graph = nodeGraph;
node.id = current_node.identity_pubkey;






//create array of possible destintations for node
nodeObj[current_node.identity_pubkey].possible_dests = nodeGraph.nodes.filter((n) => {
return n.pub_key != current_node.identity_pubkey
Expand Down Expand Up @@ -127,11 +124,10 @@ async function promptForActivities() {
}

activity.action = await input({ message: 'What action?', default: "keysend" });
activity.frequency = await input({ message: 'At what time would you like to run this action?', default: 0 });
activity.frequency = parseInt(activity.frequency);

let amount = await input({ message: 'How many sats?', default: 1000 });
activity.amount = parseInt(amount);
activity.frequency = await getFrequency()
activity.amount = await getAmountInSats()

activities.push(activity);

const anotherOne = await confirm({ message: 'Create another one?', default: false });
Expand Down
3 changes: 3 additions & 0 deletions activity-generator/bin/validation/err_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ErrorConfig = {
"POSITIVE_INTEGER": "must be a positive interger"
}
36 changes: 36 additions & 0 deletions activity-generator/bin/validation/inputGetters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { input } from '@inquirer/prompts';
import { ErrorConfig } from './err_config.js';

const getFrequency = async () => {
const errMessage = `Frequency ${ErrorConfig.POSITIVE_INTEGER}`
try {
const freq = await input({ message: 'At what time would you like to run this action?', default: 0 });
if (isNaN(parseInt(freq)) || parseInt(freq) < 1) {
throw new Error(errMessage)
}
return parseInt(freq)
} catch (err) {
if (err.message === errMessage) {
console.error(err.message)
return await getFrequency()
}
}
}
const getAmountInSats = async () => {
const errMessage = `Amount ${ErrorConfig.POSITIVE_INTEGER}`
try {
const amt = await input({ message: 'How many sats?', default: 1000 });
if (isNaN(parseInt(amt)) || parseInt(amt) < 1) {
throw new Error(errMessage)
}
return parseInt(amt)
} catch (err) {
console.error(err.message)
if (err.message === errMessage) {
return await getAmountInSats()
}
}
}


export { getFrequency, getAmountInSats }

0 comments on commit 156d989

Please sign in to comment.