Load hive data on user retrieval

This commit is contained in:
Chris Blanchard 2015-08-31 15:46:35 +01:00
parent c1f4679686
commit 1e347d26e5
2 changed files with 23 additions and 6 deletions

View file

@ -5,6 +5,7 @@
*
*/
var _ = require("lodash");
var async = require("async");
var mongoose = require("mongoose");
var Profile = mongoose.model("Profile");
@ -36,13 +37,12 @@ function User (user) {
this.steam.id = this.getSteamId();
this.hive.id = this.getHiveId();
}
}
User.prototype.getSteamId = () => {
if (this.steam.url === null) return null;
var urlId = this.steam.url.match(/\d*$/);
if (!urlId) return null;
if (!urlId || urlId[0].length === 0) return null;
return steam.convertToText(urlId[0]);
};
@ -75,11 +75,28 @@ User.find = (id, callback) => {
if (error) return callback(error);
if (response.statusCode !== 200) return callback(new Error("Unable to auth user against API"));
let user = new User(body);
Profile.findOrCreate(user, (error, profile) => {
async.parallel([
// Retrieve or create user profile from local store
callback => {
Profile.findOrCreate(user, (error, profile) => {
if (error) return callback(error);
user.profile = profile.toJson();
return callback(null, profile);
});
},
// Retrive Hive Stats
callback => {
hiveClient.getUserStats(user, (error, response, body) => {
if (error || response.statusCode !== 200 || body.id === null) return callback();
_.assign(user.hive, body);
return callback(null, body);
});
}
], function (error, result) {
if (error) return callback(error);
user.profile = profile.toJson();
console.log(user);
return callback(null, user);
});
})
});
};

View file

@ -6,6 +6,6 @@ var assert = require("chai").assert;
var fs = require("fs");
var path = require("path");
describe("ENSL Client", function () {
describe("Hive Client", function () {
});