-
Notifications
You must be signed in to change notification settings - Fork 6
/
user.js
118 lines (102 loc) · 5.24 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/***************************************************************************/
/* Augeo.io is a web application that uses Natural Language Processing to */
/* classify a user's internet activity into different 'skills'. */
/* Copyright (C) 2016 Brian Redd */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/***************************************************************************/
/***************************************************************************/
/* Description: Logic for GITHUB_USEr database collection */
/***************************************************************************/
// Required libraries
var Mongoose = require('mongoose');
// Required local modules
var AugeoDB = require('../../database');
var Logger = require('../../../module/logger');
// Constants
var COLLECTION = 'github_user-collection';
// Global variables
var AugeoUser = AugeoDB.model('AUGEO_USER');
var log = new Logger();
// Schema declaration
var GITHUB_USER = Mongoose.Schema({
accessToken: String,
augeoUser: {type: Mongoose.Schema.Types.ObjectId, ref:'AUGEO_USER'}, // Foreign key
githubId: String,
name: String,
profileImageUrl: String,
screenName: String
});
/***************************************************************************/
/* Static Methods: Accessible at Model level */
/***************************************************************************/
GITHUB_USER.statics.add = function(username, user, logData, callback) {
this.create(user, function(error0, githubUser) {
if(error0) {
log.functionError(COLLECTION, 'add', logData.parentProcess, logData.username,
'Failed to save GITHUB_USER for user with GithubId: ' + (user)?user.githubId:'invalid' + ' Error: ' + error0);
callback();
} else {
log.functionCall(COLLECTION, 'add', logData.parentProcess, logData.username, {'user.githubId':(user)?user.githubId:'invalid'}, 'Saved GITHUB_USER');
// Add reference to AugeoUser
AugeoUser.getUserWithUsername(username, logData, function(augeoUser) {
augeoUser.github = githubUser._id;
augeoUser.save(function(error1) {
if(error1) {
log.functionError(COLLECTION, 'add', logData.parentProcess, logData.username, 'Failed to save AUGEO_USER for username: ' + username + '. Error: ' + error1);
} else {
log.functionCall(COLLECTION, 'add', logData.parentProcess, logData.username, {'username': username});
}
callback(augeoUser);
});
});
}
});
};
GITHUB_USER.statics.getAllUsers = function(logData, callback) {
this.find({}).populate('augeoUser').exec(function(error, users) {
if(error) {
log.functionError(COLLECTION, 'getAllUsers', logData.parentProcess, logData.username, 'Failed to get Github users:' + error);
callback();
} else {
log.functionCall(COLLECTION, 'getAllUsers', logData.parentProcess, logData.username);
callback(users);
}
});
};
GITHUB_USER.statics.getUserWithScreenName = function(screenName, logData, callback) {
this.findOne({'screenName':screenName})
.exec(function(error, githubUser) {
if(error) {
log.functionError(COLLECTION, 'getUserWithScreenName', logData.parentProcess, logData.username,
'Failed to retrieve Github user with screen name: ' + screenName + '. Error: ' + error);
} else {
log.functionCall(COLLECTION, 'getUserWithScreenName', logData.parentProcess, logData.username);
}
callback(githubUser);
});
};
GITHUB_USER.statics.remove = function(augeoId, logData, callback) {
this.findOneAndRemove({augeoUser:augeoId})
.exec(function(error, user) {
if(error) {
log.functionError(COLLECTION, 'remove', logData.parentProcess, logData.username, 'Failed to remove Github user with augeoId: ' + augeoId + '. Error: ' + error);
} else {
log.functionCall(COLLECTION, 'remove', logData.parentProcess, logData.username, {'augeoId':augeoId});
}
callback(user);
});
};
// Declare model
module.exports = AugeoDB.model('GITHUB_USER', GITHUB_USER);