Skip to content

Commit

Permalink
Add in user command for information updates and password reset
Browse files Browse the repository at this point in the history
  • Loading branch information
keithwhor committed Nov 5, 2016
1 parent 2f9dc3d commit 13d2697
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ To get started with stdlib, first make sure you have Node 6.x installed,
the stdlib CLI tools with:

```
$ npm install lib@1.0.1 -g
$ npm install lib -g
```

And you're now ready to start building!
Expand Down
147 changes: 147 additions & 0 deletions cli/commands/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
'use strict';

const Command = require('cmnd').Command;
const APIResource = require('api-res');
const Credentials = require('../credentials.js');

const chalk = require('chalk');

let formatDigits = (num, figs) => {

num = (parseInt(Math.max(num, 0)) || 0).toString();
let zeroes = Math.max(figs - num.length, 0);
return `${Array(zeroes + 1).join('0')}${num}`;

};

let formatDate = function(str) {

let date = new Date(str);
let months = 'January February March April May June July August September October November December'.split(' ');
let ends = ['th', 'st', 'nd', 'rd', 'th'];

let y = chalk.bold(date.getFullYear());
let m = chalk.bold(months[date.getMonth()]);
let d = chalk.bold(date.getDate());
let e = chalk.bold(ends[d] || 'th');
let hh = chalk.bold(formatDigits(date.getHours(), 2));
let mm = chalk.bold(formatDigits(date.getMinutes(), 2));
let ss = chalk.bold(formatDigits(date.getSeconds(), 2));
let ms = chalk.bold(formatDigits(date.valueOf() % 1000, 3));

return `${m} ${d}${e}, ${y} at ${hh}:${mm}:${ss}.${ms}`;

};

class UserCommand extends Command {

constructor() {

super('user');

}

help() {

return {
description: 'Retrieves (and sets) current user information',
flags: {
s: '<key> <value> Sets a specified key-value pair'
},
vflags: {
set: '<key> <value> Sets a specified key-value pair',
'reset-password': '<email> Sends a password reset request for the specified e-mail address'
}
};

}

run(params, callback) {

let host = 'api.polybit.com';
let port = 443;

let hostname = (params.flags.h && params.flags.h[0]) || '';
let matches = hostname.match(/^(https?:\/\/)?(.*?)(:\d+)?$/);

if (hostname && matches) {
host = matches[2];
port = parseInt((matches[3] || '').substr(1) || (hostname.indexOf('https') === 0 ? 443 : 80));
}

let resource = new APIResource(host, port);
resource.authorize(Credentials.read('ACCESS_TOKEN'));

// If resetting password
if (params.vflags['reset-password']) {
let resetEmail = params.vflags['reset-password'][0];
return resource.request('v1/password_reset_requests').create({}, {email: resetEmail}, (err, response) => {

if (err) {
return callback(err);
}

console.log('Password reset e-mail sent. Check the inbox for ' + resetEmail + ' for more details.');
return callback(null);

});
}

let set = params.vflags.set || params.flags.s || [];
let update = null;

if (set.length) {
update = {};
update[set[0]] = set.slice(1).join(' ');
}

let fnComplete = (user, callback) => {

var len = 20;
Object.keys(user).forEach(function(k) {
var alen = Math.max(1, len - k.length + 1);
console.log(k + ': ' + Array(alen).join(' ') + user[k]);
});

console.log();
callback(null);

};

resource.request('v1/users').index({me: true}, (err, response) => {

if (err) {
return callback(err);
}

let user = response.data[0];
if (!user) {
return callback(new Error('We couldn\'t retrieve your user data. Try again shortly.'));
}

if (!update) {
return fnComplete(user, callback);
}

resource.request('v1/users').update(user.id, {}, update, (err, response) => {

if (err) {
return callback(err);
}

let user = response.data[0];
if (!user) {
return callback(new Error('We couldn\'t set your user data. Try again shortly.'));
}

fnComplete(user, callback);

});

});

}

}

module.exports = UserCommand;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lib",
"version": "1.0.1",
"version": "1.0.2",
"description": "Standard Library for Microservices",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 13d2697

Please sign in to comment.