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
152 lines (133 loc) · 3.25 KB
/
userService.js
File metadata and controls
152 lines (133 loc) · 3.25 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
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
import { users } from "../data/users.js";
import DataError from "../modules/dataError.js";
import AbstractUser from "../modules/abstractUser.js";
// default: default class for this file
export default class UserService {
// private properties
#employees = []
#customers = []
#errors = []
#loggerService
/**
* Constructs with a logger
* Makes load operation to load data from users.js
* @param {BaseLogger} loggerService
*/
constructor(loggerService) {
this.#loggerService = loggerService
this.#load()
}
/**
* private load function to load data in users.js to this class
*/
#load() {
for (const user of users) {
this.add(user)
}
}
/**
* Add a user to an appropriate list.
* If an error occurs then push the error to the errors list
* @param {AbstractUser} abstractUser
*/
add(abstractUser) {
if (abstractUser.type == "customer") {
if (!this.#checkCustomerValidtyForErrors(abstractUser))
this.#customers.push(abstractUser)
}
else if (abstractUser.type == "employee") {
if (!this.#checkEmployeeValidtyForErrors(abstractUser))
this.#employees.push(abstractUser)
}
else
this.#errors.push(new DataError("Wrong user type" , abstractUser))
this.#loggerService.log(abstractUser)
}
/**
* Check user validty.
* If there will be a problem, then DataError occurs in of errors list
* @param {AbstractUser} abstractUser
* @param {string} extension
* @returns Is there an error or not
*/
#checkUserValidtyForErrors(abstractUser, extension) {
let required = "id firstName lastName age city " + extension
let requiredFields = required.split(" ")
let hasErrors = false
for (const field of requiredFields) {
if (!abstractUser[field]) {
hasErrors = true
this.#errors.push(new DataError(`Validation problem. ${field} is required.`, abstractUser))
}
}
if (Number.isNaN(Number.parseInt(+abstractUser.age))) {
hasErrors = true
this.#errors.push(new DataError(`Validation problem. ${abstractUser.age} is not a number.`, abstractUser))
}
return (hasErrors)
}
/**
*
* @param {AbstractUser} abstractUser
* @returns Is there an error or not
*
*/
#checkCustomerValidtyForErrors(abstractUser) {
return (this.#checkUserValidtyForErrors(abstractUser, "creditCardNumber"))
}
/**
*
* @param {AbstractUser} abstractUser
* @returns Is there an error or not
*
*/
#checkEmployeeValidtyForErrors(abstractUser) {
return (this.#checkUserValidtyForErrors(abstractUser, "salary"))
}
/**
* list of customers
* @returns {Array<Customer>}
*/
listCustomers() {
return (this.#customers)
}
/**
* list of employees
* @returns {Array<Employee>}
*/
listEmployees() {
return (this.#employees)
}
/**
* list of errors
* @returns {Array<DataError>}
*/
listErrors() {
return (this.#errors)
}
/**
* Fetch the user using id
* @param {number} id
* @returns user
*/
getById(id) {
let user = this.#customers.find(c => c.id == id)
if (!user)
return (user)
return (this.#employees.find(e => e.id == id))
}
/**
*
* @returns sorted customer array
*/
getCustomersSorted() {
return (this.#customers.sort((c1, c2) => {
if (c1.id > c2.id)
return (1);
else if (c1.firstName === c2.firstName)
return (0);
else
return (-1);
}))
}
}