-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoUser.js
153 lines (136 loc) · 3.64 KB
/
NoUser.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// NoUser.js
// Description:
// "NoUser.js" is a advanced user system for NoService.
// Copyright 2018 NOOXY. All Rights Reserved.
let Utils;
'use strict';
const USER_MODEL_NAME = 'User';
// the nouser module
function NoUser() {
let _model_module;
let _user_model;
let countries;
// import database from specified path
this.importModel = (model, callback)=> {
_model_module = model;
model.exist(USER_MODEL_NAME, (err, has_model)=> {
if(err) {
callback(err);
}
else if(!has_model) {
model.define(USER_MODEL_NAME, {
model_type: "Object",
do_timestamp: true,
model_key: "userid",
structure: {
userid: 'VARCHAR(255)',
email: 'VARCHAR(320)',
gender: 'VARCHAR(1)',
address: 'TEXT',
phonenumber: 'VARCHAR(50)',
birthday: 'DATE',
country: 'VARCHAR(160)',
aboutme: 'TEXT'
}
}, (err, user_model)=> {
_user_model = user_model;
callback(err);
});
}
else {
model.get(USER_MODEL_NAME, (err, user_model)=> {
_user_model = user_model;
callback(false);
});
}
});
};
this.importUtils = (utils)=> {
Utils = utils;
};
// import countries by list
this.importCountries = (list) => {
countries = list;
};
this.getUserMeta = (userid, callback) => {
_user_model.get(userid, (err, user) => {
if(user) {
let user_meta = {
userid: user.userid,
email: user.email,
gender : user.gender,
phonenumber : user.phonenumber,
birthday : user.birthday,
country : user.country,
address : user.address,
aboutme : user.aboutme
}
try {
user_meta.gender = user_meta.gender.replace('M', 'male');
user_meta.gender = user_meta.gender.replace('F', 'female');
user_meta.gender = user_meta.gender.replace('O', 'other');
}
catch(e) {
}
callback(false, user_meta);
}
else {
callback(false, null);
}
});
};
this.updateUser = (userid, jsondata, callback) => {
let pwdhash = null;
try {
jsondata.gender = jsondata.gender.replace('female', 'F');
jsondata.gender = jsondata.gender.replace('male', 'M');
jsondata.gender = jsondata.gender.replace('other', 'O');
}
catch (e) {
}
try{
if(!Utils.validateEmail(jsondata.email)) {
let err = new Error("Email invalid.");
callback(err);
}
else if(jsondata.gender!='M'&&jsondata.gender!='F'&&jsondata.gender!='O') {
let err = new Error("Gender invalid.");
callback(err);
}
else if(isNaN(Date.parse(jsondata.birthday))) {
let err = new Error("Birthday invalid.");
callback(err);
}
else if(!countries.includes(jsondata.country)) {
let err = new Error("Country invalid.");
callback(err);
}
else {
_user_model.update({
userid: userid,
email: jsondata.email,
gender: jsondata.gender,
phonenumber: jsondata.phonenumber,
birthday: jsondata.birthday,
country: jsondata.country,
address: jsondata.address,
aboutme: jsondata.aboutme
}, callback);
}
}
catch (e) {
callback(e);
}
};
this.sendNotification = ()=> {
};
this.sendMail = ()=> {
};
this.deleteUser = (userid, callback) => {
_user_model.remove(userid, callback);
};
this.close = () => {
_model_module.close();
};
};
module.exports = NoUser;