nodejs-quickstart-structure 1.18.0 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -4
- package/README.md +2 -1
- package/bin/index.js +93 -92
- package/lib/generator.js +1 -1
- package/lib/modules/caching-setup.js +76 -73
- package/lib/modules/config-files.js +4 -0
- package/lib/modules/kafka-setup.js +249 -191
- package/lib/modules/project-setup.js +1 -0
- package/package.json +13 -2
- package/templates/clean-architecture/js/src/errors/BadRequestError.js +11 -10
- package/templates/clean-architecture/js/src/errors/BadRequestError.spec.js.ejs +22 -21
- package/templates/clean-architecture/js/src/errors/NotFoundError.js +11 -10
- package/templates/clean-architecture/js/src/errors/NotFoundError.spec.js.ejs +22 -21
- package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.js.ejs +69 -39
- package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.spec.js.ejs +142 -81
- package/templates/clean-architecture/js/src/infrastructure/webserver/server.js.ejs +1 -1
- package/templates/clean-architecture/js/src/interfaces/controllers/userController.js.ejs +156 -75
- package/templates/clean-architecture/js/src/interfaces/controllers/userController.spec.js.ejs +234 -138
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.js.ejs +27 -21
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.spec.js.ejs +66 -49
- package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/user.types.js.ejs +19 -17
- package/templates/clean-architecture/js/src/interfaces/routes/api.js +12 -10
- package/templates/clean-architecture/js/src/usecases/DeleteUser.js +11 -0
- package/templates/clean-architecture/js/src/usecases/DeleteUser.spec.js.ejs +47 -0
- package/templates/clean-architecture/js/src/usecases/UpdateUser.js +11 -0
- package/templates/clean-architecture/js/src/usecases/UpdateUser.spec.js.ejs +48 -0
- package/templates/clean-architecture/js/src/utils/errorMessages.js +14 -0
- package/templates/clean-architecture/ts/src/errors/BadRequestError.spec.ts.ejs +22 -21
- package/templates/clean-architecture/ts/src/errors/BadRequestError.ts +9 -8
- package/templates/clean-architecture/ts/src/errors/NotFoundError.spec.ts.ejs +22 -21
- package/templates/clean-architecture/ts/src/errors/NotFoundError.ts +9 -8
- package/templates/clean-architecture/ts/src/index.ts.ejs +1 -1
- package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.spec.ts.ejs +175 -85
- package/templates/clean-architecture/ts/src/infrastructure/repositories/userRepository.ts.ejs +74 -0
- package/templates/clean-architecture/ts/src/interfaces/controllers/userController.spec.ts.ejs +331 -185
- package/templates/clean-architecture/ts/src/interfaces/controllers/userController.ts.ejs +173 -84
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -51
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.ts.ejs +29 -21
- package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/user.types.ts.ejs +17 -15
- package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.ts +13 -11
- package/templates/clean-architecture/ts/src/usecases/deleteUser.spec.ts.ejs +47 -0
- package/templates/clean-architecture/ts/src/usecases/deleteUser.ts +9 -0
- package/templates/clean-architecture/ts/src/usecases/updateUser.spec.ts.ejs +48 -0
- package/templates/clean-architecture/ts/src/usecases/updateUser.ts +9 -0
- package/templates/clean-architecture/ts/src/utils/errorMessages.ts +12 -0
- package/templates/common/.gitattributes +46 -0
- package/templates/common/.snyk.ejs +45 -0
- package/templates/common/Dockerfile +17 -9
- package/templates/common/README.md.ejs +295 -263
- package/templates/common/caching/clean/js/DeleteUser.js.ejs +27 -0
- package/templates/common/caching/clean/js/UpdateUser.js.ejs +27 -0
- package/templates/common/caching/clean/ts/deleteUser.ts.ejs +24 -0
- package/templates/common/caching/clean/ts/updateUser.ts.ejs +25 -0
- package/templates/common/caching/ts/memoryCache.ts.ejs +73 -64
- package/templates/common/caching/ts/redisClient.ts.ejs +89 -80
- package/templates/common/database/js/models/User.js.ejs +79 -53
- package/templates/common/database/js/models/User.js.mongoose.ejs +23 -19
- package/templates/common/database/js/models/User.spec.js.ejs +94 -84
- package/templates/common/database/ts/models/User.spec.ts.ejs +100 -84
- package/templates/common/database/ts/models/User.ts.ejs +87 -61
- package/templates/common/database/ts/models/User.ts.mongoose.ejs +30 -25
- package/templates/common/health/js/healthRoute.js.ejs +50 -47
- package/templates/common/health/ts/healthRoute.ts.ejs +49 -46
- package/templates/common/jest.e2e.config.js.ejs +8 -8
- package/templates/common/kafka/js/messaging/baseConsumer.js.ejs +30 -30
- package/templates/common/kafka/js/messaging/userEventSchema.js.ejs +12 -11
- package/templates/common/kafka/js/messaging/welcomeEmailConsumer.js.ejs +44 -31
- package/templates/common/kafka/js/messaging/welcomeEmailConsumer.spec.js.ejs +86 -49
- package/templates/common/kafka/js/services/kafkaService.js.ejs +93 -93
- package/templates/common/kafka/js/utils/kafkaEvents.js.ejs +7 -0
- package/templates/common/kafka/ts/messaging/userEventSchema.spec.ts.ejs +51 -51
- package/templates/common/kafka/ts/messaging/userEventSchema.ts.ejs +12 -11
- package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.spec.ts.ejs +86 -49
- package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.ts.ejs +38 -25
- package/templates/common/kafka/ts/services/kafkaService.ts.ejs +95 -95
- package/templates/common/kafka/ts/utils/kafkaEvents.ts.ejs +5 -0
- package/templates/common/package.json.ejs +10 -2
- package/templates/common/shutdown/js/gracefulShutdown.js.ejs +65 -61
- package/templates/common/shutdown/js/gracefulShutdown.spec.js.ejs +149 -160
- package/templates/common/shutdown/ts/gracefulShutdown.spec.ts.ejs +179 -158
- package/templates/common/shutdown/ts/gracefulShutdown.ts.ejs +59 -55
- package/templates/common/src/tests/e2e/e2e.users.test.js.ejs +120 -49
- package/templates/common/src/tests/e2e/e2e.users.test.ts.ejs +120 -49
- package/templates/common/swagger.yml.ejs +118 -66
- package/templates/db/mysql/V1__Initial_Setup.sql.ejs +10 -9
- package/templates/db/postgres/V1__Initial_Setup.sql.ejs +10 -9
- package/templates/mvc/js/src/controllers/userController.js.ejs +246 -105
- package/templates/mvc/js/src/controllers/userController.spec.js.ejs +481 -209
- package/templates/mvc/js/src/errors/BadRequestError.js +11 -10
- package/templates/mvc/js/src/errors/BadRequestError.spec.js.ejs +22 -21
- package/templates/mvc/js/src/errors/NotFoundError.js +11 -10
- package/templates/mvc/js/src/errors/NotFoundError.spec.js.ejs +22 -21
- package/templates/mvc/js/src/graphql/resolvers/user.resolvers.js.ejs +25 -19
- package/templates/mvc/js/src/graphql/resolvers/user.resolvers.spec.js.ejs +64 -47
- package/templates/mvc/js/src/graphql/typeDefs/user.types.js.ejs +19 -17
- package/templates/mvc/js/src/index.js.ejs +1 -1
- package/templates/mvc/js/src/routes/api.js +10 -8
- package/templates/mvc/js/src/routes/api.spec.js.ejs +41 -36
- package/templates/mvc/js/src/utils/errorMessages.js +14 -0
- package/templates/mvc/ts/src/controllers/userController.spec.ts.ejs +481 -203
- package/templates/mvc/ts/src/controllers/userController.ts.ejs +248 -107
- package/templates/mvc/ts/src/errors/BadRequestError.spec.ts.ejs +22 -21
- package/templates/mvc/ts/src/errors/BadRequestError.ts +9 -8
- package/templates/mvc/ts/src/errors/NotFoundError.spec.ts.ejs +27 -21
- package/templates/mvc/ts/src/errors/NotFoundError.ts +9 -8
- package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -51
- package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.ts.ejs +29 -21
- package/templates/mvc/ts/src/graphql/typeDefs/user.types.ts.ejs +17 -15
- package/templates/mvc/ts/src/index.ts.ejs +156 -153
- package/templates/mvc/ts/src/routes/api.spec.ts.ejs +59 -40
- package/templates/mvc/ts/src/routes/api.ts +12 -10
- package/templates/mvc/ts/src/utils/errorMessages.ts +12 -0
- package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.ts.ejs +0 -37
|
@@ -1,107 +1,248 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
<%_ if (
|
|
22
|
-
|
|
23
|
-
<%_
|
|
24
|
-
return await User.
|
|
25
|
-
<%_ } -%>
|
|
26
|
-
|
|
27
|
-
<%_ }
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
<%_
|
|
31
|
-
const users = await User.
|
|
32
|
-
<%_ } -%>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
await
|
|
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
|
-
const
|
|
75
|
-
<%_ } else { -%>
|
|
76
|
-
const
|
|
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
|
-
|
|
1
|
+
import { ERROR_MESSAGES } from '@/utils/errorMessages';
|
|
2
|
+
<% if (communication !== 'GraphQL') { -%>
|
|
3
|
+
import { Request, Response, NextFunction } from 'express';
|
|
4
|
+
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
5
|
+
<% } -%>
|
|
6
|
+
import User from '@/models/User';
|
|
7
|
+
import logger from '@/utils/logger';
|
|
8
|
+
<%_ if (caching === 'Redis') { -%>
|
|
9
|
+
import cacheService from '@/config/redisClient';
|
|
10
|
+
<%_ } else if (caching === 'Memory Cache') { -%>
|
|
11
|
+
import cacheService from '@/config/memoryCache';
|
|
12
|
+
<%_ } -%><%_ if (communication === 'Kafka') { -%>
|
|
13
|
+
import { kafkaService } from '@/services/kafkaService';
|
|
14
|
+
import { KAFKA_ACTIONS } from '@/utils/kafkaEvents';
|
|
15
|
+
<%_ } -%>
|
|
16
|
+
|
|
17
|
+
export class UserController {
|
|
18
|
+
<% if (communication === 'GraphQL') { -%>
|
|
19
|
+
async getUsers() {
|
|
20
|
+
try {
|
|
21
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
22
|
+
const users = await cacheService.getOrSet('users:all', async () => {
|
|
23
|
+
<%_ if (database === 'MongoDB' || database === 'None') { -%>
|
|
24
|
+
return await User.find();
|
|
25
|
+
<%_ } else { -%>
|
|
26
|
+
return await User.findAll();
|
|
27
|
+
<%_ } -%>
|
|
28
|
+
}, 60);
|
|
29
|
+
<%_ } else { -%>
|
|
30
|
+
<%_ if (database === 'MongoDB' || database === 'None') { -%>
|
|
31
|
+
const users = await User.find();
|
|
32
|
+
<%_ } else { -%>
|
|
33
|
+
const users = await User.findAll();
|
|
34
|
+
<%_ } -%>
|
|
35
|
+
<%_ } -%>
|
|
36
|
+
return users;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
logger.error(`${ERROR_MESSAGES.FETCH_USERS_ERROR}:`, error);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async createUser(data: { name: string, email: string }) {
|
|
44
|
+
try {
|
|
45
|
+
const { name, email } = data;
|
|
46
|
+
const user = await User.create({ name, email });
|
|
47
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
48
|
+
await cacheService.del('users:all');
|
|
49
|
+
<%_ } -%>
|
|
50
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
const userId = (user as any).id || (user as any)._id;
|
|
53
|
+
await kafkaService.sendMessage('user-topic', JSON.stringify({
|
|
54
|
+
action: KAFKA_ACTIONS.USER_CREATED,
|
|
55
|
+
payload: { id: userId, email: user.email }
|
|
56
|
+
}), userId?.toString() || '');
|
|
57
|
+
<%_ } -%>
|
|
58
|
+
return user;
|
|
59
|
+
} catch (error) {
|
|
60
|
+
logger.error(`${ERROR_MESSAGES.CREATE_USER_ERROR}:`, error);
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async updateUser(id: string, data: { name?: string, email?: string }) {
|
|
66
|
+
try {
|
|
67
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
68
|
+
const updatedUser = await User.findByIdAndUpdate(id, data, { new: true });
|
|
69
|
+
if (!updatedUser) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
70
|
+
<%_ } else if (database === 'None') { -%>
|
|
71
|
+
const user = await User.findByPk(id);
|
|
72
|
+
if (!user) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
73
|
+
await User.update(id, data);
|
|
74
|
+
const updatedUser = await User.findByPk(id);
|
|
75
|
+
<%_ } else { -%>
|
|
76
|
+
const user = await User.findByPk(id);
|
|
77
|
+
if (!user) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
78
|
+
await user.update(data);
|
|
79
|
+
const updatedUser = user;
|
|
80
|
+
<%_ } -%>
|
|
81
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
82
|
+
await cacheService.del('users:all');
|
|
83
|
+
<%_ } -%>
|
|
84
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
85
|
+
await kafkaService.sendMessage('user-topic', JSON.stringify({
|
|
86
|
+
action: KAFKA_ACTIONS.USER_UPDATED,
|
|
87
|
+
payload: { id, email: updatedUser?.email }
|
|
88
|
+
}), id);
|
|
89
|
+
<%_ } -%>
|
|
90
|
+
return updatedUser;
|
|
91
|
+
} catch (error) {
|
|
92
|
+
logger.error(`${ERROR_MESSAGES.UPDATE_USER_ERROR}:`, error);
|
|
93
|
+
throw error;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async deleteUser(id: string) {
|
|
98
|
+
try {
|
|
99
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
100
|
+
const deleted = await User.findByIdAndDelete(id);
|
|
101
|
+
if (!deleted) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
102
|
+
<%_ } else if (database === 'None') { -%>
|
|
103
|
+
const deleted = await User.destroy(id);
|
|
104
|
+
if (!deleted) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
105
|
+
<%_ } else { -%>
|
|
106
|
+
const user = await User.findByPk(id);
|
|
107
|
+
if (!user) throw new Error(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
108
|
+
await user.destroy();
|
|
109
|
+
<%_ } -%>
|
|
110
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
111
|
+
await cacheService.del('users:all');
|
|
112
|
+
<%_ } -%>
|
|
113
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
114
|
+
await kafkaService.sendMessage('user-topic', JSON.stringify({
|
|
115
|
+
action: KAFKA_ACTIONS.USER_DELETED,
|
|
116
|
+
payload: { id }
|
|
117
|
+
}), id);
|
|
118
|
+
<%_ } -%>
|
|
119
|
+
return true;
|
|
120
|
+
} catch (error) {
|
|
121
|
+
logger.error(`${ERROR_MESSAGES.DELETE_USER_ERROR}:`, error);
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
<% } else { -%>
|
|
126
|
+
async getUsers(req: Request, res: Response, next: NextFunction) {
|
|
127
|
+
try {
|
|
128
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
129
|
+
const users = await cacheService.getOrSet('users:all', async () => {
|
|
130
|
+
<%_ if (database === 'MongoDB' || database === 'None') { -%>
|
|
131
|
+
return await User.find();
|
|
132
|
+
<%_ } else { -%>
|
|
133
|
+
return await User.findAll();
|
|
134
|
+
<%_ } -%>
|
|
135
|
+
}, 60);
|
|
136
|
+
<%_ } else { -%>
|
|
137
|
+
<%_ if (database === 'MongoDB' || database === 'None') { -%>
|
|
138
|
+
const users = await User.find();
|
|
139
|
+
<%_ } else { -%>
|
|
140
|
+
const users = await User.findAll();
|
|
141
|
+
<%_ } -%>
|
|
142
|
+
<%_ } -%>
|
|
143
|
+
res.json(users);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
logger.error(`${ERROR_MESSAGES.FETCH_USERS_ERROR}:`, error);
|
|
146
|
+
next(error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async createUser(req: Request, res: Response, next: NextFunction) {
|
|
151
|
+
try {
|
|
152
|
+
const { name, email } = req.body || {};
|
|
153
|
+
const user = await User.create({ name, email });
|
|
154
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
155
|
+
await cacheService.del('users:all');
|
|
156
|
+
<%_ } -%>
|
|
157
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
158
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
159
|
+
const userId = (user as any).id || (user as any)._id;
|
|
160
|
+
await kafkaService.sendMessage('user-topic', JSON.stringify({
|
|
161
|
+
action: KAFKA_ACTIONS.USER_CREATED,
|
|
162
|
+
payload: { id: userId, email: user.email }
|
|
163
|
+
}), userId?.toString() || '');
|
|
164
|
+
<%_ } -%>
|
|
165
|
+
res.status(HTTP_STATUS.CREATED).json(user);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
logger.error('Error creating user:', error);
|
|
168
|
+
next(error);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async updateUser(req: Request, res: Response, next: NextFunction) {
|
|
173
|
+
try {
|
|
174
|
+
const { id } = req.params;
|
|
175
|
+
const { name, email } = req.body || {};
|
|
176
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
177
|
+
const updatedUser = await User.findByIdAndUpdate(id, { name, email }, { new: true });
|
|
178
|
+
if (!updatedUser) {
|
|
179
|
+
return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND });
|
|
180
|
+
}
|
|
181
|
+
<%_ } else if (database === 'None') { -%>
|
|
182
|
+
const user = await User.findByPk(id);
|
|
183
|
+
if (!user) {
|
|
184
|
+
return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND });
|
|
185
|
+
}
|
|
186
|
+
await User.update(id, { name, email });
|
|
187
|
+
const updatedUser = await User.findByPk(id);
|
|
188
|
+
<%_ } else { -%>
|
|
189
|
+
const user = await User.findByPk(id);
|
|
190
|
+
if (!user) {
|
|
191
|
+
return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND });
|
|
192
|
+
}
|
|
193
|
+
await user.update({ name, email });
|
|
194
|
+
const updatedUser = user;
|
|
195
|
+
<%_ } -%>
|
|
196
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
197
|
+
await cacheService.del('users:all');
|
|
198
|
+
<%_ } -%>
|
|
199
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
200
|
+
await kafkaService.sendMessage('user-topic', JSON.stringify({
|
|
201
|
+
action: KAFKA_ACTIONS.USER_UPDATED,
|
|
202
|
+
payload: { id, email: updatedUser?.email }
|
|
203
|
+
}), id);
|
|
204
|
+
<%_ } -%>
|
|
205
|
+
res.status(HTTP_STATUS.OK).json(updatedUser);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
logger.error(`${ERROR_MESSAGES.UPDATE_USER_ERROR}:`, error);
|
|
208
|
+
next(error);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async deleteUser(req: Request, res: Response, next: NextFunction) {
|
|
213
|
+
try {
|
|
214
|
+
const { id } = req.params;
|
|
215
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
216
|
+
const deleted = await User.findByIdAndDelete(id);
|
|
217
|
+
if (!deleted) {
|
|
218
|
+
return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND });
|
|
219
|
+
}
|
|
220
|
+
<%_ } else if (database === 'None') { -%>
|
|
221
|
+
const deleted = await User.destroy(id);
|
|
222
|
+
if (!deleted) {
|
|
223
|
+
return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND });
|
|
224
|
+
}
|
|
225
|
+
<%_ } else { -%>
|
|
226
|
+
const user = await User.findByPk(id);
|
|
227
|
+
if (!user) {
|
|
228
|
+
return res.status(HTTP_STATUS.NOT_FOUND).json({ error: ERROR_MESSAGES.USER_NOT_FOUND });
|
|
229
|
+
}
|
|
230
|
+
await user.destroy();
|
|
231
|
+
<%_ } -%>
|
|
232
|
+
<%_ if (caching === 'Redis' || caching === 'Memory Cache') { -%>
|
|
233
|
+
await cacheService.del('users:all');
|
|
234
|
+
<%_ } -%>
|
|
235
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
236
|
+
await kafkaService.sendMessage('user-topic', JSON.stringify({
|
|
237
|
+
action: KAFKA_ACTIONS.USER_DELETED,
|
|
238
|
+
payload: { id }
|
|
239
|
+
}), id);
|
|
240
|
+
<%_ } -%>
|
|
241
|
+
res.status(HTTP_STATUS.OK).json({ message: 'User deleted successfully' });
|
|
242
|
+
} catch (error) {
|
|
243
|
+
logger.error(`${ERROR_MESSAGES.DELETE_USER_ERROR}:`, error);
|
|
244
|
+
next(error);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
<% } -%>
|
|
248
|
+
}
|
|
@@ -1,21 +1,22 @@
|
|
|
1
|
-
import { BadRequestError } from '@/errors/BadRequestError';
|
|
2
|
-
import { ApiError } from '@/errors/ApiError';
|
|
3
|
-
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
expect(error.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
1
|
+
import { BadRequestError } from '@/errors/BadRequestError';
|
|
2
|
+
import { ApiError } from '@/errors/ApiError';
|
|
3
|
+
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
4
|
+
import { ERROR_MESSAGES } from '@/utils/errorMessages';
|
|
5
|
+
|
|
6
|
+
describe('BadRequestError', () => {
|
|
7
|
+
it('should extend ApiError', () => {
|
|
8
|
+
const error = new BadRequestError();
|
|
9
|
+
expect(error).toBeInstanceOf(ApiError);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should have default message "Bad Request"', () => {
|
|
13
|
+
const error = new BadRequestError();
|
|
14
|
+
expect(error.message).toBe(ERROR_MESSAGES.BAD_REQUEST);
|
|
15
|
+
expect(error.statusCode).toBe(HTTP_STATUS.BAD_REQUEST);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should accept a custom message', () => {
|
|
19
|
+
const error = new BadRequestError('Custom bad request');
|
|
20
|
+
expect(error.message).toBe('Custom bad request');
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ApiError } from '@/errors/ApiError';
|
|
2
|
-
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
import { ApiError } from '@/errors/ApiError';
|
|
2
|
+
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
3
|
+
import { ERROR_MESSAGES } from '@/utils/errorMessages';
|
|
4
|
+
|
|
5
|
+
export class BadRequestError extends ApiError {
|
|
6
|
+
constructor(message: string = ERROR_MESSAGES.BAD_REQUEST) {
|
|
7
|
+
super(HTTP_STATUS.BAD_REQUEST, message);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
import { NotFoundError } from '@/errors/NotFoundError';
|
|
2
|
-
import { ApiError } from '@/errors/ApiError';
|
|
3
|
-
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
expect(error.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
});
|
|
1
|
+
import { NotFoundError } from '@/errors/NotFoundError';
|
|
2
|
+
import { ApiError } from '@/errors/ApiError';
|
|
3
|
+
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
4
|
+
import { ERROR_MESSAGES } from '@/utils/errorMessages';
|
|
5
|
+
|
|
6
|
+
describe('NotFoundError', () => {
|
|
7
|
+
it('should extend ApiError', () => {
|
|
8
|
+
const error = new NotFoundError();
|
|
9
|
+
expect(error).toBeInstanceOf(ApiError);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('should have default message "Resource not found"', () => {
|
|
13
|
+
const error = new NotFoundError();
|
|
14
|
+
expect(error.message).toBe(ERROR_MESSAGES.RESOURCE_NOT_FOUND);
|
|
15
|
+
expect(error.statusCode).toBe(HTTP_STATUS.NOT_FOUND);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('should accept a custom message', () => {
|
|
19
|
+
const error = new NotFoundError(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
20
|
+
expect(error.message).toBe(ERROR_MESSAGES.USER_NOT_FOUND);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should have the correct status code', () => {
|
|
24
|
+
const error = new NotFoundError();
|
|
25
|
+
expect(error.statusCode).toBe(HTTP_STATUS.NOT_FOUND);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { ApiError } from '@/errors/ApiError';
|
|
2
|
-
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
1
|
+
import { ApiError } from '@/errors/ApiError';
|
|
2
|
+
import { HTTP_STATUS } from '@/utils/httpCodes';
|
|
3
|
+
import { ERROR_MESSAGES } from '@/utils/errorMessages';
|
|
4
|
+
|
|
5
|
+
export class NotFoundError extends ApiError {
|
|
6
|
+
constructor(message: string = ERROR_MESSAGES.RESOURCE_NOT_FOUND) {
|
|
7
|
+
super(HTTP_STATUS.NOT_FOUND, message);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -1,51 +1,68 @@
|
|
|
1
|
-
import { userResolvers } from '@/graphql/resolvers/user.resolvers';
|
|
2
|
-
|
|
3
|
-
const mockGetUsers = jest.fn().mockResolvedValue([{ id: '1', name: 'John Doe', email: 'john@example.com' }]);
|
|
4
|
-
const mockCreateUser = jest.fn().mockResolvedValue({ id: '1', name: 'Jane', email: 'jane@example.com' });
|
|
5
|
-
|
|
6
|
-
jest.mock('@/controllers/userController', () => {
|
|
7
|
-
return {
|
|
8
|
-
UserController: jest.fn().mockImplementation(() => ({
|
|
9
|
-
getUsers: (...args: unknown[]) => mockGetUsers(...args),
|
|
10
|
-
createUser: (...args: unknown[]) => mockCreateUser(...args)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
expect(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
1
|
+
import { userResolvers } from '@/graphql/resolvers/user.resolvers';
|
|
2
|
+
|
|
3
|
+
const mockGetUsers = jest.fn().mockResolvedValue([{ id: '1', name: 'John Doe', email: 'john@example.com' }]);
|
|
4
|
+
const mockCreateUser = jest.fn().mockResolvedValue({ id: '1', name: 'Jane', email: 'jane@example.com' });
|
|
5
|
+
|
|
6
|
+
jest.mock('@/controllers/userController', () => {
|
|
7
|
+
return {
|
|
8
|
+
UserController: jest.fn().mockImplementation(() => ({
|
|
9
|
+
getUsers: (...args: unknown[]) => mockGetUsers(...args),
|
|
10
|
+
createUser: (...args: unknown[]) => mockCreateUser(...args),
|
|
11
|
+
updateUser: jest.fn().mockImplementation((id, data) => Promise.resolve({ id, ...data })),
|
|
12
|
+
deleteUser: jest.fn().mockImplementation(() => Promise.resolve(true))
|
|
13
|
+
}))
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('User Resolvers', () => {
|
|
18
|
+
afterEach(() => {
|
|
19
|
+
jest.clearAllMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('Query.getAllUsers', () => {
|
|
23
|
+
it('should return all users', async () => {
|
|
24
|
+
const result = await userResolvers.Query.getAllUsers();
|
|
25
|
+
expect(result).toEqual([{ id: '1', name: 'John Doe', email: 'john@example.com' }]);
|
|
26
|
+
expect(mockGetUsers).toHaveBeenCalledTimes(1);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('Mutation.createUser', () => {
|
|
31
|
+
it('should create and return a new user', async () => {
|
|
32
|
+
const result = await userResolvers.Mutation.createUser(null, { name: 'Jane', email: 'jane@example.com' });
|
|
33
|
+
expect(result).toEqual({ id: '1', name: 'Jane', email: 'jane@example.com' });
|
|
34
|
+
expect(mockCreateUser).toHaveBeenCalledWith({ name: 'Jane', email: 'jane@example.com' });
|
|
35
|
+
expect(mockCreateUser).toHaveBeenCalledTimes(1);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('Mutation.updateUser', () => {
|
|
40
|
+
it('should update and return the user', async () => {
|
|
41
|
+
const payload = { name: 'Updated' };
|
|
42
|
+
const result = await userResolvers.Mutation.updateUser(null, { id: '1', ...payload });
|
|
43
|
+
expect(result).toMatchObject(payload);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('Mutation.deleteUser', () => {
|
|
48
|
+
it('should delete and return true', async () => {
|
|
49
|
+
const result = await userResolvers.Mutation.deleteUser(null, { id: '1' });
|
|
50
|
+
expect(result).toBe(true);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
54
|
+
describe('User.id', () => {
|
|
55
|
+
it('should return parent.id if available', () => {
|
|
56
|
+
const parent = { id: '123' };
|
|
57
|
+
const result = userResolvers.User.id(parent as { id?: string; _id?: unknown });
|
|
58
|
+
expect(result).toBe('123');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should fallback to parent._id if id is not available', () => {
|
|
62
|
+
const parent = { _id: '456' };
|
|
63
|
+
const result = userResolvers.User.id(parent as { id?: string; _id?: unknown });
|
|
64
|
+
expect(result).toBe('456');
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
<%_ } -%>
|
|
68
|
+
});
|