-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-original.js
More file actions
254 lines (216 loc) · 6.25 KB
/
test-original.js
File metadata and controls
254 lines (216 loc) · 6.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// UserService.js - Original version
// This file demonstrates various types of changes for diff viewer testing
import { Database } from './database';
import { Logger } from './logger';
import { validateEmail, validatePassword } from './validators';
function hello() {
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
console.log('Hello, world!');
}
/**
* UserService handles all user-related operations
* including authentication, profile management, and permissions
*/
class UserService {
constructor(database, logger) {
this.db = database;
this.log = logger;
this.cache = new Map();
}
/**
* Authenticates a user with email and password
* @param {string} email - User's email address
* @param {string} password - User's password
* @returns {Promise<User>} Authenticated user object
*/
async authenticateUser(email, password) {
if (!validateEmail(email)) {
throw new Error('Invalid email format');
}
const user = await this.db.findUserByEmail(email);
if (!user) {
throw new Error('User not found');
}
const isValid = await this.verifyPassword(password, user.passwordHash);
if (!isValid) {
throw new Error('Invalid password');
}
this.log.info(`User authenticated: ${email}`);
return user;
}
/**
* Creates a new user account
*/
async createUser(userData) {
const { email, password, name, role } = userData;
if (!validateEmail(email)) {
throw new Error('Invalid email format');
}
if (!validatePassword(password)) {
throw new Error('Password does not meet requirements');
}
const existingUser = await this.db.findUserByEmail(email);
if (existingUser) {
throw new Error('User already exists');
}
const passwordHash = await this.hashPassword(password);
const newUser = {
email,
name,
role: role || 'user',
passwordHash,
createdAt: new Date(),
updatedAt: new Date(),
};
const userId = await this.db.insertUser(newUser);
this.log.info(`New user created: ${email}`);
return { ...newUser, id: userId };
}
/**
* Updates user profile information
*/
async updateUserProfile(userId, updates) {
const user = await this.db.findUserById(userId);
if (!user) {
throw new Error('User not found');
}
const allowedFields = ['name', 'email', 'phone'];
const filteredUpdates = {};
for (const field of allowedFields) {
if (updates[field] !== undefined) {
filteredUpdates[field] = updates[field];
}
}
if (filteredUpdates.email && filteredUpdates.email !== user.email) {
if (!validateEmail(filteredUpdates.email)) {
throw new Error('Invalid email format');
}
const existingUser = await this.db.findUserByEmail(filteredUpdates.email);
if (existingUser) {
throw new Error('Email already in use');
}
}
filteredUpdates.updatedAt = new Date();
await this.db.updateUser(userId, filteredUpdates);
this.log.info(`User profile updated: ${userId}`);
return { ...user, ...filteredUpdates };
}
/**
* Deletes a user account
*/
async deleteUser(userId) {
const user = await this.db.findUserById(userId);
if (!user) {
throw new Error('User not found');
}
await this.db.deleteUser(userId);
this.cache.delete(userId);
this.log.info(`User deleted: ${userId}`);
return { success: true };
}
/**
* Gets user by ID with caching
*/
async getUserById(userId) {
if (this.cache.has(userId)) {
return this.cache.get(userId);
}
const user = await this.db.findUserById(userId);
if (user) {
this.cache.set(userId, user);
}
return user;
}
/**
* Changes user password
*/
async changePassword(userId, oldPassword, newPassword) {
const user = await this.db.findUserById(userId);
if (!user) {
throw new Error('User not found');
}
const isValid = await this.verifyPassword(oldPassword, user.passwordHash);
if (!isValid) {
throw new Error('Current password is incorrect');
}
if (!validatePassword(newPassword)) {
throw new Error('New password does not meet requirements');
}
const newPasswordHash = await this.hashPassword(newPassword);
await this.db.updateUser(userId, { passwordHash: newPasswordHash, updatedAt: new Date() });
this.log.info(`Password changed for user: ${userId}`);
return { success: true };
}
/**
* Lists all users with pagination
*/
async listUsers(page = 1, limit = 10) {
const offset = (page - 1) * limit;
const users = await this.db.findAllUsers(limit, offset);
const total = await this.db.countUsers();
return {
users,
page,
limit,
total,
totalPages: Math.ceil(total / limit),
};
}
/**
* Searches users by name or email
*/
async searchUsers(query) {
const users = await this.db.searchUsers(query);
return users;
}
/**
* Helper: Hash password
*/
async hashPassword(password) {
// Simplified - in production use bcrypt
return Buffer.from(password).toString('base64');
}
/**
* Helper: Verify password
*/
async verifyPassword(password, hash) {
const testHash = await this.hashPassword(password);
return testHash === hash;
}
/**
* Helper: Clear cache
*/
clearCache() {
this.cache.clear();
this.log.info('User cache cleared');
}
}
export default UserService;