forked from engindemirog/javaScriptStarterKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserService.js
More file actions
60 lines (55 loc) · 1.84 KB
/
Copy pathuserService.js
File metadata and controls
60 lines (55 loc) · 1.84 KB
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
import { users } from "../data/users.js";
import DataError from "../model/dataError.js";
export class UserService{
constructor(loggerService){
this.users = [];
this.customers = [];
this.employees = [];
this.errors = [];
this.loggerService = loggerService;
}
load(){
for (const user of users) {
if (user.type == "customer") {
if (!this.checkCustomerValidityForErrors(user)) {
console.log(`${user.firstName} ${user.lastName} add to customer array`)
this.customers.push(user)
}
}
else if (user.type == "employee"){
console.log(`${user.firstName} ${user.lastName} add to employee array`)
this.employees.push(user)
}
else{
console.log(`${user.firstName} ${user.lastName} add but type errors`)
this.errors.push(new DataError("user type error", user))
}
}
}
checkCustomerValidityForErrors(user){
let requiredFields = "id firstName lastName age city".split(" ");
let hasErrors = false;
for (const iter of requiredFields) {
if (!user[iter]) {
hasErrors = true;
this.errors.push(DataError("object user doesn`t providing credentials", user))
}
}
if (Number.isNaN(Number.parseInt(user.age))) {
hasErrors = true
this.errors.push(new DataError("Customer user age is not a number", user))
}
return hasErrors;
}
add(user){
console.log("eklendi" + user.firstName);
this.users.push(user);
this.loggerService.log(user);
}
list(){
return this.users;
}
getById(id){
return this.users.find(u => u.id === id)
}
}