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