nodejs-structure-cli 1.0.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/README.md +32 -0
- package/bin/index.js +143 -0
- package/lib/generator.js +145 -0
- package/lib/modules/app-setup.js +479 -0
- package/lib/modules/caching-setup.js +76 -0
- package/lib/modules/config-files.js +151 -0
- package/lib/modules/database-setup.js +116 -0
- package/lib/modules/kafka-setup.js +249 -0
- package/lib/modules/project-setup.js +32 -0
- package/lib/prompts.js +128 -0
- package/package.json +66 -0
- package/templates/clean-architecture/js/src/domain/models/User.js.ejs +11 -0
- package/templates/clean-architecture/js/src/errors/ApiError.js +14 -0
- package/templates/clean-architecture/js/src/errors/BadRequestError.js +11 -0
- package/templates/clean-architecture/js/src/errors/BadRequestError.spec.js.ejs +22 -0
- package/templates/clean-architecture/js/src/errors/NotFoundError.js +11 -0
- package/templates/clean-architecture/js/src/errors/NotFoundError.spec.js.ejs +22 -0
- package/templates/clean-architecture/js/src/index.js.ejs +56 -0
- package/templates/clean-architecture/js/src/infrastructure/config/env.js.ejs +47 -0
- package/templates/clean-architecture/js/src/infrastructure/log/logger.js +36 -0
- package/templates/clean-architecture/js/src/infrastructure/log/logger.spec.js.ejs +63 -0
- package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.js.ejs +88 -0
- package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.spec.js.ejs +142 -0
- package/templates/clean-architecture/js/src/infrastructure/webserver/middleware/errorMiddleware.js +30 -0
- package/templates/clean-architecture/js/src/infrastructure/webserver/server.js.ejs +93 -0
- package/templates/clean-architecture/js/src/infrastructure/webserver/swagger.js.ejs +6 -0
- package/templates/clean-architecture/js/src/interfaces/controllers/userController.js.ejs +190 -0
- package/templates/clean-architecture/js/src/interfaces/controllers/userController.spec.js.ejs +234 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/context.js.ejs +13 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/context.spec.js.ejs +31 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/index.js.ejs +5 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/index.js.ejs +6 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.js.ejs +27 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.spec.js.ejs +66 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/index.js.ejs +6 -0
- package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/user.types.js.ejs +19 -0
- package/templates/clean-architecture/js/src/interfaces/routes/api.js.ejs +17 -0
- package/templates/clean-architecture/js/src/interfaces/routes/api.spec.js.ejs +38 -0
- package/templates/clean-architecture/js/src/usecases/CreateUser.js.ejs +14 -0
- package/templates/clean-architecture/js/src/usecases/CreateUser.spec.js.ejs +51 -0
- 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/GetAllUsers.js +12 -0
- package/templates/clean-architecture/js/src/usecases/GetAllUsers.spec.js.ejs +61 -0
- package/templates/clean-architecture/js/src/usecases/UpdateUser.js.ejs +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/js/src/utils/httpCodes.js +9 -0
- package/templates/clean-architecture/ts/src/config/env.ts.ejs +46 -0
- package/templates/clean-architecture/ts/src/config/swagger.ts.ejs +6 -0
- package/templates/clean-architecture/ts/src/domain/user.ts.ejs +9 -0
- package/templates/clean-architecture/ts/src/errors/ApiError.ts +15 -0
- package/templates/clean-architecture/ts/src/errors/BadRequestError.spec.ts.ejs +22 -0
- package/templates/clean-architecture/ts/src/errors/BadRequestError.ts +9 -0
- package/templates/clean-architecture/ts/src/errors/NotFoundError.spec.ts.ejs +22 -0
- package/templates/clean-architecture/ts/src/errors/NotFoundError.ts +9 -0
- package/templates/clean-architecture/ts/src/index.ts.ejs +144 -0
- package/templates/clean-architecture/ts/src/infrastructure/log/logger.spec.ts.ejs +63 -0
- package/templates/clean-architecture/ts/src/infrastructure/log/logger.ts +36 -0
- package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.spec.ts.ejs +175 -0
- package/templates/clean-architecture/ts/src/infrastructure/repositories/userRepository.ts.ejs +125 -0
- package/templates/clean-architecture/ts/src/interfaces/controllers/userController.spec.ts.ejs +331 -0
- package/templates/clean-architecture/ts/src/interfaces/controllers/userController.ts.ejs +208 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/context.spec.ts.ejs +32 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/context.ts.ejs +17 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/index.ts.ejs +3 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/index.ts.ejs +4 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.ts.ejs +29 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/index.ts.ejs +4 -0
- package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/user.types.ts.ejs +17 -0
- package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.spec.ts.ejs +40 -0
- package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.ts.ejs +18 -0
- package/templates/clean-architecture/ts/src/usecases/createUser.spec.ts.ejs +51 -0
- package/templates/clean-architecture/ts/src/usecases/createUser.ts.ejs +11 -0
- 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/getAllUsers.spec.ts.ejs +63 -0
- package/templates/clean-architecture/ts/src/usecases/getAllUsers.ts +10 -0
- package/templates/clean-architecture/ts/src/usecases/updateUser.spec.ts.ejs +48 -0
- package/templates/clean-architecture/ts/src/usecases/updateUser.ts.ejs +10 -0
- package/templates/clean-architecture/ts/src/utils/errorMessages.ts +12 -0
- package/templates/clean-architecture/ts/src/utils/errorMiddleware.ts.ejs +27 -0
- package/templates/clean-architecture/ts/src/utils/httpCodes.ts +7 -0
- package/templates/common/.cursorrules.ejs +60 -0
- package/templates/common/.dockerignore +12 -0
- package/templates/common/.env.example.ejs +60 -0
- package/templates/common/.gitattributes +46 -0
- package/templates/common/.gitlab-ci.yml.ejs +86 -0
- package/templates/common/.lintstagedrc +6 -0
- package/templates/common/.prettierrc +7 -0
- package/templates/common/.snyk.ejs +45 -0
- package/templates/common/Dockerfile +73 -0
- package/templates/common/Jenkinsfile.ejs +87 -0
- package/templates/common/README.md.ejs +148 -0
- package/templates/common/_github/workflows/ci.yml.ejs +46 -0
- package/templates/common/_github/workflows/security.yml.ejs +36 -0
- package/templates/common/_gitignore +5 -0
- package/templates/common/_husky/pre-commit +4 -0
- package/templates/common/caching/clean/js/CreateUser.js.ejs +29 -0
- package/templates/common/caching/clean/js/DeleteUser.js.ejs +27 -0
- package/templates/common/caching/clean/js/GetAllUsers.js.ejs +37 -0
- package/templates/common/caching/clean/js/UpdateUser.js.ejs +27 -0
- package/templates/common/caching/clean/ts/createUser.ts.ejs +27 -0
- package/templates/common/caching/clean/ts/deleteUser.ts.ejs +24 -0
- package/templates/common/caching/clean/ts/getAllUsers.ts.ejs +34 -0
- package/templates/common/caching/clean/ts/updateUser.ts.ejs +25 -0
- package/templates/common/caching/js/memoryCache.js.ejs +60 -0
- package/templates/common/caching/js/memoryCache.spec.js.ejs +101 -0
- package/templates/common/caching/js/redisClient.js.ejs +75 -0
- package/templates/common/caching/js/redisClient.spec.js.ejs +147 -0
- package/templates/common/caching/ts/memoryCache.spec.ts.ejs +102 -0
- package/templates/common/caching/ts/memoryCache.ts.ejs +73 -0
- package/templates/common/caching/ts/redisClient.spec.ts.ejs +157 -0
- package/templates/common/caching/ts/redisClient.ts.ejs +89 -0
- package/templates/common/database/js/database.js.ejs +19 -0
- package/templates/common/database/js/database.spec.js.ejs +56 -0
- package/templates/common/database/js/models/User.js.ejs +91 -0
- package/templates/common/database/js/models/User.js.mongoose.ejs +35 -0
- package/templates/common/database/js/models/User.spec.js.ejs +94 -0
- package/templates/common/database/js/mongoose.js.ejs +33 -0
- package/templates/common/database/js/mongoose.spec.js.ejs +43 -0
- package/templates/common/database/ts/database.spec.ts.ejs +56 -0
- package/templates/common/database/ts/database.ts.ejs +21 -0
- package/templates/common/database/ts/models/User.spec.ts.ejs +100 -0
- package/templates/common/database/ts/models/User.ts.ejs +102 -0
- package/templates/common/database/ts/models/User.ts.mongoose.ejs +34 -0
- package/templates/common/database/ts/mongoose.spec.ts.ejs +42 -0
- package/templates/common/database/ts/mongoose.ts.ejs +28 -0
- package/templates/common/docker-compose.yml.ejs +159 -0
- package/templates/common/ecosystem.config.js.ejs +40 -0
- package/templates/common/eslint.config.mjs.ejs +77 -0
- package/templates/common/health/js/healthRoute.js.ejs +50 -0
- package/templates/common/health/js/healthRoute.spec.js.ejs +70 -0
- package/templates/common/health/ts/healthRoute.spec.ts.ejs +76 -0
- package/templates/common/health/ts/healthRoute.ts.ejs +49 -0
- package/templates/common/jest.config.js.ejs +32 -0
- package/templates/common/jest.e2e.config.js.ejs +8 -0
- package/templates/common/kafka/js/config/kafka.js +9 -0
- package/templates/common/kafka/js/config/kafka.spec.js.ejs +27 -0
- package/templates/common/kafka/js/messaging/baseConsumer.js.ejs +30 -0
- package/templates/common/kafka/js/messaging/baseConsumer.spec.js.ejs +58 -0
- package/templates/common/kafka/js/messaging/userEventSchema.js.ejs +12 -0
- package/templates/common/kafka/js/messaging/userEventSchema.spec.js.ejs +27 -0
- package/templates/common/kafka/js/messaging/welcomeEmailConsumer.js.ejs +44 -0
- package/templates/common/kafka/js/messaging/welcomeEmailConsumer.spec.js.ejs +86 -0
- package/templates/common/kafka/js/services/kafkaService.js.ejs +93 -0
- package/templates/common/kafka/js/services/kafkaService.spec.js.ejs +106 -0
- package/templates/common/kafka/js/utils/kafkaEvents.js.ejs +7 -0
- package/templates/common/kafka/ts/config/kafka.spec.ts.ejs +27 -0
- package/templates/common/kafka/ts/config/kafka.ts +7 -0
- package/templates/common/kafka/ts/messaging/baseConsumer.spec.ts.ejs +50 -0
- package/templates/common/kafka/ts/messaging/baseConsumer.ts.ejs +27 -0
- package/templates/common/kafka/ts/messaging/userEventSchema.spec.ts.ejs +51 -0
- package/templates/common/kafka/ts/messaging/userEventSchema.ts.ejs +12 -0
- package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.spec.ts.ejs +86 -0
- package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.ts.ejs +38 -0
- package/templates/common/kafka/ts/services/kafkaService.spec.ts.ejs +81 -0
- package/templates/common/kafka/ts/services/kafkaService.ts.ejs +95 -0
- package/templates/common/kafka/ts/utils/kafkaEvents.ts.ejs +5 -0
- package/templates/common/migrate-mongo-config.js.ejs +31 -0
- package/templates/common/migrations/init.js.ejs +23 -0
- package/templates/common/package.json.ejs +137 -0
- package/templates/common/prompts/add-feature.md.ejs +26 -0
- package/templates/common/prompts/project-context.md.ejs +43 -0
- package/templates/common/prompts/troubleshoot.md.ejs +28 -0
- package/templates/common/public/css/style.css +147 -0
- package/templates/common/scripts/run-e2e.js.ejs +63 -0
- package/templates/common/shutdown/js/gracefulShutdown.js.ejs +65 -0
- package/templates/common/shutdown/js/gracefulShutdown.spec.js.ejs +149 -0
- package/templates/common/shutdown/ts/gracefulShutdown.spec.ts.ejs +179 -0
- package/templates/common/shutdown/ts/gracefulShutdown.ts.ejs +59 -0
- package/templates/common/sonar-project.properties.ejs +27 -0
- package/templates/common/src/config/auth.js.ejs +19 -0
- package/templates/common/src/config/auth.ts.ejs +19 -0
- package/templates/common/src/controllers/authController.js.ejs +101 -0
- package/templates/common/src/controllers/authController.ts.ejs +101 -0
- package/templates/common/src/middleware/auth.js.ejs +20 -0
- package/templates/common/src/middleware/auth.ts.ejs +25 -0
- package/templates/common/src/middleware/upload.js.ejs +31 -0
- package/templates/common/src/middleware/upload.ts.ejs +32 -0
- package/templates/common/src/routes/authRoutes.js.ejs +20 -0
- package/templates/common/src/routes/authRoutes.ts.ejs +20 -0
- package/templates/common/src/tests/e2e/e2e.users.test.js.ejs +120 -0
- package/templates/common/src/tests/e2e/e2e.users.test.ts.ejs +120 -0
- package/templates/common/src/utils/errorMiddleware.spec.js.ejs +79 -0
- package/templates/common/src/utils/errorMiddleware.spec.ts.ejs +94 -0
- package/templates/common/swagger.yml.ejs +118 -0
- package/templates/common/tsconfig.json +23 -0
- package/templates/common/views/ejs/index.ejs +55 -0
- package/templates/common/views/pug/index.pug +40 -0
- package/templates/db/mysql/V1__Initial_Setup.sql.ejs +10 -0
- package/templates/db/postgres/V1__Initial_Setup.sql.ejs +10 -0
- package/templates/mvc/js/src/config/env.js.ejs +46 -0
- package/templates/mvc/js/src/config/swagger.js.ejs +6 -0
- package/templates/mvc/js/src/controllers/userController.js.ejs +288 -0
- package/templates/mvc/js/src/controllers/userController.spec.js.ejs +481 -0
- package/templates/mvc/js/src/errors/ApiError.js +14 -0
- package/templates/mvc/js/src/errors/BadRequestError.js +11 -0
- package/templates/mvc/js/src/errors/BadRequestError.spec.js.ejs +22 -0
- package/templates/mvc/js/src/errors/NotFoundError.js +11 -0
- package/templates/mvc/js/src/errors/NotFoundError.spec.js.ejs +22 -0
- package/templates/mvc/js/src/graphql/context.js.ejs +7 -0
- package/templates/mvc/js/src/graphql/context.spec.js.ejs +29 -0
- package/templates/mvc/js/src/graphql/index.js.ejs +5 -0
- package/templates/mvc/js/src/graphql/resolvers/index.js.ejs +6 -0
- package/templates/mvc/js/src/graphql/resolvers/user.resolvers.js.ejs +25 -0
- package/templates/mvc/js/src/graphql/resolvers/user.resolvers.spec.js.ejs +64 -0
- package/templates/mvc/js/src/graphql/typeDefs/index.js.ejs +6 -0
- package/templates/mvc/js/src/graphql/typeDefs/user.types.js.ejs +19 -0
- package/templates/mvc/js/src/index.js.ejs +141 -0
- package/templates/mvc/js/src/routes/api.js.ejs +15 -0
- package/templates/mvc/js/src/routes/api.spec.js.ejs +41 -0
- package/templates/mvc/js/src/utils/errorMessages.js +14 -0
- package/templates/mvc/js/src/utils/errorMiddleware.js +29 -0
- package/templates/mvc/js/src/utils/httpCodes.js +9 -0
- package/templates/mvc/js/src/utils/logger.js +40 -0
- package/templates/mvc/js/src/utils/logger.spec.js.ejs +63 -0
- package/templates/mvc/ts/src/config/env.ts.ejs +45 -0
- package/templates/mvc/ts/src/config/swagger.ts.ejs +6 -0
- package/templates/mvc/ts/src/controllers/userController.spec.ts.ejs +481 -0
- package/templates/mvc/ts/src/controllers/userController.ts.ejs +292 -0
- package/templates/mvc/ts/src/errors/ApiError.ts +15 -0
- package/templates/mvc/ts/src/errors/BadRequestError.spec.ts.ejs +22 -0
- package/templates/mvc/ts/src/errors/BadRequestError.ts +9 -0
- package/templates/mvc/ts/src/errors/NotFoundError.spec.ts.ejs +27 -0
- package/templates/mvc/ts/src/errors/NotFoundError.ts +9 -0
- package/templates/mvc/ts/src/graphql/context.spec.ts.ejs +30 -0
- package/templates/mvc/ts/src/graphql/context.ts.ejs +12 -0
- package/templates/mvc/ts/src/graphql/index.ts.ejs +3 -0
- package/templates/mvc/ts/src/graphql/resolvers/index.ts.ejs +4 -0
- package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -0
- package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.ts.ejs +29 -0
- package/templates/mvc/ts/src/graphql/typeDefs/index.ts.ejs +4 -0
- package/templates/mvc/ts/src/graphql/typeDefs/user.types.ts.ejs +17 -0
- package/templates/mvc/ts/src/index.ts.ejs +157 -0
- package/templates/mvc/ts/src/routes/api.spec.ts.ejs +59 -0
- package/templates/mvc/ts/src/routes/api.ts.ejs +17 -0
- package/templates/mvc/ts/src/utils/errorMessages.ts +12 -0
- package/templates/mvc/ts/src/utils/errorMiddleware.ts.ejs +27 -0
- package/templates/mvc/ts/src/utils/httpCodes.ts +7 -0
- package/templates/mvc/ts/src/utils/logger.spec.ts.ejs +63 -0
- package/templates/mvc/ts/src/utils/logger.ts +36 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const GetAllUsers = require('@/usecases/GetAllUsers');
|
|
2
|
+
const UserRepository = require('@/infrastructure/repositories/UserRepository');
|
|
3
|
+
<%_ if (caching !== 'None') { -%>
|
|
4
|
+
const cacheService = require('<% if (caching === "Redis") { %>@/infrastructure/caching/redisClient<% } else { %>@/infrastructure/caching/memoryCache<% } %>');
|
|
5
|
+
<%_ } -%>
|
|
6
|
+
|
|
7
|
+
jest.mock('@/infrastructure/repositories/UserRepository');
|
|
8
|
+
<%_ if (caching !== 'None') { -%>
|
|
9
|
+
jest.mock('<% if (caching === "Redis") { %>@/infrastructure/caching/redisClient<% } else { %>@/infrastructure/caching/memoryCache<% } %>', () => ({
|
|
10
|
+
get: jest.fn(),
|
|
11
|
+
set: jest.fn(),
|
|
12
|
+
del: jest.fn()
|
|
13
|
+
}));
|
|
14
|
+
<%_ } -%>
|
|
15
|
+
|
|
16
|
+
describe('GetAllUsers UseCase', () => {
|
|
17
|
+
let getAllUsers;
|
|
18
|
+
let mockUserRepository;
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
mockUserRepository = new UserRepository();
|
|
22
|
+
getAllUsers = new GetAllUsers(mockUserRepository);
|
|
23
|
+
jest.clearAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should retrieve all users', async () => {
|
|
27
|
+
const expectedUsers = [{ id: 1, name: 'Alice', email: 'alice@example.com' }];
|
|
28
|
+
mockUserRepository.getUsers.mockResolvedValue(expectedUsers);
|
|
29
|
+
<%_ if (caching !== 'None') { -%>
|
|
30
|
+
cacheService.get.mockResolvedValue(null); // Cache miss
|
|
31
|
+
<%_ } -%>
|
|
32
|
+
const result = await getAllUsers.execute();
|
|
33
|
+
|
|
34
|
+
expect(mockUserRepository.getUsers).toHaveBeenCalledTimes(1);
|
|
35
|
+
expect(result).toEqual(expectedUsers);
|
|
36
|
+
<%_ if (caching !== 'None') { -%>
|
|
37
|
+
expect(cacheService.set).toHaveBeenCalled();
|
|
38
|
+
<%_ } -%>
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
<%_ if (caching !== 'None') { -%>
|
|
42
|
+
it('should return from cache if available', async () => {
|
|
43
|
+
const cachedUsers = [{ id: 1, name: 'Cached', email: 'cached@example.com' }];
|
|
44
|
+
cacheService.get.mockResolvedValue(cachedUsers);
|
|
45
|
+
|
|
46
|
+
const result = await getAllUsers.execute();
|
|
47
|
+
|
|
48
|
+
expect(mockUserRepository.getUsers).not.toHaveBeenCalled();
|
|
49
|
+
expect(result).toEqual(cachedUsers);
|
|
50
|
+
});
|
|
51
|
+
<%_ } -%>
|
|
52
|
+
|
|
53
|
+
it('should throw an error if repository fails', async () => {
|
|
54
|
+
const error = new Error('Database error');
|
|
55
|
+
mockUserRepository.getUsers.mockRejectedValue(error);
|
|
56
|
+
<%_ if (caching !== 'None') { -%>
|
|
57
|
+
cacheService.get.mockResolvedValue(null); // Cache miss
|
|
58
|
+
<%_ } -%>
|
|
59
|
+
await expect(getAllUsers.execute()).rejects.toThrow(error);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const UpdateUser = require('@/usecases/UpdateUser');
|
|
2
|
+
const UserRepository = require('@/infrastructure/repositories/UserRepository');
|
|
3
|
+
<%_ if (caching !== 'None') { -%>
|
|
4
|
+
const cacheService = require('<% if (caching === "Redis") { %>@/infrastructure/caching/redisClient<% } else { %>@/infrastructure/caching/memoryCache<% } %>');
|
|
5
|
+
<%_ } -%>
|
|
6
|
+
|
|
7
|
+
jest.mock('@/infrastructure/repositories/UserRepository');
|
|
8
|
+
<%_ if (caching !== 'None') { -%>
|
|
9
|
+
jest.mock('<% if (caching === "Redis") { %>@/infrastructure/caching/redisClient<% } else { %>@/infrastructure/caching/memoryCache<% } %>', () => ({
|
|
10
|
+
get: jest.fn(),
|
|
11
|
+
set: jest.fn(),
|
|
12
|
+
del: jest.fn()
|
|
13
|
+
}));
|
|
14
|
+
<%_ } -%>
|
|
15
|
+
|
|
16
|
+
describe('UpdateUser UseCase', () => {
|
|
17
|
+
let updateUser;
|
|
18
|
+
let mockUserRepository;
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
mockUserRepository = new UserRepository();
|
|
22
|
+
updateUser = new UpdateUser(mockUserRepository);
|
|
23
|
+
jest.clearAllMocks();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('should update and return the user', async () => {
|
|
27
|
+
const id = 1;
|
|
28
|
+
const data = { name: 'Updated Name' };
|
|
29
|
+
const expectedUser = { id, name: 'Updated Name', email: 'test@test.com' };
|
|
30
|
+
|
|
31
|
+
mockUserRepository.update.mockResolvedValue(expectedUser);
|
|
32
|
+
|
|
33
|
+
const result = await updateUser.execute(id, data);
|
|
34
|
+
|
|
35
|
+
expect(mockUserRepository.update).toHaveBeenCalledWith(id, data);
|
|
36
|
+
expect(result).toEqual(expectedUser);
|
|
37
|
+
<%_ if (caching !== 'None') { -%>
|
|
38
|
+
expect(cacheService.del).toHaveBeenCalledWith('users:all');
|
|
39
|
+
<%_ } %>
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('should throw an error if repository fails', async () => {
|
|
43
|
+
const error = new Error('Database error');
|
|
44
|
+
mockUserRepository.update.mockRejectedValue(error);
|
|
45
|
+
|
|
46
|
+
await expect(updateUser.execute(1, { name: 'Test' })).rejects.toThrow(error);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const ERROR_MESSAGES = {
|
|
2
|
+
USER_NOT_FOUND: 'User not found',
|
|
3
|
+
RESOURCE_NOT_FOUND: 'Resource not found',
|
|
4
|
+
INVALID_USER_DATA: 'Invalid user event data',
|
|
5
|
+
INTERNAL_SERVER_ERROR: 'Internal Server Error',
|
|
6
|
+
BAD_REQUEST: 'Bad Request',
|
|
7
|
+
FETCH_USERS_ERROR: 'Error fetching users',
|
|
8
|
+
CREATE_USER_ERROR: 'Error creating user',
|
|
9
|
+
UPDATE_USER_ERROR: 'Error updating user',
|
|
10
|
+
DELETE_USER_ERROR: 'Error deleting user',
|
|
11
|
+
DATABASE_PING_FAILED: 'Health Check Database Ping Failed',
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
module.exports = ERROR_MESSAGES;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import logger from '@/infrastructure/log/logger';
|
|
4
|
+
|
|
5
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
6
|
+
dotenv.config();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const envSchema = z.object({
|
|
10
|
+
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
|
|
11
|
+
PORT: z.string().transform(Number).default('3000'),
|
|
12
|
+
<%_ if (database !== 'None') { -%>
|
|
13
|
+
DB_HOST: z.string(),
|
|
14
|
+
<%_ if (database === 'MySQL') { -%>
|
|
15
|
+
DB_USER: z.string(),
|
|
16
|
+
DB_PASSWORD: z.string(),
|
|
17
|
+
DB_NAME: z.string(),
|
|
18
|
+
DB_PORT: z.string().transform(Number),
|
|
19
|
+
<%_ } else if (database === 'PostgreSQL') { -%>
|
|
20
|
+
DB_USER: z.string(),
|
|
21
|
+
DB_PASSWORD: z.string(),
|
|
22
|
+
DB_NAME: z.string(),
|
|
23
|
+
DB_PORT: z.string().transform(Number),
|
|
24
|
+
<%_ } else if (database === 'MongoDB') { -%>
|
|
25
|
+
DB_NAME: z.string(),
|
|
26
|
+
DB_PORT: z.string().transform(Number),
|
|
27
|
+
<%_ } -%>
|
|
28
|
+
<%_ } -%>
|
|
29
|
+
<%_ if (caching === 'Redis') { -%>
|
|
30
|
+
REDIS_HOST: z.string(),
|
|
31
|
+
REDIS_PORT: z.string().transform(Number),
|
|
32
|
+
REDIS_PASSWORD: z.string().optional(),
|
|
33
|
+
<%_ } -%>
|
|
34
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
35
|
+
KAFKA_BROKER: z.string(),
|
|
36
|
+
<%_ } -%>
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const _env = envSchema.safeParse(process.env);
|
|
40
|
+
|
|
41
|
+
if (!_env.success) {
|
|
42
|
+
logger.error('❌ Invalid environment variables:', _env.error.format());
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export const env = _env.data;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class ApiError extends Error {
|
|
2
|
+
statusCode: number;
|
|
3
|
+
isOperational: boolean;
|
|
4
|
+
|
|
5
|
+
constructor(statusCode: number, message: string, isOperational = true, stack = '') {
|
|
6
|
+
super(message);
|
|
7
|
+
this.statusCode = statusCode;
|
|
8
|
+
this.isOperational = isOperational;
|
|
9
|
+
if (stack) {
|
|
10
|
+
this.stack = stack;
|
|
11
|
+
} else {
|
|
12
|
+
Error.captureStackTrace(this, this.constructor);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { env } from '@/config/env';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import cors from 'cors';
|
|
4
|
+
import helmet from 'helmet';
|
|
5
|
+
import hpp from 'hpp';
|
|
6
|
+
import rateLimit from 'express-rate-limit';
|
|
7
|
+
import logger from '@/infrastructure/log/logger';
|
|
8
|
+
import morgan from 'morgan';
|
|
9
|
+
import { errorMiddleware } from '@/utils/errorMiddleware';
|
|
10
|
+
import { setupGracefulShutdown } from '@/utils/gracefulShutdown';
|
|
11
|
+
import healthRoutes from '@/interfaces/routes/healthRoute';
|
|
12
|
+
<% if (communication === 'REST APIs' || communication === 'Kafka') { -%>
|
|
13
|
+
import userRoutes from '@/interfaces/routes/userRoutes';
|
|
14
|
+
import swaggerUi from 'swagger-ui-express';
|
|
15
|
+
import swaggerSpecs from '@/config/swagger';<% } %>
|
|
16
|
+
<%_ if (communication === 'Kafka') { -%>import { kafkaService } from '@/infrastructure/messaging/kafkaClient';<%_ } -%>
|
|
17
|
+
<%_ if (communication === 'GraphQL') { -%>
|
|
18
|
+
import { ApolloServer } from '@apollo/server';
|
|
19
|
+
import { expressMiddleware } from '@as-integrations/express4';
|
|
20
|
+
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
|
|
21
|
+
import { unwrapResolverError } from '@apollo/server/errors';
|
|
22
|
+
import { ApiError } from '@/errors/ApiError';
|
|
23
|
+
import { typeDefs, resolvers } from '@/interfaces/graphql';
|
|
24
|
+
import { gqlContext, MyContext } from '@/interfaces/graphql/context';
|
|
25
|
+
<%_ } -%>
|
|
26
|
+
|
|
27
|
+
const app = express();
|
|
28
|
+
const port = env.PORT;
|
|
29
|
+
|
|
30
|
+
// Security Middleware
|
|
31
|
+
<%_ if (communication === 'GraphQL') { -%>
|
|
32
|
+
app.use(helmet({
|
|
33
|
+
crossOriginEmbedderPolicy: false,
|
|
34
|
+
contentSecurityPolicy: {
|
|
35
|
+
directives: {
|
|
36
|
+
imgSrc: [`'self'`, 'data:', 'apollo-server-landing-page.cdn.apollographql.com'],
|
|
37
|
+
scriptSrc: [`'self'`, `https: 'unsafe-inline'`],
|
|
38
|
+
manifestSrc: [`'self'`, 'apollo-server-landing-page.cdn.apollographql.com'],
|
|
39
|
+
frameSrc: [`'self'`, 'sandbox.embed.apollographql.com'],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
}));
|
|
43
|
+
<%_ } else { -%>
|
|
44
|
+
app.use(helmet());
|
|
45
|
+
<%_ } -%>
|
|
46
|
+
app.use(hpp());
|
|
47
|
+
app.use(cors({ origin: '*', methods: ['GET', 'POST', 'PUT', 'DELETE'] }));
|
|
48
|
+
const limiter = rateLimit({ windowMs: 10 * 60 * 1000, max: 100 });
|
|
49
|
+
app.use(limiter);
|
|
50
|
+
|
|
51
|
+
app.use(express.json());
|
|
52
|
+
<% if (includeMulter) { %>
|
|
53
|
+
import path from 'path';
|
|
54
|
+
app.use('/uploads', express.static(path.join(__dirname, '../uploads')));
|
|
55
|
+
<% } %>
|
|
56
|
+
app.use(morgan('combined', { stream: { write: (message) => logger.info(message.trim()) } }));
|
|
57
|
+
|
|
58
|
+
<%_ if (communication === 'REST APIs' || communication === 'Kafka') { -%>
|
|
59
|
+
app.use('/api/users', userRoutes);
|
|
60
|
+
<%_ } -%>
|
|
61
|
+
<%_ if (communication === 'REST APIs' || communication === 'Kafka') { -%>
|
|
62
|
+
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));
|
|
63
|
+
<%_ } -%>
|
|
64
|
+
app.use('/health', healthRoutes);
|
|
65
|
+
|
|
66
|
+
// Start Server Logic
|
|
67
|
+
const startServer = async () => {
|
|
68
|
+
<%_ if (communication === 'GraphQL') { -%>
|
|
69
|
+
// GraphQL Setup
|
|
70
|
+
const apolloServer = new ApolloServer<MyContext>({
|
|
71
|
+
typeDefs,
|
|
72
|
+
resolvers,
|
|
73
|
+
plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })],
|
|
74
|
+
formatError: (formattedError, error) => {
|
|
75
|
+
const originalError = unwrapResolverError(error);
|
|
76
|
+
if (originalError instanceof ApiError) {
|
|
77
|
+
return {
|
|
78
|
+
...formattedError,
|
|
79
|
+
message: originalError.message,
|
|
80
|
+
extensions: {
|
|
81
|
+
...formattedError.extensions,
|
|
82
|
+
code: originalError.statusCode.toString(),
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
logger.error(`GraphQL Error: ${formattedError.message}`);
|
|
88
|
+
if (originalError instanceof Error && originalError.stack && process.env.NODE_ENV === 'development') {
|
|
89
|
+
logger.error(originalError.stack);
|
|
90
|
+
}
|
|
91
|
+
return formattedError;
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
await apolloServer.start();
|
|
95
|
+
app.use('/graphql', expressMiddleware(apolloServer, { context: gqlContext }));
|
|
96
|
+
<%_ } -%>
|
|
97
|
+
app.use(errorMiddleware);
|
|
98
|
+
const server = app.listen(port, () => {
|
|
99
|
+
logger.info(`Server running on port ${port}`);
|
|
100
|
+
<%_ if (communication === 'Kafka') { -%>
|
|
101
|
+
kafkaService.connect()
|
|
102
|
+
.then(async () => {
|
|
103
|
+
logger.info('Kafka connected');
|
|
104
|
+
})
|
|
105
|
+
.catch(err => {
|
|
106
|
+
logger.error('Failed to connect to Kafka after retries:', (err as Error).message);
|
|
107
|
+
});
|
|
108
|
+
<%_ } -%>
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
setupGracefulShutdown(server<% if(communication === 'Kafka') { %>, kafkaService<% } %>);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
<%_ if (database !== 'None') { -%>
|
|
115
|
+
// Database Sync
|
|
116
|
+
<%_ if (database !== 'None') { -%>
|
|
117
|
+
import <% if (database === 'MongoDB') { %>connectDB<% } else { %>sequelize<% } %> from '@/infrastructure/database/database';
|
|
118
|
+
<%_ } -%>
|
|
119
|
+
|
|
120
|
+
const syncDatabase = async () => {
|
|
121
|
+
let retries = 30;
|
|
122
|
+
while (retries) {
|
|
123
|
+
try {
|
|
124
|
+
<%_ if (database === 'MongoDB') { -%>
|
|
125
|
+
await connectDB();
|
|
126
|
+
<%_ } else { -%>
|
|
127
|
+
await sequelize.sync();
|
|
128
|
+
<%_ } -%>
|
|
129
|
+
logger.info('Database synced');
|
|
130
|
+
await startServer();
|
|
131
|
+
break;
|
|
132
|
+
} catch (error) {
|
|
133
|
+
logger.error('Error syncing database:', error);
|
|
134
|
+
retries -= 1;
|
|
135
|
+
logger.info(`Retries left: ${retries}`);
|
|
136
|
+
await new Promise(res => setTimeout(res, 5000));
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
syncDatabase();
|
|
142
|
+
<%_ } else { -%>
|
|
143
|
+
startServer();
|
|
144
|
+
<%_ } -%>
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
jest.mock('winston-daily-rotate-file');
|
|
2
|
+
jest.mock('winston', () => {
|
|
3
|
+
const mockLogger = {
|
|
4
|
+
add: jest.fn(),
|
|
5
|
+
info: jest.fn(),
|
|
6
|
+
error: jest.fn(),
|
|
7
|
+
warn: jest.fn()
|
|
8
|
+
};
|
|
9
|
+
const format = {
|
|
10
|
+
combine: jest.fn(),
|
|
11
|
+
timestamp: jest.fn(),
|
|
12
|
+
json: jest.fn(),
|
|
13
|
+
simple: jest.fn()
|
|
14
|
+
};
|
|
15
|
+
const transports = {
|
|
16
|
+
Console: jest.fn(),
|
|
17
|
+
DailyRotateFile: jest.fn()
|
|
18
|
+
};
|
|
19
|
+
return {
|
|
20
|
+
format,
|
|
21
|
+
transports,
|
|
22
|
+
createLogger: jest.fn().mockReturnValue(mockLogger)
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
<% if (architecture === 'MVC') { -%>
|
|
27
|
+
import logger from '@/utils/logger';
|
|
28
|
+
<% } else { -%>
|
|
29
|
+
import logger from '@/infrastructure/log/logger';
|
|
30
|
+
<% } -%>
|
|
31
|
+
|
|
32
|
+
describe('Logger', () => {
|
|
33
|
+
it('should export a logger instance', () => {
|
|
34
|
+
expect(logger).toBeDefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should have info method', () => {
|
|
38
|
+
expect(typeof logger.info).toBe('function');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should have error method', () => {
|
|
42
|
+
expect(typeof logger.error).toBe('function');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should call info', () => {
|
|
46
|
+
logger.info('test message');
|
|
47
|
+
expect(logger.info).toHaveBeenCalledWith('test message');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should call error', () => {
|
|
51
|
+
logger.error('test error');
|
|
52
|
+
expect(logger.error).toHaveBeenCalledWith('test error');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should use JSON format in production environment', () => {
|
|
56
|
+
const winston = require('winston');
|
|
57
|
+
jest.resetModules();
|
|
58
|
+
process.env.NODE_ENV = 'production';
|
|
59
|
+
require('<% if (architecture === "MVC") { %>@/utils/logger<% } else { %>@/infrastructure/log/logger<% } %>');
|
|
60
|
+
expect(winston.format.json).toHaveBeenCalled();
|
|
61
|
+
process.env.NODE_ENV = 'test';
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import winston from 'winston';
|
|
2
|
+
import 'winston-daily-rotate-file';
|
|
3
|
+
|
|
4
|
+
const logger = winston.createLogger({
|
|
5
|
+
level: 'info',
|
|
6
|
+
format: winston.format.combine(
|
|
7
|
+
winston.format.timestamp(),
|
|
8
|
+
winston.format.json()
|
|
9
|
+
),
|
|
10
|
+
defaultMeta: { service: 'user-service' },
|
|
11
|
+
transports: [
|
|
12
|
+
new winston.transports.DailyRotateFile({
|
|
13
|
+
filename: 'logs/error-%DATE%.log',
|
|
14
|
+
datePattern: 'YYYY-MM-DD',
|
|
15
|
+
zippedArchive: true,
|
|
16
|
+
maxSize: '20m',
|
|
17
|
+
maxFiles: '14d',
|
|
18
|
+
level: 'error',
|
|
19
|
+
}),
|
|
20
|
+
new winston.transports.DailyRotateFile({
|
|
21
|
+
filename: 'logs/combined-%DATE%.log',
|
|
22
|
+
datePattern: 'YYYY-MM-DD',
|
|
23
|
+
zippedArchive: true,
|
|
24
|
+
maxSize: '20m',
|
|
25
|
+
maxFiles: '14d',
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
logger.add(new winston.transports.Console({
|
|
31
|
+
format: process.env.NODE_ENV !== 'production'
|
|
32
|
+
? winston.format.simple()
|
|
33
|
+
: winston.format.json(),
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
export default logger;
|