Skip to content

Commit

Permalink
feat(users): shows archived & not archived users in one list
Browse files Browse the repository at this point in the history
  • Loading branch information
Ephigenia committed Mar 26, 2018
1 parent bb0617e commit a1f721d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Delete a single entry

## Users

List user accounts while client-side search in name, email & note, sort by email and list only time_trackers and admins
List user accounts while client-side search in name, email & note, sort by email and list only time_trackers and admins. Archived users will be grey.

mite user --search frank --role admin,time_tracker --sort email

Expand Down
2 changes: 1 addition & 1 deletion source/mite-customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async.parallel([
alignment: 'right',
},
3: {
width: 80,
width: 50,
wrapWord: true,
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/mite-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mite[method](opts, (err, responseData) => {
alignment: 'right',
},
5: {
width: 80,
width: 50,
wrapWord: true,
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/mite-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ mite[method](opts, (err, responseData) => {
alignment: 'right',
},
4: {
width: 80,
width: 50,
wrapWord: true,
}
}
Expand Down
166 changes: 82 additions & 84 deletions source/mite-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const miteApi = require('mite-api');
const chalk = require('chalk');
const tableLib = require('table')
const table = tableLib.table;
const async = require('async');

const pkg = require('./../package.json');
const config = require('./config.js');
Expand Down Expand Up @@ -56,18 +57,6 @@ program
'optional search for users who’s email contains the given query string ' +
'while beeing not case-sensivite. No support multiple values.'
)
.option(
'-l, --limit <limit>',
'optional numeric number of items to return (default 1000)',
null,
((val) => parseInt(val, 10))
)
.option(
'-o, --offset <offset>',
'optional numeric offset of items to return (default 0)',
0,
((val) => parseInt(val, 10))
)
.option(
'--role <role>',
'optional user role to filter, multiple arguments comma-separated',
Expand All @@ -79,98 +68,107 @@ program
})
)
.option(
'-a, --archived',
'When used will only return archived users which are not returned when ' +
'not used',
false
'-a, --archived <value>',
'When used will only show either archived users or not archived users',
((val) => {
if (typeof val !== 'string') {
return val;
}
return ['true', 'yes', 'ja', 'ok', '1'].indexOf(val.toLowerCase()) > -1;
}),
null
)
.parse(process.argv);

const mite = miteApi(config.get());
const opts = {
offset: program.offset
limit: 1000,
offset: 0
};
if (typeof program.limit === 'number') {
opts.limit = program.limit;
}
if (program.email) {
opts.email = program.email;
}
if (program.name) {
opts.name = program.name;
}
let method = 'getUsers';
if (program.archived) {
method = 'getArchivedUsers';
}

// @TODO add client side sorting
// @TODO add client side search

mite[method](opts, (err, results) => {
async.parallel([
async.apply(mite.getUsers, opts),
async.apply(mite.getArchivedUsers, opts),
], (err, results) => {
if (err) {
throw err;
}

const tableData = results.map((row) => {
return row.user;
})
// filter by user roles
.filter((user) => {
if (!program.role) {
return user;
}
return program.role.indexOf(user.role) > -1;
})
// filter users when "search" was used
.filter((user) => {
if (!program.search) {
const allUsers = [].concat(results[0], results[1]);
const tableData = allUsers
.map((row) => row.user)
// filter by archived or not
.filter((user) => {
if (program.archived === null) {
return true;
}
return user.archived === program.archived;
})
// filter by user roles
.filter((user) => {
if (!program.role) {
return user;
}
return program.role.indexOf(user.role) > -1;
})
// filter users when "search" was used
.filter((user) => {
if (!program.search) {
return user;
}
const regexp = new RegExp(program.search, 'i');
const target = [user.name, user.email, user.note].join('');
return target.match(regexp);
})
// optional sort
.sort((u1, u2) => {
if (!program.sort) return 0;
const sortByAttributeName = program.sort;
var val1 = String(u1[sortByAttributeName]).toLowerCase();
var val2 = String(u2[sortByAttributeName]).toLowerCase();
if (val1 > val2) {
return 1;
} else if (val1 < val2) {
return -1;
} else {
return 0;
}
})
// colorize the user name depending on his role
.map((user) => {
switch(user.role) {
case 'admin':
user.name = chalk.yellow(user.name);
break;
case 'owner':
user.name = chalk.red(user.name);
break;
}
return user;
}
const regexp = new RegExp(program.search, 'i');
const target = [user.name, user.email, user.note].join('');
return target.match(regexp);
})
// optional sort
.sort((u1, u2) => {
if (!program.sort) return 0;
const sortByAttributeName = program.sort;
var val1 = String(u1[sortByAttributeName]).toLowerCase();
var val2 = String(u2[sortByAttributeName]).toLowerCase();
if (val1 > val2) {
return 1;
} else if (val1 < val2) {
return -1;
} else {
return 0;
}
})
// colorize the user name depending on his role
.map((user) => {
switch(user.role) {
case 'admin':
user.name = chalk.yellow(user.name);
break;
case 'owner':
user.name = chalk.red(user.name);
break;
}
return user;
})
.map((user, index) => {
return [
index + 1,
user.id,
user.role,
user.name,
user.email,
user.note.replace(/\r?\n/g, ' ')
];
});
})
.map((user, index) => {
return [
user.id,
user.role,
user.name,
user.email,
user.note.replace(/\r?\n/g, ' ')
].map((v) => {
if (user.archived) {
return chalk.grey(v);
}
return v;
});
});

// table header
tableData.unshift([
'#',
'id',
'role',
'name',
Expand All @@ -182,7 +180,6 @@ mite[method](opts, (err, results) => {
border: tableLib.getBorderCharacters('norc'),
columns: {
0: {
width: 4,
alignment: 'right',
},
1: {
Expand All @@ -196,4 +193,5 @@ mite[method](opts, (err, results) => {
}
};
console.log(table(tableData, tableConfig));

});

0 comments on commit a1f721d

Please sign in to comment.