1 const UserEntity
= require("../entities/user");
2 const params
= require("../config/parameters");
6 create: function(newUser
, callback
)
8 // Determine initials from name parts
9 let nameParts
= newUser
.name
.split(/[ -]+/);
10 let initials
= nameParts
.map( n
=> { return n
.charAt(0).toLowerCase(); }).join("");
11 // First retrieve all users with similar prefix initials
12 UserEntity
.getInitialsByPrefix(initials
, (err
,userArray
) => {
13 if (!!userArray
&& userArray
.length
== 1)
14 initials
= initials
+ "2"; //thus number == users count for this hash
15 else if (!!userArray
&& userArray
.length
> 1)
17 // Pick the highest number after initials (if any), and increment
18 let numbers
= userArray
.map( u
=> {
19 let digitMatch
= u
.initials
.match(/[0-9]/);
21 return 1; //irrelevant
22 let firstDigit
= digitMatch
.index
;
23 return parseInt(u
.initials
.slice(digitMatch
.index
));
25 initials
= initials
+ (Math
.max(...numbers
)+1);
27 Object
.assign(newUser
, {initials: initials
});
28 UserEntity
.insert(newUser
, callback
);
32 whitelistCheck: function(email
)
34 if (params
.whitelist
.length
== 0)
35 return true; //no whitelist, everyone allowed
36 for (let w
of params
.whitelist
)
38 if ((w
.indexOf('@') >= 0 && w
==email
) || !!email
.match(new RegExp(w
+"$")))
44 logout: function(uid
, token
, cb
)
46 UserEntity
.removeToken(uid
, token
, cb
);
49 cleanUsersDb: function()
51 UserEntity
.getUnlogged( (err
,unlogged
) => {
52 UserEntity
.remove(unlogged
);
57 module
.exports
= UserModel
;