-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserService.js
More file actions
217 lines (181 loc) · 6.5 KB
/
userService.js
File metadata and controls
217 lines (181 loc) · 6.5 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
/**
* User Service - Enterprise JavaScript Sample
*
* This service manages user CRUD operations with validation,
* error handling, and audit logging. Used in Module 2 exercises
* for unit test generation with Copilot Chat.
*/
class UserNotFoundError extends Error {
constructor(userId) {
super(`User not found: ${userId}`);
this.name = 'UserNotFoundError';
this.userId = userId;
}
}
class DuplicateEmailError extends Error {
constructor(email) {
super(`Email already in use: ${email}`);
this.name = 'DuplicateEmailError';
this.email = email;
}
}
class ValidationError extends Error {
constructor(field, message) {
super(`Validation failed for ${field}: ${message}`);
this.name = 'ValidationError';
this.field = field;
}
}
class UserService {
constructor(database, logger) {
this.db = database;
this.logger = logger;
}
/**
* Retrieve all users with optional pagination.
* @param {Object} options - { page: number, pageSize: number, sortBy: string, sortOrder: 'asc'|'desc' }
* @returns {Promise<{ users: Array, total: number, page: number, pageSize: number }>}
*/
async getAllUsers(options = {}) {
const { page = 1, pageSize = 20, sortBy = 'createdAt', sortOrder = 'desc' } = options;
if (page < 1) throw new ValidationError('page', 'must be at least 1');
if (pageSize < 1 || pageSize > 100) throw new ValidationError('pageSize', 'must be between 1 and 100');
this.logger.info('Fetching users', { page, pageSize, sortBy, sortOrder });
const offset = (page - 1) * pageSize;
const users = await this.db.query('users', {
offset,
limit: pageSize,
orderBy: sortBy,
order: sortOrder,
});
const total = await this.db.count('users');
return {
users,
total,
page,
pageSize,
};
}
/**
* Retrieve a single user by ID.
* @param {string} userId - The user's unique identifier
* @returns {Promise<Object>} The user object
* @throws {UserNotFoundError} if user does not exist
*/
async getUserById(userId) {
if (!userId || typeof userId !== 'string') {
throw new ValidationError('userId', 'must be a non-empty string');
}
this.logger.info('Fetching user', { userId });
const user = await this.db.findById('users', userId);
if (!user) {
this.logger.warn('User not found', { userId });
throw new UserNotFoundError(userId);
}
return user;
}
/**
* Create a new user with validation.
* @param {Object} userData - { name, email, department, role }
* @returns {Promise<Object>} The created user with generated ID and timestamps
* @throws {DuplicateEmailError} if email is already in use
* @throws {ValidationError} if required fields are missing or invalid
*/
async createUser(userData) {
const { name, email, department, role = 'user' } = userData || {};
// Validation
if (!name || name.trim().length === 0) {
throw new ValidationError('name', 'is required');
}
if (name.length > 100) {
throw new ValidationError('name', 'must be 100 characters or fewer');
}
if (!email || !this._isValidEmail(email)) {
throw new ValidationError('email', 'must be a valid email address');
}
if (!department || department.trim().length === 0) {
throw new ValidationError('department', 'is required');
}
// Check for duplicate email
const existingUser = await this.db.findOne('users', { email: email.toLowerCase() });
if (existingUser) {
this.logger.warn('Duplicate email attempt', { email });
throw new DuplicateEmailError(email);
}
const newUser = {
id: this._generateId(),
name: name.trim(),
email: email.toLowerCase().trim(),
department: department.trim(),
role,
active: true,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
const created = await this.db.insert('users', newUser);
this.logger.info('User created', { userId: created.id, email: created.email });
return created;
}
/**
* Update an existing user.
* @param {string} userId - The user's unique identifier
* @param {Object} updates - Fields to update
* @returns {Promise<Object>} The updated user
* @throws {UserNotFoundError} if user does not exist
* @throws {DuplicateEmailError} if new email is already in use by another user
*/
async updateUser(userId, updates) {
const user = await this.getUserById(userId);
const allowedFields = ['name', 'email', 'department', 'role', 'active'];
const sanitizedUpdates = {};
for (const [key, value] of Object.entries(updates)) {
if (allowedFields.includes(key)) {
sanitizedUpdates[key] = typeof value === 'string' ? value.trim() : value;
}
}
if (sanitizedUpdates.email) {
sanitizedUpdates.email = sanitizedUpdates.email.toLowerCase();
if (!this._isValidEmail(sanitizedUpdates.email)) {
throw new ValidationError('email', 'must be a valid email address');
}
const existingUser = await this.db.findOne('users', { email: sanitizedUpdates.email });
if (existingUser && existingUser.id !== userId) {
throw new DuplicateEmailError(sanitizedUpdates.email);
}
}
if (sanitizedUpdates.name !== undefined && sanitizedUpdates.name.length === 0) {
throw new ValidationError('name', 'cannot be empty');
}
sanitizedUpdates.updatedAt = new Date().toISOString();
const updated = await this.db.update('users', userId, sanitizedUpdates);
this.logger.info('User updated', { userId, fields: Object.keys(sanitizedUpdates) });
return updated;
}
/**
* Soft-delete a user by setting active to false.
* @param {string} userId - The user's unique identifier
* @returns {Promise<boolean>} true if user was deactivated
* @throws {UserNotFoundError} if user does not exist
*/
async deleteUser(userId) {
const user = await this.getUserById(userId);
if (!user.active) {
this.logger.info('User already deactivated', { userId });
return false;
}
await this.db.update('users', userId, {
active: false,
updatedAt: new Date().toISOString(),
});
this.logger.info('User deactivated', { userId });
return true;
}
_isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
_generateId() {
return `usr_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
}
}
module.exports = { UserService, UserNotFoundError, DuplicateEmailError, ValidationError };