Skip to content

Commit

Permalink
feat(services): adds list of services
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Mar 26, 2018
1 parent 0a32f3a commit c0464f5
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Simple CLI tool for creating, listing, starting and stopping time tracking entries in [Mite](https://mite.yo.lk).
Simple CLI tool for creating, listing, starting and stopping time tracking entries in [Mite](https://mite.yo.lk) using the [mite-api](https://www.npmjs.com/package/mite-api) npm package which is using the [official mite api](https://mite.yo.lk/api/index.html)

[![MIT License](https://badges.frapsoft.com/os/mit/mit.svg?v=102)](https://github.com/ellerbrock/open-source-badge/)
[![NPM Package](https://badge.fury.io/js/mite-cli.svg)](https://www.npmjs.com/package/mite-cli)
Expand Down
4 changes: 2 additions & 2 deletions source/mite-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ program
.parse(process.argv);

const opts = {
'limit': 10000
limit: 10000
};
if (program.customer_id) {
opts.customer_id = program.customer_id;
Expand Down Expand Up @@ -108,7 +108,7 @@ mite[method](opts, (err, responseData) => {
data.customer_name + ' (' + data.customer_id + ')',
budget,
rate,
data.note.replace(/[\r\n]+/, ' '),
data.note.replace(/\r?\n/g, ' '),
];
});
tableData.unshift([
Expand Down
138 changes: 138 additions & 0 deletions source/mite-services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#!/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',
'hourly_rate'
];
const SORT_OPTIONS_DEFAULT = 'name';

program
.version(pkg.version)
.description('list, filter & search for projects')
.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 ' +
'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 sor
)
.option(
'--billable <true|false>',
'wheter to show only billable or not-billable entries, no filter is used ' +
'when argument is not used',
((val) => {
if (typeof val !== 'string') {
return val;
}
return ['true', 'yes', 'ja', 'ok', '1'].indexOf(val.toLowerCase()) > -1;
}),
null
)
.parse(process.argv);

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

let method = 'getServices';
if (program.archived) {
method = 'getArchivedServices';
}

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

const tableData = responseData.map((v) => v.service)
.filter((a) => {
if (program.billable === null) {
return true;
}
return program.billable === a.billable;
})
.sort((a, b) => {
if (!program.sort) {
return 0;
}
const sortByAttributeName = program.sort;
var val1 = String(a[sortByAttributeName]).toLowerCase();
var val2 = String(b[sortByAttributeName]).toLowerCase();
if (val1 > val2) {
return 1;
} else if (val1 < val2) {
return -1;
} else {
return 0;
}
})
.map((service) => {
return [
service.id,
service.name,
service.billable ? 'yes' : 'no',
formater.budget(BUDGET_TYPE.CENTS, service.hourly_rate),
service.note.replace(/\r?\n/g, ' '),
];
});
tableData.unshift([
'id',
'name',
'billable',
'rate',
'note',
].map(v=> chalk.bold(v)));
const tableConfig = {
border: tableLib.getBorderCharacters('norc'),
columns: {
3: {
alignment: 'right',
},
4: {
width: 80,
wrapWord: true,
}
}
};
console.log(table(tableData, tableConfig));
});
2 changes: 1 addition & 1 deletion source/mite-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mite[method](opts, (err, results) => {
user.role,
user.name,
user.email,
user.note
user.note.replace(/\r?\n/g, ' ')
];
});

Expand Down
1 change: 1 addition & 0 deletions source/mite.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ program
.command('start', 'start the tracker for the given id, will also stop allready running entry')
.command('users', 'list, filter & search for users')
.command('projects', 'list, filter & search projects')
.command('services', 'list, filter & search services')
.description(pkg.description)
.parse(process.argv)

0 comments on commit c0464f5

Please sign in to comment.