nestforge-generator 0.1.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/LICENSE +21 -0
- package/README.md +371 -0
- package/README.pt-BR.md +370 -0
- package/dist/features/auth-strategy.js +19 -0
- package/dist/features/auth-strategy.js.map +1 -0
- package/dist/features/database.js +59 -0
- package/dist/features/database.js.map +1 -0
- package/dist/features/dependencies.js +57 -0
- package/dist/features/dependencies.js.map +1 -0
- package/dist/features/language.js +172 -0
- package/dist/features/language.js.map +1 -0
- package/dist/features/markers.js +116 -0
- package/dist/features/markers.js.map +1 -0
- package/dist/generator.js +120 -0
- package/dist/generator.js.map +1 -0
- package/dist/index.js +59 -0
- package/dist/index.js.map +1 -0
- package/dist/prompts.js +137 -0
- package/dist/prompts.js.map +1 -0
- package/package.json +76 -0
- package/templates/drizzle/.env.example +45 -0
- package/templates/drizzle/.env.test +38 -0
- package/templates/drizzle/.github/workflows/ci.yml +119 -0
- package/templates/drizzle/ARCHITECTURE.md +227 -0
- package/templates/drizzle/ARCHITECTURE.pt-BR.md +225 -0
- package/templates/drizzle/CODE_OF_CONDUCT.md +29 -0
- package/templates/drizzle/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/drizzle/CONTRIBUTING.md +57 -0
- package/templates/drizzle/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/drizzle/Dockerfile +43 -0
- package/templates/drizzle/LICENSE +21 -0
- package/templates/drizzle/README.md +257 -0
- package/templates/drizzle/README.pt-BR.md +257 -0
- package/templates/drizzle/ROADMAP.md +143 -0
- package/templates/drizzle/ROADMAP.pt-BR.md +143 -0
- package/templates/drizzle/TESTING.md +136 -0
- package/templates/drizzle/TESTING.pt-BR.md +136 -0
- package/templates/drizzle/docker-compose.yml +82 -0
- package/templates/drizzle/docs/adding-a-module.md +585 -0
- package/templates/drizzle/docs/features-markers.md +457 -0
- package/templates/drizzle/drizzle.config.ts +49 -0
- package/templates/drizzle/nest-cli.json +8 -0
- package/templates/drizzle/package.json +96 -0
- package/templates/drizzle/src/app.module.ts +72 -0
- package/templates/drizzle/src/auth/auth.controller.ts +283 -0
- package/templates/drizzle/src/auth/auth.module.ts +81 -0
- package/templates/drizzle/src/auth/auth.service.spec.ts +111 -0
- package/templates/drizzle/src/auth/auth.service.ts +631 -0
- package/templates/drizzle/src/auth/drizzle-session.store.ts +263 -0
- package/templates/drizzle/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/drizzle/src/auth/dto/login.dto.ts +10 -0
- package/templates/drizzle/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/drizzle/src/auth/dto/register.dto.ts +11 -0
- package/templates/drizzle/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/drizzle/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/drizzle/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/drizzle/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/drizzle/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/drizzle/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/drizzle/src/auth/session.service.spec.ts +74 -0
- package/templates/drizzle/src/auth/session.service.ts +96 -0
- package/templates/drizzle/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/drizzle/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/drizzle/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/drizzle/src/auth/token.service.ts +175 -0
- package/templates/drizzle/src/common/constants/permissions.ts +8 -0
- package/templates/drizzle/src/common/constants/role-permissions.ts +9 -0
- package/templates/drizzle/src/common/constants/role.enum.ts +5 -0
- package/templates/drizzle/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/drizzle/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/drizzle/src/common/decorators/public.decorator.ts +4 -0
- package/templates/drizzle/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/drizzle/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/drizzle/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/drizzle/src/common/guards/permissions.guard.ts +28 -0
- package/templates/drizzle/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/drizzle/src/common/guards/roles.guard.ts +24 -0
- package/templates/drizzle/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/drizzle/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/drizzle/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/drizzle/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/drizzle/src/common/utils/hash.util.ts +5 -0
- package/templates/drizzle/src/config/env.validation.ts +50 -0
- package/templates/drizzle/src/database/database-lifecycle.service.ts +30 -0
- package/templates/drizzle/src/database/database.constants.ts +2 -0
- package/templates/drizzle/src/database/database.decorators.ts +5 -0
- package/templates/drizzle/src/database/database.module.ts +96 -0
- package/templates/drizzle/src/database/database.types.ts +28 -0
- package/templates/drizzle/src/database/schema/index.ts +11 -0
- package/templates/drizzle/src/database/schema/mysql.schema.ts +215 -0
- package/templates/drizzle/src/database/schema/postgres.schema.ts +218 -0
- package/templates/drizzle/src/database/schema/sqlite.schema.ts +190 -0
- package/templates/drizzle/src/database/seed.ts +152 -0
- package/templates/drizzle/src/health/health.controller.ts +49 -0
- package/templates/drizzle/src/health/health.module.ts +19 -0
- package/templates/drizzle/src/health/indicators/drizzle-health.indicator.spec.ts +71 -0
- package/templates/drizzle/src/health/indicators/drizzle-health.indicator.ts +52 -0
- package/templates/drizzle/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/drizzle/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/drizzle/src/mail/mail.module.ts +12 -0
- package/templates/drizzle/src/mail/mail.processor.ts +48 -0
- package/templates/drizzle/src/mail/mail.service.ts +24 -0
- package/templates/drizzle/src/mail/templates/email-templates.ts +28 -0
- package/templates/drizzle/src/main.ts +145 -0
- package/templates/drizzle/src/metrics/metrics.controller.ts +22 -0
- package/templates/drizzle/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/drizzle/src/metrics/metrics.module.ts +12 -0
- package/templates/drizzle/src/metrics/metrics.service.ts +34 -0
- package/templates/drizzle/src/types/express-session.d.ts +13 -0
- package/templates/drizzle/src/users/dto/create-user.dto.ts +12 -0
- package/templates/drizzle/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/drizzle/src/users/dto/update-user.dto.ts +6 -0
- package/templates/drizzle/src/users/entities/user.entity.ts +23 -0
- package/templates/drizzle/src/users/users.controller.ts +165 -0
- package/templates/drizzle/src/users/users.module.ts +10 -0
- package/templates/drizzle/src/users/users.service.spec.ts +301 -0
- package/templates/drizzle/src/users/users.service.ts +213 -0
- package/templates/drizzle/test/auth.e2e-spec.ts +114 -0
- package/templates/drizzle/test/session-auth.e2e-spec.ts +163 -0
- package/templates/drizzle/test/users.e2e-spec.ts +212 -0
- package/templates/drizzle/test/utils/clean-database.ts +104 -0
- package/templates/drizzle/test/utils/e2e-setup.ts +77 -0
- package/templates/drizzle/tsconfig.build.json +4 -0
- package/templates/drizzle/tsconfig.json +25 -0
- package/templates/drizzle/vitest.config.ts +20 -0
- package/templates/drizzle/vitest.e2e.config.ts +19 -0
- package/templates/prisma/.env.example +44 -0
- package/templates/prisma/.env.test +37 -0
- package/templates/prisma/.github/workflows/ci.yml +109 -0
- package/templates/prisma/ARCHITECTURE.md +70 -0
- package/templates/prisma/ARCHITECTURE.pt-BR.md +63 -0
- package/templates/prisma/CODE_OF_CONDUCT.md +29 -0
- package/templates/prisma/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/prisma/CONTRIBUTING.md +57 -0
- package/templates/prisma/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/prisma/Dockerfile +33 -0
- package/templates/prisma/LICENSE +21 -0
- package/templates/prisma/README.md +267 -0
- package/templates/prisma/README.pt-BR.md +267 -0
- package/templates/prisma/ROADMAP.md +75 -0
- package/templates/prisma/ROADMAP.pt-BR.md +65 -0
- package/templates/prisma/TESTING.md +123 -0
- package/templates/prisma/TESTING.pt-BR.md +123 -0
- package/templates/prisma/docker-compose.yml +78 -0
- package/templates/prisma/docs/adding-a-module.md +230 -0
- package/templates/prisma/docs/features-markers.md +50 -0
- package/templates/prisma/nest-cli.json +8 -0
- package/templates/prisma/package.json +95 -0
- package/templates/prisma/prisma/schema.prisma +105 -0
- package/templates/prisma/prisma/seed.ts +46 -0
- package/templates/prisma/src/app.module.ts +72 -0
- package/templates/prisma/src/auth/auth.controller.ts +283 -0
- package/templates/prisma/src/auth/auth.module.ts +59 -0
- package/templates/prisma/src/auth/auth.service.spec.ts +53 -0
- package/templates/prisma/src/auth/auth.service.ts +224 -0
- package/templates/prisma/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/prisma/src/auth/dto/login.dto.ts +10 -0
- package/templates/prisma/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/prisma/src/auth/dto/register.dto.ts +11 -0
- package/templates/prisma/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/prisma/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/prisma/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/prisma/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/prisma/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/prisma/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/prisma/src/auth/session.service.spec.ts +74 -0
- package/templates/prisma/src/auth/session.service.ts +96 -0
- package/templates/prisma/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/prisma/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/prisma/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/prisma/src/auth/token.service.ts +84 -0
- package/templates/prisma/src/common/constants/permissions.ts +8 -0
- package/templates/prisma/src/common/constants/role-permissions.ts +9 -0
- package/templates/prisma/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/prisma/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/prisma/src/common/decorators/public.decorator.ts +4 -0
- package/templates/prisma/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/prisma/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/prisma/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/prisma/src/common/guards/permissions.guard.ts +28 -0
- package/templates/prisma/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/prisma/src/common/guards/roles.guard.ts +24 -0
- package/templates/prisma/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/prisma/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/prisma/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/prisma/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/prisma/src/common/utils/hash.util.ts +5 -0
- package/templates/prisma/src/config/env.validation.ts +49 -0
- package/templates/prisma/src/database/prisma.module.ts +9 -0
- package/templates/prisma/src/database/prisma.service.ts +13 -0
- package/templates/prisma/src/health/health.controller.ts +49 -0
- package/templates/prisma/src/health/health.module.ts +19 -0
- package/templates/prisma/src/health/indicators/prisma-health.indicator.spec.ts +22 -0
- package/templates/prisma/src/health/indicators/prisma-health.indicator.ts +19 -0
- package/templates/prisma/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/prisma/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/prisma/src/mail/mail.module.ts +12 -0
- package/templates/prisma/src/mail/mail.processor.ts +48 -0
- package/templates/prisma/src/mail/mail.service.ts +24 -0
- package/templates/prisma/src/mail/templates/email-templates.ts +28 -0
- package/templates/prisma/src/main.ts +92 -0
- package/templates/prisma/src/metrics/metrics.controller.ts +22 -0
- package/templates/prisma/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/prisma/src/metrics/metrics.module.ts +12 -0
- package/templates/prisma/src/metrics/metrics.service.ts +34 -0
- package/templates/prisma/src/types/express-session.d.ts +13 -0
- package/templates/prisma/src/users/dto/create-user.dto.ts +12 -0
- package/templates/prisma/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/prisma/src/users/dto/update-user.dto.ts +6 -0
- package/templates/prisma/src/users/entities/user.entity.ts +20 -0
- package/templates/prisma/src/users/users.controller.ts +165 -0
- package/templates/prisma/src/users/users.module.ts +10 -0
- package/templates/prisma/src/users/users.service.spec.ts +104 -0
- package/templates/prisma/src/users/users.service.ts +114 -0
- package/templates/prisma/test/auth.e2e-spec.ts +75 -0
- package/templates/prisma/test/session-auth.e2e-spec.ts +163 -0
- package/templates/prisma/test/users.e2e-spec.ts +106 -0
- package/templates/prisma/test/utils/clean-database.ts +14 -0
- package/templates/prisma/test/utils/e2e-setup.ts +58 -0
- package/templates/prisma/tsconfig.build.json +4 -0
- package/templates/prisma/tsconfig.json +25 -0
- package/templates/prisma/vitest.config.ts +20 -0
- package/templates/prisma/vitest.e2e.config.ts +19 -0
- package/templates/typeorm/.env.example +45 -0
- package/templates/typeorm/.env.test +38 -0
- package/templates/typeorm/.github/workflows/ci.yml +116 -0
- package/templates/typeorm/ARCHITECTURE.md +158 -0
- package/templates/typeorm/ARCHITECTURE.pt-BR.md +156 -0
- package/templates/typeorm/CODE_OF_CONDUCT.md +29 -0
- package/templates/typeorm/CODE_OF_CONDUCT.pt-BR.md +27 -0
- package/templates/typeorm/CONTRIBUTING.md +57 -0
- package/templates/typeorm/CONTRIBUTING.pt-BR.md +54 -0
- package/templates/typeorm/Dockerfile +43 -0
- package/templates/typeorm/LICENSE +21 -0
- package/templates/typeorm/README.md +266 -0
- package/templates/typeorm/README.pt-BR.md +266 -0
- package/templates/typeorm/ROADMAP.md +79 -0
- package/templates/typeorm/ROADMAP.pt-BR.md +67 -0
- package/templates/typeorm/TESTING.md +114 -0
- package/templates/typeorm/TESTING.pt-BR.md +114 -0
- package/templates/typeorm/docker-compose.yml +78 -0
- package/templates/typeorm/docs/adding-a-module.md +369 -0
- package/templates/typeorm/docs/features-markers.md +50 -0
- package/templates/typeorm/nest-cli.json +8 -0
- package/templates/typeorm/package.json +98 -0
- package/templates/typeorm/src/app.module.ts +72 -0
- package/templates/typeorm/src/auth/auth.controller.ts +283 -0
- package/templates/typeorm/src/auth/auth.module.ts +86 -0
- package/templates/typeorm/src/auth/auth.service.spec.ts +111 -0
- package/templates/typeorm/src/auth/auth.service.ts +346 -0
- package/templates/typeorm/src/auth/dto/forgot-password.dto.ts +9 -0
- package/templates/typeorm/src/auth/dto/login.dto.ts +10 -0
- package/templates/typeorm/src/auth/dto/refresh-token.dto.ts +9 -0
- package/templates/typeorm/src/auth/dto/register.dto.ts +11 -0
- package/templates/typeorm/src/auth/dto/reset-password.dto.ts +10 -0
- package/templates/typeorm/src/auth/entities/email-verification-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/oauth-account.entity.ts +50 -0
- package/templates/typeorm/src/auth/entities/password-reset-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/refresh-token.entity.ts +69 -0
- package/templates/typeorm/src/auth/entities/session.entity.ts +50 -0
- package/templates/typeorm/src/auth/guards/github-auth.guard.ts +10 -0
- package/templates/typeorm/src/auth/guards/google-auth.guard.ts +10 -0
- package/templates/typeorm/src/auth/guards/jwt-auth.guard.ts +25 -0
- package/templates/typeorm/src/auth/guards/session-auth.guard.spec.ts +77 -0
- package/templates/typeorm/src/auth/guards/session-auth.guard.ts +37 -0
- package/templates/typeorm/src/auth/session.service.spec.ts +74 -0
- package/templates/typeorm/src/auth/session.service.ts +96 -0
- package/templates/typeorm/src/auth/strategies/github.strategy.ts +41 -0
- package/templates/typeorm/src/auth/strategies/google.strategy.ts +43 -0
- package/templates/typeorm/src/auth/strategies/jwt.strategy.ts +25 -0
- package/templates/typeorm/src/auth/token.service.ts +115 -0
- package/templates/typeorm/src/common/constants/permissions.ts +8 -0
- package/templates/typeorm/src/common/constants/role-permissions.ts +9 -0
- package/templates/typeorm/src/common/constants/role.enum.ts +5 -0
- package/templates/typeorm/src/common/decorators/current-user.decorator.ts +8 -0
- package/templates/typeorm/src/common/decorators/permissions.decorator.ts +7 -0
- package/templates/typeorm/src/common/decorators/public.decorator.ts +4 -0
- package/templates/typeorm/src/common/decorators/roles.decorator.ts +6 -0
- package/templates/typeorm/src/common/filters/http-exception.filter.ts +34 -0
- package/templates/typeorm/src/common/guards/permissions.guard.spec.ts +54 -0
- package/templates/typeorm/src/common/guards/permissions.guard.ts +28 -0
- package/templates/typeorm/src/common/guards/roles.guard.spec.ts +37 -0
- package/templates/typeorm/src/common/guards/roles.guard.ts +24 -0
- package/templates/typeorm/src/common/interceptors/logging.interceptor.ts +23 -0
- package/templates/typeorm/src/common/middleware/csrf.middleware.spec.ts +97 -0
- package/templates/typeorm/src/common/middleware/csrf.middleware.ts +46 -0
- package/templates/typeorm/src/common/utils/avatar-storage.util.ts +32 -0
- package/templates/typeorm/src/common/utils/hash.util.ts +5 -0
- package/templates/typeorm/src/config/env.validation.ts +50 -0
- package/templates/typeorm/src/database/data-source.ts +20 -0
- package/templates/typeorm/src/database/database.module.ts +39 -0
- package/templates/typeorm/src/database/seed.ts +81 -0
- package/templates/typeorm/src/database/typeorm-options.ts +49 -0
- package/templates/typeorm/src/health/health.controller.ts +49 -0
- package/templates/typeorm/src/health/health.module.ts +19 -0
- package/templates/typeorm/src/health/indicators/redis-health.indicator.spec.ts +54 -0
- package/templates/typeorm/src/health/indicators/redis-health.indicator.ts +33 -0
- package/templates/typeorm/src/health/indicators/typeorm-health.indicator.spec.ts +49 -0
- package/templates/typeorm/src/health/indicators/typeorm-health.indicator.ts +37 -0
- package/templates/typeorm/src/mail/mail.module.ts +12 -0
- package/templates/typeorm/src/mail/mail.processor.ts +48 -0
- package/templates/typeorm/src/mail/mail.service.ts +24 -0
- package/templates/typeorm/src/mail/templates/email-templates.ts +28 -0
- package/templates/typeorm/src/main.ts +107 -0
- package/templates/typeorm/src/metrics/metrics.controller.ts +22 -0
- package/templates/typeorm/src/metrics/metrics.interceptor.ts +30 -0
- package/templates/typeorm/src/metrics/metrics.module.ts +12 -0
- package/templates/typeorm/src/metrics/metrics.service.ts +34 -0
- package/templates/typeorm/src/types/express-session.d.ts +13 -0
- package/templates/typeorm/src/users/dto/create-user.dto.ts +12 -0
- package/templates/typeorm/src/users/dto/find-users-query.dto.ts +12 -0
- package/templates/typeorm/src/users/dto/update-user.dto.ts +6 -0
- package/templates/typeorm/src/users/entities/user.entity.ts +113 -0
- package/templates/typeorm/src/users/users.controller.ts +165 -0
- package/templates/typeorm/src/users/users.module.ts +13 -0
- package/templates/typeorm/src/users/users.service.spec.ts +183 -0
- package/templates/typeorm/src/users/users.service.ts +124 -0
- package/templates/typeorm/test/auth.e2e-spec.ts +111 -0
- package/templates/typeorm/test/session-auth.e2e-spec.ts +159 -0
- package/templates/typeorm/test/users.e2e-spec.ts +183 -0
- package/templates/typeorm/test/utils/clean-database.ts +65 -0
- package/templates/typeorm/test/utils/e2e-setup.ts +77 -0
- package/templates/typeorm/tsconfig.build.json +4 -0
- package/templates/typeorm/tsconfig.json +25 -0
- package/templates/typeorm/vitest.config.ts +20 -0
- package/templates/typeorm/vitest.e2e.config.ts +19 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// nestforge:feature-file:redis
|
|
2
|
+
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
|
3
|
+
import { HealthCheckError } from '@nestjs/terminus';
|
|
4
|
+
|
|
5
|
+
const connectMock = vi.fn();
|
|
6
|
+
const pingMock = vi.fn();
|
|
7
|
+
const disconnectMock = vi.fn();
|
|
8
|
+
|
|
9
|
+
vi.mock('ioredis', () => ({
|
|
10
|
+
default: vi.fn().mockImplementation(() => ({
|
|
11
|
+
connect: connectMock,
|
|
12
|
+
ping: pingMock,
|
|
13
|
+
disconnect: disconnectMock,
|
|
14
|
+
})),
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
// precisa ser importado depois do vi.mock, senão pega o ioredis real
|
|
18
|
+
const { RedisHealthIndicator } = await import('./redis-health.indicator');
|
|
19
|
+
|
|
20
|
+
describe('RedisHealthIndicator', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
connectMock.mockReset();
|
|
23
|
+
pingMock.mockReset();
|
|
24
|
+
disconnectMock.mockReset();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('retorna status up quando o PING responde PONG', async () => {
|
|
28
|
+
connectMock.mockResolvedValue(undefined);
|
|
29
|
+
pingMock.mockResolvedValue('PONG');
|
|
30
|
+
|
|
31
|
+
const indicator = new RedisHealthIndicator();
|
|
32
|
+
const result = await indicator.isHealthy('redis');
|
|
33
|
+
|
|
34
|
+
expect(result).toEqual({ redis: { status: 'up' } });
|
|
35
|
+
expect(disconnectMock).toHaveBeenCalled();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('lança HealthCheckError quando o PING não responde PONG', async () => {
|
|
39
|
+
connectMock.mockResolvedValue(undefined);
|
|
40
|
+
pingMock.mockResolvedValue('ALGO_INESPERADO');
|
|
41
|
+
|
|
42
|
+
const indicator = new RedisHealthIndicator();
|
|
43
|
+
|
|
44
|
+
await expect(indicator.isHealthy('redis')).rejects.toBeInstanceOf(HealthCheckError);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('lança HealthCheckError quando a conexão falha', async () => {
|
|
48
|
+
connectMock.mockRejectedValue(new Error('ECONNREFUSED'));
|
|
49
|
+
|
|
50
|
+
const indicator = new RedisHealthIndicator();
|
|
51
|
+
|
|
52
|
+
await expect(indicator.isHealthy('redis')).rejects.toBeInstanceOf(HealthCheckError);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// nestforge:feature-file:redis
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { HealthIndicatorResult, HealthCheckError } from '@nestjs/terminus';
|
|
4
|
+
import Redis from 'ioredis';
|
|
5
|
+
|
|
6
|
+
@Injectable()
|
|
7
|
+
export class RedisHealthIndicator {
|
|
8
|
+
async isHealthy(key: string): Promise<HealthIndicatorResult> {
|
|
9
|
+
const client = new Redis({
|
|
10
|
+
host: process.env.REDIS_HOST ?? 'localhost',
|
|
11
|
+
port: Number(process.env.REDIS_PORT ?? 6379),
|
|
12
|
+
lazyConnect: true,
|
|
13
|
+
maxRetriesPerRequest: 1,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
await client.connect();
|
|
18
|
+
const response = await client.ping();
|
|
19
|
+
|
|
20
|
+
if (response !== 'PONG') {
|
|
21
|
+
throw new Error('Resposta inesperada do Redis');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return { [key]: { status: 'up' } };
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new HealthCheckError('Redis indisponível', {
|
|
27
|
+
[key]: { status: 'down', message: (error as Error).message },
|
|
28
|
+
});
|
|
29
|
+
} finally {
|
|
30
|
+
client.disconnect();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {
|
|
2
|
+
describe,
|
|
3
|
+
expect,
|
|
4
|
+
it,
|
|
5
|
+
vi,
|
|
6
|
+
} from 'vitest';
|
|
7
|
+
import { HealthCheckError } from '@nestjs/terminus';
|
|
8
|
+
import { DataSource } from 'typeorm';
|
|
9
|
+
import { TypeOrmHealthIndicator } from './typeorm-health.indicator';
|
|
10
|
+
|
|
11
|
+
describe('TypeOrmHealthIndicator', () => {
|
|
12
|
+
it('retorna status up quando a query executa com sucesso', async () => {
|
|
13
|
+
const dataSource = {
|
|
14
|
+
query: vi.fn().mockResolvedValue([{ result: 1 }]),
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const indicator = new TypeOrmHealthIndicator(
|
|
18
|
+
dataSource as unknown as DataSource,
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
const result = await indicator.isHealthy(
|
|
22
|
+
'database',
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
expect(result).toEqual({
|
|
26
|
+
database: {
|
|
27
|
+
status: 'up',
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('lança HealthCheckError quando a query falha', async () => {
|
|
33
|
+
const dataSource = {
|
|
34
|
+
query: vi
|
|
35
|
+
.fn()
|
|
36
|
+
.mockRejectedValue(
|
|
37
|
+
new Error('conexão recusada'),
|
|
38
|
+
),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const indicator = new TypeOrmHealthIndicator(
|
|
42
|
+
dataSource as unknown as DataSource,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
await expect(
|
|
46
|
+
indicator.isHealthy('database'),
|
|
47
|
+
).rejects.toBeInstanceOf(HealthCheckError);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import {
|
|
3
|
+
HealthCheckError,
|
|
4
|
+
HealthIndicatorResult,
|
|
5
|
+
} from '@nestjs/terminus';
|
|
6
|
+
import { DataSource } from 'typeorm';
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class TypeOrmHealthIndicator {
|
|
10
|
+
constructor(
|
|
11
|
+
private readonly dataSource: DataSource,
|
|
12
|
+
) { }
|
|
13
|
+
|
|
14
|
+
async isHealthy(
|
|
15
|
+
key: string,
|
|
16
|
+
): Promise<HealthIndicatorResult> {
|
|
17
|
+
try {
|
|
18
|
+
await this.dataSource.query('SELECT 1');
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
[key]: {
|
|
22
|
+
status: 'up',
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new HealthCheckError(
|
|
27
|
+
'Banco de dados indisponível',
|
|
28
|
+
{
|
|
29
|
+
[key]: {
|
|
30
|
+
status: 'down',
|
|
31
|
+
message: (error as Error).message,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// nestforge:feature-file:redis
|
|
2
|
+
import { Module } from '@nestjs/common';
|
|
3
|
+
import { BullModule } from '@nestjs/bullmq';
|
|
4
|
+
import { MailService, MAIL_QUEUE } from './mail.service';
|
|
5
|
+
import { MailProcessor } from './mail.processor';
|
|
6
|
+
|
|
7
|
+
@Module({
|
|
8
|
+
imports: [BullModule.registerQueue({ name: MAIL_QUEUE })],
|
|
9
|
+
providers: [MailService, MailProcessor],
|
|
10
|
+
exports: [MailService],
|
|
11
|
+
})
|
|
12
|
+
export class MailModule { }
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// nestforge:feature-file:redis
|
|
2
|
+
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
|
3
|
+
import { Logger } from '@nestjs/common';
|
|
4
|
+
import { Job } from 'bullmq';
|
|
5
|
+
import * as nodemailer from 'nodemailer';
|
|
6
|
+
import { MAIL_QUEUE, MailJob } from './mail.service';
|
|
7
|
+
import { resetPasswordTemplate, verifyEmailTemplate } from './templates/email-templates';
|
|
8
|
+
|
|
9
|
+
interface MailJobData {
|
|
10
|
+
to: string;
|
|
11
|
+
name: string;
|
|
12
|
+
token: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@Processor(MAIL_QUEUE)
|
|
16
|
+
export class MailProcessor extends WorkerHost {
|
|
17
|
+
private readonly logger = new Logger(MailProcessor.name);
|
|
18
|
+
|
|
19
|
+
private readonly transporter = nodemailer.createTransport({
|
|
20
|
+
host: process.env.MAIL_HOST,
|
|
21
|
+
port: Number(process.env.MAIL_PORT ?? 1025),
|
|
22
|
+
secure: false,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
async process(job: Job<MailJobData>) {
|
|
26
|
+
const { to, name, token } = job.data;
|
|
27
|
+
|
|
28
|
+
switch (job.name) {
|
|
29
|
+
case MailJob.VerifyEmail:
|
|
30
|
+
await this.send(to, 'Confirme seu e-mail', verifyEmailTemplate(name, token));
|
|
31
|
+
break;
|
|
32
|
+
case MailJob.ResetPassword:
|
|
33
|
+
await this.send(to, 'Redefinição de senha', resetPasswordTemplate(name, token));
|
|
34
|
+
break;
|
|
35
|
+
default:
|
|
36
|
+
this.logger.warn(`Job de e-mail desconhecido: ${job.name}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
private async send(to: string, subject: string, html: string) {
|
|
41
|
+
await this.transporter.sendMail({
|
|
42
|
+
from: process.env.MAIL_FROM,
|
|
43
|
+
to,
|
|
44
|
+
subject,
|
|
45
|
+
html,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// nestforge:feature-file:redis
|
|
2
|
+
import { Injectable } from '@nestjs/common';
|
|
3
|
+
import { InjectQueue } from '@nestjs/bullmq';
|
|
4
|
+
import { Queue } from 'bullmq';
|
|
5
|
+
|
|
6
|
+
export const MAIL_QUEUE = 'mail';
|
|
7
|
+
|
|
8
|
+
export enum MailJob {
|
|
9
|
+
VerifyEmail = 'verify-email',
|
|
10
|
+
ResetPassword = 'reset-password',
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
@Injectable()
|
|
14
|
+
export class MailService {
|
|
15
|
+
constructor(@InjectQueue(MAIL_QUEUE) private readonly mailQueue: Queue) { }
|
|
16
|
+
|
|
17
|
+
queueVerificationEmail(to: string, name: string, token: string) {
|
|
18
|
+
return this.mailQueue.add(MailJob.VerifyEmail, { to, name, token });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
queuePasswordResetEmail(to: string, name: string, token: string) {
|
|
22
|
+
return this.mailQueue.add(MailJob.ResetPassword, { to, name, token });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// nestforge:feature-file:redis
|
|
2
|
+
const appUrl = process.env.APP_URL ?? 'http://localhost:3000';
|
|
3
|
+
|
|
4
|
+
export function verifyEmailTemplate(name: string, token: string): string {
|
|
5
|
+
const link = `${appUrl}/auth/verify-email?token=${token}`;
|
|
6
|
+
|
|
7
|
+
return `
|
|
8
|
+
<div style="font-family: sans-serif; line-height: 1.5;">
|
|
9
|
+
<h2>Olá, ${name}!</h2>
|
|
10
|
+
<p>Confirme seu e-mail clicando no link abaixo:</p>
|
|
11
|
+
<p><a href="${link}">${link}</a></p>
|
|
12
|
+
<p>Esse link expira em 24 horas.</p>
|
|
13
|
+
</div>
|
|
14
|
+
`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function resetPasswordTemplate(name: string, token: string): string {
|
|
18
|
+
const link = `${appUrl}/auth/reset-password?token=${token}`;
|
|
19
|
+
|
|
20
|
+
return `
|
|
21
|
+
<div style="font-family: sans-serif; line-height: 1.5;">
|
|
22
|
+
<h2>Olá, ${name}!</h2>
|
|
23
|
+
<p>Recebemos um pedido para redefinir sua senha. Clique no link abaixo para continuar:</p>
|
|
24
|
+
<p><a href="${link}">${link}</a></p>
|
|
25
|
+
<p>Se você não pediu isso, pode ignorar este e-mail. O link expira em 1 hora.</p>
|
|
26
|
+
</div>
|
|
27
|
+
`;
|
|
28
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { NestFactory, Reflector } from '@nestjs/core';
|
|
2
|
+
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
import { Logger } from 'nestjs-pino';
|
|
5
|
+
import helmet from 'helmet';
|
|
6
|
+
import { ClassSerializerInterceptor } from '@nestjs/common';
|
|
7
|
+
|
|
8
|
+
// nestforge:feature:validation
|
|
9
|
+
import { ZodValidationPipe } from 'nestjs-zod';
|
|
10
|
+
// nestforge:feature:validation:end
|
|
11
|
+
|
|
12
|
+
// nestforge:feature:swagger
|
|
13
|
+
import { patchNestJsSwagger } from 'nestjs-zod';
|
|
14
|
+
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
|
15
|
+
// nestforge:feature:swagger:end
|
|
16
|
+
|
|
17
|
+
import { AppModule } from './app.module';
|
|
18
|
+
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
|
|
19
|
+
|
|
20
|
+
// nestforge:feature:auth:session
|
|
21
|
+
import session from 'express-session';
|
|
22
|
+
import { TypeormStore } from 'connect-typeorm';
|
|
23
|
+
import { DataSource } from 'typeorm';
|
|
24
|
+
import { SessionEntity } from './auth/entities/session.entity';
|
|
25
|
+
// nestforge:feature:auth:session:end
|
|
26
|
+
|
|
27
|
+
async function bootstrap() {
|
|
28
|
+
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
|
|
29
|
+
bufferLogs: true,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
app.useStaticAssets(join(process.cwd(), 'uploads'), { prefix: '/uploads' });
|
|
33
|
+
|
|
34
|
+
app.useLogger(app.get(Logger));
|
|
35
|
+
app.use(helmet());
|
|
36
|
+
|
|
37
|
+
// nestforge:feature:auth:session
|
|
38
|
+
const dataSource = app.get(DataSource);
|
|
39
|
+
const sessionsRepository =
|
|
40
|
+
dataSource.getRepository(SessionEntity);
|
|
41
|
+
|
|
42
|
+
const sessionMaxAge = Number(
|
|
43
|
+
process.env.SESSION_MAX_AGE ??
|
|
44
|
+
7 * 24 * 60 * 60 * 1000,
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
if (process.env.NODE_ENV === 'production') {
|
|
48
|
+
app.set('trust proxy', 1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
app.use(
|
|
52
|
+
session({
|
|
53
|
+
name: 'nestforge.sid',
|
|
54
|
+
secret: process.env.SESSION_SECRET as string,
|
|
55
|
+
resave: false,
|
|
56
|
+
saveUninitialized: false,
|
|
57
|
+
store: new TypeormStore({
|
|
58
|
+
cleanupLimit: 2,
|
|
59
|
+
limitSubquery: false,
|
|
60
|
+
ttl: Math.floor(sessionMaxAge / 1000),
|
|
61
|
+
}).connect(sessionsRepository),
|
|
62
|
+
cookie: {
|
|
63
|
+
httpOnly: true,
|
|
64
|
+
secure:
|
|
65
|
+
process.env.NODE_ENV === 'production',
|
|
66
|
+
sameSite: 'lax',
|
|
67
|
+
maxAge: sessionMaxAge,
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
// nestforge:feature:auth:session:end
|
|
72
|
+
|
|
73
|
+
const corsOrigins = (
|
|
74
|
+
process.env.CORS_ORIGINS ?? 'http://localhost:5173'
|
|
75
|
+
)
|
|
76
|
+
.split(',')
|
|
77
|
+
.map((origin) => origin.trim())
|
|
78
|
+
.filter(Boolean);
|
|
79
|
+
|
|
80
|
+
app.enableCors({
|
|
81
|
+
origin: corsOrigins,
|
|
82
|
+
credentials: true,
|
|
83
|
+
});
|
|
84
|
+
// nestforge:feature:validation
|
|
85
|
+
app.useGlobalPipes(new ZodValidationPipe());
|
|
86
|
+
// nestforge:feature:validation:end
|
|
87
|
+
app.useGlobalFilters(new HttpExceptionFilter());
|
|
88
|
+
app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
|
|
89
|
+
|
|
90
|
+
// nestforge:feature:swagger
|
|
91
|
+
patchNestJsSwagger();
|
|
92
|
+
|
|
93
|
+
const config = new DocumentBuilder()
|
|
94
|
+
.setTitle('NestForge API')
|
|
95
|
+
.setDescription('Production-ready NestJS starter API docs')
|
|
96
|
+
.setVersion('0.1.0')
|
|
97
|
+
.addBearerAuth()
|
|
98
|
+
.build();
|
|
99
|
+
const document = SwaggerModule.createDocument(app, config);
|
|
100
|
+
SwaggerModule.setup('docs', app, document);
|
|
101
|
+
// nestforge:feature:swagger:end
|
|
102
|
+
|
|
103
|
+
const port = process.env.PORT ?? 3000;
|
|
104
|
+
await app.listen(port);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
bootstrap();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Controller, Get, Res } from '@nestjs/common';
|
|
2
|
+
import { Response } from 'express';
|
|
3
|
+
// nestforge:feature:swagger
|
|
4
|
+
import { ApiExcludeController } from '@nestjs/swagger';
|
|
5
|
+
// nestforge:feature:swagger:end
|
|
6
|
+
import { Public } from '../common/decorators/public.decorator';
|
|
7
|
+
import { MetricsService } from './metrics.service';
|
|
8
|
+
|
|
9
|
+
// nestforge:feature:swagger
|
|
10
|
+
@ApiExcludeController()
|
|
11
|
+
// nestforge:feature:swagger:end
|
|
12
|
+
@Controller('metrics')
|
|
13
|
+
export class MetricsController {
|
|
14
|
+
constructor(private readonly metricsService: MetricsService) { }
|
|
15
|
+
|
|
16
|
+
@Public()
|
|
17
|
+
@Get()
|
|
18
|
+
async getMetrics(@Res() res: Response) {
|
|
19
|
+
res.set('Content-Type', this.metricsService.getContentType());
|
|
20
|
+
res.send(await this.metricsService.getMetrics());
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
|
|
2
|
+
import { Observable, tap } from 'rxjs';
|
|
3
|
+
import { MetricsService } from './metrics.service';
|
|
4
|
+
|
|
5
|
+
@Injectable()
|
|
6
|
+
export class MetricsInterceptor implements NestInterceptor {
|
|
7
|
+
constructor(private readonly metricsService: MetricsService) { }
|
|
8
|
+
|
|
9
|
+
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
|
|
10
|
+
const request = context.switchToHttp().getRequest();
|
|
11
|
+
const response = context.switchToHttp().getResponse();
|
|
12
|
+
const start = process.hrtime();
|
|
13
|
+
const route = request.route?.path ?? request.url;
|
|
14
|
+
|
|
15
|
+
return next.handle().pipe(
|
|
16
|
+
tap(() => {
|
|
17
|
+
const [seconds, nanoseconds] = process.hrtime(start);
|
|
18
|
+
const duration = seconds + nanoseconds / 1e9;
|
|
19
|
+
const labels = {
|
|
20
|
+
method: request.method,
|
|
21
|
+
route,
|
|
22
|
+
status_code: String(response.statusCode),
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
this.metricsService.httpRequestDuration.observe(labels, duration);
|
|
26
|
+
this.metricsService.httpRequestsTotal.inc(labels);
|
|
27
|
+
}),
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { APP_INTERCEPTOR } from '@nestjs/core';
|
|
3
|
+
import { MetricsService } from './metrics.service';
|
|
4
|
+
import { MetricsController } from './metrics.controller';
|
|
5
|
+
import { MetricsInterceptor } from './metrics.interceptor';
|
|
6
|
+
|
|
7
|
+
@Module({
|
|
8
|
+
controllers: [MetricsController],
|
|
9
|
+
providers: [MetricsService, { provide: APP_INTERCEPTOR, useClass: MetricsInterceptor }],
|
|
10
|
+
exports: [MetricsService],
|
|
11
|
+
})
|
|
12
|
+
export class MetricsModule { }
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import * as client from 'prom-client';
|
|
3
|
+
|
|
4
|
+
@Injectable()
|
|
5
|
+
export class MetricsService {
|
|
6
|
+
readonly registry = new client.Registry();
|
|
7
|
+
|
|
8
|
+
readonly httpRequestDuration = new client.Histogram({
|
|
9
|
+
name: 'http_request_duration_seconds',
|
|
10
|
+
help: 'Duração das requisições HTTP em segundos',
|
|
11
|
+
labelNames: ['method', 'route', 'status_code'],
|
|
12
|
+
buckets: [0.05, 0.1, 0.3, 0.5, 1, 2, 5],
|
|
13
|
+
registers: [this.registry],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
readonly httpRequestsTotal = new client.Counter({
|
|
17
|
+
name: 'http_requests_total',
|
|
18
|
+
help: 'Total de requisições HTTP recebidas',
|
|
19
|
+
labelNames: ['method', 'route', 'status_code'],
|
|
20
|
+
registers: [this.registry],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
constructor() {
|
|
24
|
+
client.collectDefaultMetrics({ register: this.registry });
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async getMetrics(): Promise<string> {
|
|
28
|
+
return this.registry.metrics();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
getContentType(): string {
|
|
32
|
+
return this.registry.contentType;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createZodDto } from 'nestjs-zod';
|
|
3
|
+
import { Role } from '../../common/constants/role.enum';
|
|
4
|
+
|
|
5
|
+
export const createUserSchema = z.object({
|
|
6
|
+
name: z.string().min(2).describe('Nome completo'),
|
|
7
|
+
email: z.string().email().describe('E-mail do usuário'),
|
|
8
|
+
password: z.string().min(8).describe('Senha (mínimo 8 caracteres)'),
|
|
9
|
+
role: z.nativeEnum(Role).optional().describe('Role do usuário (padrão: USER)'),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export class CreateUserDto extends createZodDto(createUserSchema) { }
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { createZodDto } from 'nestjs-zod';
|
|
3
|
+
import { Role } from '../../common/constants/role.enum';
|
|
4
|
+
|
|
5
|
+
export const findUsersQuerySchema = z.object({
|
|
6
|
+
page: z.coerce.number().int().min(1).default(1).describe('Página atual'),
|
|
7
|
+
limit: z.coerce.number().int().min(1).max(100).default(10).describe('Itens por página (máx. 100)'),
|
|
8
|
+
search: z.string().optional().describe('Busca por nome ou e-mail'),
|
|
9
|
+
role: z.nativeEnum(Role).optional().describe('Filtra por role'),
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export class FindUsersQueryDto extends createZodDto(findUsersQuerySchema) { }
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Exclude } from 'class-transformer';
|
|
2
|
+
import {
|
|
3
|
+
Column,
|
|
4
|
+
CreateDateColumn,
|
|
5
|
+
Entity,
|
|
6
|
+
PrimaryGeneratedColumn,
|
|
7
|
+
UpdateDateColumn,
|
|
8
|
+
OneToMany,
|
|
9
|
+
} from 'typeorm';
|
|
10
|
+
import { Role } from '../../common/constants/role.enum';
|
|
11
|
+
// nestforge:feature:auth:token
|
|
12
|
+
import { RefreshTokenEntity } from '../../auth/entities/refresh-token.entity';
|
|
13
|
+
// nestforge:feature:auth:token:end
|
|
14
|
+
import { OAuthAccountEntity } from '../../auth/entities/oauth-account.entity';
|
|
15
|
+
// nestforge:feature:redis,auth:password
|
|
16
|
+
import { PasswordResetTokenEntity } from '../../auth/entities/password-reset-token.entity';
|
|
17
|
+
import { EmailVerificationTokenEntity } from '../../auth/entities/email-verification-token.entity';
|
|
18
|
+
// nestforge:feature:redis,auth:password:end
|
|
19
|
+
|
|
20
|
+
@Entity({ name: 'users' })
|
|
21
|
+
export class UserEntity {
|
|
22
|
+
@PrimaryGeneratedColumn('uuid')
|
|
23
|
+
id!: string;
|
|
24
|
+
|
|
25
|
+
@Column({ type: 'varchar', length: 120 })
|
|
26
|
+
name!: string;
|
|
27
|
+
|
|
28
|
+
@Column({
|
|
29
|
+
type: 'varchar',
|
|
30
|
+
length: 255,
|
|
31
|
+
unique: true,
|
|
32
|
+
})
|
|
33
|
+
email!: string;
|
|
34
|
+
|
|
35
|
+
@Exclude()
|
|
36
|
+
@Column({
|
|
37
|
+
name: 'password_hash',
|
|
38
|
+
type: 'varchar',
|
|
39
|
+
length: 255,
|
|
40
|
+
nullable: true,
|
|
41
|
+
})
|
|
42
|
+
passwordHash!: string | null;
|
|
43
|
+
|
|
44
|
+
@Column({
|
|
45
|
+
type: 'varchar',
|
|
46
|
+
length: 20,
|
|
47
|
+
default: Role.USER,
|
|
48
|
+
})
|
|
49
|
+
role!: Role;
|
|
50
|
+
|
|
51
|
+
@Column({
|
|
52
|
+
name: 'avatar_url',
|
|
53
|
+
type: 'varchar',
|
|
54
|
+
length: 500,
|
|
55
|
+
nullable: true,
|
|
56
|
+
})
|
|
57
|
+
avatarUrl!: string | null;
|
|
58
|
+
|
|
59
|
+
@Column({
|
|
60
|
+
name: 'email_verified_at',
|
|
61
|
+
nullable: true,
|
|
62
|
+
// nestforge:feature:database:postgres
|
|
63
|
+
type: 'timestamp with time zone',
|
|
64
|
+
// nestforge:feature:database:postgres:end
|
|
65
|
+
// nestforge:feature:database:mysql
|
|
66
|
+
type: 'datetime',
|
|
67
|
+
// nestforge:feature:database:mysql:end
|
|
68
|
+
// nestforge:feature:database:sqlite
|
|
69
|
+
type: 'datetime',
|
|
70
|
+
// nestforge:feature:database:sqlite:end
|
|
71
|
+
})
|
|
72
|
+
emailVerifiedAt!: Date | null;
|
|
73
|
+
|
|
74
|
+
@CreateDateColumn({ name: 'created_at' })
|
|
75
|
+
createdAt!: Date;
|
|
76
|
+
|
|
77
|
+
@UpdateDateColumn({ name: 'updated_at' })
|
|
78
|
+
updatedAt!: Date;
|
|
79
|
+
|
|
80
|
+
// nestforge:feature:auth:token
|
|
81
|
+
@OneToMany(
|
|
82
|
+
() => RefreshTokenEntity,
|
|
83
|
+
(refreshToken) => refreshToken.user,
|
|
84
|
+
)
|
|
85
|
+
refreshTokens!: RefreshTokenEntity[];
|
|
86
|
+
// nestforge:feature:auth:token:end
|
|
87
|
+
|
|
88
|
+
@OneToMany(
|
|
89
|
+
() => OAuthAccountEntity,
|
|
90
|
+
(oauthAccount) => oauthAccount.user,
|
|
91
|
+
)
|
|
92
|
+
oauthAccounts!: OAuthAccountEntity[];
|
|
93
|
+
|
|
94
|
+
// nestforge:feature:redis,auth:password
|
|
95
|
+
@OneToMany(
|
|
96
|
+
() => PasswordResetTokenEntity,
|
|
97
|
+
(token) => token.user,
|
|
98
|
+
)
|
|
99
|
+
passwordResetTokens!: PasswordResetTokenEntity[];
|
|
100
|
+
|
|
101
|
+
@OneToMany(
|
|
102
|
+
() => EmailVerificationTokenEntity,
|
|
103
|
+
(token) => token.user,
|
|
104
|
+
)
|
|
105
|
+
emailVerificationTokens!: EmailVerificationTokenEntity[];
|
|
106
|
+
// nestforge:feature:redis,auth:password:end
|
|
107
|
+
|
|
108
|
+
constructor(partial?: Partial<UserEntity>) {
|
|
109
|
+
if (partial) {
|
|
110
|
+
Object.assign(this, partial);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|