Skip to content

Commit

Permalink
feat(customers): adds list of customers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Mar 26, 2018
1 parent c0464f5 commit 9f634c5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
121 changes: 121 additions & 0 deletions source/mite-customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
#!/usr/bin/env node
'use strict'

const program = require('commander');
const miteApi = require('mite-api');
const chalk = require('chalk');
const tableLib = require('table')
const table = tableLib.table;

const pkg = require('./../package.json');
const config = require('./config.js');
const formater = require('./lib/formater');
const BUDGET_TYPE = formater.BUDGET_TYPE;
const SORT_OPTIONS = [
'id',
'name',
'updated_at',
'created_at',
];
const SORT_OPTIONS_DEFAULT = 'name';

program
.version(pkg.version)
.description('list, filter & search for servuces')
.option(
'--search <query>',
'optional search string which must be somewhere in the services’ name ' +
'(case insensitive)'
)
.option(
'-a, --archived',
'When used will only return archived service which are not returned when ' +
'not used.',
false
)
.option(
'--sort <column>',
`optional column the results should be case-insensitive ordered by `+
`(default: "${SORT_OPTIONS_DEFAULT}"), ` +
`valid values: ${SORT_OPTIONS.join(', ')}`,
(value) => {
if (SORT_OPTIONS.indexOf(value) === -1) {
console.error(
'Invalid value for sort option: "%s", valid values are: ',
value,
SORT_OPTIONS.join(', ')
);
process.exit(2);
}
return value;
},
'name' // default sort
)
.parse(process.argv);

const opts = {
limit: 1000
};
if (program.search) {
opts.name = program.search;
}

let method = 'getCustomers';
if (program.archived) {
method = 'getArchivedCustomers';
}

const mite = miteApi(config.get());
mite[method](opts, (err, responseData) => {
if (err) {
throw err;
}

const tableData = responseData.map((v) => v.customer)
.sort((a, b) => {
if (!program.sort) {
return 0;
}
var val1 = String(a[program.sort]).toLowerCase();
var val2 = String(b[program.sort]).toLowerCase();
if (val1 > val2) {
return 1;
} else if (val1 < val2) {
return -1;
} else {
return 0;
}
})
.map((customer) => {
let rate = formater.budget(BUDGET_TYPE.CENTS, customer.hourly_rate);
if (!customer.hourly_rate) {
rate = '-';
}
return [
customer.id,
customer.name,
rate,
customer.note.replace(/\r?\n/g, ' '),
]
});

tableData.unshift([
'id',
'name',
'rate',
'note'
].map(v=> chalk.bold(v)));
const tableConfig = {
border: tableLib.getBorderCharacters('norc'),
columns: {
2: {
alignment: 'right',
},
3: {
width: 80,
wrapWord: true,
}
}
};
console.log(table(tableData, tableConfig));
});
2 changes: 1 addition & 1 deletion source/mite-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ program
}
return value;
},
'name' // default sor
SORT_OPTIONS_DEFAULT // default sor
)
.option(
'--search <query>',
Expand Down
6 changes: 3 additions & 3 deletions source/mite-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const SORT_OPTIONS_DEFAULT = 'name';

program
.version(pkg.version)
.description('list, filter & search for projects')
.description('list, filter & search for servuces')
.option(
'--search <query>',
'optional search string which must be somewhere in the services’ name ' +
'(case insensitive)'
)
.option(
'-a, --archived',
'When used will only return archived projects which are not returned when ' +
'When used will only return archived service which are not returned when ' +
'not used.',
false
)
Expand All @@ -50,7 +50,7 @@ program
}
return value;
},
'name' // default sor
SORT_OPTIONS_DEFAULT // default sor
)
.option(
'--billable <true|false>',
Expand Down
2 changes: 2 additions & 0 deletions source/mite.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ program
.command('users', 'list, filter & search for users')
.command('projects', 'list, filter & search projects')
.command('services', 'list, filter & search services')
.command('customers', 'list, filter & search customers')
.alias('clients')
.description(pkg.description)
.parse(process.argv)

0 comments on commit 9f634c5

Please sign in to comment.