@koalarx/nest-cli 1.0.1

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.
Files changed (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +17 -0
  3. package/code-base/startup-project/.gitattributes +1 -0
  4. package/code-base/startup-project/LICENSE +21 -0
  5. package/code-base/startup-project/README.md +5 -0
  6. package/code-base/startup-project/eslint.config.mjs +22 -0
  7. package/code-base/startup-project/nest-cli.json +35 -0
  8. package/code-base/startup-project/package.json +68 -0
  9. package/code-base/startup-project/prisma/schema.prisma +30 -0
  10. package/code-base/startup-project/src/application/person/create/create-person.handler.spec.ts +22 -0
  11. package/code-base/startup-project/src/application/person/create/create-person.handler.ts +39 -0
  12. package/code-base/startup-project/src/application/person/create/create-person.request.ts +18 -0
  13. package/code-base/startup-project/src/application/person/create/create-person.response.ts +3 -0
  14. package/code-base/startup-project/src/application/person/create/create-person.validator.ts +16 -0
  15. package/code-base/startup-project/src/application/person/create-person-job/create-person-job.ts +57 -0
  16. package/code-base/startup-project/src/application/person/delete/delete-person.handler.spec.ts +24 -0
  17. package/code-base/startup-project/src/application/person/delete/delete-person.handler.ts +33 -0
  18. package/code-base/startup-project/src/application/person/delete-inative-job/delete-inactive-job.ts +49 -0
  19. package/code-base/startup-project/src/application/person/events/inactive-person/inactive-person-event.ts +3 -0
  20. package/code-base/startup-project/src/application/person/events/inactive-person/inactive-person-handler.ts +27 -0
  21. package/code-base/startup-project/src/application/person/events/person-event.job.ts +11 -0
  22. package/code-base/startup-project/src/application/person/read/read-person.handler.spec.ts +29 -0
  23. package/code-base/startup-project/src/application/person/read/read-person.handler.ts +37 -0
  24. package/code-base/startup-project/src/application/person/read/read-person.response.ts +30 -0
  25. package/code-base/startup-project/src/application/person/read-many/read-many-person.handler.spec.ts +72 -0
  26. package/code-base/startup-project/src/application/person/read-many/read-many-person.handler.ts +42 -0
  27. package/code-base/startup-project/src/application/person/read-many/read-many-person.request.ts +15 -0
  28. package/code-base/startup-project/src/application/person/read-many/read-many-person.response.ts +16 -0
  29. package/code-base/startup-project/src/application/person/read-many/read-many.validator.ts +16 -0
  30. package/code-base/startup-project/src/application/person/update/update-person.handler.spec.ts +42 -0
  31. package/code-base/startup-project/src/application/person/update/update-person.handler.ts +56 -0
  32. package/code-base/startup-project/src/application/person/update/update-person.request.ts +26 -0
  33. package/code-base/startup-project/src/application/person/update/update-person.validator.ts +17 -0
  34. package/code-base/startup-project/src/domain/entities/person/person-phone.ts +10 -0
  35. package/code-base/startup-project/src/domain/entities/person/person.ts +18 -0
  36. package/code-base/startup-project/src/domain/mapping/mapping.profile.ts +10 -0
  37. package/code-base/startup-project/src/domain/mapping/person.mapping.ts +24 -0
  38. package/code-base/startup-project/src/domain/repositories/iperson.repository.ts +14 -0
  39. package/code-base/startup-project/src/infra/app.module.ts +27 -0
  40. package/code-base/startup-project/src/infra/controllers/controller.module.ts +16 -0
  41. package/code-base/startup-project/src/infra/controllers/controllers.module.ts +12 -0
  42. package/code-base/startup-project/src/infra/controllers/person/create-person.controller.ts +30 -0
  43. package/code-base/startup-project/src/infra/controllers/person/delete-person.controller.ts +22 -0
  44. package/code-base/startup-project/src/infra/controllers/person/person.controller.e2e-spec.ts +120 -0
  45. package/code-base/startup-project/src/infra/controllers/person/person.module.ts +31 -0
  46. package/code-base/startup-project/src/infra/controllers/person/read-many-person.controller.ts +29 -0
  47. package/code-base/startup-project/src/infra/controllers/person/read-person.controller.ts +26 -0
  48. package/code-base/startup-project/src/infra/controllers/person/router.config.ts +9 -0
  49. package/code-base/startup-project/src/infra/controllers/person/update-person.controller.ts +30 -0
  50. package/code-base/startup-project/src/infra/database/db-transaction-context.ts +18 -0
  51. package/code-base/startup-project/src/infra/database/repositories/person.repository.ts +51 -0
  52. package/code-base/startup-project/src/infra/database/repositories/repositories.module.ts +14 -0
  53. package/code-base/startup-project/src/infra/main.ts +30 -0
  54. package/code-base/startup-project/src/test/create-e2e-test-app.ts +17 -0
  55. package/code-base/startup-project/src/test/create-unit-test-app.ts +24 -0
  56. package/code-base/startup-project/src/test/mockup/person/create-person-request.mockup.ts +8 -0
  57. package/code-base/startup-project/src/test/repositories/person.repository.ts +27 -0
  58. package/code-base/startup-project/src/test/setup-e2e.ts +7 -0
  59. package/code-base/startup-project/tsconfig.json +31 -0
  60. package/code-base/startup-project/vitest.config.e2e.mts +18 -0
  61. package/code-base/startup-project/vitest.config.mts +10 -0
  62. package/commands/new-project/index.d.ts +3 -0
  63. package/commands/new-project/index.js +80 -0
  64. package/index.d.ts +1 -0
  65. package/index.js +152 -0
  66. package/package.json +61 -0
  67. package/utils/copy-folder.d.ts +3 -0
  68. package/utils/copy-folder.js +59 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Igor D. Rangel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # @koalarx/nest-cli
2
+
3
+ Biblioteca de CLI para criação de projetos utilizando Koala Nest
4
+
5
+ ## Instalação
6
+
7
+ ```bash
8
+ npm i -g @koalarx/nest-cli
9
+ ```
10
+
11
+ > Versão do Node >= 20.18.0
12
+
13
+ ## Uso
14
+
15
+ ```bash
16
+ koala-nest new app-demo
17
+ ```
@@ -0,0 +1 @@
1
+ eol=lf
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Igor D. Rangel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ <p align="center">
2
+ <a href="https://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3
+ </p>
4
+
5
+ <h1 align="center">[projectName]</h1>
@@ -0,0 +1,22 @@
1
+ import js from "@eslint/js";
2
+ import { defineConfig } from "eslint/config";
3
+ import globals from "globals";
4
+ import tseslint from "typescript-eslint";
5
+
6
+ export default defineConfig([
7
+ { files: ["**/*.{js,mjs,cjs,ts}"] },
8
+ { files: ["**/*.{js,mjs,cjs,ts}"], languageOptions: { globals: globals.node } },
9
+ { files: ["**/*.{js,mjs,cjs,ts}"], plugins: { js }, extends: ["js/recommended"] },
10
+ tseslint.configs.recommended,
11
+ { ignores: ["node_modules", "dist"] },
12
+ {
13
+ rules: {
14
+ "@typescript-eslint/ban-ts-comment": "off",
15
+ "@typescript-eslint/no-unused-vars": "off",
16
+ "@typescript-eslint/no-unsafe-function-type": "off",
17
+ "@typescript-eslint/no-useless-constructor": "off",
18
+ "@typescript-eslint/no-new": "off",
19
+ "@typescript-eslint/no-explicit-any": "off"
20
+ }
21
+ }
22
+ ]);
@@ -0,0 +1,35 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/nest-cli",
3
+ "collection": "@nestjs/schematics",
4
+ "monorepo": true,
5
+ "projects": {
6
+ "koala-nest": {
7
+ "type": "library",
8
+ "root": "apps/koala-nest",
9
+ "entryFile": "index",
10
+ "sourceRoot": "apps/koala-nest/src",
11
+ "compilerOptions": {
12
+ "tsConfigPath": "apps/koala-nest/tsconfig.lib.json"
13
+ }
14
+ },
15
+ "example": {
16
+ "type": "application",
17
+ "root": "apps/example",
18
+ "entryFile": "example/src/infra/main",
19
+ "sourceRoot": "apps/example/src",
20
+ "compilerOptions": {
21
+ "deleteOutDir": true,
22
+ "tsConfigPath": "apps/example/tsconfig.app.json",
23
+ "plugins": [
24
+ {
25
+ "name": "@nestjs/swagger",
26
+ "options": {
27
+ "classValidatorShim": false,
28
+ "introspectComments": true
29
+ }
30
+ }
31
+ ]
32
+ }
33
+ }
34
+ }
35
+ }
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "demo",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "author": "",
6
+ "private": true,
7
+ "license": "UNLICENSED",
8
+ "scripts": {
9
+ "build": "nest build",
10
+ "start": "nest start",
11
+ "start:dev": "nest start --watch",
12
+ "start:debug": "nest start --debug --watch",
13
+ "test": "vitest run",
14
+ "test:watch": "vitest",
15
+ "test:e2e": "vitest run --config ./vitest.config.e2e.mts",
16
+ "test:all": "npm run test && npm run test:e2e"
17
+ },
18
+ "dependencies": {
19
+ "@koalarx/utils": "3.1.3",
20
+ "@nestjs/common": "11.0.12",
21
+ "@nestjs/config": "4.0.1",
22
+ "@nestjs/core": "11.0.12",
23
+ "@nestjs/mapped-types": "2.1.0",
24
+ "@nestjs/platform-express": "11.0.12",
25
+ "@nestjs/swagger": "11.0.7",
26
+ "@prisma/client": "6.5.0",
27
+ "@scalar/nestjs-api-reference": "0.4.2",
28
+ "cookie-parser": "1.4.6",
29
+ "dotenv": "16.0.3",
30
+ "express-basic-auth": "1.2.1",
31
+ "ioredis": "5.3.2",
32
+ "reflect-metadata": "0.1.13",
33
+ "rimraf": "3.0.2",
34
+ "zod": "3.22.4",
35
+ "zod-validation-error": "1.5.0"
36
+ },
37
+ "devDependencies": {
38
+ "@eslint/js": "9.23.0",
39
+ "@faker-js/faker": "^9.6.0",
40
+ "@nestjs/cli": "11.0.5",
41
+ "@nestjs/schematics": "10.0.2",
42
+ "@nestjs/testing": "11.0.12",
43
+ "@types/cookie-parser": "1.4.8",
44
+ "@types/express": "5.0.1",
45
+ "@types/jest": "29.5.14",
46
+ "@types/node": "22.13.11",
47
+ "@types/supertest": "6.0.2",
48
+ "@typescript-eslint/eslint-plugin": "8.27.0",
49
+ "@typescript-eslint/parser": "8.27.0",
50
+ "@vitest/coverage-v8": "3.0.9",
51
+ "eslint": "9.23.0",
52
+ "eslint-config-prettier": "10.1.1",
53
+ "eslint-plugin-prettier": "5.2.3",
54
+ "eslint-plugin-vitest": "0.5.4",
55
+ "eslint-plugin-vitest-globals": "1.5.0",
56
+ "globals": "16.0.0",
57
+ "prettier": "3.5.3",
58
+ "prisma": "6.5.0",
59
+ "source-map-support": "0.5.20",
60
+ "supertest": "7.1.0",
61
+ "tsconfig-paths": "4.2.0",
62
+ "typescript": "5.1.3",
63
+ "typescript-eslint": "8.27.0",
64
+ "unplugin-swc": "1.5.1",
65
+ "vite-tsconfig-paths": "5.1.4",
66
+ "vitest": "3.0.9"
67
+ }
68
+ }
@@ -0,0 +1,30 @@
1
+ generator client {
2
+ provider = "prisma-client-js"
3
+ binaryTargets = ["native", "linux-musl"]
4
+ }
5
+
6
+ datasource db {
7
+ provider = "postgresql"
8
+ url = env("DATABASE_URL")
9
+ directUrl = env("DIRECT_URL")
10
+ }
11
+
12
+ model Person {
13
+ id Int @id @default(autoincrement())
14
+ name String
15
+ active Boolean @default(true)
16
+
17
+ phones PersonPhone[]
18
+
19
+ @@map("person")
20
+ }
21
+
22
+ model PersonPhone {
23
+ id Int @id @default(autoincrement())
24
+ phone String
25
+
26
+ personId Int @map("person_id")
27
+ person Person @relation(fields: [personId], references: [id], onDelete: Cascade)
28
+
29
+ @@map("person_phone")
30
+ }
@@ -0,0 +1,22 @@
1
+ import { createUnitTestApp } from "@/test/create-unit-test-app"
2
+ import { createPersonRequestMockup } from "@/test/mockup/person/create-person-request.mockup"
3
+ import { CreatePersonHandler } from "./create-person.handler"
4
+
5
+ describe('CreatePersonHandler', () => {
6
+ const app = createUnitTestApp()
7
+
8
+ it('should create a person', async () => {
9
+ const handler = app.get(CreatePersonHandler)
10
+ const request = createPersonRequestMockup
11
+
12
+ const result = await handler.handle(request)
13
+
14
+ expect(result.isOk()).toBeTruthy()
15
+
16
+ if (result.isOk()) {
17
+ expect(result.value).toEqual({
18
+ id: expect.any(Number)
19
+ })
20
+ }
21
+ })
22
+ })
@@ -0,0 +1,39 @@
1
+ import { Person } from '@/domain/entities/person/person'
2
+ import { IPersonRepository } from '@/domain/repositories/iperson.repository'
3
+ import { AutoMappingService } from '@koalarx/nest/core/mapping/auto-mapping.service'
4
+ import { RequestHandlerBase } from '@koalarx/nest/core/request-overflow/request-handler.base'
5
+ import {
6
+ ok,
7
+ RequestResult,
8
+ } from '@koalarx/nest/core/request-overflow/request-result'
9
+ import { Injectable } from '@nestjs/common'
10
+ import { CreatePersonRequest } from './create-person.request'
11
+ import { CreatePersonResponse } from './create-person.response'
12
+ import { CreatePersonValidator } from './create-person.validator'
13
+
14
+ @Injectable()
15
+ export class CreatePersonHandler extends RequestHandlerBase<
16
+ CreatePersonRequest,
17
+ RequestResult<Error, CreatePersonResponse>
18
+ > {
19
+ constructor(
20
+ private readonly mapper: AutoMappingService,
21
+ private readonly repository: IPersonRepository,
22
+ ) {
23
+ super()
24
+ }
25
+
26
+ async handle(
27
+ req: CreatePersonRequest,
28
+ ): Promise<RequestResult<Error, CreatePersonResponse>> {
29
+ const person = this.mapper.map(
30
+ new CreatePersonValidator(req).validate(),
31
+ CreatePersonRequest,
32
+ Person,
33
+ )
34
+
35
+ const result = await this.repository.save(person)
36
+
37
+ return ok({ id: result.id })
38
+ }
39
+ }
@@ -0,0 +1,18 @@
1
+ import { AutoMap } from '@koalarx/nest/core/mapping/auto-mapping.decorator'
2
+ import { ApiProperty } from '@nestjs/swagger'
3
+
4
+ export class CreatePersonPhoneRequest {
5
+ @ApiProperty()
6
+ @AutoMap()
7
+ phone: string
8
+ }
9
+
10
+ export class CreatePersonRequest {
11
+ @ApiProperty()
12
+ @AutoMap()
13
+ name: string
14
+
15
+ @ApiProperty({ type: [CreatePersonPhoneRequest] })
16
+ @AutoMap({ type: CreatePersonPhoneRequest, isArray: { addTo: true } })
17
+ phones: Array<CreatePersonPhoneRequest>
18
+ }
@@ -0,0 +1,3 @@
1
+ import { CreatedRegistreResponseBase } from '@koalarx/nest/core/controllers/created-registre-response.base'
2
+
3
+ export class CreatePersonResponse extends CreatedRegistreResponseBase<number> {}
@@ -0,0 +1,16 @@
1
+ import { RequestValidatorBase } from '@koalarx/nest/core/request-overflow/request-validator.base'
2
+ import { CreatePersonRequest } from './create-person.request'
3
+ import { z, ZodType, ZodTypeDef } from 'zod'
4
+
5
+ export class CreatePersonValidator extends RequestValidatorBase<CreatePersonRequest> {
6
+ protected get schema(): ZodType<any, ZodTypeDef, any> {
7
+ return z.object({
8
+ name: z.string(),
9
+ phones: z.array(
10
+ z.object({
11
+ phone: z.string(),
12
+ }),
13
+ ),
14
+ })
15
+ }
16
+ }
@@ -0,0 +1,57 @@
1
+ import { IPersonRepository } from '@/domain/repositories/iperson.repository'
2
+ import {
3
+ CronJob,
4
+ CronJobResponse,
5
+ } from '@koalarx/nest/core/backgroud-services/cron-service/cron-job'
6
+ import { EventQueue } from '@koalarx/nest/core/backgroud-services/event-service/event-queue'
7
+ import { ok } from '@koalarx/nest/core/request-overflow/request-result'
8
+ import { ILoggingService } from '@koalarx/nest/services/logging/ilogging.service'
9
+ import { IRedLockService } from '@koalarx/nest/services/redlock/ired-lock.service'
10
+ import { Injectable } from '@nestjs/common'
11
+ import { CreatePersonHandler } from '../create/create-person.handler'
12
+ import { InactivePersonEvent } from '../events/inactive-person/inactive-person-event'
13
+ import { PersonEventJob } from '../events/person-event.job'
14
+
15
+ @Injectable()
16
+ export class CreatePersonJob extends CronJob {
17
+ constructor(
18
+ redlockService: IRedLockService,
19
+ loggingService: ILoggingService,
20
+ private readonly createPerson: CreatePersonHandler,
21
+ private readonly repository: IPersonRepository,
22
+ ) {
23
+ super(redlockService, loggingService)
24
+ }
25
+
26
+ protected async run(): Promise<CronJobResponse> {
27
+ const result = await this.createPerson.handle({
28
+ name: 'John Doe',
29
+ phones: [{phone: '22999999999'}],
30
+ })
31
+
32
+ if (result.isOk()) {
33
+ const person = await this.repository.read(result.value.id)
34
+
35
+ if (person) {
36
+ const jobs = new PersonEventJob()
37
+ jobs.addEvent(new InactivePersonEvent())
38
+
39
+ EventQueue.dispatchEventsForAggregate(jobs._id)
40
+ }
41
+
42
+ console.log('Person created with id:', result.value.id)
43
+ } else {
44
+ console.error('Error creating person:', result.value)
45
+ }
46
+
47
+ return ok(null)
48
+ }
49
+
50
+ protected async isActive(): Promise<boolean> {
51
+ return true
52
+ }
53
+
54
+ protected defineTimeInMinutes(): number {
55
+ return 1
56
+ }
57
+ }
@@ -0,0 +1,24 @@
1
+ import { createUnitTestApp } from "@/test/create-unit-test-app"
2
+ import { createPersonRequestMockup } from "@/test/mockup/person/create-person-request.mockup"
3
+ import { CreatePersonHandler } from "../create/create-person.handler"
4
+ import { DeletePersonHandler } from "./delete-person.handler"
5
+
6
+ describe('DeletePersonHandler', () => {
7
+ const app = createUnitTestApp()
8
+
9
+ it('should delete a person', async () => {
10
+ const createResult = await app
11
+ .get(CreatePersonHandler)
12
+ .handle(createPersonRequestMockup)
13
+
14
+ expect(createResult.isOk()).toBeTruthy()
15
+
16
+ if (createResult.isOk()) {
17
+ const result = await app
18
+ .get(DeletePersonHandler)
19
+ .handle(createResult.value.id)
20
+
21
+ expect(result.isOk()).toBeTruthy()
22
+ }
23
+ })
24
+ })
@@ -0,0 +1,33 @@
1
+ import { IPersonRepository } from '@/domain/repositories/iperson.repository'
2
+ import { ResourceNotFoundError } from '@koalarx/nest/core/errors/resource-not-found.error'
3
+ import { RequestHandlerBase } from '@koalarx/nest/core/request-overflow/request-handler.base'
4
+ import {
5
+ failure,
6
+ ok,
7
+ RequestResult,
8
+ } from '@koalarx/nest/core/request-overflow/request-result'
9
+ import { Injectable } from '@nestjs/common'
10
+
11
+ @Injectable()
12
+ export class DeletePersonHandler extends RequestHandlerBase<
13
+ number,
14
+ RequestResult<ResourceNotFoundError, null>
15
+ > {
16
+ constructor(private readonly repository: IPersonRepository) {
17
+ super()
18
+ }
19
+
20
+ async handle(
21
+ id: number,
22
+ ): Promise<RequestResult<ResourceNotFoundError, null>> {
23
+ const person = await this.repository.read(id)
24
+
25
+ if (!person) {
26
+ return failure(new ResourceNotFoundError('Pessoa'))
27
+ }
28
+
29
+ await this.repository.delete(id)
30
+
31
+ return ok(null)
32
+ }
33
+ }
@@ -0,0 +1,49 @@
1
+ import {
2
+ CronJob,
3
+ CronJobResponse,
4
+ } from '@koalarx/nest/core/backgroud-services/cron-service/cron-job'
5
+ import { ok } from '@koalarx/nest/core/request-overflow/request-result'
6
+ import { ILoggingService } from '@koalarx/nest/services/logging/ilogging.service'
7
+ import { IRedLockService } from '@koalarx/nest/services/redlock/ired-lock.service'
8
+ import { Injectable } from '@nestjs/common'
9
+ import { DeletePersonHandler } from '../delete/delete-person.handler'
10
+ import { ReadManyPersonHandler } from '../read-many/read-many-person.handler'
11
+ import { ReadManyPersonRequest } from '../read-many/read-many-person.request'
12
+
13
+ @Injectable()
14
+ export class DeleteInactiveJob extends CronJob {
15
+ constructor(
16
+ redlockService: IRedLockService,
17
+ loggingService: ILoggingService,
18
+ private readonly readManyPerson: ReadManyPersonHandler,
19
+ private readonly deletePerson: DeletePersonHandler,
20
+ ) {
21
+ super(redlockService, loggingService)
22
+ }
23
+
24
+ protected async run(): Promise<CronJobResponse> {
25
+ const result = await this.readManyPerson.handle(
26
+ new ReadManyPersonRequest({ active: false }),
27
+ )
28
+
29
+ if (result.isOk()) {
30
+ for (const person of result.value.items) {
31
+ await this.deletePerson.handle(person.id)
32
+
33
+ console.log('Person with id was deleted:', person.id)
34
+ }
35
+ } else {
36
+ console.error('Error to search inactive people:', result.value)
37
+ }
38
+
39
+ return ok(null)
40
+ }
41
+
42
+ protected async isActive(): Promise<boolean> {
43
+ return true
44
+ }
45
+
46
+ protected defineTimeInMinutes(): number {
47
+ return 1
48
+ }
49
+ }
@@ -0,0 +1,3 @@
1
+ import { EventClass } from '@koalarx/nest/core/backgroud-services/event-service/event-class'
2
+
3
+ export class InactivePersonEvent extends EventClass {}
@@ -0,0 +1,27 @@
1
+ import { IPersonRepository } from '@/domain/repositories/iperson.repository'
2
+ import { EventHandler } from '@koalarx/nest/core/backgroud-services/event-service/event-handler'
3
+ import { Injectable } from '@nestjs/common'
4
+ import { ReadManyPersonRequest } from '../../read-many/read-many-person.request'
5
+ import { InactivePersonEvent } from './inactive-person-event'
6
+
7
+ @Injectable()
8
+ export class InactivePersonHandler extends EventHandler<InactivePersonEvent> {
9
+ constructor(private readonly repository: IPersonRepository) {
10
+ super()
11
+ }
12
+
13
+ async handleEvent(): Promise<void> {
14
+ const result = await this.repository.readMany(
15
+ new ReadManyPersonRequest({ active: true }),
16
+ )
17
+
18
+ for (const person of result.items) {
19
+ person.active = false
20
+ await this.repository.save(person)
21
+ }
22
+
23
+ console.log(
24
+ 'InactivePersonHandler: Registros ativos inativados com sucesso!',
25
+ )
26
+ }
27
+ }
@@ -0,0 +1,11 @@
1
+ import { Person } from '@/domain/entities/person/person'
2
+ import { EventHandler } from '@koalarx/nest/core/backgroud-services/event-service/event-handler'
3
+ import { EventJob } from '@koalarx/nest/core/backgroud-services/event-service/event-job'
4
+ import { Type } from '@nestjs/common'
5
+ import { InactivePersonHandler } from './inactive-person/inactive-person-handler'
6
+
7
+ export class PersonEventJob extends EventJob<Person> {
8
+ defineHandlers(): Type<EventHandler<any>>[] {
9
+ return [InactivePersonHandler]
10
+ }
11
+ }
@@ -0,0 +1,29 @@
1
+ import { createUnitTestApp } from "@/test/create-unit-test-app"
2
+ import { createPersonRequestMockup } from "@/test/mockup/person/create-person-request.mockup"
3
+ import { CreatePersonHandler } from "../create/create-person.handler"
4
+ import { ReadPersonHandler } from "./read-person.handler"
5
+
6
+ describe('ReadPersonHandler', () => {
7
+ const app = createUnitTestApp()
8
+
9
+ it('should get a person by id', async () => {
10
+ const person = createPersonRequestMockup
11
+ const createResult = await app
12
+ .get(CreatePersonHandler)
13
+ .handle(createPersonRequestMockup)
14
+
15
+ expect(createResult.isOk()).toBeTruthy()
16
+
17
+ if (createResult.isOk()) {
18
+ const result = await app
19
+ .get(ReadPersonHandler)
20
+ .handle(createResult.value.id)
21
+
22
+ expect(result.value).toEqual({
23
+ ...person,
24
+ id: createResult.value.id,
25
+ status: 'inactive'
26
+ })
27
+ }
28
+ })
29
+ })
@@ -0,0 +1,37 @@
1
+ import { Person } from '@/domain/entities/person/person'
2
+ import { IPersonRepository } from '@/domain/repositories/iperson.repository'
3
+ import { ResourceNotFoundError } from '@koalarx/nest/core/errors/resource-not-found.error'
4
+ import { AutoMappingService } from '@koalarx/nest/core/mapping/auto-mapping.service'
5
+ import { RequestHandlerBase } from '@koalarx/nest/core/request-overflow/request-handler.base'
6
+ import {
7
+ failure,
8
+ ok,
9
+ RequestResult,
10
+ } from '@koalarx/nest/core/request-overflow/request-result'
11
+ import { Injectable } from '@nestjs/common'
12
+ import { ReadPersonResponse } from './read-person.response'
13
+
14
+ @Injectable()
15
+ export class ReadPersonHandler extends RequestHandlerBase<
16
+ number,
17
+ RequestResult<ResourceNotFoundError, ReadPersonResponse>
18
+ > {
19
+ constructor(
20
+ private readonly mapper: AutoMappingService,
21
+ private readonly repository: IPersonRepository,
22
+ ) {
23
+ super()
24
+ }
25
+
26
+ async handle(
27
+ id: number,
28
+ ): Promise<RequestResult<ResourceNotFoundError, ReadPersonResponse>> {
29
+ const person = await this.repository.read(id)
30
+
31
+ if (!person) {
32
+ return failure(new ResourceNotFoundError('Pessoa'))
33
+ }
34
+
35
+ return ok(this.mapper.map(person, Person, ReadPersonResponse))
36
+ }
37
+ }
@@ -0,0 +1,30 @@
1
+ import { AutoMap } from '@koalarx/nest/core/mapping/auto-mapping.decorator'
2
+ import { ApiProperty } from '@nestjs/swagger'
3
+
4
+ export class ReadPersonPhoneResponse {
5
+ @ApiProperty()
6
+ @AutoMap()
7
+ id: number
8
+
9
+ @ApiProperty()
10
+ @AutoMap()
11
+ phone: string
12
+ }
13
+
14
+ export class ReadPersonResponse {
15
+ @ApiProperty()
16
+ @AutoMap()
17
+ id: number
18
+
19
+ @ApiProperty()
20
+ @AutoMap()
21
+ name: string
22
+
23
+ @ApiProperty({ type: [ReadPersonPhoneResponse] })
24
+ @AutoMap({ type: ReadPersonPhoneResponse, isArray: true})
25
+ phones: Array<ReadPersonPhoneResponse>
26
+
27
+ @ApiProperty()
28
+ @AutoMap()
29
+ status: string
30
+ }