-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUsers.js
78 lines (73 loc) · 1.91 KB
/
Users.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
class usersDictionary {
constructor() {
this.users = [];
}
addOrUpdateUser(userId, donationType, bloodType, location, phoneNumber, event) {
if (!this._isExist(userId)) {
var user = {
id: userId,
donationType: donationType,
bloodType: bloodType,
location: {},
phoneNumber: phoneNumber,
eventEmitter: event
};
this.users.push(user);
}
else {
this._updateUser(userId, donationType, bloodType, location, phoneNumber, event);
}
}
_isExist(userId) {
var exists = false;
if (this.users.length > 0) {
for (var i = 0; i < this.users.length; i++) {
if (this.users[i].id == userId) {
exists = true;
break;
}
}
}
return exists;
}
_updateUser(userId, donationType, bloodType, location, phoneNumber, event) {
var isUpdated = false;
if (this.users.length && this.users.length > 0) {
for (var i = 0; i < this.users.length; i++) {
if (this.users[i].id == userId) {
if (donationType != null) {
this.users[i].donationType = donationType;
}
if(event !=null){
this.users[i].eventEmitter = event;
}
if (bloodType != null) {
this.users[i].bloodType = bloodType;
}
if (location != null) {
Object.assign(this.users[i].location, location);
}
if (phoneNumber != null) {
this.users[i].phoneNumber = phoneNumber;
}
isUpdated = true;
break;
}
}
}
return isUpdated;
}
getUser(userId) {
var user = null;
if (this.users.length && this.users.length > 0) {
for (var i = 0; i < this.users.length; i++) {
if (this.users[i].id == userId) {
user = this.users[i];
break;
}
}
}
return user;
}
}
module.exports = usersDictionary;