ensl_gathers/lib/user/user.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-22 23:30:14 +00:00
"use strict";
/*
* Implements User Model
*
*/
2015-08-28 14:00:14 +00:00
var client = require("../ensl/client")();
var mongoose = require("mongoose");
var Profile = mongoose.model("Profile");
2015-07-22 23:30:14 +00:00
2015-08-28 14:00:14 +00:00
function User (user) {
this.id = user['id'];
this.online = true;
2015-07-22 23:30:14 +00:00
this.username = user['username'];
this.country = user['country'];
this.time_zone = user['time_zone'];
this.avatar = client.baseUrl + user['avatar'];
this.admin = user['admin'];
2015-08-05 00:06:09 +00:00
this.team = user['team'];
2015-08-27 12:06:54 +00:00
this.bans = user['bans'];
2015-08-28 20:28:50 +00:00
this.steam = {
url: user['steam']['url'] || null,
nickname: user['steam']['nickname'] || null
};
2015-08-28 14:00:14 +00:00
this.profile = null;
}
2015-07-22 23:30:14 +00:00
2015-08-28 20:28:50 +00:00
User.prototype.updateProfile = function (data, callback) {
let self = this;
Profile.update({
userId: self.id
}, data, function (error) {
if (error) return callback(error);
Profile.findOne({userId: self.id}, function (error, profile) {
if (error) return callback(error);
self.profile = profile.toJson();
return callback(error, profile);
});
});
};
2015-08-28 14:00:14 +00:00
User.find = (id, callback) => {
client.getUserById({
id: id
}, (error, response, body) => {
if (error) return callback(error);
if (response.statusCode !== 200) return callback(new Error("Unable to auth user against API"));
2015-08-28 14:17:08 +00:00
let user = new User(body);
Profile.findOrCreate(user, function (error, profile) {
2015-08-28 14:00:14 +00:00
if (error) return callback(error);
2015-08-28 14:17:08 +00:00
user.profile = profile.toJson();
return callback(null, user);
2015-08-28 14:00:14 +00:00
});
});
};
module.exports = User;