Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 62 additions & 57 deletions dist/paratii.core.users.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _schemas = require('./schemas.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var joi = require('joi');
Expand All @@ -46,36 +48,35 @@ var ParatiiUsers = exports.ParatiiUsers = function () {
this.config = config;
}
/**
* @typedef {Array} userSchema
* @property {string} id the Ethereum address of the user
* @property {string=} name
* @property {string=} email
*/

/**
* Creates a user, fields id, name and email go to the smart contract Users, other fields are stored on IPFS.
* @param {userSchema} options information about the video ( id, name, email ... )
* @return {Promise} the id of the newly created user
* @example let userData = {
* id: '0x12456....',
* name: 'Humbert Humbert',
* email: 'humbert@humbert.ru',
* ipfsData: 'some-hash'
* }
* let result = await paratii.eth.users.create(userData)
* })
* Register the data of this user.
* @param {userSchema} options information about the user ( id, name, email ... )
* @return {Promise} information about the user ( id, name, email ... )
* @example await paratii.core.users.create({
* id: 'some-video-id',
* name: 'some-nickname',
* email: 'some@email.com',
* })
*/


(0, _createClass3.default)(ParatiiUsers, [{
key: 'create',
value: function create(options) {
var paratii, keysForBlockchain, optionsKeys, optionsBlockchain, optionsIpfs, hash;
var result, paratii, keysForBlockchain, optionsKeys, optionsBlockchain, optionsIpfs, hash;
return _regenerator2.default.async(function create$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
// FIXME: do some joi validation here
result = joi.validate(options, _schemas.userSchema, { allowUnknown: false });

if (!result.error) {
_context.next = 3;
break;
}

throw result.error;

case 3:
paratii = this.config.paratii;
keysForBlockchain = ['id', 'name'];
optionsKeys = (0, _keys2.default)(options);
Expand All @@ -89,27 +90,27 @@ var ParatiiUsers = exports.ParatiiUsers = function () {
optionsIpfs[key] = options[key];
}
});
_context.next = 8;
_context.next = 11;
return _regenerator2.default.awrap(paratii.ipfs.local.addJSON(optionsIpfs));

case 8:
case 11:
hash = _context.sent;

optionsBlockchain['ipfsData'] = hash;
// FIXME: add error handling if call to db fails.

if (!(options.email !== undefined)) {
_context.next = 13;
_context.next = 16;
break;
}

_context.next = 13;
_context.next = 16;
return _regenerator2.default.awrap(paratii.db.users.setEmail(options.id, options.email));

case 13:
case 16:
return _context.abrupt('return', paratii.eth.users.create(optionsBlockchain));

case 14:
case 17:
case 'end':
return _context.stop();
}
Expand All @@ -134,39 +135,49 @@ var ParatiiUsers = exports.ParatiiUsers = function () {
}, {
key: 'upsert',
value: function upsert(options) {
var data, userId;
var result, data, userId;
return _regenerator2.default.async(function upsert$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
result = joi.validate(options, _schemas.userSchema, { allowUnknown: false });

if (!result.error) {
_context2.next = 3;
break;
}

throw result.error;

case 3:
data = null;
userId = '';

if (!options.id) {
_context2.next = 7;
_context2.next = 10;
break;
}

userId = options.id;
_context2.next = 6;
_context2.next = 9;
return _regenerator2.default.awrap(this.get(userId));

case 6:
case 9:
data = _context2.sent;

case 7:
case 10:
if (data) {
_context2.next = 11;
_context2.next = 14;
break;
}

return _context2.abrupt('return', this.create(options));

case 11:
case 14:
delete options.id;
return _context2.abrupt('return', this.update(userId, options, data));

case 13:
case 16:
case 'end':
return _context2.stop();
}
Expand Down Expand Up @@ -197,32 +208,15 @@ var ParatiiUsers = exports.ParatiiUsers = function () {
}, {
key: 'update',
value: function update(userId, options) {
var schema, result, error, data, key;
var data, key, result;
return _regenerator2.default.async(function update$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
schema = joi.object({
name: joi.string().default(null).empty(''),
email: joi.string().default(null).empty('')
});
result = joi.validate(options, schema);
error = result.error;

if (!error) {
_context3.next = 5;
break;
}

throw error;

case 5:
options = result.value;

_context3.next = 8;
_context3.next = 2;
return _regenerator2.default.awrap(this.get(userId));

case 8:
case 2:
data = _context3.sent;

for (key in options) {
Expand All @@ -233,13 +227,23 @@ var ParatiiUsers = exports.ParatiiUsers = function () {

data['id'] = userId;

_context3.next = 13;
result = joi.validate(data, _schemas.userSchema, { allowUnknown: false });

if (!result.error) {
_context3.next = 8;
break;
}

throw result.error;

case 8:
_context3.next = 10;
return _regenerator2.default.awrap(this.create(data));

case 13:
case 10:
return _context3.abrupt('return', data);

case 14:
case 11:
case 'end':
return _context3.stop();
}
Expand Down Expand Up @@ -278,6 +282,7 @@ var ParatiiUsers = exports.ParatiiUsers = function () {
newUserRecord = originalUserRecord;

newUserRecord.id = newAccount;

_context4.next = 13;
return _regenerator2.default.awrap(paratii.eth.users.create(newUserRecord));

Expand Down
76 changes: 40 additions & 36 deletions dist/paratii.eth.users.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _schemas = require('./schemas.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var joi = require('joi');
Expand Down Expand Up @@ -57,65 +59,57 @@ var ParatiiEthUsers = exports.ParatiiEthUsers = function () {
}, null, this);
}
/**
* Creates a user
* @param {Object} options information about the user
* @param {string} options.id valid address
* @param {string} options.name name of the user
* @param {string} options.email email of the user
* @param {string} options.ipfsData ipfs hash
* @return {Promise<string>} the id of the newly created user
* See {@link ParatiiCoreUsers#create}
* Register the data of this user.
* @param {ethUserSchema} options information about the user ( id, name ... )
* @return {Promise} information about the user ( id, name, ... )
* @example await paratii.eth.users.create({
* id: 'some-video-id',
* name: 'some-nickname',
* ipfsData: 'ipfsHash',
* })
*/

}, {
key: 'create',
value: function create(options) {
var schema, msg, result, error, contract;
var msg, result, error, contract;
return _regenerator2.default.async(function create$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
schema = joi.object({
id: joi.string(),
name: joi.string().allow('').optional().default(''),
email: joi.string().allow('').optional().default(''),
ipfsData: joi.string()
});

if (this.eth.web3.utils.isAddress(options.id)) {
_context2.next = 4;
_context2.next = 3;
break;
}

msg = 'The "id" argument should be a valid address, not ' + options.id;
throw Error(msg);

case 4:
result = joi.validate(options, schema);
case 3:
result = joi.validate(options, _schemas.ethUserSchema);
error = result.error;

if (!error) {
_context2.next = 8;
_context2.next = 7;
break;
}

throw error;

case 8:
case 7:
options = result.value;

_context2.next = 11;
_context2.next = 10;
return _regenerator2.default.awrap(this.getRegistry());

case 11:
case 10:
contract = _context2.sent;
_context2.next = 14;
return _regenerator2.default.awrap(contract.methods.create(options.id, options.name, options.email, options.ipfsData).send());
_context2.next = 13;
return _regenerator2.default.awrap(contract.methods.create(options.id, options.name, options.ipfsData).send());

case 14:
case 13:
return _context2.abrupt('return', options.id);

case 15:
case 14:
case 'end':
return _context2.stop();
}
Expand Down Expand Up @@ -151,8 +145,7 @@ var ParatiiEthUsers = exports.ParatiiEthUsers = function () {
result = {
id: userId,
name: userInfo[0],
email: userInfo[1],
ipfsData: userInfo[2]
ipfsData: userInfo[1]
};
return _context3.abrupt('return', result);

Expand All @@ -175,28 +168,39 @@ var ParatiiEthUsers = exports.ParatiiEthUsers = function () {
}, {
key: 'update',
value: function update(userId, options) {
var data, key;
var result, error, data, key;
return _regenerator2.default.async(function update$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
options.id = userId;
_context4.next = 3;
result = joi.validate(options, _schemas.ethUserSchema);
error = result.error;

if (!error) {
_context4.next = 5;
break;
}

throw error;

case 5:
_context4.next = 7;
return _regenerator2.default.awrap(this.get(userId));

case 3:
case 7:
data = _context4.sent;

for (key in options) {
data[key] = options[key];
}
_context4.next = 7;
_context4.next = 11;
return _regenerator2.default.awrap(this.create(data));

case 7:
case 11:
return _context4.abrupt('return', data);

case 8:
case 12:
case 'end':
return _context4.stop();
}
Expand Down
Loading