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,190 @@
|
|
|
1
|
+
// nestforge:feature-file:database:sqlite
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { sql } from 'drizzle-orm';
|
|
4
|
+
import {
|
|
5
|
+
index,
|
|
6
|
+
integer,
|
|
7
|
+
sqliteTable,
|
|
8
|
+
text,
|
|
9
|
+
uniqueIndex,
|
|
10
|
+
} from 'drizzle-orm/sqlite-core';
|
|
11
|
+
|
|
12
|
+
export const users = sqliteTable('users', {
|
|
13
|
+
id: text('id')
|
|
14
|
+
.$defaultFn(() => randomUUID())
|
|
15
|
+
.primaryKey(),
|
|
16
|
+
name: text('name').notNull(),
|
|
17
|
+
email: text('email').notNull().unique(),
|
|
18
|
+
passwordHash: text('password_hash'),
|
|
19
|
+
role: text('role', {
|
|
20
|
+
enum: ['ADMIN', 'MANAGER', 'USER'],
|
|
21
|
+
})
|
|
22
|
+
.default('USER')
|
|
23
|
+
.notNull(),
|
|
24
|
+
avatarUrl: text('avatar_url'),
|
|
25
|
+
emailVerifiedAt: integer('email_verified_at', {
|
|
26
|
+
mode: 'timestamp_ms',
|
|
27
|
+
}),
|
|
28
|
+
createdAt: integer('created_at', {
|
|
29
|
+
mode: 'timestamp_ms',
|
|
30
|
+
})
|
|
31
|
+
.default(sql`(unixepoch() * 1000)`)
|
|
32
|
+
.notNull(),
|
|
33
|
+
updatedAt: integer('updated_at', {
|
|
34
|
+
mode: 'timestamp_ms',
|
|
35
|
+
})
|
|
36
|
+
.default(sql`(unixepoch() * 1000)`)
|
|
37
|
+
.$onUpdate(() => new Date())
|
|
38
|
+
.notNull(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export const oauthAccounts = sqliteTable(
|
|
42
|
+
'oauth_accounts',
|
|
43
|
+
{
|
|
44
|
+
id: text('id')
|
|
45
|
+
.$defaultFn(() => randomUUID())
|
|
46
|
+
.primaryKey(),
|
|
47
|
+
provider: text('provider').notNull(),
|
|
48
|
+
providerUserId: text(
|
|
49
|
+
'provider_user_id',
|
|
50
|
+
).notNull(),
|
|
51
|
+
userId: text('user_id')
|
|
52
|
+
.notNull()
|
|
53
|
+
.references(() => users.id, {
|
|
54
|
+
onDelete: 'cascade',
|
|
55
|
+
}),
|
|
56
|
+
createdAt: integer('created_at', {
|
|
57
|
+
mode: 'timestamp_ms',
|
|
58
|
+
})
|
|
59
|
+
.default(sql`(unixepoch() * 1000)`)
|
|
60
|
+
.notNull(),
|
|
61
|
+
},
|
|
62
|
+
(table) => [
|
|
63
|
+
uniqueIndex(
|
|
64
|
+
'oauth_accounts_provider_provider_user_id_key',
|
|
65
|
+
).on(table.provider, table.providerUserId),
|
|
66
|
+
index('oauth_accounts_user_id_idx').on(table.userId),
|
|
67
|
+
],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
// nestforge:feature:auth:token
|
|
71
|
+
export const refreshTokens = sqliteTable(
|
|
72
|
+
'refresh_tokens',
|
|
73
|
+
{
|
|
74
|
+
id: text('id')
|
|
75
|
+
.$defaultFn(() => randomUUID())
|
|
76
|
+
.primaryKey(),
|
|
77
|
+
tokenHash: text('token_hash').notNull().unique(),
|
|
78
|
+
userId: text('user_id')
|
|
79
|
+
.notNull()
|
|
80
|
+
.references(() => users.id, {
|
|
81
|
+
onDelete: 'cascade',
|
|
82
|
+
}),
|
|
83
|
+
expiresAt: integer('expires_at', {
|
|
84
|
+
mode: 'timestamp_ms',
|
|
85
|
+
}).notNull(),
|
|
86
|
+
revokedAt: integer('revoked_at', {
|
|
87
|
+
mode: 'timestamp_ms',
|
|
88
|
+
}),
|
|
89
|
+
createdAt: integer('created_at', {
|
|
90
|
+
mode: 'timestamp_ms',
|
|
91
|
+
})
|
|
92
|
+
.default(sql`(unixepoch() * 1000)`)
|
|
93
|
+
.notNull(),
|
|
94
|
+
},
|
|
95
|
+
(table) => [
|
|
96
|
+
index('refresh_tokens_user_id_idx').on(table.userId),
|
|
97
|
+
],
|
|
98
|
+
);
|
|
99
|
+
// nestforge:feature:auth:token:end
|
|
100
|
+
|
|
101
|
+
// nestforge:feature:redis,auth:password
|
|
102
|
+
export const passwordResetTokens = sqliteTable(
|
|
103
|
+
'password_reset_tokens',
|
|
104
|
+
{
|
|
105
|
+
id: text('id')
|
|
106
|
+
.$defaultFn(() => randomUUID())
|
|
107
|
+
.primaryKey(),
|
|
108
|
+
tokenHash: text('token_hash').notNull().unique(),
|
|
109
|
+
userId: text('user_id')
|
|
110
|
+
.notNull()
|
|
111
|
+
.references(() => users.id, {
|
|
112
|
+
onDelete: 'cascade',
|
|
113
|
+
}),
|
|
114
|
+
expiresAt: integer('expires_at', {
|
|
115
|
+
mode: 'timestamp_ms',
|
|
116
|
+
}).notNull(),
|
|
117
|
+
usedAt: integer('used_at', {
|
|
118
|
+
mode: 'timestamp_ms',
|
|
119
|
+
}),
|
|
120
|
+
createdAt: integer('created_at', {
|
|
121
|
+
mode: 'timestamp_ms',
|
|
122
|
+
})
|
|
123
|
+
.default(sql`(unixepoch() * 1000)`)
|
|
124
|
+
.notNull(),
|
|
125
|
+
},
|
|
126
|
+
(table) => [
|
|
127
|
+
index('password_reset_tokens_user_id_idx').on(
|
|
128
|
+
table.userId,
|
|
129
|
+
),
|
|
130
|
+
],
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
export const emailVerificationTokens = sqliteTable(
|
|
134
|
+
'email_verification_tokens',
|
|
135
|
+
{
|
|
136
|
+
id: text('id')
|
|
137
|
+
.$defaultFn(() => randomUUID())
|
|
138
|
+
.primaryKey(),
|
|
139
|
+
tokenHash: text('token_hash').notNull().unique(),
|
|
140
|
+
userId: text('user_id')
|
|
141
|
+
.notNull()
|
|
142
|
+
.references(() => users.id, {
|
|
143
|
+
onDelete: 'cascade',
|
|
144
|
+
}),
|
|
145
|
+
expiresAt: integer('expires_at', {
|
|
146
|
+
mode: 'timestamp_ms',
|
|
147
|
+
}).notNull(),
|
|
148
|
+
usedAt: integer('used_at', {
|
|
149
|
+
mode: 'timestamp_ms',
|
|
150
|
+
}),
|
|
151
|
+
createdAt: integer('created_at', {
|
|
152
|
+
mode: 'timestamp_ms',
|
|
153
|
+
})
|
|
154
|
+
.default(sql`(unixepoch() * 1000)`)
|
|
155
|
+
.notNull(),
|
|
156
|
+
},
|
|
157
|
+
(table) => [
|
|
158
|
+
index('email_verification_tokens_user_id_idx').on(
|
|
159
|
+
table.userId,
|
|
160
|
+
),
|
|
161
|
+
],
|
|
162
|
+
);
|
|
163
|
+
// nestforge:feature:redis,auth:password:end
|
|
164
|
+
|
|
165
|
+
// nestforge:feature:auth:session
|
|
166
|
+
export const sessions = sqliteTable(
|
|
167
|
+
'sessions',
|
|
168
|
+
{
|
|
169
|
+
id: text('id').primaryKey(),
|
|
170
|
+
expiredAt: integer('expired_at').notNull(),
|
|
171
|
+
json: text('json').notNull(),
|
|
172
|
+
destroyedAt: integer('destroyed_at', {
|
|
173
|
+
mode: 'timestamp_ms',
|
|
174
|
+
}),
|
|
175
|
+
},
|
|
176
|
+
(table) => [
|
|
177
|
+
index('sessions_expired_at_idx').on(
|
|
178
|
+
table.expiredAt,
|
|
179
|
+
),
|
|
180
|
+
],
|
|
181
|
+
);
|
|
182
|
+
// nestforge:feature:auth:session:end
|
|
183
|
+
|
|
184
|
+
export type User = typeof users.$inferSelect;
|
|
185
|
+
export type NewUser = typeof users.$inferInsert;
|
|
186
|
+
|
|
187
|
+
export type OAuthAccount =
|
|
188
|
+
typeof oauthAccounts.$inferSelect;
|
|
189
|
+
export type NewOAuthAccount =
|
|
190
|
+
typeof oauthAccounts.$inferInsert;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// nestforge:feature-file:auth:password
|
|
2
|
+
import {
|
|
3
|
+
randomUUID,
|
|
4
|
+
} from 'node:crypto';
|
|
5
|
+
import * as bcrypt from 'bcryptjs';
|
|
6
|
+
import { eq } from 'drizzle-orm';
|
|
7
|
+
import { Role } from '../common/constants/role.enum';
|
|
8
|
+
import {
|
|
9
|
+
users,
|
|
10
|
+
} from './schema';
|
|
11
|
+
import * as schema from './schema';
|
|
12
|
+
|
|
13
|
+
// nestforge:feature:database:postgres
|
|
14
|
+
import { Pool } from 'pg';
|
|
15
|
+
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
16
|
+
// nestforge:feature:database:postgres:end
|
|
17
|
+
|
|
18
|
+
// nestforge:feature:database:mysql
|
|
19
|
+
import { createPool } from 'mysql2/promise';
|
|
20
|
+
import { drizzle } from 'drizzle-orm/mysql2';
|
|
21
|
+
// nestforge:feature:database:mysql:end
|
|
22
|
+
|
|
23
|
+
// nestforge:feature:database:sqlite
|
|
24
|
+
import BetterSqlite3 from 'better-sqlite3';
|
|
25
|
+
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
|
26
|
+
// nestforge:feature:database:sqlite:end
|
|
27
|
+
|
|
28
|
+
interface SeedUser {
|
|
29
|
+
name: string;
|
|
30
|
+
email: string;
|
|
31
|
+
password: string;
|
|
32
|
+
role: Role;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const SEED_USERS: SeedUser[] = [
|
|
36
|
+
{
|
|
37
|
+
name: 'Admin',
|
|
38
|
+
email: 'admin@nestforge.dev',
|
|
39
|
+
password: 'admin123',
|
|
40
|
+
role: Role.ADMIN,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'Manager',
|
|
44
|
+
email: 'manager@nestforge.dev',
|
|
45
|
+
password: 'manager123',
|
|
46
|
+
role: Role.MANAGER,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'Usuário',
|
|
50
|
+
email: 'user@nestforge.dev',
|
|
51
|
+
password: 'user1234',
|
|
52
|
+
role: Role.USER,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const databaseUrl = process.env.DATABASE_URL;
|
|
57
|
+
|
|
58
|
+
if (!databaseUrl) {
|
|
59
|
+
throw new Error(
|
|
60
|
+
'DATABASE_URL não foi definida',
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// nestforge:feature:database:postgres
|
|
65
|
+
const client = new Pool({
|
|
66
|
+
connectionString: databaseUrl,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const database = drizzle(client, {
|
|
70
|
+
schema,
|
|
71
|
+
});
|
|
72
|
+
// nestforge:feature:database:postgres:end
|
|
73
|
+
|
|
74
|
+
// nestforge:feature:database:mysql
|
|
75
|
+
const client = createPool(databaseUrl);
|
|
76
|
+
|
|
77
|
+
const database = drizzle(client, {
|
|
78
|
+
schema,
|
|
79
|
+
mode: 'default',
|
|
80
|
+
});
|
|
81
|
+
// nestforge:feature:database:mysql:end
|
|
82
|
+
|
|
83
|
+
// nestforge:feature:database:sqlite
|
|
84
|
+
const databasePath = databaseUrl.replace(
|
|
85
|
+
/^file:/,
|
|
86
|
+
'',
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const client = new BetterSqlite3(
|
|
90
|
+
databasePath,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const database = drizzle(client, {
|
|
94
|
+
schema,
|
|
95
|
+
});
|
|
96
|
+
// nestforge:feature:database:sqlite:end
|
|
97
|
+
|
|
98
|
+
async function seed(): Promise<void> {
|
|
99
|
+
try {
|
|
100
|
+
for (const seedUser of SEED_USERS) {
|
|
101
|
+
const [existingUser] = await database
|
|
102
|
+
.select({
|
|
103
|
+
id: users.id,
|
|
104
|
+
})
|
|
105
|
+
.from(users)
|
|
106
|
+
.where(eq(users.email, seedUser.email))
|
|
107
|
+
.limit(1);
|
|
108
|
+
|
|
109
|
+
if (!existingUser) {
|
|
110
|
+
const passwordHash =
|
|
111
|
+
await bcrypt.hash(
|
|
112
|
+
seedUser.password,
|
|
113
|
+
10,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
await database.insert(users).values({
|
|
117
|
+
id: randomUUID(),
|
|
118
|
+
name: seedUser.name,
|
|
119
|
+
email: seedUser.email,
|
|
120
|
+
passwordHash,
|
|
121
|
+
role: seedUser.role,
|
|
122
|
+
emailVerifiedAt: new Date(),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
console.log(
|
|
127
|
+
`Seed: ${seedUser.email} / ${seedUser.password} (${seedUser.role})`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
} finally {
|
|
131
|
+
// nestforge:feature:database:postgres
|
|
132
|
+
await client.end();
|
|
133
|
+
// nestforge:feature:database:postgres:end
|
|
134
|
+
|
|
135
|
+
// nestforge:feature:database:mysql
|
|
136
|
+
await client.end();
|
|
137
|
+
// nestforge:feature:database:mysql:end
|
|
138
|
+
|
|
139
|
+
// nestforge:feature:database:sqlite
|
|
140
|
+
client.close();
|
|
141
|
+
// nestforge:feature:database:sqlite:end
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
seed().catch((error: unknown) => {
|
|
146
|
+
console.error(
|
|
147
|
+
'Erro ao executar o seed:',
|
|
148
|
+
error,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
process.exitCode = 1;
|
|
152
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Controller, Get } from '@nestjs/common';
|
|
2
|
+
import {
|
|
3
|
+
DiskHealthIndicator,
|
|
4
|
+
HealthCheck,
|
|
5
|
+
HealthCheckService,
|
|
6
|
+
MemoryHealthIndicator,
|
|
7
|
+
} from '@nestjs/terminus';
|
|
8
|
+
// nestforge:feature:swagger
|
|
9
|
+
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
10
|
+
// nestforge:feature:swagger:end
|
|
11
|
+
import { Public } from '../common/decorators/public.decorator';
|
|
12
|
+
import { DrizzleHealthIndicator } from './indicators/drizzle-health.indicator';
|
|
13
|
+
// nestforge:feature:redis
|
|
14
|
+
import { RedisHealthIndicator } from './indicators/redis-health.indicator';
|
|
15
|
+
// nestforge:feature:redis:end
|
|
16
|
+
|
|
17
|
+
// nestforge:feature:swagger
|
|
18
|
+
@ApiTags('health')
|
|
19
|
+
// nestforge:feature:swagger:end
|
|
20
|
+
@Controller('health')
|
|
21
|
+
export class HealthController {
|
|
22
|
+
constructor(
|
|
23
|
+
private readonly health: HealthCheckService,
|
|
24
|
+
private readonly databaseIndicator: DrizzleHealthIndicator,
|
|
25
|
+
// nestforge:feature:redis
|
|
26
|
+
private readonly redisIndicator: RedisHealthIndicator,
|
|
27
|
+
// nestforge:feature:redis:end
|
|
28
|
+
private readonly memory: MemoryHealthIndicator,
|
|
29
|
+
private readonly disk: DiskHealthIndicator,
|
|
30
|
+
) { }
|
|
31
|
+
|
|
32
|
+
@Public()
|
|
33
|
+
@Get()
|
|
34
|
+
@HealthCheck()
|
|
35
|
+
// nestforge:feature:swagger
|
|
36
|
+
@ApiOperation({ summary: 'Verifica a saúde da API (banco, memória, disco e demais dependências configuradas)' })
|
|
37
|
+
// nestforge:feature:swagger:end
|
|
38
|
+
check() {
|
|
39
|
+
return this.health.check([
|
|
40
|
+
() => this.databaseIndicator.isHealthy('database'),
|
|
41
|
+
// nestforge:feature:redis
|
|
42
|
+
() => this.redisIndicator.isHealthy('redis'),
|
|
43
|
+
// nestforge:feature:redis:end
|
|
44
|
+
() => this.memory.checkHeap('memory_heap', 300 * 1024 * 1024),
|
|
45
|
+
() => this.memory.checkRSS('memory_rss', 300 * 1024 * 1024),
|
|
46
|
+
() => this.disk.checkStorage('disk', { path: '/', thresholdPercent: 0.9 }),
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { TerminusModule } from '@nestjs/terminus';
|
|
3
|
+
import { HealthController } from './health.controller';
|
|
4
|
+
import { DrizzleHealthIndicator } from './indicators/drizzle-health.indicator';
|
|
5
|
+
// nestforge:feature:redis
|
|
6
|
+
import { RedisHealthIndicator } from './indicators/redis-health.indicator';
|
|
7
|
+
// nestforge:feature:redis:end
|
|
8
|
+
|
|
9
|
+
@Module({
|
|
10
|
+
imports: [TerminusModule],
|
|
11
|
+
controllers: [HealthController],
|
|
12
|
+
providers: [
|
|
13
|
+
DrizzleHealthIndicator,
|
|
14
|
+
// nestforge:feature:redis
|
|
15
|
+
RedisHealthIndicator,
|
|
16
|
+
// nestforge:feature:redis:end
|
|
17
|
+
],
|
|
18
|
+
})
|
|
19
|
+
export class HealthModule { }
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
describe,
|
|
3
|
+
expect,
|
|
4
|
+
it,
|
|
5
|
+
vi,
|
|
6
|
+
} from 'vitest';
|
|
7
|
+
import { HealthCheckError } from '@nestjs/terminus';
|
|
8
|
+
import type { DatabaseClient } from '../../database/database.types';
|
|
9
|
+
import { DrizzleHealthIndicator } from './drizzle-health.indicator';
|
|
10
|
+
|
|
11
|
+
describe('DrizzleHealthIndicator', () => {
|
|
12
|
+
it('retorna status up quando a consulta executa com sucesso', async () => {
|
|
13
|
+
const get = vi.fn().mockReturnValue({
|
|
14
|
+
result: 1,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const databaseClient = {
|
|
18
|
+
query: vi.fn().mockResolvedValue([
|
|
19
|
+
{
|
|
20
|
+
result: 1,
|
|
21
|
+
},
|
|
22
|
+
]),
|
|
23
|
+
prepare: vi.fn(() => ({
|
|
24
|
+
get,
|
|
25
|
+
})),
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const indicator =
|
|
29
|
+
new DrizzleHealthIndicator(
|
|
30
|
+
databaseClient as unknown as DatabaseClient,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
const result = await indicator.isHealthy(
|
|
34
|
+
'database',
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
expect(result).toEqual({
|
|
38
|
+
database: {
|
|
39
|
+
status: 'up',
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('lança HealthCheckError quando a consulta falha', async () => {
|
|
45
|
+
const get = vi.fn(() => {
|
|
46
|
+
throw new Error('conexão recusada');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const databaseClient = {
|
|
50
|
+
query: vi
|
|
51
|
+
.fn()
|
|
52
|
+
.mockRejectedValue(
|
|
53
|
+
new Error('conexão recusada'),
|
|
54
|
+
),
|
|
55
|
+
prepare: vi.fn(() => ({
|
|
56
|
+
get,
|
|
57
|
+
})),
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const indicator =
|
|
61
|
+
new DrizzleHealthIndicator(
|
|
62
|
+
databaseClient as unknown as DatabaseClient,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
await expect(
|
|
66
|
+
indicator.isHealthy('database'),
|
|
67
|
+
).rejects.toBeInstanceOf(
|
|
68
|
+
HealthCheckError,
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Inject,
|
|
3
|
+
Injectable,
|
|
4
|
+
} from '@nestjs/common';
|
|
5
|
+
import {
|
|
6
|
+
HealthCheckError,
|
|
7
|
+
HealthIndicatorResult,
|
|
8
|
+
} from '@nestjs/terminus';
|
|
9
|
+
import { DATABASE_CLIENT } from '../../database/database.constants';
|
|
10
|
+
import type { DatabaseClient } from '../../database/database.types';
|
|
11
|
+
|
|
12
|
+
@Injectable()
|
|
13
|
+
export class DrizzleHealthIndicator {
|
|
14
|
+
constructor(
|
|
15
|
+
@Inject(DATABASE_CLIENT)
|
|
16
|
+
private readonly client: DatabaseClient,
|
|
17
|
+
) { }
|
|
18
|
+
|
|
19
|
+
async isHealthy(
|
|
20
|
+
key: string,
|
|
21
|
+
): Promise<HealthIndicatorResult> {
|
|
22
|
+
try {
|
|
23
|
+
// nestforge:feature:database:postgres
|
|
24
|
+
await this.client.query('SELECT 1');
|
|
25
|
+
// nestforge:feature:database:postgres:end
|
|
26
|
+
|
|
27
|
+
// nestforge:feature:database:mysql
|
|
28
|
+
await this.client.query('SELECT 1');
|
|
29
|
+
// nestforge:feature:database:mysql:end
|
|
30
|
+
|
|
31
|
+
// nestforge:feature:database:sqlite
|
|
32
|
+
this.client.prepare('SELECT 1').get();
|
|
33
|
+
// nestforge:feature:database:sqlite:end
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
[key]: {
|
|
37
|
+
status: 'up',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw new HealthCheckError(
|
|
42
|
+
'Banco de dados indisponível',
|
|
43
|
+
{
|
|
44
|
+
[key]: {
|
|
45
|
+
status: 'down',
|
|
46
|
+
message: (error as Error).message,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -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,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
|
+
}
|