From e1989c0a529056f6e1da1cf5553814d8a1c4d006 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 20 Jun 2023 16:50:55 +0200 Subject: [PATCH 01/29] WIP - first shot of massive ddd-hexagon refactor --- .env.dist | 7 +- package-lock.json | 1617 +++++++++-------- package.json | 18 +- .../migration.sql | 24 +- prisma/schema.prisma | 36 +- src/app.module.ts | 79 +- src/libs/api/api-error.response.ts | 19 + src/libs/api/id.response.dto.ts | 7 + src/libs/api/paginated.response.base.ts | 8 + src/libs/api/response.base.ts | 23 + src/libs/db/prisma-repository.base.ts | 46 + src/libs/db/prisma-service.ts | 15 + src/libs/ddd/aggregate-root.base.ts | 35 + src/libs/ddd/command.base.ts | 52 + src/libs/ddd/domain-event.base.ts | 52 + src/libs/ddd/entity.base.ts | 150 ++ src/libs/ddd/index.ts | 7 + src/libs/ddd/mapper.interface.ts | 11 + src/libs/ddd/repository.port.ts | 42 + src/libs/ddd/value-object.base.ts | 71 + src/libs/exceptions/exception.base.ts | 63 + src/libs/exceptions/exception.codes.ts | 15 + src/libs/exceptions/exceptions.ts | 82 + src/libs/exceptions/index.ts | 3 + src/libs/guard.ts | 55 + src/libs/ports/logger.port.ts | 6 + src/libs/ports/prisma-repository.port.ts | 4 + src/libs/types/index.ts | 6 + src/libs/types/object-literal.type.ts | 6 + .../utils/convert-props-to-object.util.ts | 47 + src/libs/utils/index.ts | 1 + src/main.ts | 2 +- src/modules/ad/ad.constants.ts | 1 - src/modules/ad/ad.di-tokens.ts | 3 + src/modules/ad/ad.mapper.ts | 228 +++ src/modules/ad/ad.module.ts | 38 +- .../ad/adapters/primaries/ad.controller.ts | 59 - src/modules/ad/adapters/primaries/ad.proto | 83 - .../ad/adapters/secondaries/ads.repository.ts | 8 - src/modules/ad/commands/create-ad.command.ts | 9 - src/modules/ad/core/ad.entity.ts | 137 ++ src/modules/ad/core/ad.errors.ts | 11 + src/modules/ad/core/ad.types.ts | 49 + .../commands/create-ad/create-ad.command.ts | 36 + .../commands/create-ad/create-ad.service.ts | 88 + .../core/events/ad-created.domain-events.ts | 68 + .../ad/core/ports/ad.repository.port.ts | 4 + .../ports/default-params-provider.port.ts | 5 + .../ports}/default-params.type.ts | 3 +- .../ad/core/ports/timezone-finder.port.ts | 3 + .../find-ad-by-id/find-ad-by-id.query.ts | 9 + src/modules/ad/core/types/address.ts | 10 + src/modules/ad/core/types/coordinates.ts | 4 + src/modules/ad/core/types/margin-durations.ts | 9 + src/modules/ad/core/types/schedule.ts | 9 + src/modules/ad/core/types/waypoint.ts | 5 + .../value-objects/address.value-object.ts | 52 + .../value-objects/coordinates.value-object.ts | 26 + .../margin-durations.value-object.ts | 79 + .../value-objects/schedule.value-object.ts | 51 + .../value-objects/waypoint.value-object.ts | 27 + src/modules/ad/domain/dtos/ad.creation.ts | 86 - src/modules/ad/domain/dtos/address.dto.ts | 62 - .../ad/domain/dtos/create-ad.request.ts | 107 -- .../dtos/validators/address-position.ts | 13 - .../decorators/address-position.validator.ts | 24 - .../decorators/has-driver-seats.validator.ts | 26 - .../is-punctual-or-recurrent.validator.ts | 27 - .../dtos/validators/frequency.mapping.ts | 7 - .../dtos/validators/has-driver-seats.ts | 19 - .../dtos/validators/has-passenger-seats.ts | 19 - .../validators/is-punctual-or-recurrent.ts | 16 - src/modules/ad/domain/entities/ad.ts | 132 -- src/modules/ad/domain/entities/address.ts | 40 - .../domain/entities/frequency.normaliser.ts | 35 - .../interfaces/param-provider.interface.ts | 4 - src/modules/ad/domain/types/day.enum.ts | 9 - src/modules/ad/domain/types/frequency.enum.ts | 4 - .../ad/domain/usecases/create-ad.usecase.ts | 111 -- .../usecases/find-ad-by-uuid.usecase.ts | 33 - .../ad/infrastructure/ad.repository.ts | 71 + .../default-params-provider.ts} | 9 +- .../message-publisher.ts | 6 +- .../ad/infrastructure/prisma-service.ts | 15 + .../ad/infrastructure/timezone-finder.ts | 8 + .../primaries => interface}/ad.presenter.ts | 2 +- src/modules/ad/interface/ad.proto | 80 + .../dtos/ad.paginated.response.dto.ts | 6 + .../ad/interface/dtos/ad.response.dto.ts | 5 + .../create-ad.grpc.controller.ts | 43 + .../grpc-controllers/dtos/address.dto.ts | 33 + .../grpc-controllers/dtos/coordinates.dto.ts | 12 + .../dtos/create-ad.request.dto.ts | 91 + .../dtos/margin-durations.dto.ts} | 2 +- .../grpc-controllers}/dtos/schedule.dto.ts | 0 .../decorators/is-schedule.validator.ts} | 8 +- .../valid-position-indexes.validator.ts | 22 + .../dtos/validators/frequency.mapping.ts | 7 + .../dtos/validators/waypoint-position.ts | 15 + .../grpc-controllers/dtos/waypoint.dto.ts | 10 + .../dtos/find-ad-by-id.request.dto.ts} | 4 +- .../find-ad-by-id.grpc.controller.ts | 37 + src/modules/ad/mappers/ad.profile.ts | 139 -- src/modules/ad/mappers/address.profile.ts | 18 - .../ad/queries/find-ad-by-uuid.query.ts | 9 - .../tests/integration/ad.repository.spec.ts | 26 +- .../default-param.provider.spec.ts | 4 +- .../tests/unit/core/create-ad.service.spec.ts | 106 ++ .../unit/domain/create-ad.usecase.spec.ts | 230 ++- .../domain/find-ad-by-uuid.usecase.spec.ts | 22 +- .../unit/domain/frequency-normalizer.spec.ts | 140 ++ .../unit/domain/frequency.mapping.spec.ts | 10 +- .../domain/has-driver-seats-validator.spec.ts | 100 - .../has-passenger-seats-validator.spec.ts | 100 - ...ec.ts => is-punctual-or-recurrent.spec.ts} | 46 +- .../unit/domain/recurrent-normaliser.spec.ts | 122 -- ...spec.ts => valid-position-indexes.spec.ts} | 22 +- .../adapters/primaries/health.controller.ts | 4 +- .../adapters/secondaries/message-publisher.ts | 4 +- .../repositories.health-indicator.usecase.ts | 6 +- src/modules/health/health.module.ts | 4 +- .../message-publisher.port.ts} | 2 +- .../unit/rpc-validation-pipe.usecase.spec.ts | 6 +- tsconfig.json | 8 +- 124 files changed, 3651 insertions(+), 2460 deletions(-) rename prisma/migrations/{20230515131219_init => 20230609141640_init}/migration.sql (71%) create mode 100644 src/libs/api/api-error.response.ts create mode 100644 src/libs/api/id.response.dto.ts create mode 100644 src/libs/api/paginated.response.base.ts create mode 100644 src/libs/api/response.base.ts create mode 100644 src/libs/db/prisma-repository.base.ts create mode 100644 src/libs/db/prisma-service.ts create mode 100644 src/libs/ddd/aggregate-root.base.ts create mode 100644 src/libs/ddd/command.base.ts create mode 100644 src/libs/ddd/domain-event.base.ts create mode 100644 src/libs/ddd/entity.base.ts create mode 100644 src/libs/ddd/index.ts create mode 100644 src/libs/ddd/mapper.interface.ts create mode 100644 src/libs/ddd/repository.port.ts create mode 100644 src/libs/ddd/value-object.base.ts create mode 100644 src/libs/exceptions/exception.base.ts create mode 100644 src/libs/exceptions/exception.codes.ts create mode 100644 src/libs/exceptions/exceptions.ts create mode 100644 src/libs/exceptions/index.ts create mode 100644 src/libs/guard.ts create mode 100644 src/libs/ports/logger.port.ts create mode 100644 src/libs/ports/prisma-repository.port.ts create mode 100644 src/libs/types/index.ts create mode 100644 src/libs/types/object-literal.type.ts create mode 100644 src/libs/utils/convert-props-to-object.util.ts create mode 100644 src/libs/utils/index.ts delete mode 100644 src/modules/ad/ad.constants.ts create mode 100644 src/modules/ad/ad.di-tokens.ts create mode 100644 src/modules/ad/ad.mapper.ts delete mode 100644 src/modules/ad/adapters/primaries/ad.controller.ts delete mode 100644 src/modules/ad/adapters/primaries/ad.proto delete mode 100644 src/modules/ad/adapters/secondaries/ads.repository.ts delete mode 100644 src/modules/ad/commands/create-ad.command.ts create mode 100644 src/modules/ad/core/ad.entity.ts create mode 100644 src/modules/ad/core/ad.errors.ts create mode 100644 src/modules/ad/core/ad.types.ts create mode 100644 src/modules/ad/core/commands/create-ad/create-ad.command.ts create mode 100644 src/modules/ad/core/commands/create-ad/create-ad.service.ts create mode 100644 src/modules/ad/core/events/ad-created.domain-events.ts create mode 100644 src/modules/ad/core/ports/ad.repository.port.ts create mode 100644 src/modules/ad/core/ports/default-params-provider.port.ts rename src/modules/ad/{domain/types => core/ports}/default-params.type.ts (83%) create mode 100644 src/modules/ad/core/ports/timezone-finder.port.ts create mode 100644 src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts create mode 100644 src/modules/ad/core/types/address.ts create mode 100644 src/modules/ad/core/types/coordinates.ts create mode 100644 src/modules/ad/core/types/margin-durations.ts create mode 100644 src/modules/ad/core/types/schedule.ts create mode 100644 src/modules/ad/core/types/waypoint.ts create mode 100644 src/modules/ad/core/value-objects/address.value-object.ts create mode 100644 src/modules/ad/core/value-objects/coordinates.value-object.ts create mode 100644 src/modules/ad/core/value-objects/margin-durations.value-object.ts create mode 100644 src/modules/ad/core/value-objects/schedule.value-object.ts create mode 100644 src/modules/ad/core/value-objects/waypoint.value-object.ts delete mode 100644 src/modules/ad/domain/dtos/ad.creation.ts delete mode 100644 src/modules/ad/domain/dtos/address.dto.ts delete mode 100644 src/modules/ad/domain/dtos/create-ad.request.ts delete mode 100644 src/modules/ad/domain/dtos/validators/address-position.ts delete mode 100644 src/modules/ad/domain/dtos/validators/decorators/address-position.validator.ts delete mode 100644 src/modules/ad/domain/dtos/validators/decorators/has-driver-seats.validator.ts delete mode 100644 src/modules/ad/domain/dtos/validators/decorators/is-punctual-or-recurrent.validator.ts delete mode 100644 src/modules/ad/domain/dtos/validators/frequency.mapping.ts delete mode 100644 src/modules/ad/domain/dtos/validators/has-driver-seats.ts delete mode 100644 src/modules/ad/domain/dtos/validators/has-passenger-seats.ts delete mode 100644 src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.ts delete mode 100644 src/modules/ad/domain/entities/ad.ts delete mode 100644 src/modules/ad/domain/entities/address.ts delete mode 100644 src/modules/ad/domain/entities/frequency.normaliser.ts delete mode 100644 src/modules/ad/domain/interfaces/param-provider.interface.ts delete mode 100644 src/modules/ad/domain/types/day.enum.ts delete mode 100644 src/modules/ad/domain/types/frequency.enum.ts delete mode 100644 src/modules/ad/domain/usecases/create-ad.usecase.ts delete mode 100644 src/modules/ad/domain/usecases/find-ad-by-uuid.usecase.ts create mode 100644 src/modules/ad/infrastructure/ad.repository.ts rename src/modules/ad/{adapters/secondaries/default-params.provider.ts => infrastructure/default-params-provider.ts} (72%) rename src/modules/ad/{adapters/secondaries => infrastructure}/message-publisher.ts (67%) create mode 100644 src/modules/ad/infrastructure/prisma-service.ts create mode 100644 src/modules/ad/infrastructure/timezone-finder.ts rename src/modules/ad/{adapters/primaries => interface}/ad.presenter.ts (84%) create mode 100644 src/modules/ad/interface/ad.proto create mode 100644 src/modules/ad/interface/dtos/ad.paginated.response.dto.ts create mode 100644 src/modules/ad/interface/dtos/ad.response.dto.ts create mode 100644 src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts rename src/modules/ad/{domain/dtos/margin.dto.ts => interface/grpc-controllers/dtos/margin-durations.dto.ts} (93%) rename src/modules/ad/{domain => interface/grpc-controllers}/dtos/schedule.dto.ts (100%) rename src/modules/ad/{domain/dtos/validators/decorators/has-passenger-seats.validator.ts => interface/grpc-controllers/dtos/validators/decorators/is-schedule.validator.ts} (67%) create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping.ts create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts rename src/modules/ad/{domain/dtos/find-ad-by-uuid.request.ts => interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts} (63%) create mode 100644 src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts delete mode 100644 src/modules/ad/mappers/ad.profile.ts delete mode 100644 src/modules/ad/mappers/address.profile.ts delete mode 100644 src/modules/ad/queries/find-ad-by-uuid.query.ts create mode 100644 src/modules/ad/tests/unit/core/create-ad.service.spec.ts create mode 100644 src/modules/ad/tests/unit/domain/frequency-normalizer.spec.ts delete mode 100644 src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts delete mode 100644 src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts rename src/modules/ad/tests/unit/domain/{is-punctual-or-reccurent.spec.ts => is-punctual-or-recurrent.spec.ts} (60%) delete mode 100644 src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts rename src/modules/ad/tests/unit/domain/{has-proper-addresses-indexes.spec.ts => valid-position-indexes.spec.ts} (66%) rename src/{interfaces/message-publisher.ts => ports/message-publisher.port.ts} (58%) diff --git a/.env.dist b/.env.dist index 2e4c53b..7ac2c10 100644 --- a/.env.dist +++ b/.env.dist @@ -22,9 +22,12 @@ DEPARTURE_MARGIN=900 # DEFAULT ROLE ROLE=passenger -# SEATS PROVIDED AS DRIVER / REQUESTED AS PASSENGER -SEATS_PROVIDED=3 +# SEATS PROPOSED AS DRIVER / REQUESTED AS PASSENGER +SEATS_PROPOSED=3 SEATS_REQUESTED=1 # ACCEPT ONLY SAME FREQUENCY REQUESTS STRICT_FREQUENCY=false + +# default timezone +DEFAULT_TIMEZONE=Europe/Paris diff --git a/package-lock.json b/package-lock.json index 2d720dc..45f60b1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,21 +15,26 @@ "@grpc/grpc-js": "^1.8.14", "@grpc/proto-loader": "^0.7.6", "@liaoliaots/nestjs-redis": "^9.0.5", - "@mobicoop/configuration-module": "^1.1.0", - "@mobicoop/message-broker-module": "^1.0.5", + "@mobicoop/configuration-module": "^1.2.0", + "@mobicoop/message-broker-module": "^1.2.0", "@nestjs/common": "^9.0.0", "@nestjs/config": "^2.3.1", "@nestjs/core": "^9.0.0", "@nestjs/cqrs": "^9.0.3", + "@nestjs/event-emitter": "^1.4.2", "@nestjs/microservices": "^9.4.0", "@nestjs/platform-express": "^9.0.0", "@nestjs/terminus": "^9.2.2", "@prisma/client": "^4.13.0", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", + "geo-tz": "^7.0.7", "ioredis": "^5.3.2", + "nestjs-request-context": "^2.1.0", + "oxide.ts": "^1.1.0", "reflect-metadata": "^0.1.13", - "rxjs": "^7.2.0" + "rxjs": "^7.2.0", + "timezonecomplete": "^5.12.4" }, "devDependencies": { "@nestjs/cli": "^9.0.0", @@ -39,6 +44,7 @@ "@types/jest": "29.5.0", "@types/node": "18.15.11", "@types/supertest": "^2.0.11", + "@types/uuid": "^9.0.2", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "dotenv-cli": "^7.2.1", @@ -70,11 +76,6 @@ "node": ">=0.8" } }, - "node_modules/@acuminous/bitsyntax/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - }, "node_modules/@ampproject/remapping": { "version": "2.2.1", "resolved": "https://registry.npmjs.org/@ampproject/remapping/-/remapping-2.2.1.tgz", @@ -89,19 +90,19 @@ } }, "node_modules/@angular-devkit/core": { - "version": "15.2.6", - "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-15.2.6.tgz", - "integrity": "sha512-YVTWZ+M+xNKdFX4EnY9QX49PZraawiaA0iTd2CUW8ZoTUvU7yOGMKZLSdz6aokTMRVfm0449wt6YL994ibOo1g==", + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-16.0.1.tgz", + "integrity": "sha512-2uz98IqkKJlgnHbWQ7VeL4pb+snGAZXIama2KXi+k9GsRntdcw+udX8rL3G9SdUGUF+m6+147Y1oRBMHsO/v4w==", "dev": true, "dependencies": { "ajv": "8.12.0", "ajv-formats": "2.1.1", "jsonc-parser": "3.2.0", - "rxjs": "6.6.7", + "rxjs": "7.8.1", "source-map": "0.7.4" }, "engines": { - "node": "^14.20.0 || ^16.13.0 || >=18.10.0", + "node": "^16.14.0 || >=18.10.0", "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", "yarn": ">= 1.13.0" }, @@ -114,50 +115,32 @@ } } }, - "node_modules/@angular-devkit/core/node_modules/rxjs": { - "version": "6.6.7", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz", - "integrity": "sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==", - "dev": true, - "dependencies": { - "tslib": "^1.9.0" - }, - "engines": { - "npm": ">=2.0.0" - } - }, - "node_modules/@angular-devkit/core/node_modules/tslib": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", - "dev": true - }, "node_modules/@angular-devkit/schematics": { - "version": "15.2.6", - "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-15.2.6.tgz", - "integrity": "sha512-f7VgnAcok7AwR/DhX0ZWskB0rFBo/KsvtIUA2qZSrpKMf8eFiwu03dv/b2mI0vnf+1FBfIQzJvO0ww45zRp6dA==", + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-16.0.1.tgz", + "integrity": "sha512-A9D0LTYmiqiBa90GKcSuWb7hUouGIbm/AHbJbjL85WLLRbQA2PwKl7P5Mpd6nS/ZC0kfG4VQY3VOaDvb3qpI9g==", "dev": true, "dependencies": { - "@angular-devkit/core": "15.2.6", + "@angular-devkit/core": "16.0.1", "jsonc-parser": "3.2.0", - "magic-string": "0.29.0", + "magic-string": "0.30.0", "ora": "5.4.1", - "rxjs": "6.6.7" + "rxjs": "7.8.1" }, "engines": { - "node": "^14.20.0 || ^16.13.0 || >=18.10.0", + "node": "^16.14.0 || >=18.10.0", "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", "yarn": ">= 1.13.0" } }, "node_modules/@angular-devkit/schematics-cli": { - "version": "15.2.6", - "resolved": "https://registry.npmjs.org/@angular-devkit/schematics-cli/-/schematics-cli-15.2.6.tgz", - "integrity": "sha512-dkmJAvLmiXIX3uAY0a7GcnEvKNN/RKR5Q/ez4OQb+jaz+2/XbAiQVmTgZ5uwU2gYkFNLvG9ZCAaQdC4JJp9xaw==", + "version": "16.0.1", + "resolved": "https://registry.npmjs.org/@angular-devkit/schematics-cli/-/schematics-cli-16.0.1.tgz", + "integrity": "sha512-6KLA125dpgd6oJGtiO2JpZAb92uOG3njQGIt7NFcuQGW/5GO7J41vMXH9cBAfdtbV8SIggSmR/cIEE9ijfj6YQ==", "dev": true, "dependencies": { - "@angular-devkit/core": "15.2.6", - "@angular-devkit/schematics": "15.2.6", + "@angular-devkit/core": "16.0.1", + "@angular-devkit/schematics": "16.0.1", "ansi-colors": "4.1.3", "inquirer": "8.2.4", "symbol-observable": "4.0.0", @@ -167,7 +150,7 @@ "schematics": "bin/schematics.js" }, "engines": { - "node": "^14.20.0 || ^16.13.0 || >=18.10.0", + "node": "^16.14.0 || >=18.10.0", "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", "yarn": ">= 1.13.0" } @@ -198,24 +181,6 @@ "node": ">=12.0.0" } }, - "node_modules/@angular-devkit/schematics/node_modules/rxjs": { - "version": "6.6.7", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz", - "integrity": "sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==", - "dev": true, - "dependencies": { - "tslib": "^1.9.0" - }, - "engines": { - "npm": ">=2.0.0" - } - }, - "node_modules/@angular-devkit/schematics/node_modules/tslib": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", - "dev": true - }, "node_modules/@automapper/classes": { "version": "8.7.7", "resolved": "https://registry.npmjs.org/@automapper/classes/-/classes-8.7.7.tgz", @@ -262,30 +227,30 @@ } }, "node_modules/@babel/compat-data": { - "version": "7.21.7", - "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.21.7.tgz", - "integrity": "sha512-KYMqFYTaenzMK4yUtf4EW9wc4N9ef80FsbMtkwool5zpwl4YrT1SdWYSTRcT94KO4hannogdS+LxY7L+arP3gA==", + "version": "7.22.3", + "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.22.3.tgz", + "integrity": "sha512-aNtko9OPOwVESUFp3MZfD8Uzxl7JzSeJpd7npIoxCasU37PFbAQRpKglkaKwlHOyeJdrREpo8TW8ldrkYWwvIQ==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/core": { - "version": "7.21.8", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.21.8.tgz", - "integrity": "sha512-YeM22Sondbo523Sz0+CirSPnbj9bG3P0CdHcBZdqUuaeOaYEFbOLoGU7lebvGP6P5J/WE9wOn7u7C4J9HvS1xQ==", + "version": "7.22.1", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.22.1.tgz", + "integrity": "sha512-Hkqu7J4ynysSXxmAahpN1jjRwVJ+NdpraFLIWflgjpVob3KNyK3/tIUc7Q7szed8WMp0JNa7Qtd1E9Oo22F9gA==", "dev": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.21.4", - "@babel/generator": "^7.21.5", - "@babel/helper-compilation-targets": "^7.21.5", - "@babel/helper-module-transforms": "^7.21.5", - "@babel/helpers": "^7.21.5", - "@babel/parser": "^7.21.8", - "@babel/template": "^7.20.7", - "@babel/traverse": "^7.21.5", - "@babel/types": "^7.21.5", + "@babel/generator": "^7.22.0", + "@babel/helper-compilation-targets": "^7.22.1", + "@babel/helper-module-transforms": "^7.22.1", + "@babel/helpers": "^7.22.0", + "@babel/parser": "^7.22.0", + "@babel/template": "^7.21.9", + "@babel/traverse": "^7.22.1", + "@babel/types": "^7.22.0", "convert-source-map": "^1.7.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", @@ -316,12 +281,12 @@ } }, "node_modules/@babel/generator": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.21.5.tgz", - "integrity": "sha512-SrKK/sRv8GesIW1bDagf9cCG38IOMYZusoe1dfg0D8aiUe3Amvoj1QtjTPAWcfrZFvIwlleLb0gxzQidL9w14w==", + "version": "7.22.3", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.22.3.tgz", + "integrity": "sha512-C17MW4wlk//ES/CJDL51kPNwl+qiBQyN7b9SKyVp11BLGFeSPoVaHrv+MNt8jwQFhQWowW88z1eeBx3pFz9v8A==", "dev": true, "dependencies": { - "@babel/types": "^7.21.5", + "@babel/types": "^7.22.3", "@jridgewell/gen-mapping": "^0.3.2", "@jridgewell/trace-mapping": "^0.3.17", "jsesc": "^2.5.1" @@ -331,12 +296,12 @@ } }, "node_modules/@babel/helper-compilation-targets": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.21.5.tgz", - "integrity": "sha512-1RkbFGUKex4lvsB9yhIfWltJM5cZKUftB2eNajaDv3dCMEp49iBG0K14uH8NnX9IPux2+mK7JGEOB0jn48/J6w==", + "version": "7.22.1", + "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.22.1.tgz", + "integrity": "sha512-Rqx13UM3yVB5q0D/KwQ8+SPfX/+Rnsy1Lw1k/UwOC4KC6qrzIQoY3lYnBu5EHKBlEHHcj0M0W8ltPSkD8rqfsQ==", "dev": true, "dependencies": { - "@babel/compat-data": "^7.21.5", + "@babel/compat-data": "^7.22.0", "@babel/helper-validator-option": "^7.21.0", "browserslist": "^4.21.3", "lru-cache": "^5.1.1", @@ -359,9 +324,9 @@ } }, "node_modules/@babel/helper-environment-visitor": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.21.5.tgz", - "integrity": "sha512-IYl4gZ3ETsWocUWgsFZLM5i1BYx9SoemminVEXadgLBa9TdeorzgLKm8wWLA6J1N/kT3Kch8XIk1laNzYoHKvQ==", + "version": "7.22.1", + "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.1.tgz", + "integrity": "sha512-Z2tgopurB/kTbidvzeBrc2To3PUP/9i5MUe+fU6QJCQDyPwSH2oRapkLw3KGECDYSjhQZCNxEvNvZlLw8JjGwA==", "dev": true, "engines": { "node": ">=6.9.0" @@ -405,19 +370,19 @@ } }, "node_modules/@babel/helper-module-transforms": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.21.5.tgz", - "integrity": "sha512-bI2Z9zBGY2q5yMHoBvJ2a9iX3ZOAzJPm7Q8Yz6YeoUjU/Cvhmi2G4QyTNyPBqqXSgTjUxRg3L0xV45HvkNWWBw==", + "version": "7.22.1", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.22.1.tgz", + "integrity": "sha512-dxAe9E7ySDGbQdCVOY/4+UcD8M9ZFqZcZhSPsPacvCG4M+9lwtDDQfI2EoaSvmf7W/8yCBkGU0m7Pvt1ru3UZw==", "dev": true, "dependencies": { - "@babel/helper-environment-visitor": "^7.21.5", + "@babel/helper-environment-visitor": "^7.22.1", "@babel/helper-module-imports": "^7.21.4", "@babel/helper-simple-access": "^7.21.5", "@babel/helper-split-export-declaration": "^7.18.6", "@babel/helper-validator-identifier": "^7.19.1", - "@babel/template": "^7.20.7", - "@babel/traverse": "^7.21.5", - "@babel/types": "^7.21.5" + "@babel/template": "^7.21.9", + "@babel/traverse": "^7.22.1", + "@babel/types": "^7.22.0" }, "engines": { "node": ">=6.9.0" @@ -484,14 +449,14 @@ } }, "node_modules/@babel/helpers": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.21.5.tgz", - "integrity": "sha512-BSY+JSlHxOmGsPTydUkPf1MdMQ3M81x5xGCOVgWM3G8XH77sJ292Y2oqcp0CbbgxhqBuI46iUz1tT7hqP7EfgA==", + "version": "7.22.3", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.22.3.tgz", + "integrity": "sha512-jBJ7jWblbgr7r6wYZHMdIqKc73ycaTcCaWRq4/2LpuPHcx7xMlZvpGQkOYc9HeSjn6rcx15CPlgVcBtZ4WZJ2w==", "dev": true, "dependencies": { - "@babel/template": "^7.20.7", - "@babel/traverse": "^7.21.5", - "@babel/types": "^7.21.5" + "@babel/template": "^7.21.9", + "@babel/traverse": "^7.22.1", + "@babel/types": "^7.22.3" }, "engines": { "node": ">=6.9.0" @@ -583,9 +548,9 @@ } }, "node_modules/@babel/parser": { - "version": "7.21.8", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.21.8.tgz", - "integrity": "sha512-6zavDGdzG3gUqAdWvlLFfk+36RilI+Pwyuuh7HItyeScCWP3k6i8vKclAQ0bM/0y/Kz/xiwvxhMv9MgTJP5gmA==", + "version": "7.22.4", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.4.tgz", + "integrity": "sha512-VLLsx06XkEYqBtE5YGPwfSGwfrjnyPP5oiGty3S8pQLFDFLaS8VwWSIxkTXpcvr5zeYLE6+MBNl2npl/YnfofA==", "dev": true, "bin": { "parser": "bin/babel-parser.js" @@ -772,33 +737,33 @@ } }, "node_modules/@babel/template": { - "version": "7.20.7", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.20.7.tgz", - "integrity": "sha512-8SegXApWe6VoNw0r9JHpSteLKTpTiLZ4rMlGIm9JQ18KiCtyQiAMEazujAHrUS5flrcqYZa75ukev3P6QmUwUw==", + "version": "7.21.9", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.21.9.tgz", + "integrity": "sha512-MK0X5k8NKOuWRamiEfc3KEJiHMTkGZNUjzMipqCGDDc6ijRl/B7RGSKVGncu4Ro/HdyzzY6cmoXuKI2Gffk7vQ==", "dev": true, "dependencies": { - "@babel/code-frame": "^7.18.6", - "@babel/parser": "^7.20.7", - "@babel/types": "^7.20.7" + "@babel/code-frame": "^7.21.4", + "@babel/parser": "^7.21.9", + "@babel/types": "^7.21.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/traverse": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.21.5.tgz", - "integrity": "sha512-AhQoI3YjWi6u/y/ntv7k48mcrCXmus0t79J9qPNlk/lAsFlCiJ047RmbfMOawySTHtywXhbXgpx/8nXMYd+oFw==", + "version": "7.22.4", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.4.tgz", + "integrity": "sha512-Tn1pDsjIcI+JcLKq1AVlZEr4226gpuAQTsLMorsYg9tuS/kG7nuwwJ4AB8jfQuEgb/COBwR/DqJxmoiYFu5/rQ==", "dev": true, "dependencies": { "@babel/code-frame": "^7.21.4", - "@babel/generator": "^7.21.5", - "@babel/helper-environment-visitor": "^7.21.5", + "@babel/generator": "^7.22.3", + "@babel/helper-environment-visitor": "^7.22.1", "@babel/helper-function-name": "^7.21.0", "@babel/helper-hoist-variables": "^7.18.6", "@babel/helper-split-export-declaration": "^7.18.6", - "@babel/parser": "^7.21.5", - "@babel/types": "^7.21.5", + "@babel/parser": "^7.22.4", + "@babel/types": "^7.22.4", "debug": "^4.1.0", "globals": "^11.1.0" }, @@ -816,9 +781,9 @@ } }, "node_modules/@babel/types": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.21.5.tgz", - "integrity": "sha512-m4AfNvVF2mVC/F7fDEdH2El3HzUg9It/XsCxZiOTTA3m3qYfcSVSbTfM6Q9xG+hYDniZssYhlXKKUMD5m8tF4Q==", + "version": "7.22.4", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.4.tgz", + "integrity": "sha512-Tx9x3UBHTTsMSW85WB2kphxYQVvrZ/t1FxD88IpSgIjiUJlCm9z+xWIDwyo1vffTwSqteqyznB8ZE9vYYk16zA==", "dev": true, "dependencies": { "@babel/helper-string-parser": "^7.21.5", @@ -892,14 +857,14 @@ } }, "node_modules/@eslint/eslintrc": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-2.0.2.tgz", - "integrity": "sha512-3W4f5tDUra+pA+FzgugqL2pRimUTDJWKr7BINqOpkZrC0uYI0NIc0/JFgBROCU07HR6GieA5m3/rsPIhDmCXTQ==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-2.0.3.tgz", + "integrity": "sha512-+5gy6OQfk+xx3q0d6jGZZC3f3KzAkXc/IanVxd1is/VIIziRqqt3ongQz0FiTUXqTk0c7aDB3OaFuKnuSoJicQ==", "dev": true, "dependencies": { "ajv": "^6.12.4", "debug": "^4.3.2", - "espree": "^9.5.1", + "espree": "^9.5.2", "globals": "^13.19.0", "ignore": "^5.2.0", "import-fresh": "^3.2.1", @@ -937,9 +902,9 @@ "dev": true }, "node_modules/@eslint/js": { - "version": "8.39.0", - "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.39.0.tgz", - "integrity": "sha512-kf9RB0Fg7NZfap83B3QOqOGg9QmD9yBudqQXzzOtn3i4y7ZUXe5ONeW34Gwi+TxhH4mvj72R1Zc300KUMa9Bng==", + "version": "8.42.0", + "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.42.0.tgz", + "integrity": "sha512-6SWlXpWU5AvId8Ac7zjzmIOqMOba/JWY8XZ4A7q7Gn1Vlfg/SFFIlrtHXt9nPn4op9ZPAkl91Jao+QQv3r/ukw==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -974,9 +939,9 @@ } }, "node_modules/@golevelup/nestjs-rabbitmq": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/@golevelup/nestjs-rabbitmq/-/nestjs-rabbitmq-3.6.0.tgz", - "integrity": "sha512-gfa+27QlQdf49g9Y1JrFp/xXFjzXEtRTlVDW7KqJTUYqq03f2AKYdOPH0Iu8ScqpPPRt+qNv9COBJD44Sizerg==", + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/@golevelup/nestjs-rabbitmq/-/nestjs-rabbitmq-3.6.1.tgz", + "integrity": "sha512-yxm7nVNRHeXiWHIFARF1NmMfIFu3WeixWkCIQzYisnS797SX1LC/kW2kIYJQvNHtcBwaro2WGhblyzvvQUJxXA==", "dependencies": { "@golevelup/nestjs-common": "^1.4.4", "@golevelup/nestjs-discovery": "^3.0.0", @@ -985,10 +950,45 @@ "amqplib": "^0.8.0" } }, + "node_modules/@golevelup/nestjs-rabbitmq/node_modules/amqplib": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.8.0.tgz", + "integrity": "sha512-icU+a4kkq4Y1PS4NNi+YPDMwdlbFcZ1EZTQT2nigW3fvOb6AOgUQ9+Mk4ue0Zu5cBg/XpDzB40oH10ysrk2dmA==", + "dependencies": { + "bitsyntax": "~0.1.0", + "bluebird": "^3.7.2", + "buffer-more-ints": "~1.0.0", + "readable-stream": "1.x >=1.1.9", + "safe-buffer": "~5.2.1", + "url-parse": "~1.5.1" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/@golevelup/nestjs-rabbitmq/node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/@grpc/grpc-js": { - "version": "1.8.14", - "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.8.14.tgz", - "integrity": "sha512-w84maJ6CKl5aApCMzFll0hxtFNT6or9WwMslobKaqWUEf1K+zhlL43bSQhFreyYWIWR+Z0xnVFC1KtLm4ZpM/A==", + "version": "1.8.15", + "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.8.15.tgz", + "integrity": "sha512-H2Bu/w6+oQ58DsRbQol66ERBk3V5ZIak/z/MDx0T4EgDnJWps807I6BvTjq0v6UvZtOcLO+ur+Q9wvniqu3OJA==", "dependencies": { "@grpc/proto-loader": "^0.7.0", "@types/node": ">=12.12.47" @@ -998,15 +998,15 @@ } }, "node_modules/@grpc/proto-loader": { - "version": "0.7.6", - "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.7.6.tgz", - "integrity": "sha512-QyAXR8Hyh7uMDmveWxDSUcJr9NAWaZ2I6IXgAYvQmfflwouTM+rArE2eEaCtLlRqO81j7pRLCt81IefUei6Zbw==", + "version": "0.7.7", + "resolved": "https://registry.npmjs.org/@grpc/proto-loader/-/proto-loader-0.7.7.tgz", + "integrity": "sha512-1TIeXOi8TuSCQprPItwoMymZXxWT0CPxUhkrkeCUH+D8U7QDwQ6b7SUz2MaLuWM2llT+J/TVFLmQI5KtML3BhQ==", "dependencies": { "@types/long": "^4.0.1", "lodash.camelcase": "^4.3.0", "long": "^4.0.0", "protobufjs": "^7.0.0", - "yargs": "^16.2.0" + "yargs": "^17.7.2" }, "bin": { "proto-loader-gen-types": "build/bin/proto-loader-gen-types.js" @@ -1015,45 +1015,10 @@ "node": ">=6" } }, - "node_modules/@grpc/proto-loader/node_modules/cliui": { - "version": "7.0.4", - "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", - "integrity": "sha512-OcRE68cOsVMXp1Yvonl/fzkQOyjLSu/8bhPDfQt0e0/Eb283TKP20Fs2MqoPsr9SwA595rRCA+QMzYc9nBP+JQ==", - "dependencies": { - "string-width": "^4.2.0", - "strip-ansi": "^6.0.0", - "wrap-ansi": "^7.0.0" - } - }, - "node_modules/@grpc/proto-loader/node_modules/yargs": { - "version": "16.2.0", - "resolved": "https://registry.npmjs.org/yargs/-/yargs-16.2.0.tgz", - "integrity": "sha512-D1mvvtDG0L5ft/jGWkLpG1+m0eQxOfaBvTNELraWj22wSVUMWxZUvYgJYcKh6jGGIkJFhH4IZPQhR4TKpc8mBw==", - "dependencies": { - "cliui": "^7.0.2", - "escalade": "^3.1.1", - "get-caller-file": "^2.0.5", - "require-directory": "^2.1.1", - "string-width": "^4.2.0", - "y18n": "^5.0.5", - "yargs-parser": "^20.2.2" - }, - "engines": { - "node": ">=10" - } - }, - "node_modules/@grpc/proto-loader/node_modules/yargs-parser": { - "version": "20.2.9", - "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-20.2.9.tgz", - "integrity": "sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==", - "engines": { - "node": ">=10" - } - }, "node_modules/@humanwhocodes/config-array": { - "version": "0.11.8", - "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz", - "integrity": "sha512-UybHIJzJnR5Qc/MsD9Kr+RpO2h+/P1GhOwdiLPXK5TWk5sgTdu88bTD9UP+CKbPPh5Rni1u0GjAdYQLemG8g+g==", + "version": "0.11.10", + "resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.10.tgz", + "integrity": "sha512-KVVjQmNUepDVGXNuoRRdmmEjruj0KfiGSbS8LVc12LMsWDQzRXJ0qdhN8L8uUigKpfEHRhlaQFY0ib1tnUbNeQ==", "dev": true, "dependencies": { "@humanwhocodes/object-schema": "^1.2.1", @@ -1113,6 +1078,15 @@ "sprintf-js": "~1.0.2" } }, + "node_modules/@istanbuljs/load-nyc-config/node_modules/camelcase": { + "version": "5.3.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", + "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/@istanbuljs/load-nyc-config/node_modules/find-up": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/find-up/-/find-up-4.1.0.tgz", @@ -1554,11 +1528,6 @@ "ioredis": "^5.0.0" } }, - "node_modules/@liaoliaots/nestjs-redis/node_modules/tslib": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", - "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==" - }, "node_modules/@lukeed/csprng": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/@lukeed/csprng/-/csprng-1.1.0.tgz", @@ -1568,12 +1537,12 @@ } }, "node_modules/@mobicoop/configuration-module": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@mobicoop/configuration-module/-/configuration-module-1.1.0.tgz", - "integrity": "sha512-4yzCrY8m40XOO3CZnWJC4kHk66sTQCwe5UjKCV/UpNkN9IGUKW+R84J/53aulmGTL95vec7g6tFIwlHJd9BCoA==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@mobicoop/configuration-module/-/configuration-module-1.2.0.tgz", + "integrity": "sha512-l0iDae7SgVVmjnCa2MBqAr3Er0yn4E7yiG8e7cs4XtNGUKrC1N0Ju56TEAraEYK9aZAZ36TCs06m1fep+rgwFA==", "dependencies": { + "@golevelup/nestjs-rabbitmq": "^3.6.0", "@liaoliaots/nestjs-redis": "^9.0.5", - "@mobicoop/message-broker-module": "^1.0.4", "@nestjs/cqrs": "^9.0.4", "@types/amqplib": "^0.10.1", "amqplib": "^0.10.3", @@ -1584,45 +1553,10 @@ "@nestjs/common": "^9.4.2" } }, - "node_modules/@mobicoop/configuration-module/node_modules/amqplib": { - "version": "0.10.3", - "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.10.3.tgz", - "integrity": "sha512-UHmuSa7n8vVW/a5HGh2nFPqAEr8+cD4dEZ6u9GjP91nHfr1a54RyAKyra7Sb5NH7NBKOUlyQSMXIp0qAixKexw==", - "dependencies": { - "@acuminous/bitsyntax": "^0.1.2", - "buffer-more-ints": "~1.0.0", - "readable-stream": "1.x >=1.1.9", - "url-parse": "~1.5.10" - }, - "engines": { - "node": ">=10" - } - }, - "node_modules/@mobicoop/configuration-module/node_modules/isarray": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", - "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==" - }, - "node_modules/@mobicoop/configuration-module/node_modules/readable-stream": { - "version": "1.1.14", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", - "integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==", - "dependencies": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.1", - "isarray": "0.0.1", - "string_decoder": "~0.10.x" - } - }, - "node_modules/@mobicoop/configuration-module/node_modules/string_decoder": { - "version": "0.10.31", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", - "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" - }, "node_modules/@mobicoop/message-broker-module": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/@mobicoop/message-broker-module/-/message-broker-module-1.0.6.tgz", - "integrity": "sha512-aVkWErc5pHz1oPRVBzvK3CvKKcUSNDvW58fbFXHbOA+md+jnyP9sH9NHyIOtVzIv0f6DbJBn9SA3x4VnSrDaBg==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/@mobicoop/message-broker-module/-/message-broker-module-1.2.0.tgz", + "integrity": "sha512-RoSHHK1GyQ/QVDmm3JS/wBfh171oChvyEp6YWmJd12krFLrPVn9MoEvZdyT3I5J31oBiUabMPle5Kdpw+Nrmww==", "dependencies": { "@golevelup/nestjs-rabbitmq": "^3.6.0", "@types/amqplib": "^0.10.1", @@ -1632,50 +1566,15 @@ "@nestjs/common": "^9.4.2" } }, - "node_modules/@mobicoop/message-broker-module/node_modules/amqplib": { - "version": "0.10.3", - "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.10.3.tgz", - "integrity": "sha512-UHmuSa7n8vVW/a5HGh2nFPqAEr8+cD4dEZ6u9GjP91nHfr1a54RyAKyra7Sb5NH7NBKOUlyQSMXIp0qAixKexw==", - "dependencies": { - "@acuminous/bitsyntax": "^0.1.2", - "buffer-more-ints": "~1.0.0", - "readable-stream": "1.x >=1.1.9", - "url-parse": "~1.5.10" - }, - "engines": { - "node": ">=10" - } - }, - "node_modules/@mobicoop/message-broker-module/node_modules/isarray": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", - "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==" - }, - "node_modules/@mobicoop/message-broker-module/node_modules/readable-stream": { - "version": "1.1.14", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", - "integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==", - "dependencies": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.1", - "isarray": "0.0.1", - "string_decoder": "~0.10.x" - } - }, - "node_modules/@mobicoop/message-broker-module/node_modules/string_decoder": { - "version": "0.10.31", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", - "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" - }, "node_modules/@nestjs/cli": { - "version": "9.4.2", - "resolved": "https://registry.npmjs.org/@nestjs/cli/-/cli-9.4.2.tgz", - "integrity": "sha512-QWpk3UkpcAIvlqh2sSc6atHyaNFl7POi45Ujd5sAtVNogzpGphOlSyh39XuJcpe0FP3Z9IxX/0AUHF7KL/VyJQ==", + "version": "9.5.0", + "resolved": "https://registry.npmjs.org/@nestjs/cli/-/cli-9.5.0.tgz", + "integrity": "sha512-Z7q+3vNsQSG2d2r2Hl/OOj5EpfjVx3OfnJ9+KuAsOdw1sKLm7+Zc6KbhMFTd/eIvfx82ww3Nk72xdmfPYCulWA==", "dev": true, "dependencies": { - "@angular-devkit/core": "15.2.6", - "@angular-devkit/schematics": "15.2.6", - "@angular-devkit/schematics-cli": "15.2.6", + "@angular-devkit/core": "16.0.1", + "@angular-devkit/schematics": "16.0.1", + "@angular-devkit/schematics-cli": "16.0.1", "@nestjs/schematics": "^9.0.4", "chalk": "4.1.2", "chokidar": "3.5.3", @@ -1693,7 +1592,7 @@ "tsconfig-paths": "4.2.0", "tsconfig-paths-webpack-plugin": "4.0.1", "typescript": "4.9.5", - "webpack": "5.80.0", + "webpack": "5.82.1", "webpack-node-externals": "3.0.0" }, "bin": { @@ -1704,9 +1603,9 @@ } }, "node_modules/@nestjs/cli/node_modules/webpack": { - "version": "5.80.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.80.0.tgz", - "integrity": "sha512-OIMiq37XK1rWO8mH9ssfFKZsXg4n6klTEDL7S8/HqbAOBBaiy8ABvXvz0dDCXeEF9gqwxSvVk611zFPjS8hJxA==", + "version": "5.82.1", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.82.1.tgz", + "integrity": "sha512-C6uiGQJ+Gt4RyHXXYt+v9f+SN1v83x68URwgxNQ98cvH8kxiuywWGP4XeNZ1paOzZ63aY3cTciCEQJNFUljlLw==", "dev": true, "dependencies": { "@types/eslint-scope": "^3.7.3", @@ -1718,7 +1617,7 @@ "acorn-import-assertions": "^1.7.6", "browserslist": "^4.14.5", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.13.0", + "enhanced-resolve": "^5.14.0", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", @@ -1788,9 +1687,9 @@ "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==" }, "node_modules/@nestjs/config": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.1.tgz", - "integrity": "sha512-Ckzel0NZ9CWhNsLfE1hxfDuxJuEbhQvGxSlmZ1/X8awjRmAA/g3kT6M1+MO1SHj1wMtPyUfd9WpwkiqFbiwQgA==", + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/@nestjs/config/-/config-2.3.2.tgz", + "integrity": "sha512-VtGV8PBpxzMzz68kdxTWqPm9v7SYCSZXQ0tC72AMNnjdmU+CVjUSLpEpdnm0XcWHxE1nV6wSI3HZxsATIV4ZxA==", "dependencies": { "dotenv": "16.0.3", "dotenv-expand": "10.0.0", @@ -1804,16 +1703,16 @@ } }, "node_modules/@nestjs/core": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.0.tgz", - "integrity": "sha512-yTLryCgFD0462wPe4HIzhyTcDgibt8Stfwb5YzcX7Ma0NM4m8uBIpcPG109KBubp8ZmV85e5mw4rl20qLQQVsQ==", + "version": "9.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/core/-/core-9.4.2.tgz", + "integrity": "sha512-S5K9GTpjBqEJtu5VxRsVaaGEBZ1bkY+Ht4+2hqZSKsI+rzcEB5hcvR+5KiMsMY1VGYvlZ99lxYz72p4h8B0mKw==", "hasInstallScript": true, "dependencies": { "@nuxtjs/opencollective": "0.3.2", "fast-safe-stringify": "2.1.1", "iterare": "1.2.1", "path-to-regexp": "3.2.0", - "tslib": "2.5.0", + "tslib": "2.5.2", "uid": "2.0.2" }, "funding": { @@ -1840,6 +1739,11 @@ } } }, + "node_modules/@nestjs/core/node_modules/tslib": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz", + "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==" + }, "node_modules/@nestjs/cqrs": { "version": "9.0.4", "resolved": "https://registry.npmjs.org/@nestjs/cqrs/-/cqrs-9.0.4.tgz", @@ -1854,13 +1758,26 @@ "rxjs": "^7.2.0" } }, + "node_modules/@nestjs/event-emitter": { + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/event-emitter/-/event-emitter-1.4.2.tgz", + "integrity": "sha512-5mskPMS4KVH6LghC+NynfdmGiMCOOv9CdgVpuWGipLrJECv5KWc7vaW5o/9BYrcqPkN7Ted6CJ+O4AfsTiRlgw==", + "dependencies": { + "eventemitter2": "6.4.9" + }, + "peerDependencies": { + "@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0", + "@nestjs/core": "^7.0.0 || ^8.0.0 || ^9.0.0", + "reflect-metadata": "^0.1.12" + } + }, "node_modules/@nestjs/microservices": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/@nestjs/microservices/-/microservices-9.4.0.tgz", - "integrity": "sha512-3IlURTijN2whedrfnLbJ3QQ4giDU1SxXcepXxtUL1MMkZAJgw2gN7sTquOXVgy/Ci5OMPO+vOjVyadjFejrgKA==", + "version": "9.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/microservices/-/microservices-9.4.2.tgz", + "integrity": "sha512-06VlJJS+QyyF5rS6WnMi6POdlGoEHEyvaOuySUm7QKfRILVUOqxcB6ANsKNNv2VPmRSLab/pYTpaQiEGazzRYA==", "dependencies": { "iterare": "1.2.1", - "tslib": "2.5.0" + "tslib": "2.5.2" }, "funding": { "type": "opencollective", @@ -1911,16 +1828,21 @@ } } }, + "node_modules/@nestjs/microservices/node_modules/tslib": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz", + "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==" + }, "node_modules/@nestjs/platform-express": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-9.4.0.tgz", - "integrity": "sha512-PpnfghpNq7mwG43z3+pacHulsabUCBMla4nUikntXT525ORpZSDvh/nLi1HLfE4w5+FcINc8/RBOyYTeRVmiRQ==", + "version": "9.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/platform-express/-/platform-express-9.4.2.tgz", + "integrity": "sha512-FVSJmVH+kndcDjvUK7xUzE3AmtQwlcXKN2hwJolyyIS7WXs6Awyb78cJGr0w27ESKoVQeSKPVbom0sLJFG153A==", "dependencies": { "body-parser": "1.20.2", "cors": "2.8.5", "express": "4.18.2", "multer": "1.4.4-lts.1", - "tslib": "2.5.0" + "tslib": "2.5.2" }, "funding": { "type": "opencollective", @@ -1931,14 +1853,19 @@ "@nestjs/core": "^9.0.0" } }, + "node_modules/@nestjs/platform-express/node_modules/tslib": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz", + "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==" + }, "node_modules/@nestjs/schematics": { - "version": "9.1.0", - "resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-9.1.0.tgz", - "integrity": "sha512-/7CyMTnPJSK9/xD9CkCqwuHPOlHVlLC2RDnbdCJ7mIO07SdbBbY14msTqtYW9VRQtsjZPLh1GTChf7ryJUImwA==", + "version": "9.2.0", + "resolved": "https://registry.npmjs.org/@nestjs/schematics/-/schematics-9.2.0.tgz", + "integrity": "sha512-wHpNJDPzM6XtZUOB3gW0J6mkFCSJilzCM3XrHI1o0C8vZmFE1snbmkIXNyoi1eV0Nxh1BMymcgz5vIMJgQtTqw==", "dev": true, "dependencies": { - "@angular-devkit/core": "15.2.4", - "@angular-devkit/schematics": "15.2.4", + "@angular-devkit/core": "16.0.1", + "@angular-devkit/schematics": "16.0.1", "jsonc-parser": "3.2.0", "pluralize": "8.0.0" }, @@ -1946,68 +1873,6 @@ "typescript": ">=4.3.5" } }, - "node_modules/@nestjs/schematics/node_modules/@angular-devkit/core": { - "version": "15.2.4", - "resolved": "https://registry.npmjs.org/@angular-devkit/core/-/core-15.2.4.tgz", - "integrity": "sha512-yl+0j1bMwJLKShsyCXw77tbJG8Sd21+itisPLL2MgEpLNAO252kr9zG4TLlFRJyKVftm2l1h78KjqvM5nbOXNg==", - "dev": true, - "dependencies": { - "ajv": "8.12.0", - "ajv-formats": "2.1.1", - "jsonc-parser": "3.2.0", - "rxjs": "6.6.7", - "source-map": "0.7.4" - }, - "engines": { - "node": "^14.20.0 || ^16.13.0 || >=18.10.0", - "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", - "yarn": ">= 1.13.0" - }, - "peerDependencies": { - "chokidar": "^3.5.2" - }, - "peerDependenciesMeta": { - "chokidar": { - "optional": true - } - } - }, - "node_modules/@nestjs/schematics/node_modules/@angular-devkit/schematics": { - "version": "15.2.4", - "resolved": "https://registry.npmjs.org/@angular-devkit/schematics/-/schematics-15.2.4.tgz", - "integrity": "sha512-/W7/vvn59PAVLzhcvD4/N/E8RDhub8ny1A7I96LTRjC5o+yvVV16YJ4YJzolrRrIEN01KmLVQJ9A58VCaweMgw==", - "dev": true, - "dependencies": { - "@angular-devkit/core": "15.2.4", - "jsonc-parser": "3.2.0", - "magic-string": "0.29.0", - "ora": "5.4.1", - "rxjs": "6.6.7" - }, - "engines": { - "node": "^14.20.0 || ^16.13.0 || >=18.10.0", - "npm": "^6.11.0 || ^7.5.6 || >=8.0.0", - "yarn": ">= 1.13.0" - } - }, - "node_modules/@nestjs/schematics/node_modules/rxjs": { - "version": "6.6.7", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.7.tgz", - "integrity": "sha512-hTdwr+7yYNIT5n4AMYp85KA6yw2Va0FLa3Rguvbpa4W3I5xynaBZo41cM3XM+4Q6fRMj3sBYIR1VAmZMXYJvRQ==", - "dev": true, - "dependencies": { - "tslib": "^1.9.0" - }, - "engines": { - "npm": ">=2.0.0" - } - }, - "node_modules/@nestjs/schematics/node_modules/tslib": { - "version": "1.14.1", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", - "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==", - "dev": true - }, "node_modules/@nestjs/terminus": { "version": "9.2.2", "resolved": "https://registry.npmjs.org/@nestjs/terminus/-/terminus-9.2.2.tgz", @@ -2074,12 +1939,12 @@ } }, "node_modules/@nestjs/testing": { - "version": "9.4.0", - "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-9.4.0.tgz", - "integrity": "sha512-xZWp363P4otcebg++gSjUcdCfTK0RorORzyFq3aLaSAQOlq8kxfFDRIKzEATR4aOUfqTMMsAA8lhnMJWf35N6A==", + "version": "9.4.2", + "resolved": "https://registry.npmjs.org/@nestjs/testing/-/testing-9.4.2.tgz", + "integrity": "sha512-4WZPJz85zLVZkhmWYq+Unr43MixISelg/TyuX1YFZYOeukIN+O6fRtAAPIKLqRQsiY0rE/h8FAEbYGWhNrRfSA==", "dev": true, "dependencies": { - "tslib": "2.5.0" + "tslib": "2.5.2" }, "funding": { "type": "opencollective", @@ -2100,6 +1965,12 @@ } } }, + "node_modules/@nestjs/testing/node_modules/tslib": { + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.2.tgz", + "integrity": "sha512-5svOrSA2w3iGFDs1HibEVBGbDrAY82bFQ3HZ3ixB+88nsbsWQoKqDRb5UBYAUPEzbBn6dAp5gRNXglySbx1MlA==", + "dev": true + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -2153,12 +2024,12 @@ } }, "node_modules/@prisma/client": { - "version": "4.13.0", - "resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.13.0.tgz", - "integrity": "sha512-YaiiICcRB2hatxsbnfB66uWXjcRw3jsZdlAVxmx0cFcTc/Ad/sKdHCcWSnqyDX47vAewkjRFwiLwrOUjswVvmA==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.15.0.tgz", + "integrity": "sha512-xnROvyABcGiwqRNdrObHVZkD9EjkJYHOmVdlKy1yGgI+XOzvMzJ4tRg3dz1pUlsyhKxXGCnjIQjWW+2ur+YXuw==", "hasInstallScript": true, "dependencies": { - "@prisma/engines-version": "4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a" + "@prisma/engines-version": "4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944" }, "engines": { "node": ">=14.17" @@ -2173,16 +2044,16 @@ } }, "node_modules/@prisma/engines": { - "version": "4.13.0", - "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.13.0.tgz", - "integrity": "sha512-HrniowHRZXHuGT9XRgoXEaP2gJLXM5RMoItaY2PkjvuZ+iHc0Zjbm/302MB8YsPdWozAPHHn+jpFEcEn71OgPw==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.15.0.tgz", + "integrity": "sha512-FTaOCGs0LL0OW68juZlGxFtYviZa4xdQj/rQEdat2txw0s3Vu/saAPKjNVXfIgUsGXmQ72HPgNr6935/P8FNAA==", "devOptional": true, "hasInstallScript": true }, "node_modules/@prisma/engines-version": { - "version": "4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a", - "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.13.0-50.1e7af066ee9cb95cf3a403c78d9aab3e6b04f37a.tgz", - "integrity": "sha512-fsQlbkhPJf08JOzKoyoD9atdUijuGBekwoOPZC3YOygXEml1MTtgXVpnUNchQlRSY82OQ6pSGQ9PxUe4arcSLQ==" + "version": "4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944", + "resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944.tgz", + "integrity": "sha512-sVOig4tjGxxlYaFcXgE71f/rtFhzyYrfyfNFUsxCIEJyVKU9rdOWIlIwQ2NQ7PntvGnn+x0XuFo4OC1jvPJKzg==" }, "node_modules/@protobufjs/aspromise": { "version": "1.1.2", @@ -2245,21 +2116,21 @@ "dev": true }, "node_modules/@sinonjs/commons": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", - "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.0.tgz", + "integrity": "sha512-jXBtWAF4vmdNmZgD5FoKsVLv3rPgDnLgPbU84LIJ3otV44vJlDRokVng5v8NFJdCf/da9legHcKaRuZs4L7faA==", "dev": true, "dependencies": { "type-detect": "4.0.8" } }, "node_modules/@sinonjs/fake-timers": { - "version": "10.0.2", - "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-10.0.2.tgz", - "integrity": "sha512-SwUDyjWnah1AaNl7kxsa7cfLhlTYoiyhDAIgyh+El30YvXs/o7OLXpYH88Zdhyx9JExKrmHDJ+10bwIcY80Jmw==", + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-10.2.0.tgz", + "integrity": "sha512-OPwQlEdg40HAj5KNF8WW6q2KG4Z+cBCZb3m4ninfTZKaBmbIJodviQsDBoYMPHkOyJJMHnOJo5j2+LKDOhOACg==", "dev": true, "dependencies": { - "@sinonjs/commons": "^2.0.0" + "@sinonjs/commons": "^3.0.0" } }, "node_modules/@tsconfig/node10": { @@ -2281,11 +2152,42 @@ "dev": true }, "node_modules/@tsconfig/node16": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.3.tgz", - "integrity": "sha512-yOlFc+7UtL/89t2ZhjPvvB/DeAr3r+Dq58IgzsFkOAvVC6NMJXmCGjbptdXdR9qsX7pKcTL+s87FtYREi2dEEQ==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==", "dev": true }, + "node_modules/@turf/boolean-point-in-polygon": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/boolean-point-in-polygon/-/boolean-point-in-polygon-6.5.0.tgz", + "integrity": "sha512-DtSuVFB26SI+hj0SjrvXowGTUCHlgevPAIsukssW6BG5MlNSBQAo70wpICBNJL6RjukXg8d2eXaAWuD/CqL00A==", + "dependencies": { + "@turf/helpers": "^6.5.0", + "@turf/invariant": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/helpers": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/helpers/-/helpers-6.5.0.tgz", + "integrity": "sha512-VbI1dV5bLFzohYYdgqwikdMVpe7pJ9X3E+dlr425wa2/sMJqYDhTO++ec38/pcPvPE6oD9WEEeU3Xu3gza+VPw==", + "funding": { + "url": "https://opencollective.com/turf" + } + }, + "node_modules/@turf/invariant": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/@turf/invariant/-/invariant-6.5.0.tgz", + "integrity": "sha512-Wv8PRNCtPD31UVbdJE/KVAWKe7l6US+lJItRR/HOEW3eh+U/JwRCSUl/KZ7bmjM/C+zLNoreM2TU6OoLACs4eg==", + "dependencies": { + "@turf/helpers": "^6.5.0" + }, + "funding": { + "url": "https://opencollective.com/turf" + } + }, "node_modules/@types/amqplib": { "version": "0.10.1", "resolved": "https://registry.npmjs.org/@types/amqplib/-/amqplib-0.10.1.tgz", @@ -2295,9 +2197,9 @@ } }, "node_modules/@types/babel__core": { - "version": "7.20.0", - "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.0.tgz", - "integrity": "sha512-+n8dL/9GWblDO0iU6eZAwEIJVr5DWigtle+Q6HLOrh/pdbXOhOtqzq8VPPE2zvNJzSKY4vH/z3iT3tn0A3ypiQ==", + "version": "7.20.1", + "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.1.tgz", + "integrity": "sha512-aACu/U/omhdk15O4Nfb+fHgH/z3QsfQzpnvRZhYhThms83ZnAOZz7zZAWO7mn2yyNQaA4xTO8GLK3uqFU4bYYw==", "dev": true, "dependencies": { "@babel/parser": "^7.20.7", @@ -2327,12 +2229,12 @@ } }, "node_modules/@types/babel__traverse": { - "version": "7.18.5", - "resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.18.5.tgz", - "integrity": "sha512-enCvTL8m/EHS/zIvJno9nE+ndYPh1/oNFzRYRmtUqJICG2VnCSBzMLW5VN2KCQU91f23tsNKR8v7VJJQMatl7Q==", + "version": "7.20.1", + "resolved": "https://registry.npmjs.org/@types/babel__traverse/-/babel__traverse-7.20.1.tgz", + "integrity": "sha512-MitHFXnhtgwsGZWtT68URpOvLN4EREih1u3QtQiN4VdAxWKRVvGCSvw/Qth0M0Qq3pJpnGOu5JaM/ydK7OGbqg==", "dev": true, "dependencies": { - "@babel/types": "^7.3.0" + "@babel/types": "^7.20.7" } }, "node_modules/@types/body-parser": { @@ -2361,9 +2263,9 @@ "dev": true }, "node_modules/@types/eslint": { - "version": "8.37.0", - "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.37.0.tgz", - "integrity": "sha512-Piet7dG2JBuDIfohBngQ3rCt7MgO9xCO4xIMKxBThCq5PNRB91IjlJ10eJVwfoNtvTErmxLzwBZ7rHZtbOMmFQ==", + "version": "8.40.0", + "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.40.0.tgz", + "integrity": "sha512-nbq2mvc/tBrK9zQQuItvjJl++GTN5j06DaPtp3hZCpngmG6Q3xoyEmd0TwZI0gAy/G1X0zhGBbr2imsGFdFV0g==", "dev": true, "dependencies": { "@types/estree": "*", @@ -2399,9 +2301,9 @@ } }, "node_modules/@types/express-serve-static-core": { - "version": "4.17.34", - "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.34.tgz", - "integrity": "sha512-fvr49XlCGoUj2Pp730AItckfjat4WNb0lb3kfrLWffd+RLeoGAMsq7UOy04PAPtoL01uKwcp6u8nhzpgpDYr3w==", + "version": "4.17.35", + "resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.17.35.tgz", + "integrity": "sha512-wALWQwrgiB2AWTT91CB62b6Yt0sNHpznUXeZEcnPU3DRdlDIz74x8Qg1UUYKSVFi+va5vKOLYRBI1bRKiLLKIg==", "dev": true, "dependencies": { "@types/node": "*", @@ -2454,9 +2356,9 @@ } }, "node_modules/@types/json-schema": { - "version": "7.0.11", - "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz", - "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", + "version": "7.0.12", + "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz", + "integrity": "sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==", "dev": true }, "node_modules/@types/long": { @@ -2482,9 +2384,9 @@ "dev": true }, "node_modules/@types/prettier": { - "version": "2.7.2", - "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.7.2.tgz", - "integrity": "sha512-KufADq8uQqo1pYKVIYzfKbJfBAc0sOeXqGbFaSpv8MRmC/zXgowNZmFcbngndGk922QDmOASEXUZCaY48gs4cg==", + "version": "2.7.3", + "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.7.3.tgz", + "integrity": "sha512-+68kP9yzs4LMp7VNh8gdzMSPZFL44MLGqiHWvttYJe+6qnuVr4Ek9wSBQoveqY/r+LwjCcU29kNVkidwim+kYA==", "dev": true }, "node_modules/@types/qs": { @@ -2500,9 +2402,9 @@ "dev": true }, "node_modules/@types/semver": { - "version": "7.3.13", - "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.3.13.tgz", - "integrity": "sha512-21cFJr9z3g5dW8B0CVI9g2O9beqaThGQ6ZFBqHfwhzLDKUxaqTIy3vnfah/UPkfOiF2pLq+tGz+W8RyCskuslw==", + "version": "7.5.0", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.0.tgz", + "integrity": "sha512-G8hZ6XJiHnuhQKR7ZmysCeJWE08o8T0AXtk5darsCaTVsYZhhgUrq53jizaR2FvsoeCwJhlmwTjkXBY5Pn/ZHw==", "dev": true }, "node_modules/@types/send": { @@ -2532,9 +2434,9 @@ "dev": true }, "node_modules/@types/superagent": { - "version": "4.1.17", - "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-4.1.17.tgz", - "integrity": "sha512-FFK/rRjNy24U6J1BvQkaNWu2ohOIF/kxRQXRsbT141YQODcOcZjzlcc4DGdI2SkTa0rhmF+X14zu6ICjCGIg+w==", + "version": "4.1.18", + "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-4.1.18.tgz", + "integrity": "sha512-LOWgpacIV8GHhrsQU+QMZuomfqXiqzz3ILLkCtKx3Us6AmomFViuzKT9D693QTKgyut2oCytMG8/efOop+DB+w==", "dev": true, "dependencies": { "@types/cookiejar": "*", @@ -2550,10 +2452,16 @@ "@types/superagent": "*" } }, + "node_modules/@types/uuid": { + "version": "9.0.2", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.2.tgz", + "integrity": "sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ==", + "dev": true + }, "node_modules/@types/validator": { - "version": "13.7.15", - "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.7.15.tgz", - "integrity": "sha512-yeinDVQunb03AEP8luErFcyf/7Lf7AzKCD0NXfgVoGCCQDNpZET8Jgq74oBgqKld3hafLbfzt/3inUdQvaFeXQ==" + "version": "13.7.17", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.7.17.tgz", + "integrity": "sha512-aqayTNmeWrZcvnG2MG9eGYI6b7S5fl+yKgPs6bAjOTwPS316R5SxBGKvtSExfyoJU7pIeHJfsHI0Ji41RVMkvQ==" }, "node_modules/@types/yargs": { "version": "17.0.24", @@ -2571,15 +2479,15 @@ "dev": true }, "node_modules/@typescript-eslint/eslint-plugin": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.59.2.tgz", - "integrity": "sha512-yVrXupeHjRxLDcPKL10sGQ/QlVrA8J5IYOEWVqk0lJaSZP7X5DfnP7Ns3cc74/blmbipQ1htFNVGsHX6wsYm0A==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.59.9.tgz", + "integrity": "sha512-4uQIBq1ffXd2YvF7MAvehWKW3zVv/w+mSfRAu+8cKbfj3nwzyqJLNcZJpQ/WZ1HLbJDiowwmQ6NO+63nCA+fqA==", "dev": true, "dependencies": { "@eslint-community/regexpp": "^4.4.0", - "@typescript-eslint/scope-manager": "5.59.2", - "@typescript-eslint/type-utils": "5.59.2", - "@typescript-eslint/utils": "5.59.2", + "@typescript-eslint/scope-manager": "5.59.9", + "@typescript-eslint/type-utils": "5.59.9", + "@typescript-eslint/utils": "5.59.9", "debug": "^4.3.4", "grapheme-splitter": "^1.0.4", "ignore": "^5.2.0", @@ -2605,14 +2513,14 @@ } }, "node_modules/@typescript-eslint/parser": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.59.2.tgz", - "integrity": "sha512-uq0sKyw6ao1iFOZZGk9F8Nro/8+gfB5ezl1cA06SrqbgJAt0SRoFhb9pXaHvkrxUpZaoLxt8KlovHNk8Gp6/HQ==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.59.9.tgz", + "integrity": "sha512-FsPkRvBtcLQ/eVK1ivDiNYBjn3TGJdXy2fhXX+rc7czWl4ARwnpArwbihSOHI2Peg9WbtGHrbThfBUkZZGTtvQ==", "dev": true, "dependencies": { - "@typescript-eslint/scope-manager": "5.59.2", - "@typescript-eslint/types": "5.59.2", - "@typescript-eslint/typescript-estree": "5.59.2", + "@typescript-eslint/scope-manager": "5.59.9", + "@typescript-eslint/types": "5.59.9", + "@typescript-eslint/typescript-estree": "5.59.9", "debug": "^4.3.4" }, "engines": { @@ -2632,13 +2540,13 @@ } }, "node_modules/@typescript-eslint/scope-manager": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.59.2.tgz", - "integrity": "sha512-dB1v7ROySwQWKqQ8rEWcdbTsFjh2G0vn8KUyvTXdPoyzSL6lLGkiXEV5CvpJsEe9xIdKV+8Zqb7wif2issoOFA==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.59.9.tgz", + "integrity": "sha512-8RA+E+w78z1+2dzvK/tGZ2cpGigBZ58VMEHDZtpE1v+LLjzrYGc8mMaTONSxKyEkz3IuXFM0IqYiGHlCsmlZxQ==", "dev": true, "dependencies": { - "@typescript-eslint/types": "5.59.2", - "@typescript-eslint/visitor-keys": "5.59.2" + "@typescript-eslint/types": "5.59.9", + "@typescript-eslint/visitor-keys": "5.59.9" }, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -2649,13 +2557,13 @@ } }, "node_modules/@typescript-eslint/type-utils": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.59.2.tgz", - "integrity": "sha512-b1LS2phBOsEy/T381bxkkywfQXkV1dWda/z0PhnIy3bC5+rQWQDS7fk9CSpcXBccPY27Z6vBEuaPBCKCgYezyQ==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-5.59.9.tgz", + "integrity": "sha512-ksEsT0/mEHg9e3qZu98AlSrONAQtrSTljL3ow9CGej8eRo7pe+yaC/mvTjptp23Xo/xIf2mLZKC6KPv4Sji26Q==", "dev": true, "dependencies": { - "@typescript-eslint/typescript-estree": "5.59.2", - "@typescript-eslint/utils": "5.59.2", + "@typescript-eslint/typescript-estree": "5.59.9", + "@typescript-eslint/utils": "5.59.9", "debug": "^4.3.4", "tsutils": "^3.21.0" }, @@ -2676,9 +2584,9 @@ } }, "node_modules/@typescript-eslint/types": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.59.2.tgz", - "integrity": "sha512-LbJ/HqoVs2XTGq5shkiKaNTuVv5tTejdHgfdjqRUGdYhjW1crm/M7og2jhVskMt8/4wS3T1+PfFvL1K3wqYj4w==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.59.9.tgz", + "integrity": "sha512-uW8H5NRgTVneSVTfiCVffBb8AbwWSKg7qcA4Ot3JI3MPCJGsB4Db4BhvAODIIYE5mNj7Q+VJkK7JxmRhk2Lyjw==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -2689,13 +2597,13 @@ } }, "node_modules/@typescript-eslint/typescript-estree": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.59.2.tgz", - "integrity": "sha512-+j4SmbwVmZsQ9jEyBMgpuBD0rKwi9RxRpjX71Brr73RsYnEr3Lt5QZ624Bxphp8HUkSKfqGnPJp1kA5nl0Sh7Q==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.59.9.tgz", + "integrity": "sha512-pmM0/VQ7kUhd1QyIxgS+aRvMgw+ZljB3eDb+jYyp6d2bC0mQWLzUDF+DLwCTkQ3tlNyVsvZRXjFyV0LkU/aXjA==", "dev": true, "dependencies": { - "@typescript-eslint/types": "5.59.2", - "@typescript-eslint/visitor-keys": "5.59.2", + "@typescript-eslint/types": "5.59.9", + "@typescript-eslint/visitor-keys": "5.59.9", "debug": "^4.3.4", "globby": "^11.1.0", "is-glob": "^4.0.3", @@ -2716,17 +2624,17 @@ } }, "node_modules/@typescript-eslint/utils": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.59.2.tgz", - "integrity": "sha512-kSuF6/77TZzyGPhGO4uVp+f0SBoYxCDf+lW3GKhtKru/L8k/Hd7NFQxyWUeY7Z/KGB2C6Fe3yf2vVi4V9TsCSQ==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-5.59.9.tgz", + "integrity": "sha512-1PuMYsju/38I5Ggblaeb98TOoUvjhRvLpLa1DoTOFaLWqaXl/1iQ1eGurTXgBY58NUdtfTXKP5xBq7q9NDaLKg==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@types/json-schema": "^7.0.9", "@types/semver": "^7.3.12", - "@typescript-eslint/scope-manager": "5.59.2", - "@typescript-eslint/types": "5.59.2", - "@typescript-eslint/typescript-estree": "5.59.2", + "@typescript-eslint/scope-manager": "5.59.9", + "@typescript-eslint/types": "5.59.9", + "@typescript-eslint/typescript-estree": "5.59.9", "eslint-scope": "^5.1.1", "semver": "^7.3.7" }, @@ -2742,12 +2650,12 @@ } }, "node_modules/@typescript-eslint/visitor-keys": { - "version": "5.59.2", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.59.2.tgz", - "integrity": "sha512-EEpsO8m3RASrKAHI9jpavNv9NlEUebV4qmF1OWxSTtKSFBpC1NCmWazDQHFivRf0O1DV11BA645yrLEVQ0/Lig==", + "version": "5.59.9", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.59.9.tgz", + "integrity": "sha512-bT7s0td97KMaLwpEBckbzj/YohnvXtqbe2XgqNvTl6RJVakY5mvENOTPvw5u66nljfZxthESpDozs86U+oLY8Q==", "dev": true, "dependencies": { - "@typescript-eslint/types": "5.59.2", + "@typescript-eslint/types": "5.59.9", "eslint-visitor-keys": "^3.3.0" }, "engines": { @@ -2759,148 +2667,148 @@ } }, "node_modules/@webassemblyjs/ast": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.11.5.tgz", - "integrity": "sha512-LHY/GSAZZRpsNQH+/oHqhRQ5FT7eoULcBqgfyTB5nQHogFnK3/7QoN7dLnwSE/JkUAF0SrRuclT7ODqMFtWxxQ==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/ast/-/ast-1.11.6.tgz", + "integrity": "sha512-IN1xI7PwOvLPgjcf180gC1bqn3q/QaOCwYUahIOhbYUu8KA/3tw2RT/T0Gidi1l7Hhj5D/INhJxiICObqpMu4Q==", "dev": true, "dependencies": { - "@webassemblyjs/helper-numbers": "1.11.5", - "@webassemblyjs/helper-wasm-bytecode": "1.11.5" + "@webassemblyjs/helper-numbers": "1.11.6", + "@webassemblyjs/helper-wasm-bytecode": "1.11.6" } }, "node_modules/@webassemblyjs/floating-point-hex-parser": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/floating-point-hex-parser/-/floating-point-hex-parser-1.11.5.tgz", - "integrity": "sha512-1j1zTIC5EZOtCplMBG/IEwLtUojtwFVwdyVMbL/hwWqbzlQoJsWCOavrdnLkemwNoC/EOwtUFch3fuo+cbcXYQ==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/floating-point-hex-parser/-/floating-point-hex-parser-1.11.6.tgz", + "integrity": "sha512-ejAj9hfRJ2XMsNHk/v6Fu2dGS+i4UaXBXGemOfQ/JfQ6mdQg/WXtwleQRLLS4OvfDhv8rYnVwH27YJLMyYsxhw==", "dev": true }, "node_modules/@webassemblyjs/helper-api-error": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-api-error/-/helper-api-error-1.11.5.tgz", - "integrity": "sha512-L65bDPmfpY0+yFrsgz8b6LhXmbbs38OnwDCf6NpnMUYqa+ENfE5Dq9E42ny0qz/PdR0LJyq/T5YijPnU8AXEpA==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-api-error/-/helper-api-error-1.11.6.tgz", + "integrity": "sha512-o0YkoP4pVu4rN8aTJgAyj9hC2Sv5UlkzCHhxqWj8butaLvnpdc2jOwh4ewE6CX0txSfLn/UYaV/pheS2Txg//Q==", "dev": true }, "node_modules/@webassemblyjs/helper-buffer": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-buffer/-/helper-buffer-1.11.5.tgz", - "integrity": "sha512-fDKo1gstwFFSfacIeH5KfwzjykIE6ldh1iH9Y/8YkAZrhmu4TctqYjSh7t0K2VyDSXOZJ1MLhht/k9IvYGcIxg==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-buffer/-/helper-buffer-1.11.6.tgz", + "integrity": "sha512-z3nFzdcp1mb8nEOFFk8DrYLpHvhKC3grJD2ardfKOzmbmJvEf/tPIqCY+sNcwZIY8ZD7IkB2l7/pqhUhqm7hLA==", "dev": true }, "node_modules/@webassemblyjs/helper-numbers": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-numbers/-/helper-numbers-1.11.5.tgz", - "integrity": "sha512-DhykHXM0ZABqfIGYNv93A5KKDw/+ywBFnuWybZZWcuzWHfbp21wUfRkbtz7dMGwGgT4iXjWuhRMA2Mzod6W4WA==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-numbers/-/helper-numbers-1.11.6.tgz", + "integrity": "sha512-vUIhZ8LZoIWHBohiEObxVm6hwP034jwmc9kuq5GdHZH0wiLVLIPcMCdpJzG4C11cHoQ25TFIQj9kaVADVX7N3g==", "dev": true, "dependencies": { - "@webassemblyjs/floating-point-hex-parser": "1.11.5", - "@webassemblyjs/helper-api-error": "1.11.5", + "@webassemblyjs/floating-point-hex-parser": "1.11.6", + "@webassemblyjs/helper-api-error": "1.11.6", "@xtuc/long": "4.2.2" } }, "node_modules/@webassemblyjs/helper-wasm-bytecode": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-wasm-bytecode/-/helper-wasm-bytecode-1.11.5.tgz", - "integrity": "sha512-oC4Qa0bNcqnjAowFn7MPCETQgDYytpsfvz4ujZz63Zu/a/v71HeCAAmZsgZ3YVKec3zSPYytG3/PrRCqbtcAvA==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-wasm-bytecode/-/helper-wasm-bytecode-1.11.6.tgz", + "integrity": "sha512-sFFHKwcmBprO9e7Icf0+gddyWYDViL8bpPjJJl0WHxCdETktXdmtWLGVzoHbqUcY4Be1LkNfwTmXOJUFZYSJdA==", "dev": true }, "node_modules/@webassemblyjs/helper-wasm-section": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-wasm-section/-/helper-wasm-section-1.11.5.tgz", - "integrity": "sha512-uEoThA1LN2NA+K3B9wDo3yKlBfVtC6rh0i4/6hvbz071E8gTNZD/pT0MsBf7MeD6KbApMSkaAK0XeKyOZC7CIA==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/helper-wasm-section/-/helper-wasm-section-1.11.6.tgz", + "integrity": "sha512-LPpZbSOwTpEC2cgn4hTydySy1Ke+XEu+ETXuoyvuyezHO3Kjdu90KK95Sh9xTbmjrCsUwvWwCOQQNta37VrS9g==", "dev": true, "dependencies": { - "@webassemblyjs/ast": "1.11.5", - "@webassemblyjs/helper-buffer": "1.11.5", - "@webassemblyjs/helper-wasm-bytecode": "1.11.5", - "@webassemblyjs/wasm-gen": "1.11.5" + "@webassemblyjs/ast": "1.11.6", + "@webassemblyjs/helper-buffer": "1.11.6", + "@webassemblyjs/helper-wasm-bytecode": "1.11.6", + "@webassemblyjs/wasm-gen": "1.11.6" } }, "node_modules/@webassemblyjs/ieee754": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/ieee754/-/ieee754-1.11.5.tgz", - "integrity": "sha512-37aGq6qVL8A8oPbPrSGMBcp38YZFXcHfiROflJn9jxSdSMMM5dS5P/9e2/TpaJuhE+wFrbukN2WI6Hw9MH5acg==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/ieee754/-/ieee754-1.11.6.tgz", + "integrity": "sha512-LM4p2csPNvbij6U1f19v6WR56QZ8JcHg3QIJTlSwzFcmx6WSORicYj6I63f9yU1kEUtrpG+kjkiIAkevHpDXrg==", "dev": true, "dependencies": { "@xtuc/ieee754": "^1.2.0" } }, "node_modules/@webassemblyjs/leb128": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/leb128/-/leb128-1.11.5.tgz", - "integrity": "sha512-ajqrRSXaTJoPW+xmkfYN6l8VIeNnR4vBOTQO9HzR7IygoCcKWkICbKFbVTNMjMgMREqXEr0+2M6zukzM47ZUfQ==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/leb128/-/leb128-1.11.6.tgz", + "integrity": "sha512-m7a0FhE67DQXgouf1tbN5XQcdWoNgaAuoULHIfGFIEVKA6tu/edls6XnIlkmS6FrXAquJRPni3ZZKjw6FSPjPQ==", "dev": true, "dependencies": { "@xtuc/long": "4.2.2" } }, "node_modules/@webassemblyjs/utf8": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/utf8/-/utf8-1.11.5.tgz", - "integrity": "sha512-WiOhulHKTZU5UPlRl53gHR8OxdGsSOxqfpqWeA2FmcwBMaoEdz6b2x2si3IwC9/fSPLfe8pBMRTHVMk5nlwnFQ==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/utf8/-/utf8-1.11.6.tgz", + "integrity": "sha512-vtXf2wTQ3+up9Zsg8sa2yWiQpzSsMyXj0qViVP6xKGCUT8p8YJ6HqI7l5eCnWx1T/FYdsv07HQs2wTFbbof/RA==", "dev": true }, "node_modules/@webassemblyjs/wasm-edit": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-edit/-/wasm-edit-1.11.5.tgz", - "integrity": "sha512-C0p9D2fAu3Twwqvygvf42iGCQ4av8MFBLiTb+08SZ4cEdwzWx9QeAHDo1E2k+9s/0w1DM40oflJOpkZ8jW4HCQ==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-edit/-/wasm-edit-1.11.6.tgz", + "integrity": "sha512-Ybn2I6fnfIGuCR+Faaz7YcvtBKxvoLV3Lebn1tM4o/IAJzmi9AWYIPWpyBfU8cC+JxAO57bk4+zdsTjJR+VTOw==", "dev": true, "dependencies": { - "@webassemblyjs/ast": "1.11.5", - "@webassemblyjs/helper-buffer": "1.11.5", - "@webassemblyjs/helper-wasm-bytecode": "1.11.5", - "@webassemblyjs/helper-wasm-section": "1.11.5", - "@webassemblyjs/wasm-gen": "1.11.5", - "@webassemblyjs/wasm-opt": "1.11.5", - "@webassemblyjs/wasm-parser": "1.11.5", - "@webassemblyjs/wast-printer": "1.11.5" + "@webassemblyjs/ast": "1.11.6", + "@webassemblyjs/helper-buffer": "1.11.6", + "@webassemblyjs/helper-wasm-bytecode": "1.11.6", + "@webassemblyjs/helper-wasm-section": "1.11.6", + "@webassemblyjs/wasm-gen": "1.11.6", + "@webassemblyjs/wasm-opt": "1.11.6", + "@webassemblyjs/wasm-parser": "1.11.6", + "@webassemblyjs/wast-printer": "1.11.6" } }, "node_modules/@webassemblyjs/wasm-gen": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-gen/-/wasm-gen-1.11.5.tgz", - "integrity": "sha512-14vteRlRjxLK9eSyYFvw1K8Vv+iPdZU0Aebk3j6oB8TQiQYuO6hj9s4d7qf6f2HJr2khzvNldAFG13CgdkAIfA==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-gen/-/wasm-gen-1.11.6.tgz", + "integrity": "sha512-3XOqkZP/y6B4F0PBAXvI1/bky7GryoogUtfwExeP/v7Nzwo1QLcq5oQmpKlftZLbT+ERUOAZVQjuNVak6UXjPA==", "dev": true, "dependencies": { - "@webassemblyjs/ast": "1.11.5", - "@webassemblyjs/helper-wasm-bytecode": "1.11.5", - "@webassemblyjs/ieee754": "1.11.5", - "@webassemblyjs/leb128": "1.11.5", - "@webassemblyjs/utf8": "1.11.5" + "@webassemblyjs/ast": "1.11.6", + "@webassemblyjs/helper-wasm-bytecode": "1.11.6", + "@webassemblyjs/ieee754": "1.11.6", + "@webassemblyjs/leb128": "1.11.6", + "@webassemblyjs/utf8": "1.11.6" } }, "node_modules/@webassemblyjs/wasm-opt": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-opt/-/wasm-opt-1.11.5.tgz", - "integrity": "sha512-tcKwlIXstBQgbKy1MlbDMlXaxpucn42eb17H29rawYLxm5+MsEmgPzeCP8B1Cl69hCice8LeKgZpRUAPtqYPgw==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-opt/-/wasm-opt-1.11.6.tgz", + "integrity": "sha512-cOrKuLRE7PCe6AsOVl7WasYf3wbSo4CeOk6PkrjS7g57MFfVUF9u6ysQBBODX0LdgSvQqRiGz3CXvIDKcPNy4g==", "dev": true, "dependencies": { - "@webassemblyjs/ast": "1.11.5", - "@webassemblyjs/helper-buffer": "1.11.5", - "@webassemblyjs/wasm-gen": "1.11.5", - "@webassemblyjs/wasm-parser": "1.11.5" + "@webassemblyjs/ast": "1.11.6", + "@webassemblyjs/helper-buffer": "1.11.6", + "@webassemblyjs/wasm-gen": "1.11.6", + "@webassemblyjs/wasm-parser": "1.11.6" } }, "node_modules/@webassemblyjs/wasm-parser": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-parser/-/wasm-parser-1.11.5.tgz", - "integrity": "sha512-SVXUIwsLQlc8srSD7jejsfTU83g7pIGr2YYNb9oHdtldSxaOhvA5xwvIiWIfcX8PlSakgqMXsLpLfbbJ4cBYew==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/wasm-parser/-/wasm-parser-1.11.6.tgz", + "integrity": "sha512-6ZwPeGzMJM3Dqp3hCsLgESxBGtT/OeCvCZ4TA1JUPYgmhAx38tTPR9JaKy0S5H3evQpO/h2uWs2j6Yc/fjkpTQ==", "dev": true, "dependencies": { - "@webassemblyjs/ast": "1.11.5", - "@webassemblyjs/helper-api-error": "1.11.5", - "@webassemblyjs/helper-wasm-bytecode": "1.11.5", - "@webassemblyjs/ieee754": "1.11.5", - "@webassemblyjs/leb128": "1.11.5", - "@webassemblyjs/utf8": "1.11.5" + "@webassemblyjs/ast": "1.11.6", + "@webassemblyjs/helper-api-error": "1.11.6", + "@webassemblyjs/helper-wasm-bytecode": "1.11.6", + "@webassemblyjs/ieee754": "1.11.6", + "@webassemblyjs/leb128": "1.11.6", + "@webassemblyjs/utf8": "1.11.6" } }, "node_modules/@webassemblyjs/wast-printer": { - "version": "1.11.5", - "resolved": "https://registry.npmjs.org/@webassemblyjs/wast-printer/-/wast-printer-1.11.5.tgz", - "integrity": "sha512-f7Pq3wvg3GSPUPzR0F6bmI89Hdb+u9WXrSKc4v+N0aV0q6r42WoF92Jp2jEorBEBRoRNXgjp53nBniDXcqZYPA==", + "version": "1.11.6", + "resolved": "https://registry.npmjs.org/@webassemblyjs/wast-printer/-/wast-printer-1.11.6.tgz", + "integrity": "sha512-JM7AhRcE+yW2GWYaKeHL5vt4xqee5N2WcezptmgyhNS+ScggqcT1OtXykhAb13Sn5Yas0j2uv9tHgrjwvzAP4A==", "dev": true, "dependencies": { - "@webassemblyjs/ast": "1.11.5", + "@webassemblyjs/ast": "1.11.6", "@xtuc/long": "4.2.2" } }, @@ -2941,9 +2849,9 @@ } }, "node_modules/acorn-import-assertions": { - "version": "1.8.0", - "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.8.0.tgz", - "integrity": "sha512-m7VZ3jwz4eK6A4Vtt8Ew1/mNbP24u0FhdyfA7fSvnJR6LMdfOYnmuIrrJAgrYfYJ10F/otaHTtrtrtmHdMNzEw==", + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.9.0.tgz", + "integrity": "sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==", "dev": true, "peerDependencies": { "acorn": "^8" @@ -3016,42 +2924,19 @@ } }, "node_modules/amqplib": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.8.0.tgz", - "integrity": "sha512-icU+a4kkq4Y1PS4NNi+YPDMwdlbFcZ1EZTQT2nigW3fvOb6AOgUQ9+Mk4ue0Zu5cBg/XpDzB40oH10ysrk2dmA==", + "version": "0.10.3", + "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.10.3.tgz", + "integrity": "sha512-UHmuSa7n8vVW/a5HGh2nFPqAEr8+cD4dEZ6u9GjP91nHfr1a54RyAKyra7Sb5NH7NBKOUlyQSMXIp0qAixKexw==", "dependencies": { - "bitsyntax": "~0.1.0", - "bluebird": "^3.7.2", + "@acuminous/bitsyntax": "^0.1.2", "buffer-more-ints": "~1.0.0", "readable-stream": "1.x >=1.1.9", - "safe-buffer": "~5.2.1", - "url-parse": "~1.5.1" + "url-parse": "~1.5.10" }, "engines": { "node": ">=10" } }, - "node_modules/amqplib/node_modules/isarray": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", - "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==" - }, - "node_modules/amqplib/node_modules/readable-stream": { - "version": "1.1.14", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", - "integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==", - "dependencies": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.1", - "isarray": "0.0.1", - "string_decoder": "~0.10.x" - } - }, - "node_modules/amqplib/node_modules/string_decoder": { - "version": "0.10.31", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", - "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" - }, "node_modules/ansi-align": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-3.0.1.tgz", @@ -3153,6 +3038,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, + "node_modules/array-source": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/array-source/-/array-source-0.0.4.tgz", + "integrity": "sha512-frNdc+zBn80vipY+GdcJkLEbMWj3xmzArYApmUGxoiV8uAu/ygcs9icPdsGdA26h0MkHUMW6EN2piIvVx+M5Mw==" + }, "node_modules/array-union": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/array-union/-/array-union-2.1.0.tgz", @@ -3326,11 +3216,6 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==" }, - "node_modules/bitsyntax/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -3356,6 +3241,35 @@ "node": ">= 6" } }, + "node_modules/bl/node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/bl/node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dev": true, + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", @@ -3418,17 +3332,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/boxen/node_modules/camelcase": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", - "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/brace-expansion": { "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", @@ -3452,9 +3355,9 @@ } }, "node_modules/browserslist": { - "version": "4.21.5", - "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.5.tgz", - "integrity": "sha512-tUkiguQGW7S3IhB7N+c2MV/HZPSCPAAiYBZXLsBhFB/PCy6ZKKsZrmBayHV9fdGV/ARIfJ14NkxKzRDjvp7L6w==", + "version": "4.21.7", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.7.tgz", + "integrity": "sha512-BauCXrQ7I2ftSqd2mvKHGo85XR0u7Ru3C/Hxsy/0TkfCtjrmAbPdzLGasmoiBxplpDXlPvdjX9u7srIMfgasNA==", "dev": true, "funding": [ { @@ -3464,13 +3367,17 @@ { "type": "tidelift", "url": "https://tidelift.com/funding/github/npm/browserslist" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" } ], "dependencies": { - "caniuse-lite": "^1.0.30001449", - "electron-to-chromium": "^1.4.284", - "node-releases": "^2.0.8", - "update-browserslist-db": "^1.0.10" + "caniuse-lite": "^1.0.30001489", + "electron-to-chromium": "^1.4.411", + "node-releases": "^2.0.12", + "update-browserslist-db": "^1.0.11" }, "bin": { "browserslist": "cli.js" @@ -3575,18 +3482,20 @@ } }, "node_modules/camelcase": { - "version": "5.3.1", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-5.3.1.tgz", - "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==", - "dev": true, + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", + "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", "engines": { - "node": ">=6" + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" } }, "node_modules/caniuse-lite": { - "version": "1.0.30001482", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001482.tgz", - "integrity": "sha512-F1ZInsg53cegyjroxLNW9DmrEQ1SuGRTO1QlpA0o2/6OpQ0gFeDRoq1yFmnr8Sakn9qwwt9DmbxHB6w167OSuQ==", + "version": "1.0.30001495", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001495.tgz", + "integrity": "sha512-F6x5IEuigtUfU5ZMQK2jsy5JqUUlEFRVZq8bO2a+ysq5K7jD6PPc9YXZj78xDNS3uNchesp1Jw47YXEqr+Viyg==", "dev": true, "funding": [ { @@ -3776,7 +3685,6 @@ "version": "8.0.1", "resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz", "integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==", - "dev": true, "dependencies": { "string-width": "^4.2.0", "strip-ansi": "^6.0.1", @@ -3882,6 +3790,33 @@ "typedarray": "^0.0.6" } }, + "node_modules/concat-stream/node_modules/isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + }, + "node_modules/concat-stream/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/concat-stream/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/consola": { "version": "2.15.3", "resolved": "https://registry.npmjs.org/consola/-/consola-2.15.3.tgz", @@ -3898,6 +3833,25 @@ "node": ">= 0.6" } }, + "node_modules/content-disposition/node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/content-type": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", @@ -4165,9 +4119,9 @@ "integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==" }, "node_modules/electron-to-chromium": { - "version": "1.4.380", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.380.tgz", - "integrity": "sha512-XKGdI4pWM78eLH2cbXJHiBnWUwFSzZM7XujsB6stDiGu9AeSqziedP6amNLpJzE3i0rLTcfAwdCTs5ecP5yeSg==", + "version": "1.4.425", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.425.tgz", + "integrity": "sha512-wv1NufHxu11zfDbY4fglYQApMswleE9FL/DSeyOyauVXDZ+Kco96JK/tPfBUaDqfRarYp2WH2hJ/5UnVywp9Jg==", "dev": true }, "node_modules/emittery": { @@ -4205,9 +4159,9 @@ } }, "node_modules/enhanced-resolve": { - "version": "5.13.0", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.13.0.tgz", - "integrity": "sha512-eyV8f0y1+bzyfh8xAwW/WTSZpLbjhqc4ne9eGSH4Zo2ejdyiNG9pU6mf9DG8a7+Auk6MFTlNOT4Y2y/9k8GKVg==", + "version": "5.14.1", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.14.1.tgz", + "integrity": "sha512-Vklwq2vDKtl0y/vtwjSesgJ5MYS7Etuk5txS8VdKL4AOS1aUlD96zqIfsOSLQsdv3xgMRbtkWM8eG9XDfKUPow==", "dev": true, "dependencies": { "graceful-fs": "^4.2.4", @@ -4258,16 +4212,16 @@ } }, "node_modules/eslint": { - "version": "8.39.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.39.0.tgz", - "integrity": "sha512-mwiok6cy7KTW7rBpo05k6+p4YVZByLNjAZ/ACB9DRCu4YDRwjXI01tWHp6KAUWelsBetTxKK/2sHB0vdS8Z2Og==", + "version": "8.42.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.42.0.tgz", + "integrity": "sha512-ulg9Ms6E1WPf67PHaEY4/6E2tEn5/f7FXGzr3t9cBMugOmf1INYvuUwwh1aXQN4MfJ6a5K2iNwP3w4AColvI9A==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.4.0", - "@eslint/eslintrc": "^2.0.2", - "@eslint/js": "8.39.0", - "@humanwhocodes/config-array": "^0.11.8", + "@eslint/eslintrc": "^2.0.3", + "@eslint/js": "8.42.0", + "@humanwhocodes/config-array": "^0.11.10", "@humanwhocodes/module-importer": "^1.0.1", "@nodelib/fs.walk": "^1.2.8", "ajv": "^6.10.0", @@ -4277,8 +4231,8 @@ "doctrine": "^3.0.0", "escape-string-regexp": "^4.0.0", "eslint-scope": "^7.2.0", - "eslint-visitor-keys": "^3.4.0", - "espree": "^9.5.1", + "eslint-visitor-keys": "^3.4.1", + "espree": "^9.5.2", "esquery": "^1.4.2", "esutils": "^2.0.2", "fast-deep-equal": "^3.1.3", @@ -4286,13 +4240,12 @@ "find-up": "^5.0.0", "glob-parent": "^6.0.2", "globals": "^13.19.0", - "grapheme-splitter": "^1.0.4", + "graphemer": "^1.4.0", "ignore": "^5.2.0", "import-fresh": "^3.0.0", "imurmurhash": "^0.1.4", "is-glob": "^4.0.0", "is-path-inside": "^3.0.3", - "js-sdsl": "^4.1.4", "js-yaml": "^4.1.0", "json-stable-stringify-without-jsonify": "^1.0.1", "levn": "^0.4.1", @@ -4361,9 +4314,9 @@ } }, "node_modules/eslint-visitor-keys": { - "version": "3.4.0", - "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.4.0.tgz", - "integrity": "sha512-HPpKPUBQcAsZOsHAFwTtIKcYlCje62XB7SEAcxjtmW6TD1WVpkS6i6/hOVtTZIl4zGj/mBqpFVGvaDneik+VoQ==", + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-3.4.1.tgz", + "integrity": "sha512-pZnmmLwYzf+kWaM/Qgrvpen51upAktaaiI01nsJD/Yr3lMOdNtq0cxkrrg16w64VtisN6okbs7Q8AfGqj4c9fA==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -4432,14 +4385,14 @@ "dev": true }, "node_modules/espree": { - "version": "9.5.1", - "resolved": "https://registry.npmjs.org/espree/-/espree-9.5.1.tgz", - "integrity": "sha512-5yxtHSZXRSW5pvv3hAlXM5+/Oswi1AUFqBmbibKb5s6bp3rGIDkyXU6xCoyuuLhijr4SFwPrXRoZjz0AZDN9tg==", + "version": "9.5.2", + "resolved": "https://registry.npmjs.org/espree/-/espree-9.5.2.tgz", + "integrity": "sha512-7OASN1Wma5fum5SrNhFMAMJxOUAbhyfQ8dQ//PJaJbNw0URTPWqIghHWt1MmAANKhHZIYOHruW4Kw4ruUWOdGw==", "dev": true, "dependencies": { "acorn": "^8.8.0", "acorn-jsx": "^5.3.2", - "eslint-visitor-keys": "^3.4.0" + "eslint-visitor-keys": "^3.4.1" }, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -4529,6 +4482,11 @@ "node": ">= 0.6" } }, + "node_modules/eventemitter2": { + "version": "6.4.9", + "resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-6.4.9.tgz", + "integrity": "sha512-JEPTiaOt9f04oa6NOkc4aH+nVp5I3wEjpHbIPqfgCdD5v5bUzy7xQqwcVO2aDQgOWhI28da57HksMrzK9HlRxg==" + }, "node_modules/events": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/events/-/events-3.3.0.tgz", @@ -4682,6 +4640,25 @@ "node": ">= 0.8" } }, + "node_modules/express/node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, "node_modules/external-editor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", @@ -4703,9 +4680,9 @@ "dev": true }, "node_modules/fast-diff": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz", - "integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==", + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.3.0.tgz", + "integrity": "sha512-VxPP4NqbUjj6MaAOafWeUn2cXWLcCtljklUtZf0Ind4XQ+QPtmA0b18zZy0jIQx+ExRVCR/ZQpBmik5lXshNsw==", "dev": true }, "node_modules/fast-glob": { @@ -4795,6 +4772,14 @@ "node": "^10.12.0 || >=12.0.0" } }, + "node_modules/file-source": { + "version": "0.6.1", + "resolved": "https://registry.npmjs.org/file-source/-/file-source-0.6.1.tgz", + "integrity": "sha512-1R1KneL7eTXmXfKxC10V/9NeGOdbsAXJ+lQ//fvvcHUgtaZcZDWNJNblxAoVOyV1cj45pOtUrR3vZTBwqcW8XA==", + "dependencies": { + "stream-source": "0.3" + } + }, "node_modules/fill-range": { "version": "7.0.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.0.1.tgz", @@ -4975,9 +4960,9 @@ } }, "node_modules/fs-monkey": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-1.0.3.tgz", - "integrity": "sha512-cybjIfiiE+pTWicSCLFHSrXZ6EilF30oh91FDP9S2B051prEa7QWfrVTQm10/dDpswBDXZugPa1Ogu8Yh+HV0Q==", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/fs-monkey/-/fs-monkey-1.0.4.tgz", + "integrity": "sha512-INM/fWAxMICjttnD0DX1rBvinKskj5G1w+oy/pnm9u/tSlnBrzFonJMcalKJ30P8RRsPzKcCG7Q8l0jx5Fh9YQ==", "dev": true }, "node_modules/fs.realpath": { @@ -5014,6 +4999,89 @@ "node": ">=6.9.0" } }, + "node_modules/geo-tz": { + "version": "7.0.7", + "resolved": "https://registry.npmjs.org/geo-tz/-/geo-tz-7.0.7.tgz", + "integrity": "sha512-Aq0sRSO1y4w62D5muRqzDmN4SWfFYnt703BLiqiHAvunlwsJs4qd3Fkl1pKSUa0bwuBmPFxIA8M1E+ilg2PSjw==", + "dependencies": { + "@turf/boolean-point-in-polygon": "^6.5.0", + "@turf/helpers": "^6.5.0", + "geobuf": "^3.0.2", + "pbf": "^3.2.1" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/geobuf": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/geobuf/-/geobuf-3.0.2.tgz", + "integrity": "sha512-ASgKwEAQQRnyNFHNvpd5uAwstbVYmiTW0Caw3fBb509tNTqXyAAPMyFs5NNihsLZhLxU1j/kjFhkhLWA9djuVg==", + "dependencies": { + "concat-stream": "^2.0.0", + "pbf": "^3.2.1", + "shapefile": "~0.6.6" + }, + "bin": { + "geobuf2json": "bin/geobuf2json", + "json2geobuf": "bin/json2geobuf", + "shp2geobuf": "bin/shp2geobuf" + } + }, + "node_modules/geobuf/node_modules/concat-stream": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-2.0.0.tgz", + "integrity": "sha512-MWufYdFw53ccGjCA+Ol7XJYpAlW6/prSMzuPOTRnJGcGzuhLn4Scrz7qf6o8bROZ514ltazcIFJZevcfbo0x7A==", + "engines": [ + "node >= 6.0" + ], + "dependencies": { + "buffer-from": "^1.0.0", + "inherits": "^2.0.3", + "readable-stream": "^3.0.2", + "typedarray": "^0.0.6" + } + }, + "node_modules/geobuf/node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/geobuf/node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/geobuf/node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, "node_modules/get-caller-file": { "version": "2.0.5", "resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz", @@ -5023,12 +5091,13 @@ } }, "node_modules/get-intrinsic": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.0.tgz", - "integrity": "sha512-L049y6nFOuom5wGyRc3/gdTLO94dySVKRACj1RmJZBQXlbTMhtNIgkWkUHq+jYmZvKf14EW1EoJnnjbmoHij0Q==", + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.1.tgz", + "integrity": "sha512-2DcsyfABl+gVHEfCOaTrWgyt+tb6MSEGmKq+kI5HwLbIYgjgmMcV8KQ41uaKz1xxUcn9tJtgFbQUEVcEbd0FYw==", "dependencies": { "function-bind": "^1.1.1", "has": "^1.0.3", + "has-proto": "^1.0.1", "has-symbols": "^1.0.3" }, "funding": { @@ -5141,6 +5210,12 @@ "integrity": "sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==", "dev": true }, + "node_modules/graphemer": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/graphemer/-/graphemer-1.4.0.tgz", + "integrity": "sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==", + "dev": true + }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -5160,6 +5235,17 @@ "node": ">=8" } }, + "node_modules/has-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.1.tgz", + "integrity": "sha512-7qE+iP+O+bgF9clE5+UoBFzE65mlBiVj3tKCrlNQ0Ogwm0BjpT/gK4SlLYDMybDh5I3TCTKnPPa0oMG7JDYrhg==", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/has-symbols": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", @@ -5225,7 +5311,6 @@ "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", - "dev": true, "funding": [ { "type": "github", @@ -5394,9 +5479,9 @@ } }, "node_modules/is-core-module": { - "version": "2.12.0", - "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.12.0.tgz", - "integrity": "sha512-RECHCBCd/viahWmwj6enj19sKbHfJrddi/6cBDsNTKbNq0f7VeaUkBo60BqzvPqo/W54ChS62Z5qyun7cfOMqQ==", + "version": "2.12.1", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.12.1.tgz", + "integrity": "sha512-Q4ZuBAe2FUsKtyQJoQHlvP8OvBERxO3jEmy1I7hcRXcJBGGHFh/aJBswbXuS9sgrDH2QUO8ilkwNPHvHMd8clg==", "dev": true, "dependencies": { "has": "^1.0.3" @@ -5495,9 +5580,9 @@ } }, "node_modules/isarray": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", - "integrity": "sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==" + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", + "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==" }, "node_modules/isexe": { "version": "2.0.0", @@ -6113,18 +6198,6 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, - "node_modules/jest-validate/node_modules/camelcase": { - "version": "6.3.0", - "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz", - "integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==", - "dev": true, - "engines": { - "node": ">=10" - }, - "funding": { - "url": "https://github.com/sponsors/sindresorhus" - } - }, "node_modules/jest-watcher": { "version": "29.5.0", "resolved": "https://registry.npmjs.org/jest-watcher/-/jest-watcher-29.5.0.tgz", @@ -6174,16 +6247,6 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, - "node_modules/js-sdsl": { - "version": "4.4.0", - "resolved": "https://registry.npmjs.org/js-sdsl/-/js-sdsl-4.4.0.tgz", - "integrity": "sha512-FfVSdx6pJ41Oa+CF7RDaFmTnCaFhua+SNYQX74riGOpl96x+2jQCqEfQ2bnXu/5DPCqlRuiqyvTJM0Qjz26IVg==", - "dev": true, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/js-sdsl" - } - }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -6294,9 +6357,9 @@ } }, "node_modules/libphonenumber-js": { - "version": "1.10.28", - "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.28.tgz", - "integrity": "sha512-1eAgjLrZA0+2Wgw4hs+4Q/kEBycxQo8ZLYnmOvZ3AlM8ImAVAJgDPlZtISLEzD1vunc2q8s2Pn7XwB7I8U3Kzw==" + "version": "1.10.34", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.10.34.tgz", + "integrity": "sha512-p6g4NaQH4gK1gre32+kV14Mk6GPo2EDcPDvjbi+D2ycsPFsN4gVWNbs0itdHLZqByg6YEK8mE7OeP200I/ScTQ==" }, "node_modules/lines-and-columns": { "version": "1.2.4", @@ -6403,9 +6466,9 @@ } }, "node_modules/magic-string": { - "version": "0.29.0", - "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.29.0.tgz", - "integrity": "sha512-WcfidHrDjMY+eLjlU+8OvwREqHwpgCeKVBUpQ3OhYYuvfaYCUgcbuBzappNzZvg/v8onU3oQj+BYpkOJe9Iw4Q==", + "version": "0.30.0", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.0.tgz", + "integrity": "sha512-LA+31JYDJLs82r2ScLrlz1GjSgu66ZV518eyWT+S8VhyQn/JL0u9MeBOvQMGYiPk1DBiSN9DDMOcXvigJZaViQ==", "dev": true, "dependencies": { "@jridgewell/sourcemap-codec": "^1.4.13" @@ -6462,12 +6525,12 @@ } }, "node_modules/memfs": { - "version": "3.5.1", - "resolved": "https://registry.npmjs.org/memfs/-/memfs-3.5.1.tgz", - "integrity": "sha512-UWbFJKvj5k+nETdteFndTpYxdeTMox/ULeqX5k/dpaQJCCFmj5EeKv3dBcyO2xmkRAx2vppRu5dVG7SOtsGOzA==", + "version": "3.5.3", + "resolved": "https://registry.npmjs.org/memfs/-/memfs-3.5.3.tgz", + "integrity": "sha512-UERzLsxzllchadvbPs5aolHh65ISpKpM+ccLbOJ8/vvpBKmAWf+la7dXFy7Mr0ySHbdHrFv5kGFCUHHe6GFEmw==", "dev": true, "dependencies": { - "fs-monkey": "^1.0.3" + "fs-monkey": "^1.0.4" }, "engines": { "node": ">= 4.0.0" @@ -6664,6 +6727,14 @@ "integrity": "sha512-Yd3UES5mWCSqR+qNT93S3UoYUkqAZ9lLg8a7g9rimsWmYGK8cVToA4/sF3RrshdyV3sAGMXVUmpMYOw+dLpOuw==", "dev": true }, + "node_modules/nestjs-request-context": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/nestjs-request-context/-/nestjs-request-context-2.1.0.tgz", + "integrity": "sha512-XSZ4CGjgVetyP0q6TSgu+urpUhS8hlY4UxDenPioqEhwpiLCjYFnWrp8NSJctn6NdON5d45LLKz/TN/5ZppL9w==", + "peerDependencies": { + "@nestjs/common": "^9.0.5" + } + }, "node_modules/node-abort-controller": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-3.1.1.tgz", @@ -6680,9 +6751,9 @@ } }, "node_modules/node-fetch": { - "version": "2.6.9", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.9.tgz", - "integrity": "sha512-DJm/CJkZkRjKKj4Zi4BsKVZh3ValV5IR5s7LVZnW+6YMh0W1BfNA8XSs6DLMGYlId5F3KnA70uu2qepcR08Qqg==", + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-4I6pdBY1EthSqDmJkiNk3JIT8cswwR9nfeW/cPdUagJYEQG7R95WRH74wpz7ma8Gh/9dI9FP+OU+0E4FvtA55w==", "dependencies": { "whatwg-url": "^5.0.0" }, @@ -6705,9 +6776,9 @@ "dev": true }, "node_modules/node-releases": { - "version": "2.0.10", - "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.10.tgz", - "integrity": "sha512-5GFldHPXVG/YZmFzJvKK2zDSzPKhEp0+ZR5SVaoSag9fsL5YgHbUHDfnG5494ISANDcK4KwPXAx2xqVEydmd7w==", + "version": "2.0.12", + "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.12.tgz", + "integrity": "sha512-QzsYKWhXTWx8h1kIvqfnC++o0pEmpRQA/aenALsL2F4pqNVr7YzcdMlDij5WBnwftRbJCNJL/O7zdKaxKPHqgQ==", "dev": true }, "node_modules/normalize-path": { @@ -6847,6 +6918,11 @@ "node": ">=0.10.0" } }, + "node_modules/oxide.ts": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/oxide.ts/-/oxide.ts-1.1.0.tgz", + "integrity": "sha512-+MkqFRQVHEe/x4/cJ6KuYz2m2VpnoBi7aKLbttGYTxmpNZalQ2RbKH2HxyfsTqXJhjh9DYxulPWfQV/hWMmzCg==" + }, "node_modules/p-limit": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", @@ -6958,13 +7034,13 @@ "dev": true }, "node_modules/path-scurry": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.7.0.tgz", - "integrity": "sha512-UkZUeDjczjYRE495+9thsgcVgsaCPkaw80slmfVFgllxY+IO8ubTsOpFVjDPROBqJdHfVPUFRHPBV/WciOVfWg==", + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.9.2.tgz", + "integrity": "sha512-qSDLy2aGFPm8i4rsbHd4MNyTcrzHFsLQykrtbuGRknZZCBBVXSv2tSCDN2Cg6Rt/GFRw8GoW9y9Ecw5rIPG1sg==", "dev": true, "dependencies": { - "lru-cache": "^9.0.0", - "minipass": "^5.0.0" + "lru-cache": "^9.1.1", + "minipass": "^5.0.0 || ^6.0.2" }, "engines": { "node": ">=16 || 14 >=14.17" @@ -6974,21 +7050,30 @@ } }, "node_modules/path-scurry/node_modules/lru-cache": { - "version": "9.1.1", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-9.1.1.tgz", - "integrity": "sha512-65/Jky17UwSb0BuB9V+MyDpsOtXKmYwzhyl+cOa9XUiI4uV2Ouy/2voFP3+al0BjZbJgMBD8FojMpAf+Z+qn4A==", + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-9.1.2.tgz", + "integrity": "sha512-ERJq3FOzJTxBbFjZ7iDs+NiK4VI9Wz+RdrrAB8dio1oV+YvdPzUEE4QNiT2VD51DkIbCYRUUzCRkssXCHqSnKQ==", "dev": true, "engines": { "node": "14 || >=16.14" } }, "node_modules/path-scurry/node_modules/minipass": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/minipass/-/minipass-5.0.0.tgz", - "integrity": "sha512-3FnjYuehv9k6ovOEbyOswadCDPX1piCfhV8ncmYtHOjuPwylVWsghTLo7rabjC3Rx5xD4HDx8Wm1xnMF7S5qFQ==", + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-6.0.2.tgz", + "integrity": "sha512-MzWSV5nYVT7mVyWCwn2o7JH13w2TBRmmSqSRCKzTw+lmft9X4z+3wjvs06Tzijo5z4W/kahUCDpRXTF+ZrmF/w==", "dev": true, "engines": { - "node": ">=8" + "node": ">=16 || 14 >=14.17" + } + }, + "node_modules/path-source": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/path-source/-/path-source-0.1.3.tgz", + "integrity": "sha512-dWRHm5mIw5kw0cs3QZLNmpUWty48f5+5v9nWD2dw3Y0Hf+s01Ag8iJEWV0Sm0kocE8kK27DrIowha03e1YR+Qw==", + "dependencies": { + "array-source": "0.0", + "file-source": "0.6" } }, "node_modules/path-to-regexp": { @@ -7005,6 +7090,18 @@ "node": ">=8" } }, + "node_modules/pbf": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/pbf/-/pbf-3.2.1.tgz", + "integrity": "sha512-ClrV7pNOn7rtmoQVF4TS1vyU0WhYRnP92fzbfF75jAIwpnzdJXf8iTd4CMEqO4yUenH6NDqLiwjqlh6QgZzgLQ==", + "dependencies": { + "ieee754": "^1.1.12", + "resolve-protobuf-schema": "^2.1.0" + }, + "bin": { + "pbf": "bin/pbf" + } + }, "node_modules/picocolors": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.0.0.tgz", @@ -7168,13 +7265,13 @@ } }, "node_modules/prisma": { - "version": "4.13.0", - "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.13.0.tgz", - "integrity": "sha512-L9mqjnSmvWIRCYJ9mQkwCtj4+JDYYTdhoyo8hlsHNDXaZLh/b4hR0IoKIBbTKxZuyHQzLopb/+0Rvb69uGV7uA==", + "version": "4.15.0", + "resolved": "https://registry.npmjs.org/prisma/-/prisma-4.15.0.tgz", + "integrity": "sha512-iKZZpobPl48gTcSZVawLMQ3lEy6BnXwtoMj7hluoGFYu2kQ6F9LBuBrUyF95zRVnNo8/3KzLXJXJ5TEnLSJFiA==", "devOptional": true, "hasInstallScript": true, "dependencies": { - "@prisma/engines": "4.13.0" + "@prisma/engines": "4.15.0" }, "bin": { "prisma": "build/index.js", @@ -7235,6 +7332,11 @@ "resolved": "https://registry.npmjs.org/long/-/long-5.2.3.tgz", "integrity": "sha512-lcHwpNoggQTObv5apGNCTdJrO69eHOZMi4BNC+rTLER8iHAqGrUVeLh/irVIM7zTw2bOXA8T6uNPeujwOLg/2Q==" }, + "node_modules/protocol-buffers-schema": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/protocol-buffers-schema/-/protocol-buffers-schema-3.6.0.tgz", + "integrity": "sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw==" + }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", @@ -7359,24 +7461,16 @@ "dev": true }, "node_modules/readable-stream": { - "version": "2.3.8", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", - "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "version": "1.1.14", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", + "integrity": "sha512-+MeVjFf4L44XUkhM1eYbD8fyEsxcV81pqMSR5gblfcLCHfZvbrqy4/qYHE+/R5HoBUT11WV5O08Cr1n3YXkWVQ==", "dependencies": { "core-util-is": "~1.0.0", - "inherits": "~2.0.3", - "isarray": "~1.0.0", - "process-nextick-args": "~2.0.0", - "safe-buffer": "~5.1.1", - "string_decoder": "~1.1.1", - "util-deprecate": "~1.0.1" + "inherits": "~2.0.1", + "isarray": "0.0.1", + "string_decoder": "~0.10.x" } }, - "node_modules/readable-stream/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" - }, "node_modules/readdirp": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", @@ -7494,6 +7588,14 @@ "node": ">=4" } }, + "node_modules/resolve-protobuf-schema": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/resolve-protobuf-schema/-/resolve-protobuf-schema-2.1.0.tgz", + "integrity": "sha512-kI5ffTiZWmJaS/huM8wZfEMer1eRd7oJQhDuxeCLe3t7N7mX3z94CN0xPxBQxFYQTSNz9T0i+v6inKqSdK8xrQ==", + "dependencies": { + "protocol-buffers-schema": "^3.3.1" + } + }, "node_modules/resolve.exports": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/resolve.exports/-/resolve.exports-2.0.2.tgz", @@ -7627,23 +7729,9 @@ } }, "node_modules/safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ] + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, "node_modules/safer-buffer": { "version": "2.1.2", @@ -7651,9 +7739,9 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, "node_modules/schema-utils": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.1.2.tgz", - "integrity": "sha512-pvjEHOgWc9OWA/f/DE3ohBWTD6EleVLf7iFUkoSwAxttdBhB9QUebQgxER2kWueOvRJXPHNnyrvvh9eZINB8Eg==", + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-3.2.0.tgz", + "integrity": "sha512-0zTyLGyDJYd/MBxG1AhJkKa6fpEBds4OQO2ut0w7OYG+ZGhGea09lijvzsqegYSik88zc7cUtIlnnO+/BvD6gQ==", "dev": true, "dependencies": { "@types/json-schema": "^7.0.8", @@ -7700,9 +7788,9 @@ "dev": true }, "node_modules/semver": { - "version": "7.5.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.0.tgz", - "integrity": "sha512-+XC0AD/R7Q2mPSRuy2Id0+CGTZ98+8f+KvwirxOKIEyid+XSx6HbC63p+O4IndTHuX5Z+JxQ0TghCkO5Cg/2HA==", + "version": "7.5.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.1.tgz", + "integrity": "sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" @@ -7801,6 +7889,28 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" }, + "node_modules/shapefile": { + "version": "0.6.6", + "resolved": "https://registry.npmjs.org/shapefile/-/shapefile-0.6.6.tgz", + "integrity": "sha512-rLGSWeK2ufzCVx05wYd+xrWnOOdSV7xNUW5/XFgx3Bc02hBkpMlrd2F1dDII7/jhWzv0MSyBFh5uJIy9hLdfuw==", + "dependencies": { + "array-source": "0.0", + "commander": "2", + "path-source": "0.1", + "slice-source": "0.4", + "stream-source": "0.3", + "text-encoding": "^0.6.4" + }, + "bin": { + "dbf2json": "bin/dbf2json", + "shp2json": "bin/shp2json" + } + }, + "node_modules/shapefile/node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -7873,6 +7983,11 @@ "node": ">=8" } }, + "node_modules/slice-source": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/slice-source/-/slice-source-0.4.1.tgz", + "integrity": "sha512-YiuPbxpCj4hD9Qs06hGAz/OZhQ0eDuALN0lRWJez0eD/RevzKqGdUx1IOMUnXgpr+sXZLq3g8ERwbAH0bCb8vg==" + }, "node_modules/source-map": { "version": "0.7.4", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.7.4.tgz", @@ -7941,6 +8056,11 @@ "node": ">= 0.8" } }, + "node_modules/stream-source": { + "version": "0.3.5", + "resolved": "https://registry.npmjs.org/stream-source/-/stream-source-0.3.5.tgz", + "integrity": "sha512-ZuEDP9sgjiAwUVoDModftG0JtYiLUV8K4ljYD1VyUMRWtbVf92474o4kuuul43iZ8t/hRuiDAx1dIJSvirrK/g==" + }, "node_modules/streamsearch": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", @@ -7950,17 +8070,9 @@ } }, "node_modules/string_decoder": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", - "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", - "dependencies": { - "safe-buffer": "~5.1.0" - } - }, - "node_modules/string_decoder/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha512-ev2QzSzWPYmy9GuqfIVildA4OdcGLeFZQrq5ys6RtiuF+RQQiZWr8TZNyAcuVXyQRYfEO+MsoB/1BuQVhOJuoQ==" }, "node_modules/string-length": { "version": "4.0.2", @@ -8117,13 +8229,13 @@ } }, "node_modules/terser": { - "version": "5.17.1", - "resolved": "https://registry.npmjs.org/terser/-/terser-5.17.1.tgz", - "integrity": "sha512-hVl35zClmpisy6oaoKALOpS0rDYLxRFLHhRuDlEGTKey9qHjS1w9GMORjuwIMt70Wan4lwsLYyWDVnWgF+KUEw==", + "version": "5.17.7", + "resolved": "https://registry.npmjs.org/terser/-/terser-5.17.7.tgz", + "integrity": "sha512-/bi0Zm2C6VAexlGgLlVxA0P2lru/sdLyfCVaRMfKVo9nWxbmz7f/sD8VPybPeSUJaJcwmCJis9pBIhcVcG1QcQ==", "dev": true, "dependencies": { - "@jridgewell/source-map": "^0.3.2", - "acorn": "^8.5.0", + "@jridgewell/source-map": "^0.3.3", + "acorn": "^8.8.2", "commander": "^2.20.0", "source-map-support": "~0.5.20" }, @@ -8135,16 +8247,16 @@ } }, "node_modules/terser-webpack-plugin": { - "version": "5.3.7", - "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.3.7.tgz", - "integrity": "sha512-AfKwIktyP7Cu50xNjXF/6Qb5lBNzYaWpU6YfoX3uZicTx0zTy0stDDCsvjDapKsSDvOeWo5MEq4TmdBy2cNoHw==", + "version": "5.3.9", + "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.3.9.tgz", + "integrity": "sha512-ZuXsqE07EcggTWQjXUj+Aot/OMcD0bMKGgF63f7UxYcu5/AJF53aIpK1YoP5xR9l6s/Hy2b+t1AM0bLNPRuhwA==", "dev": true, "dependencies": { "@jridgewell/trace-mapping": "^0.3.17", "jest-worker": "^27.4.5", "schema-utils": "^3.1.1", "serialize-javascript": "^6.0.1", - "terser": "^5.16.5" + "terser": "^5.16.8" }, "engines": { "node": ">= 10.13.0" @@ -8217,6 +8329,12 @@ "node": ">=8" } }, + "node_modules/text-encoding": { + "version": "0.6.4", + "resolved": "https://registry.npmjs.org/text-encoding/-/text-encoding-0.6.4.tgz", + "integrity": "sha512-hJnc6Qg3dWoOMkqP53F0dzRIgtmsAge09kxUIqGrEUS4qr5rWLckGYaQAVr+opBrIMRErGgy6f5aPnyPpyGRfg==", + "deprecated": "no longer maintained" + }, "node_modules/text-table": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz", @@ -8229,6 +8347,14 @@ "integrity": "sha512-w89qg7PI8wAdvX60bMDP+bFoD5Dvhm9oLheFp5O4a2QF0cSBGsBX4qZmadPMvVqlLJBBci+WqGGOAPvcDeNSVg==", "dev": true }, + "node_modules/timezonecomplete": { + "version": "5.12.4", + "resolved": "https://registry.npmjs.org/timezonecomplete/-/timezonecomplete-5.12.4.tgz", + "integrity": "sha512-K+ocagBAl5wu9Ifh5oHKhRRLb0wP7j0VjAzjboZsT6bnVmtJNRe3Wnk2IPp0C4Uc8HpLly3gbfUrTlJ3M7vCPA==", + "dependencies": { + "tzdata": "^1.0.25" + } + }, "node_modules/tmp": { "version": "0.0.33", "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", @@ -8334,9 +8460,9 @@ } }, "node_modules/ts-loader": { - "version": "9.4.2", - "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.4.2.tgz", - "integrity": "sha512-OmlC4WVmFv5I0PpaxYb+qGeGOdm5giHU7HwDDUjw59emP2UYMHy9fFSDcYgSNoH8sXcj4hGCSEhlDZ9ULeDraA==", + "version": "9.4.3", + "resolved": "https://registry.npmjs.org/ts-loader/-/ts-loader-9.4.3.tgz", + "integrity": "sha512-n3hBnm6ozJYzwiwt5YRiJZkzktftRpMiBApHaJPoWLA+qetQBAXkHqCLM6nwSdRDimqVtA5ocIkcTRLMTt7yzA==", "dev": true, "dependencies": { "chalk": "^4.1.0", @@ -8433,9 +8559,9 @@ } }, "node_modules/tslib": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.5.0.tgz", - "integrity": "sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==" + "version": "2.4.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.4.1.tgz", + "integrity": "sha512-tGyy4dAjRIEwI7BzsB0lynWgOpfqjUdq91XXAlIWD2OwKBH7oCl/GZG/HT4BOHrTlPMOASlMQ7veyTqpmRcrNA==" }, "node_modules/tsutils": { "version": "3.21.0", @@ -8520,6 +8646,11 @@ "node": ">=4.2.0" } }, + "node_modules/tzdata": { + "version": "1.0.38", + "resolved": "https://registry.npmjs.org/tzdata/-/tzdata-1.0.38.tgz", + "integrity": "sha512-KIgVvZTLt+DWzr3MOENNLCLdsNB+usedRYYHCVfVbA7TDewj8mfjlWmj3Mv6FfdrvfeE6Oprt+qE47YiL90duQ==" + }, "node_modules/uid": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/uid/-/uid-2.0.2.tgz", @@ -8696,9 +8827,9 @@ "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" }, "node_modules/webpack": { - "version": "5.81.0", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.81.0.tgz", - "integrity": "sha512-AAjaJ9S4hYCVODKLQTgG5p5e11hiMawBwV2v8MYLE0C/6UAGLuAF4n1qa9GOwdxnicaP+5k6M5HrLmD4+gIB8Q==", + "version": "5.86.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.86.0.tgz", + "integrity": "sha512-3BOvworZ8SO/D4GVP+GoRC3fVeg5MO4vzmq8TJJEkdmopxyazGDxN8ClqN12uzrZW9Tv8EED8v5VSb6Sqyi0pg==", "dev": true, "peer": true, "dependencies": { @@ -8708,10 +8839,10 @@ "@webassemblyjs/wasm-edit": "^1.11.5", "@webassemblyjs/wasm-parser": "^1.11.5", "acorn": "^8.7.1", - "acorn-import-assertions": "^1.7.6", + "acorn-import-assertions": "^1.9.0", "browserslist": "^4.14.5", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.13.0", + "enhanced-resolve": "^5.14.1", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", @@ -8937,7 +9068,6 @@ "version": "17.7.2", "resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz", "integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==", - "dev": true, "dependencies": { "cliui": "^8.0.1", "escalade": "^3.1.1", @@ -8955,7 +9085,6 @@ "version": "21.1.1", "resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz", "integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==", - "dev": true, "engines": { "node": ">=12" } diff --git a/package.json b/package.json index e83732c..310dcf9 100644 --- a/package.json +++ b/package.json @@ -40,21 +40,26 @@ "@grpc/grpc-js": "^1.8.14", "@grpc/proto-loader": "^0.7.6", "@liaoliaots/nestjs-redis": "^9.0.5", - "@mobicoop/configuration-module": "^1.1.0", - "@mobicoop/message-broker-module": "^1.0.5", + "@mobicoop/configuration-module": "^1.2.0", + "@mobicoop/message-broker-module": "^1.2.0", "@nestjs/common": "^9.0.0", "@nestjs/config": "^2.3.1", "@nestjs/core": "^9.0.0", "@nestjs/cqrs": "^9.0.3", + "@nestjs/event-emitter": "^1.4.2", "@nestjs/microservices": "^9.4.0", "@nestjs/platform-express": "^9.0.0", "@nestjs/terminus": "^9.2.2", "@prisma/client": "^4.13.0", "class-transformer": "^0.5.1", "class-validator": "^0.14.0", + "geo-tz": "^7.0.7", "ioredis": "^5.3.2", + "nestjs-request-context": "^2.1.0", + "oxide.ts": "^1.1.0", "reflect-metadata": "^0.1.13", - "rxjs": "^7.2.0" + "rxjs": "^7.2.0", + "timezonecomplete": "^5.12.4" }, "devDependencies": { "@nestjs/cli": "^9.0.0", @@ -64,6 +69,7 @@ "@types/jest": "29.5.0", "@types/node": "18.15.11", "@types/supertest": "^2.0.11", + "@types/uuid": "^9.0.2", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "dotenv-cli": "^7.2.1", @@ -98,7 +104,7 @@ "main.ts" ], "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", + "testRegex": ".*\\.service.spec\\.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, @@ -118,6 +124,10 @@ "main.ts" ], "coverageDirectory": "../coverage", + "moduleNameMapper": { + "^@libs(.*)": "/libs/$1", + "^@modules(.*)": "/modules/$1" + }, "testEnvironment": "node" } } diff --git a/prisma/migrations/20230515131219_init/migration.sql b/prisma/migrations/20230609141640_init/migration.sql similarity index 71% rename from prisma/migrations/20230515131219_init/migration.sql rename to prisma/migrations/20230609141640_init/migration.sql index daef9bb..a413ac9 100644 --- a/prisma/migrations/20230515131219_init/migration.sql +++ b/prisma/migrations/20230609141640_init/migration.sql @@ -10,13 +10,13 @@ CREATE TABLE "ad" ( "frequency" "Frequency" NOT NULL, "fromDate" DATE NOT NULL, "toDate" DATE NOT NULL, - "monTime" TEXT, - "tueTime" TEXT, - "wedTime" TEXT, - "thuTime" TEXT, - "friTime" TEXT, - "satTime" TEXT, - "sunTime" TEXT, + "monTime" TIMESTAMPTZ, + "tueTime" TIMESTAMPTZ, + "wedTime" TIMESTAMPTZ, + "thuTime" TIMESTAMPTZ, + "friTime" TIMESTAMPTZ, + "satTime" TIMESTAMPTZ, + "sunTime" TIMESTAMPTZ, "monMargin" INTEGER NOT NULL, "tueMargin" INTEGER NOT NULL, "wedMargin" INTEGER NOT NULL, @@ -24,8 +24,8 @@ CREATE TABLE "ad" ( "friMargin" INTEGER NOT NULL, "satMargin" INTEGER NOT NULL, "sunMargin" INTEGER NOT NULL, - "seatsDriver" SMALLINT NOT NULL, - "seatsPassenger" SMALLINT NOT NULL, + "seatsProposed" SMALLINT NOT NULL, + "seatsRequested" SMALLINT NOT NULL, "strict" BOOLEAN NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -34,7 +34,7 @@ CREATE TABLE "ad" ( ); -- CreateTable -CREATE TABLE "address" ( +CREATE TABLE "waypoint" ( "uuid" UUID NOT NULL, "adUuid" UUID NOT NULL, "position" SMALLINT NOT NULL, @@ -49,8 +49,8 @@ CREATE TABLE "address" ( "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, - CONSTRAINT "address_pkey" PRIMARY KEY ("uuid") + CONSTRAINT "waypoint_pkey" PRIMARY KEY ("uuid") ); -- AddForeignKey -ALTER TABLE "address" ADD CONSTRAINT "address_adUuid_fkey" FOREIGN KEY ("adUuid") REFERENCES "ad"("uuid") ON DELETE CASCADE ON UPDATE CASCADE; +ALTER TABLE "waypoint" ADD CONSTRAINT "waypoint_adUuid_fkey" FOREIGN KEY ("adUuid") REFERENCES "ad"("uuid") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index c6dbca0..ea20ac2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -12,20 +12,20 @@ datasource db { } model Ad { - uuid String @id @default(uuid()) @db.Uuid - userUuid String @db.Uuid + uuid String @id @default(uuid()) @db.Uuid + userUuid String @db.Uuid driver Boolean passenger Boolean frequency Frequency - fromDate DateTime @db.Date - toDate DateTime @db.Date - monTime String? - tueTime String? - wedTime String? - thuTime String? - friTime String? - satTime String? - sunTime String? + fromDate DateTime @db.Date + toDate DateTime @db.Date + monTime DateTime? @db.Timestamptz() + tueTime DateTime? @db.Timestamptz() + wedTime DateTime? @db.Timestamptz() + thuTime DateTime? @db.Timestamptz() + friTime DateTime? @db.Timestamptz() + satTime DateTime? @db.Timestamptz() + sunTime DateTime? @db.Timestamptz() monMargin Int tueMargin Int wedMargin Int @@ -33,17 +33,17 @@ model Ad { friMargin Int satMargin Int sunMargin Int - seatsDriver Int @db.SmallInt - seatsPassenger Int @db.SmallInt + seatsProposed Int @db.SmallInt + seatsRequested Int @db.SmallInt strict Boolean - createdAt DateTime @default(now()) - updatedAt DateTime @default(now()) @updatedAt - addresses Address[] + createdAt DateTime @default(now()) + updatedAt DateTime @default(now()) @updatedAt + waypoints Waypoint[] @@map("ad") } -model Address { +model Waypoint { uuid String @id @default(uuid()) @db.Uuid adUuid String @db.Uuid position Int @db.SmallInt @@ -59,7 +59,7 @@ model Address { updatedAt DateTime @default(now()) @updatedAt Ad Ad @relation(fields: [adUuid], references: [uuid], onDelete: Cascade) - @@map("address") + @@map("waypoint") } enum Frequency { diff --git a/src/app.module.ts b/src/app.module.ts index 4b2ab5c..ee0f470 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,6 +1,6 @@ import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; -import { HealthModule } from './modules/health/health.module'; +// import { HealthModule } from './modules/health/health.module'; import { AdModule } from './modules/ad/ad.module'; import { AutomapperModule } from '@automapper/nestjs'; import { classes } from '@automapper/classes'; @@ -12,55 +12,48 @@ import { ConfigurationModule, ConfigurationModuleOptions, } from '@mobicoop/configuration-module'; +import { EventEmitterModule } from '@nestjs/event-emitter'; +import { RequestContextModule } from 'nestjs-request-context'; @Module({ imports: [ ConfigModule.forRoot({ isGlobal: true }), + EventEmitterModule.forRoot(), + RequestContextModule, AutomapperModule.forRoot({ strategyInitializer: classes() }), - MessageBrokerModule.forRootAsync( - { - imports: [ConfigModule], - inject: [ConfigService], - useFactory: async ( - configService: ConfigService, - ): Promise => ({ + MessageBrokerModule.forRootAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async ( + configService: ConfigService, + ): Promise => ({ + uri: configService.get('MESSAGE_BROKER_URI'), + exchange: configService.get('MESSAGE_BROKER_EXCHANGE'), + name: 'ad', + }), + }), + ConfigurationModule.forRootAsync({ + imports: [ConfigModule], + inject: [ConfigService], + useFactory: async ( + configService: ConfigService, + ): Promise => ({ + domain: configService.get('SERVICE_CONFIGURATION_DOMAIN'), + messageBroker: { uri: configService.get('MESSAGE_BROKER_URI'), exchange: configService.get('MESSAGE_BROKER_EXCHANGE'), - }), - }, - false, - ), - ConfigurationModule.forRootAsync( - { - imports: [ConfigModule], - inject: [ConfigService], - useFactory: async ( - configService: ConfigService, - ): Promise => ({ - domain: configService.get('SERVICE_CONFIGURATION_DOMAIN'), - messageBroker: { - uri: configService.get('MESSAGE_BROKER_URI'), - exchange: configService.get('MESSAGE_BROKER_EXCHANGE'), - }, - redis: { - host: configService.get('REDIS_HOST'), - password: configService.get('REDIS_PASSWORD'), - port: configService.get('REDIS_PORT'), - }, - setConfigurationBrokerRoutingKeys: [ - 'configuration.create', - 'configuration.update', - ], - deleteConfigurationRoutingKey: 'configuration.delete', - propagateConfigurationRoutingKey: 'configuration.propagate', - setConfigurationBrokerQueue: 'ad-configuration-create-update', - deleteConfigurationQueue: 'ad-configuration-delete', - propagateConfigurationQueue: 'ad-configuration-propagate', - }), - }, - true, - ), - HealthModule, + }, + redis: { + host: configService.get('REDIS_HOST'), + password: configService.get('REDIS_PASSWORD'), + port: configService.get('REDIS_PORT'), + }, + setConfigurationBrokerQueue: 'ad-configuration-create-update', + deleteConfigurationQueue: 'ad-configuration-delete', + propagateConfigurationQueue: 'ad-configuration-propagate', + }), + }), + // HealthModule, AdModule, ], controllers: [], diff --git a/src/libs/api/api-error.response.ts b/src/libs/api/api-error.response.ts new file mode 100644 index 0000000..db47f4c --- /dev/null +++ b/src/libs/api/api-error.response.ts @@ -0,0 +1,19 @@ +export class ApiErrorResponse { + readonly statusCode: number; + + readonly message: string; + + readonly error: string; + + readonly correlationId: string; + + readonly subErrors?: string[]; + + constructor(body: ApiErrorResponse) { + this.statusCode = body.statusCode; + this.message = body.message; + this.error = body.error; + this.correlationId = body.correlationId; + this.subErrors = body.subErrors; + } +} diff --git a/src/libs/api/id.response.dto.ts b/src/libs/api/id.response.dto.ts new file mode 100644 index 0000000..aa995e7 --- /dev/null +++ b/src/libs/api/id.response.dto.ts @@ -0,0 +1,7 @@ +export class IdResponse { + constructor(id: string) { + this.id = id; + } + + readonly id: string; +} diff --git a/src/libs/api/paginated.response.base.ts b/src/libs/api/paginated.response.base.ts new file mode 100644 index 0000000..8e8c3d3 --- /dev/null +++ b/src/libs/api/paginated.response.base.ts @@ -0,0 +1,8 @@ +import { Paginated } from '../ddd'; + +export abstract class PaginatedResponseDto extends Paginated { + readonly count: number; + readonly limit: number; + readonly page: number; + abstract readonly data: readonly T[]; +} diff --git a/src/libs/api/response.base.ts b/src/libs/api/response.base.ts new file mode 100644 index 0000000..58dc0e6 --- /dev/null +++ b/src/libs/api/response.base.ts @@ -0,0 +1,23 @@ +import { IdResponse } from './id.response.dto'; + +export interface BaseResponseProps { + id: string; + createdAt: Date; + updatedAt: Date; +} + +/** + * Most of our response objects will have properties like + * id, createdAt and updatedAt so we can move them to a + * separate class and extend it to avoid duplication. + */ +export class ResponseBase extends IdResponse { + constructor(props: BaseResponseProps) { + super(props.id); + this.createdAt = new Date(props.createdAt).toISOString(); + this.updatedAt = new Date(props.updatedAt).toISOString(); + } + + readonly createdAt: string; + readonly updatedAt: string; +} diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts new file mode 100644 index 0000000..4a30d09 --- /dev/null +++ b/src/libs/db/prisma-repository.base.ts @@ -0,0 +1,46 @@ +import { EventEmitter2 } from '@nestjs/event-emitter'; +import { AggregateRoot, Mapper, RepositoryPort } from '../ddd'; +import { ObjectLiteral } from '../types'; +import { LoggerPort } from '../ports/logger.port'; +import { None, Option, Some } from 'oxide.ts'; +import { PrismaRepositoryPort } from '../ports/prisma-repository.port'; + +export abstract class PrismaRepositoryBase< + Aggregate extends AggregateRoot, + DbModel extends ObjectLiteral, +> implements RepositoryPort +{ + protected constructor( + protected readonly prisma: PrismaRepositoryPort | any, + protected readonly mapper: Mapper, + protected readonly eventEmitter: EventEmitter2, + protected readonly logger: LoggerPort, + ) {} + + async findOneById(uuid: string): Promise> { + try { + const entity = await this.prisma.findUnique({ + where: { uuid }, + }); + + return entity ? Some(this.mapper.toDomain(entity)) : None; + } catch (e) { + console.log('ouch on findOneById'); + } + } + + async insert(entity: Aggregate): Promise { + try { + await this.prisma.create({ + data: this.mapper.toPersistence(entity), + }); + } catch (e) { + console.log(e); + console.log('ouch on insert'); + } + } + + async healthCheck(): Promise { + return true; + } +} diff --git a/src/libs/db/prisma-service.ts b/src/libs/db/prisma-service.ts new file mode 100644 index 0000000..edf6532 --- /dev/null +++ b/src/libs/db/prisma-service.ts @@ -0,0 +1,15 @@ +import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; +import { PrismaClient } from '@prisma/client'; + +@Injectable() +export class PrismaService extends PrismaClient implements OnModuleInit { + async onModuleInit() { + await this.$connect(); + } + + async enableShutdownHooks(app: INestApplication) { + this.$on('beforeExit', async () => { + await app.close(); + }); + } +} diff --git a/src/libs/ddd/aggregate-root.base.ts b/src/libs/ddd/aggregate-root.base.ts new file mode 100644 index 0000000..a887396 --- /dev/null +++ b/src/libs/ddd/aggregate-root.base.ts @@ -0,0 +1,35 @@ +import { Entity } from './entity.base'; +import { EventEmitter2 } from '@nestjs/event-emitter'; +import { LoggerPort } from '@libs/ports/logger.port'; +import { DomainEvent } from './domain-event.base'; + +export abstract class AggregateRoot extends Entity { + private _domainEvents: DomainEvent[] = []; + + get domainEvents(): DomainEvent[] { + return this._domainEvents; + } + + protected addEvent(domainEvent: DomainEvent): void { + this._domainEvents.push(domainEvent); + } + + public clearEvents(): void { + this._domainEvents = []; + } + + public async publishEvents( + logger: LoggerPort, + eventEmitter: EventEmitter2, + ): Promise { + await Promise.all( + this.domainEvents.map(async (event) => { + logger.debug( + `"${event.constructor.name}" event published for aggregate ${this.constructor.name} : ${this.id}`, + ); + return eventEmitter.emitAsync(event.constructor.name, event); + }), + ); + this.clearEvents(); + } +} diff --git a/src/libs/ddd/command.base.ts b/src/libs/ddd/command.base.ts new file mode 100644 index 0000000..046db48 --- /dev/null +++ b/src/libs/ddd/command.base.ts @@ -0,0 +1,52 @@ +import { v4 } from 'uuid'; +import { ArgumentNotProvidedException } from '../exceptions'; +import { Guard } from '../guard'; + +export type CommandProps = Omit & Partial; + +type CommandMetadata = { + /** ID for correlation purposes (for commands that + * arrive from other microservices,logs correlation, etc). */ + readonly correlationId: string; + + /** + * Causation id to reconstruct execution order if needed + */ + readonly causationId?: string; + + /** + * ID of a user who invoker the command. Can be useful for + * logging and tracking execution of commands and events + */ + readonly userId?: string; + + /** + * Time when the command occurred. Mostly for tracing purposes + */ + readonly timestamp: number; +}; + +export class Command { + /** + * Command id, in case if we want to save it + * for auditing purposes and create a correlation/causation chain + */ + readonly id: string; + + readonly metadata: CommandMetadata; + + constructor(props: CommandProps) { + if (Guard.isEmpty(props)) { + throw new ArgumentNotProvidedException( + 'Command props should not be empty', + ); + } + this.id = props.id || v4(); + this.metadata = { + correlationId: props?.metadata?.correlationId, + causationId: props?.metadata?.causationId, + timestamp: props?.metadata?.timestamp || Date.now(), + userId: props?.metadata?.userId, + }; + } +} diff --git a/src/libs/ddd/domain-event.base.ts b/src/libs/ddd/domain-event.base.ts new file mode 100644 index 0000000..c905dca --- /dev/null +++ b/src/libs/ddd/domain-event.base.ts @@ -0,0 +1,52 @@ +import { ArgumentNotProvidedException } from '../exceptions'; +import { Guard } from '../guard'; +import { v4 } from 'uuid'; + +type DomainEventMetadata = { + /** Timestamp when this domain event occurred */ + readonly timestamp: number; + + /** ID for correlation purposes (for Integration Events,logs correlation, etc). + */ + readonly correlationId: string; + + /** + * Causation id used to reconstruct execution order if needed + */ + readonly causationId?: string; + + /** + * User ID for debugging and logging purposes + */ + readonly userId?: string; +}; + +export type DomainEventProps = Omit & { + aggregateId: string; + metadata?: DomainEventMetadata; +}; + +export abstract class DomainEvent { + public readonly id: string; + + /** Aggregate ID where domain event occurred */ + public readonly aggregateId: string; + + public readonly metadata: DomainEventMetadata; + + constructor(props: DomainEventProps) { + if (Guard.isEmpty(props)) { + throw new ArgumentNotProvidedException( + 'DomainEvent props should not be empty', + ); + } + this.id = v4(); + this.aggregateId = props.aggregateId; + this.metadata = { + correlationId: props?.metadata?.correlationId, + causationId: props?.metadata?.causationId, + timestamp: props?.metadata?.timestamp || Date.now(), + userId: props?.metadata?.userId, + }; + } +} diff --git a/src/libs/ddd/entity.base.ts b/src/libs/ddd/entity.base.ts new file mode 100644 index 0000000..b946f43 --- /dev/null +++ b/src/libs/ddd/entity.base.ts @@ -0,0 +1,150 @@ +import { + ArgumentNotProvidedException, + ArgumentInvalidException, + ArgumentOutOfRangeException, +} from '../exceptions'; +import { Guard } from '../guard'; +import { convertPropsToObject } from '../utils'; + +export type AggregateID = string; + +export interface BaseEntityProps { + id: AggregateID; + createdAt: Date; + updatedAt: Date; +} + +export interface CreateEntityProps { + id: AggregateID; + props: T; + createdAt?: Date; + updatedAt?: Date; +} + +export abstract class Entity { + constructor({ + id, + createdAt, + updatedAt, + props, + }: CreateEntityProps) { + this.setId(id); + this.validateProps(props); + const now = new Date(); + this._createdAt = createdAt || now; + this._updatedAt = updatedAt || now; + this.props = props; + this.validate(); + } + + protected readonly props: EntityProps; + + /** + * ID is set in the concrete entity implementation to support + * different ID types depending on your needs. + * For example it could be a UUID for aggregate root, + * and shortid / nanoid for child entities. + */ + protected abstract _id: AggregateID; + + private readonly _createdAt: Date; + + private _updatedAt: Date; + + get id(): AggregateID { + return this._id; + } + + private setId(id: AggregateID): void { + this._id = id; + } + + get createdAt(): Date { + return this._createdAt; + } + + get updatedAt(): Date { + return this._updatedAt; + } + + static isEntity(entity: unknown): entity is Entity { + return entity instanceof Entity; + } + + /** + * Checks if two entities are the same Entity by comparing ID field. + * @param object Entity + */ + public equals(object?: Entity): boolean { + if (object === null || object === undefined) { + return false; + } + + if (this === object) { + return true; + } + + if (!Entity.isEntity(object)) { + return false; + } + + return this.id ? this.id === object.id : false; + } + + /** + * Returns entity properties. + * @return {*} {Props & EntityProps} + * @memberof Entity + */ + public getProps(): EntityProps & BaseEntityProps { + const propsCopy = { + id: this._id, + createdAt: this._createdAt, + updatedAt: this._updatedAt, + ...this.props, + }; + return Object.freeze(propsCopy); + } + + /** + * Convert an Entity and all sub-entities/Value Objects it + * contains to a plain object with primitive types. Can be + * useful when logging an entity during testing/debugging + */ + public toObject(): unknown { + const plainProps = convertPropsToObject(this.props); + + const result = { + id: this._id, + createdAt: this._createdAt, + updatedAt: this._updatedAt, + ...plainProps, + }; + return Object.freeze(result); + } + + /** + * There are certain rules that always have to be true (invariants) + * for each entity. Validate method is called every time before + * saving an entity to the database to make sure those rules are respected. + */ + public abstract validate(): void; + + private validateProps(props: EntityProps): void { + const MAX_PROPS = 50; + + if (Guard.isEmpty(props)) { + throw new ArgumentNotProvidedException( + 'Entity props should not be empty', + ); + } + if (typeof props !== 'object') { + throw new ArgumentInvalidException('Entity props should be an object'); + } + if (Object.keys(props as any).length > MAX_PROPS) { + throw new ArgumentOutOfRangeException( + `Entity props should not have more than ${MAX_PROPS} properties`, + ); + } + } +} diff --git a/src/libs/ddd/index.ts b/src/libs/ddd/index.ts new file mode 100644 index 0000000..e0e4a3a --- /dev/null +++ b/src/libs/ddd/index.ts @@ -0,0 +1,7 @@ +export * from './aggregate-root.base'; +export * from './command.base'; +export * from './domain-event.base'; +export * from './entity.base'; +export * from './mapper.interface'; +export * from './repository.port'; +export * from './value-object.base'; diff --git a/src/libs/ddd/mapper.interface.ts b/src/libs/ddd/mapper.interface.ts new file mode 100644 index 0000000..3ce9520 --- /dev/null +++ b/src/libs/ddd/mapper.interface.ts @@ -0,0 +1,11 @@ +import { Entity } from './entity.base'; + +export interface Mapper< + DomainEntity extends Entity, + DbRecord, + Response = any, +> { + toPersistence(entity: DomainEntity): DbRecord; + toDomain(record: any): DomainEntity; + toResponse(entity: DomainEntity): Response; +} diff --git a/src/libs/ddd/repository.port.ts b/src/libs/ddd/repository.port.ts new file mode 100644 index 0000000..8d675ce --- /dev/null +++ b/src/libs/ddd/repository.port.ts @@ -0,0 +1,42 @@ +import { Option } from 'oxide.ts'; + +/* Most of repositories will probably need generic + save/find/delete operations, so it's easier + to have some shared interfaces. + More specific queries should be defined + in a respective repository. +*/ + +export class Paginated { + readonly count: number; + readonly limit: number; + readonly page: number; + readonly data: readonly T[]; + + constructor(props: Paginated) { + this.count = props.count; + this.limit = props.limit; + this.page = props.page; + this.data = props.data; + } +} + +export type OrderBy = { field: string | true; param: 'asc' | 'desc' }; + +export type PaginatedQueryParams = { + limit: number; + page: number; + offset: number; + orderBy: OrderBy; +}; + +export interface RepositoryPort { + insert(entity: Entity | Entity[]): Promise; + findOneById(id: string): Promise>; + healthCheck(): Promise; + // findAll(): Promise; + // findAllPaginated(params: PaginatedQueryParams): Promise>; + // delete(entity: Entity): Promise; + + // transaction(handler: () => Promise): Promise; +} diff --git a/src/libs/ddd/value-object.base.ts b/src/libs/ddd/value-object.base.ts new file mode 100644 index 0000000..348d7a9 --- /dev/null +++ b/src/libs/ddd/value-object.base.ts @@ -0,0 +1,71 @@ +import { ArgumentNotProvidedException } from '../exceptions'; +import { Guard } from '../guard'; +import { convertPropsToObject } from '../utils'; + +/** + * Domain Primitive is an object that contains only a single value + */ +export type Primitives = string | number | boolean; +export interface DomainPrimitive { + value: T; +} + +type ValueObjectProps = T extends Primitives | Date ? DomainPrimitive : T; + +export abstract class ValueObject { + protected readonly props: ValueObjectProps; + + constructor(props: ValueObjectProps) { + this.checkIfEmpty(props); + this.validate(props); + this.props = props; + } + + protected abstract validate(props: ValueObjectProps): void; + + static isValueObject(obj: unknown): obj is ValueObject { + return obj instanceof ValueObject; + } + + /** + * Check if two Value Objects are equal. Checks structural equality. + * @param vo ValueObject + */ + public equals(vo?: ValueObject): boolean { + if (vo === null || vo === undefined) { + return false; + } + return JSON.stringify(this) === JSON.stringify(vo); + } + + /** + * Unpack a value object to get its raw properties + */ + public unpack(): T { + if (this.isDomainPrimitive(this.props)) { + return this.props.value; + } + + const propsCopy = convertPropsToObject(this.props); + + return Object.freeze(propsCopy); + } + + private checkIfEmpty(props: ValueObjectProps): void { + if ( + Guard.isEmpty(props) || + (this.isDomainPrimitive(props) && Guard.isEmpty(props.value)) + ) { + throw new ArgumentNotProvidedException('Property cannot be empty'); + } + } + + private isDomainPrimitive( + obj: unknown, + ): obj is DomainPrimitive { + if (Object.prototype.hasOwnProperty.call(obj, 'value')) { + return true; + } + return false; + } +} diff --git a/src/libs/exceptions/exception.base.ts b/src/libs/exceptions/exception.base.ts new file mode 100644 index 0000000..4c9ebbc --- /dev/null +++ b/src/libs/exceptions/exception.base.ts @@ -0,0 +1,63 @@ +export interface SerializedException { + message: string; + code: string; + correlationId: string; + stack?: string; + cause?: string; + metadata?: unknown; + /** + * ^ Consider adding optional `metadata` object to + * exceptions (if language doesn't support anything + * similar by default) and pass some useful technical + * information about the exception when throwing. + * This will make debugging easier. + */ +} + +/** + * Base class for custom exceptions. + * + * @abstract + * @class ExceptionBase + * @extends {Error} + */ +export abstract class ExceptionBase extends Error { + abstract code: string; + + public readonly correlationId: string; + + /** + * @param {string} message + * @param {ObjectLiteral} [metadata={}] + * **BE CAREFUL** not to include sensitive info in 'metadata' + * to prevent leaks since all exception's data will end up + * in application's log files. Only include non-sensitive + * info that may help with debugging. + */ + constructor( + readonly message: string, + readonly cause?: Error, + readonly metadata?: unknown, + ) { + super(message); + Error.captureStackTrace(this, this.constructor); + } + + /** + * By default in NodeJS Error objects are not + * serialized properly when sending plain objects + * to external processes. This method is a workaround. + * Keep in mind not to return a stack trace to user when in production. + * https://iaincollins.medium.com/error-handling-in-javascript-a6172ccdf9af + */ + toJSON(): SerializedException { + return { + message: this.message, + code: this.code, + stack: this.stack, + correlationId: this.correlationId, + cause: JSON.stringify(this.cause), + metadata: this.metadata, + }; + } +} diff --git a/src/libs/exceptions/exception.codes.ts b/src/libs/exceptions/exception.codes.ts new file mode 100644 index 0000000..3291bfb --- /dev/null +++ b/src/libs/exceptions/exception.codes.ts @@ -0,0 +1,15 @@ +/** + * Adding a `code` string with a custom status code for every + * exception is a good practice, since when that exception + * is transferred to another process `instanceof` check + * cannot be performed anymore so a `code` string is used instead. + * code constants can be stored in a separate file so they + * can be shared and reused on a receiving side (code sharing is + * useful when developing fullstack apps or microservices) + */ +export const ARGUMENT_INVALID = 'GENERIC.ARGUMENT_INVALID'; +export const ARGUMENT_OUT_OF_RANGE = 'GENERIC.ARGUMENT_OUT_OF_RANGE'; +export const ARGUMENT_NOT_PROVIDED = 'GENERIC.ARGUMENT_NOT_PROVIDED'; +export const NOT_FOUND = 'GENERIC.NOT_FOUND'; +export const CONFLICT = 'GENERIC.CONFLICT'; +export const INTERNAL_SERVER_ERROR = 'GENERIC.INTERNAL_SERVER_ERROR'; diff --git a/src/libs/exceptions/exceptions.ts b/src/libs/exceptions/exceptions.ts new file mode 100644 index 0000000..7044dbd --- /dev/null +++ b/src/libs/exceptions/exceptions.ts @@ -0,0 +1,82 @@ +import { + ARGUMENT_INVALID, + ARGUMENT_NOT_PROVIDED, + ARGUMENT_OUT_OF_RANGE, + CONFLICT, + INTERNAL_SERVER_ERROR, + NOT_FOUND, +} from '.'; +import { ExceptionBase } from './exception.base'; + +/** + * Used to indicate that an incorrect argument was provided to a method/function/class constructor + * + * @class ArgumentInvalidException + * @extends {ExceptionBase} + */ +export class ArgumentInvalidException extends ExceptionBase { + readonly code = ARGUMENT_INVALID; +} + +/** + * Used to indicate that an argument was not provided (is empty object/array, null of undefined). + * + * @class ArgumentNotProvidedException + * @extends {ExceptionBase} + */ +export class ArgumentNotProvidedException extends ExceptionBase { + readonly code = ARGUMENT_NOT_PROVIDED; +} + +/** + * Used to indicate that an argument is out of allowed range + * (for example: incorrect string/array length, number not in allowed min/max range etc) + * + * @class ArgumentOutOfRangeException + * @extends {ExceptionBase} + */ +export class ArgumentOutOfRangeException extends ExceptionBase { + readonly code = ARGUMENT_OUT_OF_RANGE; +} + +/** + * Used to indicate conflicting entities (usually in the database) + * + * @class ConflictException + * @extends {ExceptionBase} + */ +export class ConflictException extends ExceptionBase { + readonly code = CONFLICT; +} + +/** + * Used to indicate that entity is not found + * + * @class NotFoundException + * @extends {ExceptionBase} + */ +export class NotFoundException extends ExceptionBase { + static readonly message = 'Not found'; + + constructor(message = NotFoundException.message) { + super(message); + } + + readonly code = NOT_FOUND; +} + +/** + * Used to indicate an internal server error that does not fall under all other errors + * + * @class InternalServerErrorException + * @extends {ExceptionBase} + */ +export class InternalServerErrorException extends ExceptionBase { + static readonly message = 'Internal server error'; + + constructor(message = InternalServerErrorException.message) { + super(message); + } + + readonly code = INTERNAL_SERVER_ERROR; +} diff --git a/src/libs/exceptions/index.ts b/src/libs/exceptions/index.ts new file mode 100644 index 0000000..f14fac5 --- /dev/null +++ b/src/libs/exceptions/index.ts @@ -0,0 +1,3 @@ +export * from './exception.base'; +export * from './exception.codes'; +export * from './exceptions'; diff --git a/src/libs/guard.ts b/src/libs/guard.ts new file mode 100644 index 0000000..31b7a2b --- /dev/null +++ b/src/libs/guard.ts @@ -0,0 +1,55 @@ +export class Guard { + /** + * Checks if value is empty. Accepts strings, numbers, booleans, objects and arrays. + */ + static isEmpty(value: unknown): boolean { + if (typeof value === 'number' || typeof value === 'boolean') { + return false; + } + if (typeof value === 'undefined' || value === null) { + return true; + } + if (value instanceof Date) { + return false; + } + if (value instanceof Object && !Object.keys(value).length) { + return true; + } + if (Array.isArray(value)) { + if (value.length === 0) { + return true; + } + if (value.every((item) => Guard.isEmpty(item))) { + return true; + } + } + if (value === '') { + return true; + } + + return false; + } + + /** + * Checks length range of a provided number/string/array + */ + static lengthIsBetween( + value: number | string | Array, + min: number, + max: number, + ): boolean { + if (Guard.isEmpty(value)) { + throw new Error( + 'Cannot check length of a value. Provided value is empty', + ); + } + const valueLength = + typeof value === 'number' + ? Number(value).toString().length + : value.length; + if (valueLength >= min && valueLength <= max) { + return true; + } + return false; + } +} diff --git a/src/libs/ports/logger.port.ts b/src/libs/ports/logger.port.ts new file mode 100644 index 0000000..43bf996 --- /dev/null +++ b/src/libs/ports/logger.port.ts @@ -0,0 +1,6 @@ +export interface LoggerPort { + log(message: string, ...meta: unknown[]): void; + error(message: string, trace?: unknown, ...meta: unknown[]): void; + warn(message: string, ...meta: unknown[]): void; + debug(message: string, ...meta: unknown[]): void; +} diff --git a/src/libs/ports/prisma-repository.port.ts b/src/libs/ports/prisma-repository.port.ts new file mode 100644 index 0000000..70cfcee --- /dev/null +++ b/src/libs/ports/prisma-repository.port.ts @@ -0,0 +1,4 @@ +export interface PrismaRepositoryPort { + findUnique(options: any): Promise; + create(entity: any): Promise; +} diff --git a/src/libs/types/index.ts b/src/libs/types/index.ts new file mode 100644 index 0000000..da248df --- /dev/null +++ b/src/libs/types/index.ts @@ -0,0 +1,6 @@ +/** Consider creating a bunch of shared custom utility + * types for different situations. + * Alternatively you can use a library like + * https://github.com/andnp/SimplyTyped + */ +export * from './object-literal.type'; diff --git a/src/libs/types/object-literal.type.ts b/src/libs/types/object-literal.type.ts new file mode 100644 index 0000000..c8d50ec --- /dev/null +++ b/src/libs/types/object-literal.type.ts @@ -0,0 +1,6 @@ +/** + * Interface of the simple literal object with any string keys. + */ +export interface ObjectLiteral { + [key: string]: unknown; +} diff --git a/src/libs/utils/convert-props-to-object.util.ts b/src/libs/utils/convert-props-to-object.util.ts new file mode 100644 index 0000000..4605fc7 --- /dev/null +++ b/src/libs/utils/convert-props-to-object.util.ts @@ -0,0 +1,47 @@ +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types */ +import { Entity } from '../ddd/entity.base'; +import { ValueObject } from '../ddd/value-object.base'; + +function isEntity(obj: unknown): obj is Entity { + /** + * 'instanceof Entity' causes error here for some reason. + * Probably creates some circular dependency. This is a workaround + * until I find a solution :) + */ + return ( + Object.prototype.hasOwnProperty.call(obj, 'toObject') && + Object.prototype.hasOwnProperty.call(obj, 'id') && + ValueObject.isValueObject((obj as Entity).id) + ); +} + +function convertToPlainObject(item: any): any { + if (ValueObject.isValueObject(item)) { + return item.unpack(); + } + if (isEntity(item)) { + return item.toObject(); + } + return item; +} + +/** + * Converts Entity/Value Objects props to a plain object. + * Useful for testing and debugging. + * @param props + */ +export function convertPropsToObject(props: any): any { + const propsCopy = structuredClone(props); + + // eslint-disable-next-line guard-for-in + for (const prop in propsCopy) { + if (Array.isArray(propsCopy[prop])) { + propsCopy[prop] = (propsCopy[prop] as Array).map((item) => { + return convertToPlainObject(item); + }); + } + propsCopy[prop] = convertToPlainObject(propsCopy[prop]); + } + + return propsCopy; +} diff --git a/src/libs/utils/index.ts b/src/libs/utils/index.ts new file mode 100644 index 0000000..546588f --- /dev/null +++ b/src/libs/utils/index.ts @@ -0,0 +1 @@ +export * from './convert-props-to-object.util'; diff --git a/src/main.ts b/src/main.ts index 1303641..1c06331 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,7 +13,7 @@ async function bootstrap() { options: { package: ['ad', 'health'], protoPath: [ - join(__dirname, 'modules/ad/adapters/primaries/ad.proto'), + join(__dirname, 'modules/ad/interface/ad.proto'), join(__dirname, 'modules/health/adapters/primaries/health.proto'), ], url: process.env.SERVICE_URL + ':' + process.env.SERVICE_PORT, diff --git a/src/modules/ad/ad.constants.ts b/src/modules/ad/ad.constants.ts deleted file mode 100644 index 08f6845..0000000 --- a/src/modules/ad/ad.constants.ts +++ /dev/null @@ -1 +0,0 @@ -export const PARAMS_PROVIDER = Symbol(); diff --git a/src/modules/ad/ad.di-tokens.ts b/src/modules/ad/ad.di-tokens.ts new file mode 100644 index 0000000..b2942e0 --- /dev/null +++ b/src/modules/ad/ad.di-tokens.ts @@ -0,0 +1,3 @@ +export const PARAMS_PROVIDER = Symbol('PARAMS_PROVIDER'); +export const TIMEZONE_FINDER = Symbol('TIMEZONE_FINDER'); +export const AD_REPOSITORY = Symbol('AD_REPOSITORY'); diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts new file mode 100644 index 0000000..40d4f45 --- /dev/null +++ b/src/modules/ad/ad.mapper.ts @@ -0,0 +1,228 @@ +import { Mapper } from '@libs/ddd'; +import { AdResponseDto } from './interface/dtos/ad.response.dto'; +import { Inject, Injectable } from '@nestjs/common'; +import { AdEntity } from './core/ad.entity'; +import { AdModel, WaypointModel } from './infrastructure/ad.repository'; +import { Frequency } from './core/ad.types'; +import { WaypointProps } from './core/value-objects/waypoint.value-object'; +import { v4 } from 'uuid'; +import { PARAMS_PROVIDER, TIMEZONE_FINDER } from './ad.di-tokens'; +import { TimezoneFinderPort } from './core/ports/timezone-finder.port'; +import { Coordinates } from './core/types/coordinates'; +import { DefaultParamsProviderPort } from './core/ports/default-params-provider.port'; +import { DefaultParams } from './core/ports/default-params.type'; +import { DateTime, TimeZone } from 'timezonecomplete'; + +/** + * Mapper constructs objects that are used in different layers: + * Record is an object that is stored in a database, + * Entity is an object that is used in application domain layer, + * and a ResponseDTO is an object returned to a user (usually as json). + */ + +@Injectable() +export class AdMapper implements Mapper { + private timezone: string; + private readonly defaultParams: DefaultParams; + + constructor( + @Inject(PARAMS_PROVIDER) + private readonly defaultParamsProvider: DefaultParamsProviderPort, + @Inject(TIMEZONE_FINDER) + private readonly timezoneFinder: TimezoneFinderPort, + ) { + this.defaultParams = defaultParamsProvider.getParams(); + } + + toPersistence = (entity: AdEntity): AdModel => { + const copy = entity.getProps(); + const timezone = this.getTimezone(copy.waypoints[0].address.coordinates); + const now = new Date(); + const record: AdModel = { + uuid: copy.id, + userUuid: copy.userId, + driver: copy.driver, + passenger: copy.passenger, + frequency: copy.frequency, + fromDate: new Date(copy.fromDate), + toDate: new Date(copy.toDate), + monTime: copy.schedule.mon + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.mon, + timezone, + ) + : undefined, + tueTime: copy.schedule.tue + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.tue, + timezone, + ) + : undefined, + wedTime: copy.schedule.wed + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.wed, + timezone, + ) + : undefined, + thuTime: copy.schedule.thu + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.thu, + timezone, + ) + : undefined, + friTime: copy.schedule.fri + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.fri, + timezone, + ) + : undefined, + satTime: copy.schedule.sat + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.sat, + timezone, + ) + : undefined, + sunTime: copy.schedule.sun + ? AdMapper.toUtcDatetime( + new Date(copy.fromDate), + copy.schedule.sun, + timezone, + ) + : undefined, + monMargin: copy.marginDurations.mon, + tueMargin: copy.marginDurations.tue, + wedMargin: copy.marginDurations.wed, + thuMargin: copy.marginDurations.thu, + friMargin: copy.marginDurations.fri, + satMargin: copy.marginDurations.sat, + sunMargin: copy.marginDurations.sun, + seatsProposed: copy.seatsProposed, + seatsRequested: copy.seatsRequested, + strict: copy.strict, + waypoints: { + create: copy.waypoints.map((waypoint: WaypointProps) => ({ + uuid: v4(), + position: waypoint.position, + name: waypoint.address.name, + houseNumber: waypoint.address.houseNumber, + street: waypoint.address.street, + locality: waypoint.address.locality, + postalCode: waypoint.address.postalCode, + country: waypoint.address.country, + lon: waypoint.address.coordinates.lon, + lat: waypoint.address.coordinates.lat, + createdAt: now, + updatedAt: now, + })), + }, + createdAt: copy.createdAt, + updatedAt: copy.updatedAt, + }; + return record; + }; + + toDomain = (record: AdModel): AdEntity => { + const entity = new AdEntity({ + id: record.uuid, + createdAt: new Date(record.createdAt), + updatedAt: new Date(record.updatedAt), + props: { + userId: record.userUuid, + driver: record.driver, + passenger: record.passenger, + frequency: Frequency[record.frequency], + fromDate: record.fromDate.toISOString(), + toDate: record.toDate.toISOString(), + schedule: { + mon: record.monTime.toISOString(), + tue: record.tueTime.toISOString(), + wed: record.wedTime.toISOString(), + thu: record.thuTime.toISOString(), + fri: record.friTime.toISOString(), + sat: record.satTime.toISOString(), + sun: record.sunTime.toISOString(), + }, + marginDurations: { + mon: record.monMargin, + tue: record.tueMargin, + wed: record.wedMargin, + thu: record.thuMargin, + fri: record.friMargin, + sat: record.satMargin, + sun: record.sunMargin, + }, + seatsProposed: record.seatsProposed, + seatsRequested: record.seatsRequested, + strict: record.strict, + waypoints: record.waypoints.create.map((waypoint: WaypointModel) => ({ + position: waypoint.position, + address: { + name: waypoint.name, + houseNumber: waypoint.houseNumber, + street: waypoint.street, + postalCode: waypoint.postalCode, + locality: waypoint.locality, + country: waypoint.country, + coordinates: { + lon: waypoint.lon, + lat: waypoint.lat, + }, + }, + })), + }, + }); + return entity; + }; + + toResponse = (entity: AdEntity): AdResponseDto => { + const props = entity.getProps(); + const response = new AdResponseDto(entity); + response.uuid = props.id; + return response; + }; + + /* ^ Data returned to the user is whitelisted to avoid leaks. + If a new property is added, like password or a + credit card number, it won't be returned + unless you specifically allow this. + (avoid blacklisting, which will return everything + but blacklisted items, which can lead to a data leak). + */ + + private getTimezone = (coordinates: Coordinates): string => { + try { + const timezones = this.timezoneFinder.timezones( + coordinates.lon, + coordinates.lat, + ); + if (timezones.length > 0) return timezones[0]; + } catch (e) {} + return this.defaultParams.DEFAULT_TIMEZONE; + }; + + private static toUtcDatetime = ( + date: Date, + time: string, + timezone: string, + ): Date => { + try { + if (!date || !time || !timezone) throw new Error(); + return new Date( + new DateTime( + `${date.toISOString().split('T')[0]}T${time}:00`, + TimeZone.zone(timezone, false), + ) + .convert(TimeZone.zone('UTC')) + .toIsoString(), + ); + } catch (e) { + return undefined; + } + }; +} diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index a7e0218..505e5df 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -1,25 +1,33 @@ import { Module } from '@nestjs/common'; -import { AdController } from './adapters/primaries/ad.controller'; +import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller'; import { DatabaseModule } from '../database/database.module'; import { CqrsModule } from '@nestjs/cqrs'; -import { AdProfile } from './mappers/ad.profile'; -import { AdsRepository } from './adapters/secondaries/ads.repository'; -import { FindAdByUuidUseCase } from './domain/usecases/find-ad-by-uuid.usecase'; -import { CreateAdUseCase } from './domain/usecases/create-ad.usecase'; -import { DefaultParamsProvider } from './adapters/secondaries/default-params.provider'; -import { PARAMS_PROVIDER } from './ad.constants'; +import { + AD_REPOSITORY, + PARAMS_PROVIDER, + TIMEZONE_FINDER, +} from './ad.di-tokens'; import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; -import { MessagePublisher } from './adapters/secondaries/message-publisher'; +import { AdRepository } from './infrastructure/ad.repository'; +import { DefaultParamsProvider } from './infrastructure/default-params-provider'; +import { MessagePublisher } from './infrastructure/message-publisher'; +import { PrismaService } from './infrastructure/prisma-service'; +import { AdMapper } from './ad.mapper'; +import { CreateAdService } from './core/commands/create-ad/create-ad.service'; +import { TimezoneFinder } from './infrastructure/timezone-finder'; @Module({ imports: [DatabaseModule, CqrsModule], - controllers: [AdController], + controllers: [CreateAdGrpcController], providers: [ - AdProfile, - AdsRepository, - FindAdByUuidUseCase, - CreateAdUseCase, + CreateAdService, + PrismaService, + AdMapper, + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, { provide: PARAMS_PROVIDER, useClass: DefaultParamsProvider, @@ -32,6 +40,10 @@ import { MessagePublisher } from './adapters/secondaries/message-publisher'; provide: MESSAGE_PUBLISHER, useClass: MessagePublisher, }, + { + provide: TIMEZONE_FINDER, + useClass: TimezoneFinder, + }, ], }) export class AdModule {} diff --git a/src/modules/ad/adapters/primaries/ad.controller.ts b/src/modules/ad/adapters/primaries/ad.controller.ts deleted file mode 100644 index b3aa278..0000000 --- a/src/modules/ad/adapters/primaries/ad.controller.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { Mapper } from '@automapper/core'; -import { InjectMapper } from '@automapper/nestjs'; -import { Controller, UsePipes } from '@nestjs/common'; -import { CommandBus, QueryBus } from '@nestjs/cqrs'; -import { GrpcMethod, RpcException } from '@nestjs/microservices'; -import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; -import { FindAdByUuidRequest } from '../../domain/dtos/find-ad-by-uuid.request'; -import { AdPresenter } from './ad.presenter'; -import { FindAdByUuidQuery } from '../../queries/find-ad-by-uuid.query'; -import { Ad } from '../../domain/entities/ad'; -import { CreateAdRequest } from '../../domain/dtos/create-ad.request'; -import { CreateAdCommand } from '../../commands/create-ad.command'; -import { DatabaseException } from '../../../database/exceptions/database.exception'; - -@UsePipes( - new RpcValidationPipe({ - whitelist: false, - forbidUnknownValues: false, - }), -) -@Controller() -export class AdController { - constructor( - private readonly commandBus: CommandBus, - private readonly queryBus: QueryBus, - @InjectMapper() private readonly mapper: Mapper, - ) {} - - @GrpcMethod('AdsService', 'FindOneByUuid') - async findOnebyUuid(data: FindAdByUuidRequest): Promise { - try { - const ad = await this.queryBus.execute(new FindAdByUuidQuery(data)); - return this.mapper.map(ad, Ad, AdPresenter); - } catch (e) { - throw new RpcException({ - code: e.code, - message: e.message, - }); - } - } - - @GrpcMethod('AdsService', 'Create') - async createAd(data: CreateAdRequest): Promise { - try { - const ad = await this.commandBus.execute(new CreateAdCommand(data)); - return this.mapper.map(ad, Ad, AdPresenter); - } catch (e) { - if (e instanceof DatabaseException) { - if (e.message.includes('Already exists')) { - throw new RpcException({ - code: 6, - message: 'Ad already exists', - }); - } - } - throw new RpcException({}); - } - } -} diff --git a/src/modules/ad/adapters/primaries/ad.proto b/src/modules/ad/adapters/primaries/ad.proto deleted file mode 100644 index 7014838..0000000 --- a/src/modules/ad/adapters/primaries/ad.proto +++ /dev/null @@ -1,83 +0,0 @@ -syntax = "proto3"; - -package ad; - -service AdsService { - rpc FindOneByUuid(AdByUuid) returns (Ad); - rpc FindAll(AdFilter) returns (Ads); - rpc Create(Ad) returns (AdByUuid); - rpc Update(Ad) returns (Ad); - rpc Delete(AdByUuid) returns (Empty); -} - -message AdByUuid { - string uuid = 1; -} - -message Ad { - string uuid = 1; - string userUuid = 2; - bool driver = 3; - bool passenger = 4; - Frequency frequency = 5; - optional string departure = 6; - string fromDate = 7; - string toDate = 8; - Schedule schedule = 9; - MarginDurations marginDurations = 10; - int32 seatsPassenger = 11; - int32 seatsDriver = 12; - bool strict = 13; - repeated Address addresses = 14; -} - -message Schedule { - optional string mon = 1; - optional string tue = 2; - optional string wed = 3; - optional string thu = 4; - optional string fri = 5; - optional string sat = 6; - optional string sun = 7; -} - -message MarginDurations { - int32 mon = 1; - int32 tue = 2; - int32 wed = 3; - int32 thu = 4; - int32 fri = 5; - int32 sat = 6; - int32 sun = 7; -} - -message Address { - string uuid = 1; - int32 position = 2; - float lon = 3; - float lat = 4; - optional string name = 5; - optional string houseNumber = 6; - optional string street = 7; - optional string locality = 8; - optional string postalCode = 9; - string country = 10; -} - - -enum Frequency { - PUNCTUAL = 1; - RECURRENT = 2; -} - -message AdFilter { - int32 page = 1; - int32 perPage = 2; -} - -message Ads { - repeated Ad data = 1; - int32 total = 2; -} - -message Empty {} diff --git a/src/modules/ad/adapters/secondaries/ads.repository.ts b/src/modules/ad/adapters/secondaries/ads.repository.ts deleted file mode 100644 index a4b0b28..0000000 --- a/src/modules/ad/adapters/secondaries/ads.repository.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { AdRepository } from '../../../database/domain/ad-repository'; -import { Ad } from '../../domain/entities/ad'; -//TODO : properly implement mutate operation to prisma -@Injectable() -export class AdsRepository extends AdRepository { - protected model = 'ad'; -} diff --git a/src/modules/ad/commands/create-ad.command.ts b/src/modules/ad/commands/create-ad.command.ts deleted file mode 100644 index b4f1e8d..0000000 --- a/src/modules/ad/commands/create-ad.command.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { CreateAdRequest } from '../domain/dtos/create-ad.request'; - -export class CreateAdCommand { - readonly createAdRequest: CreateAdRequest; - - constructor(request: CreateAdRequest) { - this.createAdRequest = request; - } -} diff --git a/src/modules/ad/core/ad.entity.ts b/src/modules/ad/core/ad.entity.ts new file mode 100644 index 0000000..3d682ab --- /dev/null +++ b/src/modules/ad/core/ad.entity.ts @@ -0,0 +1,137 @@ +import { AggregateRoot, AggregateID } from '@libs/ddd'; +import { v4 } from 'uuid'; +import { AdCreatedDomainEvent } from './events/ad-created.domain-events'; +import { AdProps, CreateAdProps, DefaultAdProps } from './ad.types'; +import { Waypoint } from './value-objects/waypoint.value-object'; +import { MarginDurationsProps } from './value-objects/margin-durations.value-object'; + +export class AdEntity extends AggregateRoot { + protected readonly _id: AggregateID; + + static create = ( + create: CreateAdProps, + defaultAdProps: DefaultAdProps, + ): AdEntity => { + const id = v4(); + const props: AdProps = { ...create }; + const ad = new AdEntity({ id, props }) + .setMissingMarginDurations(defaultAdProps.marginDurations) + .setMissingStrict(defaultAdProps.strict) + .setDefaultDriverAndPassengerParameters({ + driver: defaultAdProps.driver, + passenger: defaultAdProps.passenger, + seatsProposed: defaultAdProps.seatsProposed, + seatsRequested: defaultAdProps.seatsRequested, + }) + .setMissingWaypointsPosition(); + ad.addEvent( + new AdCreatedDomainEvent({ + aggregateId: id, + userId: props.userId, + driver: props.driver, + passenger: props.passenger, + frequency: props.frequency, + fromDate: props.fromDate, + toDate: props.toDate, + monTime: props.schedule.mon, + tueTime: props.schedule.tue, + wedTime: props.schedule.wed, + thuTime: props.schedule.thu, + friTime: props.schedule.fri, + satTime: props.schedule.sat, + sunTime: props.schedule.sun, + monMarginDuration: props.marginDurations.mon, + tueMarginDuration: props.marginDurations.tue, + wedMarginDuration: props.marginDurations.wed, + thuMarginDuration: props.marginDurations.thu, + friMarginDuration: props.marginDurations.fri, + satMarginDuration: props.marginDurations.sat, + sunMarginDuration: props.marginDurations.sun, + seatsProposed: props.seatsProposed, + seatsRequested: props.seatsRequested, + strict: props.strict, + waypoints: props.waypoints.map((waypoint: Waypoint) => ({ + position: waypoint.position, + name: waypoint.address.name, + houseNumber: waypoint.address.houseNumber, + street: waypoint.address.street, + postalCode: waypoint.address.postalCode, + locality: waypoint.address.locality, + country: waypoint.address.postalCode, + lon: waypoint.address.coordinates.lon, + lat: waypoint.address.coordinates.lat, + })), + }), + ); + return ad; + }; + + private setMissingMarginDurations = ( + defaultMarginDurations: MarginDurationsProps, + ): AdEntity => { + if (!this.props.marginDurations) this.props.marginDurations = {}; + if (!this.props.marginDurations.mon) + this.props.marginDurations.mon = defaultMarginDurations.mon; + if (!this.props.marginDurations.tue) + this.props.marginDurations.tue = defaultMarginDurations.tue; + if (!this.props.marginDurations.wed) + this.props.marginDurations.wed = defaultMarginDurations.wed; + if (!this.props.marginDurations.thu) + this.props.marginDurations.thu = defaultMarginDurations.thu; + if (!this.props.marginDurations.fri) + this.props.marginDurations.fri = defaultMarginDurations.fri; + if (!this.props.marginDurations.sat) + this.props.marginDurations.sat = defaultMarginDurations.sat; + if (!this.props.marginDurations.sun) + this.props.marginDurations.sun = defaultMarginDurations.sun; + return this; + }; + + private setMissingStrict = (strict: boolean): AdEntity => { + if (this.props.strict === undefined) this.props.strict = strict; + return this; + }; + + private setDefaultDriverAndPassengerParameters = ( + defaultDriverAndPassengerParameters: DefaultDriverAndPassengerParameters, + ): AdEntity => { + this.props.driver = !!this.props.driver; + this.props.passenger = !!this.props.passenger; + if (!this.props.driver && !this.props.passenger) { + this.props.driver = defaultDriverAndPassengerParameters.driver; + this.props.seatsProposed = + defaultDriverAndPassengerParameters.seatsProposed; + this.props.passenger = defaultDriverAndPassengerParameters.passenger; + this.props.seatsRequested = + defaultDriverAndPassengerParameters.seatsRequested; + return this; + } + if (!this.props.seatsProposed || this.props.seatsProposed <= 0) + this.props.seatsProposed = + defaultDriverAndPassengerParameters.seatsProposed; + if (!this.props.seatsRequested || this.props.seatsRequested <= 0) + this.props.seatsRequested = + defaultDriverAndPassengerParameters.seatsRequested; + return this; + }; + + private setMissingWaypointsPosition = (): AdEntity => { + if (this.props.waypoints[0].position === undefined) { + for (let i = 0; i < this.props.waypoints.length; i++) { + this.props.waypoints[i].position = i; + } + } + return this; + }; + + validate(): void { + // entity business rules validation to protect it's invariant before saving entity to a database + } +} + +interface DefaultDriverAndPassengerParameters { + driver: boolean; + passenger: boolean; + seatsProposed: number; + seatsRequested: number; +} diff --git a/src/modules/ad/core/ad.errors.ts b/src/modules/ad/core/ad.errors.ts new file mode 100644 index 0000000..474edf9 --- /dev/null +++ b/src/modules/ad/core/ad.errors.ts @@ -0,0 +1,11 @@ +import { ExceptionBase } from '@libs/exceptions'; + +export class AdAlreadyExistsError extends ExceptionBase { + static readonly message = 'Ad already exists'; + + public readonly code = 'AD.ALREADY_EXISTS'; + + constructor(cause?: Error, metadata?: unknown) { + super(AdAlreadyExistsError.message, cause, metadata); + } +} diff --git a/src/modules/ad/core/ad.types.ts b/src/modules/ad/core/ad.types.ts new file mode 100644 index 0000000..b24502d --- /dev/null +++ b/src/modules/ad/core/ad.types.ts @@ -0,0 +1,49 @@ +import { MarginDurationsProps } from './value-objects/margin-durations.value-object'; +import { ScheduleProps } from './value-objects/schedule.value-object'; +import { WaypointProps } from './value-objects/waypoint.value-object'; + +// All properties that an Ad has +export interface AdProps { + userId: string; + driver: boolean; + passenger: boolean; + frequency: Frequency; + fromDate: string; + toDate: string; + schedule: ScheduleProps; + marginDurations: MarginDurationsProps; + seatsProposed: number; + seatsRequested: number; + strict: boolean; + waypoints: WaypointProps[]; +} + +// Properties that are needed for an Ad creation +export interface CreateAdProps { + userId: string; + driver: boolean; + passenger: boolean; + frequency: Frequency; + fromDate: string; + toDate: string; + schedule: ScheduleProps; + marginDurations: MarginDurationsProps; + seatsProposed: number; + seatsRequested: number; + strict: boolean; + waypoints: WaypointProps[]; +} + +export interface DefaultAdProps { + driver: boolean; + passenger: boolean; + marginDurations: MarginDurationsProps; + strict: boolean; + seatsProposed: number; + seatsRequested: number; +} + +export enum Frequency { + PUNCTUAL = 'PUNCTUAL', + RECURRENT = 'RECURRENT', +} diff --git a/src/modules/ad/core/commands/create-ad/create-ad.command.ts b/src/modules/ad/core/commands/create-ad/create-ad.command.ts new file mode 100644 index 0000000..f67d0c4 --- /dev/null +++ b/src/modules/ad/core/commands/create-ad/create-ad.command.ts @@ -0,0 +1,36 @@ +import { Command, CommandProps } from '@libs/ddd'; +import { Frequency } from '@modules/ad/core/ad.types'; +import { Schedule } from '../../types/schedule'; +import { MarginDurations } from '../../types/margin-durations'; +import { Waypoint } from '../../types/waypoint'; + +export class CreateAdCommand extends Command { + readonly userId: string; + readonly driver?: boolean; + readonly passenger?: boolean; + readonly frequency?: Frequency; + readonly fromDate: string; + readonly toDate: string; + readonly schedule: Schedule; + readonly marginDurations?: MarginDurations; + readonly seatsProposed?: number; + readonly seatsRequested?: number; + readonly strict?: boolean; + readonly waypoints: Waypoint[]; + + constructor(props: CommandProps) { + super(props); + this.userId = props.userId; + this.driver = props.driver; + this.passenger = props.passenger; + this.frequency = props.frequency; + this.fromDate = props.fromDate; + this.toDate = props.toDate; + this.schedule = props.schedule; + this.marginDurations = props.marginDurations; + this.seatsProposed = props.seatsProposed; + this.seatsRequested = props.seatsRequested; + this.strict = props.strict; + this.waypoints = props.waypoints; + } +} diff --git a/src/modules/ad/core/commands/create-ad/create-ad.service.ts b/src/modules/ad/core/commands/create-ad/create-ad.service.ts new file mode 100644 index 0000000..1a618e9 --- /dev/null +++ b/src/modules/ad/core/commands/create-ad/create-ad.service.ts @@ -0,0 +1,88 @@ +import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'; +import { CreateAdCommand } from './create-ad.command'; +import { DefaultParams } from '@modules/ad/core/ports/default-params.type'; +import { Inject } from '@nestjs/common'; +import { AD_REPOSITORY, PARAMS_PROVIDER } from '@modules/ad/ad.di-tokens'; +import { AdRepositoryPort } from '@modules/ad/core/ports/ad.repository.port'; +import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; +import { Err, Ok, Result } from 'oxide.ts'; +import { AggregateID } from '@libs/ddd'; +import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; +import { AdEntity } from '@modules/ad/core/ad.entity'; +import { ConflictException } from '@libs/exceptions'; +import { Waypoint } from '../../types/waypoint'; + +@CommandHandler(CreateAdCommand) +export class CreateAdService implements ICommandHandler { + private readonly defaultParams: DefaultParams; + + constructor( + @Inject(AD_REPOSITORY) + private readonly repository: AdRepositoryPort, + @Inject(PARAMS_PROVIDER) + private readonly defaultParamsProvider: DefaultParamsProviderPort, + ) { + this.defaultParams = defaultParamsProvider.getParams(); + } + + async execute( + command: CreateAdCommand, + ): Promise> { + const ad = AdEntity.create( + { + userId: command.userId, + driver: command.driver, + passenger: command.passenger, + frequency: command.frequency, + fromDate: command.fromDate, + toDate: command.toDate, + schedule: command.schedule, + marginDurations: command.marginDurations, + seatsProposed: command.seatsProposed, + seatsRequested: command.seatsRequested, + strict: command.strict, + waypoints: command.waypoints.map((waypoint: Waypoint) => ({ + position: waypoint.position, + address: { + name: waypoint.name, + houseNumber: waypoint.houseNumber, + street: waypoint.street, + postalCode: waypoint.postalCode, + locality: waypoint.locality, + country: waypoint.country, + coordinates: { + lon: waypoint.lon, + lat: waypoint.lat, + }, + }, + })), + }, + { + driver: this.defaultParams.DRIVER, + passenger: this.defaultParams.PASSENGER, + marginDurations: { + mon: this.defaultParams.MON_MARGIN, + tue: this.defaultParams.TUE_MARGIN, + wed: this.defaultParams.WED_MARGIN, + thu: this.defaultParams.THU_MARGIN, + fri: this.defaultParams.FRI_MARGIN, + sat: this.defaultParams.SAT_MARGIN, + sun: this.defaultParams.SUN_MARGIN, + }, + strict: this.defaultParams.STRICT, + seatsProposed: this.defaultParams.SEATS_PROPOSED, + seatsRequested: this.defaultParams.SEATS_REQUESTED, + }, + ); + + try { + await this.repository.insert(ad); + return Ok(ad.id); + } catch (error: any) { + if (error instanceof ConflictException) { + return Err(new AdAlreadyExistsError(error)); + } + throw error; + } + } +} diff --git a/src/modules/ad/core/events/ad-created.domain-events.ts b/src/modules/ad/core/events/ad-created.domain-events.ts new file mode 100644 index 0000000..812e960 --- /dev/null +++ b/src/modules/ad/core/events/ad-created.domain-events.ts @@ -0,0 +1,68 @@ +import { DomainEvent, DomainEventProps } from '@libs/ddd'; + +export class AdCreatedDomainEvent extends DomainEvent { + readonly userId: string; + readonly driver: boolean; + readonly passenger: boolean; + readonly frequency: string; + readonly fromDate: string; + readonly toDate: string; + readonly monTime: string; + readonly tueTime: string; + readonly wedTime: string; + readonly thuTime: string; + readonly friTime: string; + readonly satTime: string; + readonly sunTime: string; + readonly monMarginDuration: number; + readonly tueMarginDuration: number; + readonly wedMarginDuration: number; + readonly thuMarginDuration: number; + readonly friMarginDuration: number; + readonly satMarginDuration: number; + readonly sunMarginDuration: number; + readonly seatsProposed: number; + readonly seatsRequested: number; + readonly strict: boolean; + readonly waypoints: Waypoint[]; + + constructor(props: DomainEventProps) { + super(props); + this.userId = props.userId; + this.driver = props.driver; + this.passenger = props.passenger; + this.frequency = props.frequency; + this.fromDate = props.fromDate; + this.toDate = props.toDate; + this.monTime = props.monTime; + this.tueTime = props.tueTime; + this.wedTime = props.wedTime; + this.thuTime = props.thuTime; + this.friTime = props.friTime; + this.satTime = props.satTime; + this.sunTime = props.sunTime; + this.monMarginDuration = props.monMarginDuration; + this.tueMarginDuration = props.tueMarginDuration; + this.wedMarginDuration = props.wedMarginDuration; + this.thuMarginDuration = props.thuMarginDuration; + this.friMarginDuration = props.friMarginDuration; + this.satMarginDuration = props.satMarginDuration; + this.sunMarginDuration = props.sunMarginDuration; + this.seatsProposed = props.seatsProposed; + this.seatsRequested = props.seatsRequested; + this.strict = props.strict; + this.waypoints = props.waypoints; + } +} + +export class Waypoint { + position: number; + name?: string; + houseNumber?: string; + street?: string; + locality?: string; + postalCode?: string; + country: string; + lon: number; + lat: number; +} diff --git a/src/modules/ad/core/ports/ad.repository.port.ts b/src/modules/ad/core/ports/ad.repository.port.ts new file mode 100644 index 0000000..93f8f77 --- /dev/null +++ b/src/modules/ad/core/ports/ad.repository.port.ts @@ -0,0 +1,4 @@ +import { RepositoryPort } from '@libs/ddd'; +import { AdEntity } from '../ad.entity'; + +export type AdRepositoryPort = RepositoryPort; diff --git a/src/modules/ad/core/ports/default-params-provider.port.ts b/src/modules/ad/core/ports/default-params-provider.port.ts new file mode 100644 index 0000000..e316b77 --- /dev/null +++ b/src/modules/ad/core/ports/default-params-provider.port.ts @@ -0,0 +1,5 @@ +import { DefaultParams } from './default-params.type'; + +export interface DefaultParamsProviderPort { + getParams(): DefaultParams; +} diff --git a/src/modules/ad/domain/types/default-params.type.ts b/src/modules/ad/core/ports/default-params.type.ts similarity index 83% rename from src/modules/ad/domain/types/default-params.type.ts rename to src/modules/ad/core/ports/default-params.type.ts index 8fee5e0..4368936 100644 --- a/src/modules/ad/domain/types/default-params.type.ts +++ b/src/modules/ad/core/ports/default-params.type.ts @@ -7,8 +7,9 @@ export type DefaultParams = { SAT_MARGIN: number; SUN_MARGIN: number; DRIVER: boolean; - SEATS_PROVIDED: number; + SEATS_PROPOSED: number; PASSENGER: boolean; SEATS_REQUESTED: number; STRICT: boolean; + DEFAULT_TIMEZONE: string; }; diff --git a/src/modules/ad/core/ports/timezone-finder.port.ts b/src/modules/ad/core/ports/timezone-finder.port.ts new file mode 100644 index 0000000..ddfbc8b --- /dev/null +++ b/src/modules/ad/core/ports/timezone-finder.port.ts @@ -0,0 +1,3 @@ +export interface TimezoneFinderPort { + timezones(lon: number, lat: number): string[]; +} diff --git a/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts b/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts new file mode 100644 index 0000000..97c8ea0 --- /dev/null +++ b/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts @@ -0,0 +1,9 @@ +import { FindAdByIdRequestDTO } from '../../../interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto'; + +export class FindAdByIdQuery { + readonly id: string; + + constructor(findAdByIdRequestDTO: FindAdByIdRequestDTO) { + this.id = findAdByIdRequestDTO.id; + } +} diff --git a/src/modules/ad/core/types/address.ts b/src/modules/ad/core/types/address.ts new file mode 100644 index 0000000..bdf8e6b --- /dev/null +++ b/src/modules/ad/core/types/address.ts @@ -0,0 +1,10 @@ +import { Coordinates } from './coordinates'; + +export type Address = { + name?: string; + houseNumber?: string; + street?: string; + locality?: string; + postalCode?: string; + country: string; +} & Coordinates; diff --git a/src/modules/ad/core/types/coordinates.ts b/src/modules/ad/core/types/coordinates.ts new file mode 100644 index 0000000..8e149ed --- /dev/null +++ b/src/modules/ad/core/types/coordinates.ts @@ -0,0 +1,4 @@ +export type Coordinates = { + lon: number; + lat: number; +}; diff --git a/src/modules/ad/core/types/margin-durations.ts b/src/modules/ad/core/types/margin-durations.ts new file mode 100644 index 0000000..8e09329 --- /dev/null +++ b/src/modules/ad/core/types/margin-durations.ts @@ -0,0 +1,9 @@ +export type MarginDurations = { + mon?: number; + tue?: number; + wed?: number; + thu?: number; + fri?: number; + sat?: number; + sun?: number; +}; diff --git a/src/modules/ad/core/types/schedule.ts b/src/modules/ad/core/types/schedule.ts new file mode 100644 index 0000000..03f8485 --- /dev/null +++ b/src/modules/ad/core/types/schedule.ts @@ -0,0 +1,9 @@ +export type Schedule = { + mon?: string; + tue?: string; + wed?: string; + thu?: string; + fri?: string; + sat?: string; + sun?: string; +}; diff --git a/src/modules/ad/core/types/waypoint.ts b/src/modules/ad/core/types/waypoint.ts new file mode 100644 index 0000000..c73e024 --- /dev/null +++ b/src/modules/ad/core/types/waypoint.ts @@ -0,0 +1,5 @@ +import { Address } from './address'; + +export type Waypoint = { + position?: number; +} & Address; diff --git a/src/modules/ad/core/value-objects/address.value-object.ts b/src/modules/ad/core/value-objects/address.value-object.ts new file mode 100644 index 0000000..66a5da7 --- /dev/null +++ b/src/modules/ad/core/value-objects/address.value-object.ts @@ -0,0 +1,52 @@ +import { ValueObject } from '@libs/ddd'; +import { CoordinatesProps } from './coordinates.value-object'; + +/** Note: + * Value Objects with multiple properties can contain + * other Value Objects inside if needed. + * */ + +export interface AddressProps { + name?: string; + houseNumber?: string; + street?: string; + locality?: string; + postalCode?: string; + country: string; + coordinates: CoordinatesProps; +} + +export class Address extends ValueObject { + get name(): string { + return this.props.name; + } + + get houseNumber(): string { + return this.props.houseNumber; + } + + get street(): string { + return this.props.street; + } + + get locality(): string { + return this.props.locality; + } + + get postalCode(): string { + return this.props.postalCode; + } + + get country(): string { + return this.props.country; + } + + get coordinates(): CoordinatesProps { + return this.props.coordinates; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + protected validate(props: AddressProps): void { + return; + } +} diff --git a/src/modules/ad/core/value-objects/coordinates.value-object.ts b/src/modules/ad/core/value-objects/coordinates.value-object.ts new file mode 100644 index 0000000..7451d8a --- /dev/null +++ b/src/modules/ad/core/value-objects/coordinates.value-object.ts @@ -0,0 +1,26 @@ +import { ValueObject } from '@libs/ddd'; + +/** Note: + * Value Objects with multiple properties can contain + * other Value Objects inside if needed. + * */ + +export interface CoordinatesProps { + lon: number; + lat: number; +} + +export class Coordinates extends ValueObject { + get lon(): number { + return this.props.lon; + } + + get lat(): number { + return this.props.lat; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + protected validate(props: CoordinatesProps): void { + return; + } +} diff --git a/src/modules/ad/core/value-objects/margin-durations.value-object.ts b/src/modules/ad/core/value-objects/margin-durations.value-object.ts new file mode 100644 index 0000000..f69ccf8 --- /dev/null +++ b/src/modules/ad/core/value-objects/margin-durations.value-object.ts @@ -0,0 +1,79 @@ +import { ValueObject } from '@libs/ddd'; + +/** Note: + * Value Objects with multiple properties can contain + * other Value Objects inside if needed. + * */ + +export interface MarginDurationsProps { + mon?: number; + tue?: number; + wed?: number; + thu?: number; + fri?: number; + sat?: number; + sun?: number; +} + +export class MarginDurations extends ValueObject { + get mon(): number { + return this.props.mon; + } + + set mon(margin: number) { + this.props.mon = margin; + } + + get tue(): number { + return this.props.tue; + } + + set tue(margin: number) { + this.props.tue = margin; + } + + get wed(): number { + return this.props.wed; + } + + set wed(margin: number) { + this.props.wed = margin; + } + + get thu(): number { + return this.props.thu; + } + + set thu(margin: number) { + this.props.thu = margin; + } + + get fri(): number { + return this.props.fri; + } + + set fri(margin: number) { + this.props.fri = margin; + } + + get sat(): number { + return this.props.sat; + } + + set sat(margin: number) { + this.props.sat = margin; + } + + get sun(): number { + return this.props.sun; + } + + set sun(margin: number) { + this.props.sun = margin; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + protected validate(props: MarginDurationsProps): void { + return; + } +} diff --git a/src/modules/ad/core/value-objects/schedule.value-object.ts b/src/modules/ad/core/value-objects/schedule.value-object.ts new file mode 100644 index 0000000..da4696c --- /dev/null +++ b/src/modules/ad/core/value-objects/schedule.value-object.ts @@ -0,0 +1,51 @@ +import { ValueObject } from '@libs/ddd'; + +/** Note: + * Value Objects with multiple properties can contain + * other Value Objects inside if needed. + * */ + +export interface ScheduleProps { + mon?: string; + tue?: string; + wed?: string; + thu?: string; + fri?: string; + sat?: string; + sun?: string; +} + +export class Schedule extends ValueObject { + get mon(): string | undefined { + return this.props.mon; + } + + get tue(): string | undefined { + return this.props.tue; + } + + get wed(): string | undefined { + return this.props.wed; + } + + get thu(): string | undefined { + return this.props.thu; + } + + get fri(): string | undefined { + return this.props.fri; + } + + get sat(): string | undefined { + return this.props.sat; + } + + get sun(): string | undefined { + return this.props.sun; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + protected validate(props: ScheduleProps): void { + return; + } +} diff --git a/src/modules/ad/core/value-objects/waypoint.value-object.ts b/src/modules/ad/core/value-objects/waypoint.value-object.ts new file mode 100644 index 0000000..1b8395d --- /dev/null +++ b/src/modules/ad/core/value-objects/waypoint.value-object.ts @@ -0,0 +1,27 @@ +import { ValueObject } from '@libs/ddd'; +import { AddressProps } from './address.value-object'; + +/** Note: + * Value Objects with multiple properties can contain + * other Value Objects inside if needed. + * */ + +export interface WaypointProps { + position: number; + address: AddressProps; +} + +export class Waypoint extends ValueObject { + get position(): number { + return this.props.position; + } + + get address(): AddressProps { + return this.props.address; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + protected validate(props: WaypointProps): void { + return; + } +} diff --git a/src/modules/ad/domain/dtos/ad.creation.ts b/src/modules/ad/domain/dtos/ad.creation.ts deleted file mode 100644 index dd0bdeb..0000000 --- a/src/modules/ad/domain/dtos/ad.creation.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { Frequency } from '../types/frequency.enum'; -import { Address } from '../entities/address'; - -export class AdCreation { - @AutoMap() - uuid: string; - - @AutoMap() - userUuid: string; - - @AutoMap() - driver: boolean; - - @AutoMap() - passenger: boolean; - - @AutoMap() - frequency: Frequency; - - @AutoMap() - fromDate: Date; - - @AutoMap() - toDate: Date; - - @AutoMap() - monTime?: string; - - @AutoMap() - tueTime?: string; - - @AutoMap() - wedTime?: string; - - @AutoMap() - thuTime?: string; - - @AutoMap() - friTime?: string; - - @AutoMap() - satTime?: string; - - @AutoMap() - sunTime?: string; - - @AutoMap() - monMargin: number; - - @AutoMap() - tueMargin: number; - - @AutoMap() - wedMargin: number; - - @AutoMap() - thuMargin: number; - - @AutoMap() - friMargin: number; - - @AutoMap() - satMargin: number; - - @AutoMap() - sunMargin: number; - - @AutoMap() - seatsDriver: number; - - @AutoMap() - seatsPassenger: number; - - @AutoMap() - strict: boolean; - - @AutoMap() - createdAt: Date; - - @AutoMap() - updatedAt?: Date; - - @AutoMap() - addresses: { create: Address[] }; -} diff --git a/src/modules/ad/domain/dtos/address.dto.ts b/src/modules/ad/domain/dtos/address.dto.ts deleted file mode 100644 index 17a7b79..0000000 --- a/src/modules/ad/domain/dtos/address.dto.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { - IsInt, - IsLatitude, - IsLongitude, - IsOptional, - IsString, - IsUUID, -} from 'class-validator'; - -export class AddressDTO { - @IsOptional() - @IsUUID(4) - @AutoMap() - uuid?: string; - - @IsOptional() - @IsUUID(4) - @AutoMap() - adUuid?: string; - - @IsOptional() - @IsInt() - @AutoMap() - position?: number; - - @IsLongitude() - @AutoMap() - lon: number; - - @IsLatitude() - @AutoMap() - lat: number; - - @IsOptional() - @AutoMap() - name?: string; - - @IsOptional() - @IsString() - @AutoMap() - houseNumber?: string; - - @IsOptional() - @IsString() - @AutoMap() - street?: string; - - @IsOptional() - @IsString() - @AutoMap() - locality?: string; - - @IsOptional() - @IsString() - @AutoMap() - postalCode?: string; - - @IsString() - @AutoMap() - country: string; -} diff --git a/src/modules/ad/domain/dtos/create-ad.request.ts b/src/modules/ad/domain/dtos/create-ad.request.ts deleted file mode 100644 index ac73eea..0000000 --- a/src/modules/ad/domain/dtos/create-ad.request.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { - IsOptional, - IsBoolean, - IsDate, - IsInt, - IsEnum, - ValidateNested, - ArrayMinSize, - IsUUID, -} from 'class-validator'; -import { Frequency } from '../types/frequency.enum'; -import { Transform, Type } from 'class-transformer'; -import { intToFrequency } from './validators/frequency.mapping'; -import { MarginDTO } from './margin.dto'; -import { ScheduleDTO } from './schedule.dto'; -import { AddressDTO } from './address.dto'; -import { IsPunctualOrRecurrent } from './validators/decorators/is-punctual-or-recurrent.validator'; -import { HasProperDriverSeats } from './validators/decorators/has-driver-seats.validator'; -import { HasProperPassengerSeats } from './validators/decorators/has-passenger-seats.validator'; -import { HasProperPositionIndexes } from './validators/decorators/address-position.validator'; - -export class CreateAdRequest { - @IsOptional() - @IsUUID(4) - @AutoMap() - uuid?: string; - - @IsUUID(4) - @AutoMap() - userUuid: string; - - @IsOptional() - @IsBoolean() - @AutoMap() - driver?: boolean; - - @IsOptional() - @IsBoolean() - @AutoMap() - passenger?: boolean; - - @Transform(({ value }) => intToFrequency(value), { - toClassOnly: true, - }) - @IsEnum(Frequency) - @AutoMap() - frequency: Frequency; - - @IsOptional() - @IsPunctualOrRecurrent() - @Type(() => Date) - @IsDate() - @AutoMap() - departure?: Date; - - @IsOptional() - @IsPunctualOrRecurrent() - @Type(() => Date) - @IsDate() - @AutoMap() - fromDate?: Date; - - @IsOptional() - @IsPunctualOrRecurrent() - @Type(() => Date) - @IsDate() - @AutoMap() - toDate?: Date; - - @IsOptional() - @Type(() => ScheduleDTO) - @IsPunctualOrRecurrent() - @ValidateNested({ each: true }) - @AutoMap() - schedule: ScheduleDTO = {}; - - @IsOptional() - @Type(() => MarginDTO) - @ValidateNested({ each: true }) - @AutoMap() - marginDurations?: MarginDTO; - - @IsOptional() - @HasProperDriverSeats() - @IsInt() - @AutoMap() - seatsDriver?: number; - - @IsOptional() - @HasProperPassengerSeats() - @IsInt() - @AutoMap() - seatsPassenger?: number; - - @IsOptional() - @IsBoolean() - @AutoMap() - strict?: boolean; - - @ArrayMinSize(2) - @Type(() => AddressDTO) - @HasProperPositionIndexes() - @ValidateNested({ each: true }) - @AutoMap() - addresses: AddressDTO[]; -} diff --git a/src/modules/ad/domain/dtos/validators/address-position.ts b/src/modules/ad/domain/dtos/validators/address-position.ts deleted file mode 100644 index fe0cb0c..0000000 --- a/src/modules/ad/domain/dtos/validators/address-position.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { AddressDTO } from '../address.dto'; - -export const hasProperPositionIndexes = (value: AddressDTO[]): boolean => { - if (value.every((address) => address.position === undefined)) return true; - if (value.every((address) => typeof address.position === 'number')) { - value.sort((a, b) => a.position - b.position); - for (let i = 1; i < value.length; i++) { - if (value[i - 1].position >= value[i].position) return false; - } - return true; - } - return false; -}; diff --git a/src/modules/ad/domain/dtos/validators/decorators/address-position.validator.ts b/src/modules/ad/domain/dtos/validators/decorators/address-position.validator.ts deleted file mode 100644 index a21283e..0000000 --- a/src/modules/ad/domain/dtos/validators/decorators/address-position.validator.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator'; -import { AddressDTO } from '../../address.dto'; -import { hasProperPositionIndexes } from '../address-position'; - -export const HasProperPositionIndexes = ( - validationOptions?: ValidationOptions, -): PropertyDecorator => - ValidateBy( - { - name: '', - constraints: [], - validator: { - validate: (value: AddressDTO[]): boolean => - hasProperPositionIndexes(value), - - defaultMessage: buildMessage( - () => - `indexes position incorrect, please provide a complete list of indexes or ordened list of adresses from start to end of journey`, - validationOptions, - ), - }, - }, - validationOptions, - ); diff --git a/src/modules/ad/domain/dtos/validators/decorators/has-driver-seats.validator.ts b/src/modules/ad/domain/dtos/validators/decorators/has-driver-seats.validator.ts deleted file mode 100644 index d1ba793..0000000 --- a/src/modules/ad/domain/dtos/validators/decorators/has-driver-seats.validator.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { - ValidateBy, - ValidationArguments, - ValidationOptions, - buildMessage, -} from 'class-validator'; -import { hasProperDriverSeats } from '../has-driver-seats'; - -export const HasProperDriverSeats = ( - validationOptions?: ValidationOptions, -): PropertyDecorator => - ValidateBy( - { - name: '', - constraints: [], - validator: { - validate: (value: any, args: ValidationArguments): boolean => - hasProperDriverSeats(args), - defaultMessage: buildMessage( - () => `driver and driver seats are not correct`, - validationOptions, - ), - }, - }, - validationOptions, - ); diff --git a/src/modules/ad/domain/dtos/validators/decorators/is-punctual-or-recurrent.validator.ts b/src/modules/ad/domain/dtos/validators/decorators/is-punctual-or-recurrent.validator.ts deleted file mode 100644 index e060e64..0000000 --- a/src/modules/ad/domain/dtos/validators/decorators/is-punctual-or-recurrent.validator.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { - ValidateBy, - ValidationArguments, - ValidationOptions, - buildMessage, -} from 'class-validator'; -import { isPunctualOrRecurrent } from '../is-punctual-or-recurrent'; - -export const IsPunctualOrRecurrent = ( - validationOptions?: ValidationOptions, -): PropertyDecorator => - ValidateBy( - { - name: '', - constraints: [], - validator: { - validate: (value, args: ValidationArguments): boolean => - isPunctualOrRecurrent(args), - defaultMessage: buildMessage( - () => - `the departure, from date, to date and schedule must be properly set on recurrent or punctual ad`, - validationOptions, - ), - }, - }, - validationOptions, - ); diff --git a/src/modules/ad/domain/dtos/validators/frequency.mapping.ts b/src/modules/ad/domain/dtos/validators/frequency.mapping.ts deleted file mode 100644 index 030fa34..0000000 --- a/src/modules/ad/domain/dtos/validators/frequency.mapping.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Frequency } from '../../types/frequency.enum'; - -export const intToFrequency = (index: number): Frequency => { - if (index == 1) return Frequency.PUNCTUAL; - if (index == 2) return Frequency.RECURRENT; - return undefined; -}; diff --git a/src/modules/ad/domain/dtos/validators/has-driver-seats.ts b/src/modules/ad/domain/dtos/validators/has-driver-seats.ts deleted file mode 100644 index fb34229..0000000 --- a/src/modules/ad/domain/dtos/validators/has-driver-seats.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ValidationArguments } from 'class-validator'; - -export const hasProperDriverSeats = (args: ValidationArguments): boolean => { - if ( - args.object['driver'] === true && - typeof args.object['seatsDriver'] === 'number' - ) - return args.object['seatsDriver'] > 0; - if ( - (args.object['driver'] === false || - args.object['driver'] === null || - args.object['driver'] === undefined) && - (args.object['seatsDriver'] === 0 || - args.object['seatsDriver'] === null || - args.object['seatsDriver'] === undefined) - ) - return true; - return false; -}; diff --git a/src/modules/ad/domain/dtos/validators/has-passenger-seats.ts b/src/modules/ad/domain/dtos/validators/has-passenger-seats.ts deleted file mode 100644 index f96eeb9..0000000 --- a/src/modules/ad/domain/dtos/validators/has-passenger-seats.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { ValidationArguments } from 'class-validator'; - -export const hasProperPassengerSeats = (args: ValidationArguments): boolean => { - if ( - args.object['passenger'] === true && - typeof args.object['seatsPassenger'] === 'number' - ) - return args.object['seatsPassenger'] > 0; - if ( - (args.object['passenger'] === false || - args.object['passenger'] === null || - args.object['passenger'] === undefined) && - (args.object['seatsPassenger'] === 0 || - args.object['seatsPassenger'] === null || - args.object['seatsPassenger'] === undefined) - ) - return true; - return false; -}; diff --git a/src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.ts b/src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.ts deleted file mode 100644 index 39124c5..0000000 --- a/src/modules/ad/domain/dtos/validators/is-punctual-or-recurrent.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { ValidationArguments } from 'class-validator'; -import { Frequency } from '../../types/frequency.enum'; - -const isPunctual = (args: ValidationArguments): boolean => - args.object['frequency'] === Frequency.PUNCTUAL && - args.object['departure'] instanceof Date && - !Object.keys(args.object['schedule']).length; - -const isRecurrent = (args: ValidationArguments): boolean => - args.object['frequency'] === Frequency.RECURRENT && - args.object['fromDate'] instanceof Date && - args.object['toDate'] instanceof Date && - Object.keys(args.object['schedule']).length > 0; - -export const isPunctualOrRecurrent = (args: ValidationArguments): boolean => - isPunctual(args) || isRecurrent(args); diff --git a/src/modules/ad/domain/entities/ad.ts b/src/modules/ad/domain/entities/ad.ts deleted file mode 100644 index d46e204..0000000 --- a/src/modules/ad/domain/entities/ad.ts +++ /dev/null @@ -1,132 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { - IsOptional, - IsString, - IsBoolean, - IsDate, - IsInt, - IsEnum, - ValidateNested, - ArrayMinSize, - IsUUID, -} from 'class-validator'; -import { Address } from '../entities/address'; -import { Frequency } from '../types/frequency.enum'; - -export class Ad { - @IsUUID(4) - @AutoMap() - uuid: string; - - @IsUUID(4) - @AutoMap() - userUuid: string; - - @IsBoolean() - @AutoMap() - driver: boolean; - - @IsBoolean() - @AutoMap() - passenger: boolean; - - @IsEnum(Frequency) - @AutoMap() - frequency: Frequency; - - @IsDate() - @AutoMap() - fromDate: Date; - - @IsDate() - @AutoMap() - toDate: Date; - - @IsOptional() - @IsDate() - @AutoMap() - monTime?: string; - - @IsOptional() - @IsString() - @AutoMap() - tueTime?: string; - - @IsOptional() - @IsString() - @AutoMap() - wedTime?: string; - - @IsOptional() - @IsString() - @AutoMap() - thuTime?: string; - - @IsOptional() - @IsString() - @AutoMap() - friTime?: string; - - @IsOptional() - @IsString() - @AutoMap() - satTime?: string; - - @IsOptional() - @IsString() - @AutoMap() - sunTime?: string; - - @IsInt() - @AutoMap() - monMargin: number; - - @IsInt() - @AutoMap() - tueMargin: number; - - @IsInt() - @AutoMap() - wedMargin: number; - - @IsInt() - @AutoMap() - thuMargin: number; - - @IsInt() - @AutoMap() - friMargin: number; - - @IsInt() - @AutoMap() - satMargin: number; - - @IsInt() - @AutoMap() - sunMargin: number; - - @IsInt() - @AutoMap() - seatsDriver: number; - - @IsInt() - @AutoMap() - seatsPassenger: number; - - @IsBoolean() - @AutoMap() - strict: boolean; - - @IsDate() - @AutoMap() - createdAt: Date; - - @IsDate() - @AutoMap() - updatedAt?: Date; - - @ArrayMinSize(2) - @ValidateNested({ each: true }) - @AutoMap(() => [Address]) - addresses: Address[]; -} diff --git a/src/modules/ad/domain/entities/address.ts b/src/modules/ad/domain/entities/address.ts deleted file mode 100644 index 756688f..0000000 --- a/src/modules/ad/domain/entities/address.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { AutoMap } from '@automapper/classes'; -import { IsInt, IsUUID } from 'class-validator'; - -export class Address { - @IsUUID(4) - @AutoMap() - uuid: string; - - @IsUUID(4) - @AutoMap() - adUuid: string; - - @IsInt() - @AutoMap() - position: number; - - @AutoMap() - lon: number; - - @AutoMap() - lat: number; - - @AutoMap() - name?: string; - - @AutoMap() - houseNumber?: string; - - @AutoMap() - street?: string; - - @AutoMap() - locality: string; - - @AutoMap() - postalCode: string; - - @AutoMap() - country: string; -} diff --git a/src/modules/ad/domain/entities/frequency.normaliser.ts b/src/modules/ad/domain/entities/frequency.normaliser.ts deleted file mode 100644 index 95e8ebd..0000000 --- a/src/modules/ad/domain/entities/frequency.normaliser.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { CreateAdRequest } from '../dtos/create-ad.request'; -import { Day } from '../types/day.enum'; - -import { Frequency } from '../types/frequency.enum'; - -export class FrequencyNormaliser { - fromDateResolver(createAdRequest: CreateAdRequest): Date { - if (createAdRequest.frequency === Frequency.PUNCTUAL) - return createAdRequest.departure; - return createAdRequest.fromDate; - } - toDateResolver(createAdRequest: CreateAdRequest): Date { - if (createAdRequest.frequency === Frequency.PUNCTUAL) - return createAdRequest.departure; - return createAdRequest.toDate; - } - scheduleResolver = ( - createAdRequest: CreateAdRequest, - day: number, - ): string => { - if ( - Object.keys(createAdRequest.schedule).length === 0 && - createAdRequest.frequency == Frequency.PUNCTUAL && - createAdRequest.departure.getDay() === day - ) - return `${createAdRequest.departure - .getHours() - .toString() - .padStart(2, '0')}:${createAdRequest.departure - .getMinutes() - .toString() - .padStart(2, '0')}`; - return createAdRequest.schedule[Day[day]]; - }; -} diff --git a/src/modules/ad/domain/interfaces/param-provider.interface.ts b/src/modules/ad/domain/interfaces/param-provider.interface.ts deleted file mode 100644 index 4169e3b..0000000 --- a/src/modules/ad/domain/interfaces/param-provider.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { DefaultParams } from '../types/default-params.type'; -export interface IProvideParams { - getParams(): DefaultParams; -} diff --git a/src/modules/ad/domain/types/day.enum.ts b/src/modules/ad/domain/types/day.enum.ts deleted file mode 100644 index 5a7f7f3..0000000 --- a/src/modules/ad/domain/types/day.enum.ts +++ /dev/null @@ -1,9 +0,0 @@ -export enum Day { - sun = 0, - mon, - tue, - wed, - thu, - fri, - sat, -} diff --git a/src/modules/ad/domain/types/frequency.enum.ts b/src/modules/ad/domain/types/frequency.enum.ts deleted file mode 100644 index 0126874..0000000 --- a/src/modules/ad/domain/types/frequency.enum.ts +++ /dev/null @@ -1,4 +0,0 @@ -export enum Frequency { - PUNCTUAL = 'PUNCTUAL', - RECURRENT = 'RECURRENT', -} diff --git a/src/modules/ad/domain/usecases/create-ad.usecase.ts b/src/modules/ad/domain/usecases/create-ad.usecase.ts deleted file mode 100644 index ac62877..0000000 --- a/src/modules/ad/domain/usecases/create-ad.usecase.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { Mapper } from '@automapper/core'; -import { InjectMapper } from '@automapper/nestjs'; -import { Inject } from '@nestjs/common'; -import { CommandHandler } from '@nestjs/cqrs'; -import { AdsRepository } from '../../adapters/secondaries/ads.repository'; -import { CreateAdCommand } from '../../commands/create-ad.command'; -import { CreateAdRequest } from '../dtos/create-ad.request'; -import { IProvideParams } from '../interfaces/param-provider.interface'; -import { DefaultParams } from '../types/default-params.type'; -import { AdCreation } from '../dtos/ad.creation'; -import { Ad } from '../entities/ad'; -import { PARAMS_PROVIDER } from '../../ad.constants'; -import { IPublishMessage } from '../../../../interfaces/message-publisher'; -import { MESSAGE_PUBLISHER } from '../../../../app.constants'; - -@CommandHandler(CreateAdCommand) -export class CreateAdUseCase { - private readonly defaultParams: DefaultParams; - private ad: AdCreation; - constructor( - private readonly repository: AdsRepository, - @Inject(MESSAGE_PUBLISHER) - private readonly messagePublisher: IPublishMessage, - @InjectMapper() private readonly mapper: Mapper, - @Inject(PARAMS_PROVIDER) - private readonly defaultParamsProvider: IProvideParams, - ) { - this.defaultParams = defaultParamsProvider.getParams(); - } - - async execute(command: CreateAdCommand): Promise { - this.ad = this.mapper.map( - command.createAdRequest, - CreateAdRequest, - AdCreation, - ); - this.setDefaultMarginDurations(); - this.setDefaultAddressesPosition(); - this.setDefaultDriverAndPassengerParameters(); - this.setDefaultStrict(); - - try { - const adCreated: Ad = await this.repository.create(this.ad); - this.messagePublisher.publish('ad.create', JSON.stringify(adCreated)); - this.messagePublisher.publish( - 'logging.ad.create.info', - JSON.stringify(adCreated), - ); - return adCreated; - } catch (error) { - let key = 'logging.ad.create.crit'; - if (error.message.includes('Already exists')) { - key = 'logging.ad.create.warning'; - } - this.messagePublisher.publish( - key, - JSON.stringify({ - command, - error, - }), - ); - throw error; - } - } - - private setDefaultMarginDurations = (): void => { - if (this.ad.monMargin === undefined) - this.ad.monMargin = this.defaultParams.MON_MARGIN; - if (this.ad.tueMargin === undefined) - this.ad.tueMargin = this.defaultParams.TUE_MARGIN; - if (this.ad.wedMargin === undefined) - this.ad.wedMargin = this.defaultParams.WED_MARGIN; - if (this.ad.thuMargin === undefined) - this.ad.thuMargin = this.defaultParams.THU_MARGIN; - if (this.ad.friMargin === undefined) - this.ad.friMargin = this.defaultParams.FRI_MARGIN; - if (this.ad.satMargin === undefined) - this.ad.satMargin = this.defaultParams.SAT_MARGIN; - if (this.ad.sunMargin === undefined) - this.ad.sunMargin = this.defaultParams.SUN_MARGIN; - }; - - private setDefaultStrict = (): void => { - if (this.ad.strict === undefined) - this.ad.strict = this.defaultParams.STRICT; - }; - - private setDefaultDriverAndPassengerParameters = (): void => { - this.ad.driver = !!this.ad.driver; - this.ad.passenger = !!this.ad.passenger; - if (!this.ad.driver && !this.ad.passenger) { - this.ad.driver = this.defaultParams.DRIVER; - this.ad.seatsDriver = this.defaultParams.SEATS_PROVIDED; - this.ad.passenger = this.defaultParams.PASSENGER; - this.ad.seatsPassenger = this.defaultParams.SEATS_REQUESTED; - return; - } - if (!this.ad.seatsDriver || this.ad.seatsDriver <= 0) - this.ad.seatsDriver = this.defaultParams.SEATS_PROVIDED; - if (!this.ad.seatsPassenger || this.ad.seatsPassenger <= 0) - this.ad.seatsPassenger = this.defaultParams.SEATS_REQUESTED; - }; - - private setDefaultAddressesPosition = (): void => { - if (this.ad.addresses.create[0].position === undefined) { - for (let i = 0; i < this.ad.addresses.create.length; i++) { - this.ad.addresses.create[i].position = i; - } - } - }; -} diff --git a/src/modules/ad/domain/usecases/find-ad-by-uuid.usecase.ts b/src/modules/ad/domain/usecases/find-ad-by-uuid.usecase.ts deleted file mode 100644 index 273b619..0000000 --- a/src/modules/ad/domain/usecases/find-ad-by-uuid.usecase.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Inject, NotFoundException } from '@nestjs/common'; -import { QueryHandler } from '@nestjs/cqrs'; -import { FindAdByUuidQuery } from '../../queries/find-ad-by-uuid.query'; -import { AdsRepository } from '../../adapters/secondaries/ads.repository'; -import { Ad } from '../entities/ad'; -import { MESSAGE_PUBLISHER } from '../../../../app.constants'; -import { IPublishMessage } from '../../../../interfaces/message-publisher'; - -@QueryHandler(FindAdByUuidQuery) -export class FindAdByUuidUseCase { - constructor( - private readonly repository: AdsRepository, - @Inject(MESSAGE_PUBLISHER) - private readonly messagePublisher: IPublishMessage, - ) {} - - async execute(findAdByUuid: FindAdByUuidQuery): Promise { - try { - const ad = await this.repository.findOneByUuid(findAdByUuid.uuid); - if (!ad) throw new NotFoundException(); - return ad; - } catch (error) { - this.messagePublisher.publish( - 'logging.ad.read.warning', - JSON.stringify({ - query: findAdByUuid, - error, - }), - ); - throw error; - } - } -} diff --git a/src/modules/ad/infrastructure/ad.repository.ts b/src/modules/ad/infrastructure/ad.repository.ts new file mode 100644 index 0000000..2b80a1e --- /dev/null +++ b/src/modules/ad/infrastructure/ad.repository.ts @@ -0,0 +1,71 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { EventEmitter2 } from '@nestjs/event-emitter'; +import { AdRepositoryPort } from '../core/ports/ad.repository.port'; +import { AdEntity } from '../core/ad.entity'; +import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base'; +import { AdMapper } from '../ad.mapper'; +import { PrismaService } from './prisma-service'; + +export type AdModel = { + uuid: string; + userUuid: string; + driver: boolean; + passenger: boolean; + frequency: string; + fromDate: Date; + toDate: Date; + monTime: Date; + tueTime: Date; + wedTime: Date; + thuTime: Date; + friTime: Date; + satTime: Date; + sunTime: Date; + monMargin: number; + tueMargin: number; + wedMargin: number; + thuMargin: number; + friMargin: number; + satMargin: number; + sunMargin: number; + seatsProposed: number; + seatsRequested: number; + strict: boolean; + waypoints: { + create: WaypointModel[]; + }; + createdAt: Date; + updatedAt: Date; +}; + +export type WaypointModel = { + uuid: string; + position: number; + lon: number; + lat: number; + name?: string; + houseNumber?: string; + street?: string; + locality?: string; + postalCode?: string; + country: string; + createdAt: Date; + updatedAt: Date; +}; + +/** + * Repository is used for retrieving/saving domain entities + * */ +@Injectable() +export class AdRepository + extends PrismaRepositoryBase + implements AdRepositoryPort +{ + constructor( + prisma: PrismaService, + mapper: AdMapper, + eventEmitter: EventEmitter2, + ) { + super(prisma.ad, mapper, eventEmitter, new Logger(AdRepository.name)); + } +} diff --git a/src/modules/ad/adapters/secondaries/default-params.provider.ts b/src/modules/ad/infrastructure/default-params-provider.ts similarity index 72% rename from src/modules/ad/adapters/secondaries/default-params.provider.ts rename to src/modules/ad/infrastructure/default-params-provider.ts index 1dc0f09..3e1ee57 100644 --- a/src/modules/ad/adapters/secondaries/default-params.provider.ts +++ b/src/modules/ad/infrastructure/default-params-provider.ts @@ -1,10 +1,10 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { DefaultParams } from '../../domain/types/default-params.type'; -import { IProvideParams } from '../../domain/interfaces/param-provider.interface'; +import { DefaultParamsProviderPort } from '../core/ports/default-params-provider.port'; +import { DefaultParams } from '../core/ports/default-params.type'; @Injectable() -export class DefaultParamsProvider implements IProvideParams { +export class DefaultParamsProvider implements DefaultParamsProviderPort { constructor(private readonly configService: ConfigService) {} getParams = (): DefaultParams => ({ MON_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), @@ -15,9 +15,10 @@ export class DefaultParamsProvider implements IProvideParams { SAT_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), SUN_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), DRIVER: this.configService.get('ROLE') == 'driver', - SEATS_PROVIDED: parseInt(this.configService.get('SEATS_PROVIDED')), + SEATS_PROPOSED: parseInt(this.configService.get('SEATS_PROPOSED')), PASSENGER: this.configService.get('ROLE') == 'passenger', SEATS_REQUESTED: parseInt(this.configService.get('SEATS_REQUESTED')), STRICT: this.configService.get('STRICT_FREQUENCY') == 'true', + DEFAULT_TIMEZONE: this.configService.get('DEFAULT_TIMEZONE'), }); } diff --git a/src/modules/ad/adapters/secondaries/message-publisher.ts b/src/modules/ad/infrastructure/message-publisher.ts similarity index 67% rename from src/modules/ad/adapters/secondaries/message-publisher.ts rename to src/modules/ad/infrastructure/message-publisher.ts index 98a963b..10b2a0d 100644 --- a/src/modules/ad/adapters/secondaries/message-publisher.ts +++ b/src/modules/ad/infrastructure/message-publisher.ts @@ -1,10 +1,10 @@ import { Inject, Injectable } from '@nestjs/common'; -import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; -import { IPublishMessage } from 'src/interfaces/message-publisher'; +import { MessagePublisherPort } from '@ports/message-publisher.port'; +import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants'; @Injectable() -export class MessagePublisher implements IPublishMessage { +export class MessagePublisher implements MessagePublisherPort { constructor( @Inject(MESSAGE_BROKER_PUBLISHER) private readonly messageBrokerPublisher: MessageBrokerPublisher, diff --git a/src/modules/ad/infrastructure/prisma-service.ts b/src/modules/ad/infrastructure/prisma-service.ts new file mode 100644 index 0000000..edf6532 --- /dev/null +++ b/src/modules/ad/infrastructure/prisma-service.ts @@ -0,0 +1,15 @@ +import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; +import { PrismaClient } from '@prisma/client'; + +@Injectable() +export class PrismaService extends PrismaClient implements OnModuleInit { + async onModuleInit() { + await this.$connect(); + } + + async enableShutdownHooks(app: INestApplication) { + this.$on('beforeExit', async () => { + await app.close(); + }); + } +} diff --git a/src/modules/ad/infrastructure/timezone-finder.ts b/src/modules/ad/infrastructure/timezone-finder.ts new file mode 100644 index 0000000..7c1ec9a --- /dev/null +++ b/src/modules/ad/infrastructure/timezone-finder.ts @@ -0,0 +1,8 @@ +import { Injectable } from '@nestjs/common'; +import { TimezoneFinderPort } from '../core/ports/timezone-finder.port'; +import { find } from 'geo-tz'; + +@Injectable() +export class TimezoneFinder implements TimezoneFinderPort { + timezones = (lon: number, lat: number): string[] => find(lat, lon); +} diff --git a/src/modules/ad/adapters/primaries/ad.presenter.ts b/src/modules/ad/interface/ad.presenter.ts similarity index 84% rename from src/modules/ad/adapters/primaries/ad.presenter.ts rename to src/modules/ad/interface/ad.presenter.ts index 1a4d67c..9f19a71 100644 --- a/src/modules/ad/adapters/primaries/ad.presenter.ts +++ b/src/modules/ad/interface/ad.presenter.ts @@ -2,5 +2,5 @@ import { AutoMap } from '@automapper/classes'; export class AdPresenter { @AutoMap() - uuid: string; + id: string; } diff --git a/src/modules/ad/interface/ad.proto b/src/modules/ad/interface/ad.proto new file mode 100644 index 0000000..dd7cbe0 --- /dev/null +++ b/src/modules/ad/interface/ad.proto @@ -0,0 +1,80 @@ +syntax = "proto3"; + +package ad; + +service AdsService { + rpc FindOneById(AdById) returns (Ad); + rpc FindAll(AdFilter) returns (Ads); + rpc Create(Ad) returns (AdById); + rpc Update(Ad) returns (Ad); + rpc Delete(AdById) returns (Empty); +} + +message AdById { + string id = 1; +} + +message Ad { + string id = 1; + string userId = 2; + bool driver = 3; + bool passenger = 4; + Frequency frequency = 5; + string fromDate = 6; + string toDate = 7; + Schedule schedule = 8; + MarginDurations marginDurations = 9; + int32 seatsProposed = 10; + int32 seatsRequested = 11; + bool strict = 12; + repeated Waypoint waypoints = 13; +} + +message Schedule { + string mon = 1; + string tue = 2; + string wed = 3; + string thu = 4; + string fri = 5; + string sat = 6; + string sun = 7; +} + +message MarginDurations { + int32 mon = 1; + int32 tue = 2; + int32 wed = 3; + int32 thu = 4; + int32 fri = 5; + int32 sat = 6; + int32 sun = 7; +} + +message Waypoint { + int32 position = 1; + float lon = 2; + float lat = 3; + string name = 4; + string houseNumber = 5; + string street = 6; + string locality = 7; + string postalCode = 8; + string country = 9; +} + +enum Frequency { + PUNCTUAL = 1; + RECURRENT = 2; +} + +message AdFilter { + int32 page = 1; + int32 perPage = 2; +} + +message Ads { + repeated Ad data = 1; + int32 total = 2; +} + +message Empty {} diff --git a/src/modules/ad/interface/dtos/ad.paginated.response.dto.ts b/src/modules/ad/interface/dtos/ad.paginated.response.dto.ts new file mode 100644 index 0000000..e680096 --- /dev/null +++ b/src/modules/ad/interface/dtos/ad.paginated.response.dto.ts @@ -0,0 +1,6 @@ +import { PaginatedResponseDto } from '@libs/api/paginated.response.base'; +import { AdResponseDto } from './ad.response.dto'; + +export class AdPaginatedResponseDto extends PaginatedResponseDto { + readonly data: readonly AdResponseDto[]; +} diff --git a/src/modules/ad/interface/dtos/ad.response.dto.ts b/src/modules/ad/interface/dtos/ad.response.dto.ts new file mode 100644 index 0000000..1ce10fd --- /dev/null +++ b/src/modules/ad/interface/dtos/ad.response.dto.ts @@ -0,0 +1,5 @@ +import { ResponseBase } from '@libs/api/response.base'; + +export class AdResponseDto extends ResponseBase { + uuid: string; +} diff --git a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts new file mode 100644 index 0000000..033263a --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts @@ -0,0 +1,43 @@ +import { Controller, UsePipes } from '@nestjs/common'; +import { CommandBus } from '@nestjs/cqrs'; +import { GrpcMethod, RpcException } from '@nestjs/microservices'; +import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; +import { AdPresenter } from '../ad.presenter'; +import { CreateAdRequestDTO } from './dtos/create-ad.request.dto'; +import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command'; +import { Result, match } from 'oxide.ts'; +import { AggregateID } from '@libs/ddd'; +import { AdAlreadyExistsError } from '../../core/ad.errors'; +import { IdResponse } from '@libs/api/id.response.dto'; + +@UsePipes( + new RpcValidationPipe({ + whitelist: false, + forbidUnknownValues: false, + }), +) +@Controller() +export class CreateAdGrpcController { + constructor(private readonly commandBus: CommandBus) {} + + @GrpcMethod('AdsService', 'Create') + async create(data: CreateAdRequestDTO): Promise { + const result: Result = + await this.commandBus.execute(new CreateAdCommand(data)); + + // Deciding what to do with a Result (similar to Rust matching) + // if Ok we return a response with an id + // if Error decide what to do with it depending on its type + return match(result, { + Ok: (id: string) => new IdResponse(id), + Err: (error: Error) => { + if (error instanceof AdAlreadyExistsError) + throw new RpcException({ + code: 6, + message: 'Ad already exists', + }); + throw new RpcException({}); + }, + }); + } +} diff --git a/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts new file mode 100644 index 0000000..fc110a8 --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts @@ -0,0 +1,33 @@ +import { AutoMap } from '@automapper/classes'; +import { IsOptional, IsString } from 'class-validator'; +import { CoordinatesDTO } from './coordinates.dto'; + +export class AddressDTO extends CoordinatesDTO { + @IsOptional() + @AutoMap() + name?: string; + + @IsOptional() + @IsString() + @AutoMap() + houseNumber?: string; + + @IsOptional() + @IsString() + @AutoMap() + street?: string; + + @IsOptional() + @IsString() + @AutoMap() + locality?: string; + + @IsOptional() + @IsString() + @AutoMap() + postalCode?: string; + + @IsString() + @AutoMap() + country: string; +} diff --git a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts new file mode 100644 index 0000000..7bce3b1 --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts @@ -0,0 +1,12 @@ +import { AutoMap } from '@automapper/classes'; +import { IsLatitude, IsLongitude } from 'class-validator'; + +export class CoordinatesDTO { + @IsLongitude() + @AutoMap() + lon: number; + + @IsLatitude() + @AutoMap() + lat: number; +} diff --git a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts new file mode 100644 index 0000000..45ba314 --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts @@ -0,0 +1,91 @@ +import { AutoMap } from '@automapper/classes'; +import { + IsOptional, + IsBoolean, + IsInt, + IsEnum, + ValidateNested, + ArrayMinSize, + IsUUID, + IsArray, + IsISO8601, +} from 'class-validator'; +import { Transform, Type } from 'class-transformer'; +import { ScheduleDTO } from './schedule.dto'; +import { MarginDurationsDTO } from './margin-durations.dto'; +import { WaypointDTO } from './waypoint.dto'; +import { intToFrequency } from './validators/frequency.mapping'; +import { IsSchedule } from './validators/decorators/is-schedule.validator'; +import { HasValidPositionIndexes } from './validators/decorators/valid-position-indexes.validator'; +import { Frequency } from '@modules/ad/core/ad.types'; + +export class CreateAdRequestDTO { + @IsUUID(4) + @AutoMap() + userId: string; + + @IsOptional() + @IsBoolean() + @AutoMap() + driver?: boolean; + + @IsOptional() + @IsBoolean() + @AutoMap() + passenger?: boolean; + + @Transform(({ value }) => intToFrequency(value), { + toClassOnly: true, + }) + @IsEnum(Frequency) + @AutoMap() + frequency: Frequency; + + @IsISO8601({ + strict: true, + strictSeparator: true, + }) + @AutoMap() + fromDate: string; + + @IsISO8601({ + strict: true, + strictSeparator: true, + }) + @AutoMap() + toDate: string; + + @Type(() => ScheduleDTO) + @IsSchedule() + @ValidateNested({ each: true }) + @AutoMap() + schedule: ScheduleDTO; + + @IsOptional() + @Type(() => MarginDurationsDTO) + @ValidateNested({ each: true }) + @AutoMap() + marginDurations?: MarginDurationsDTO; + + @IsOptional() + @IsInt() + @AutoMap() + seatsProposed?: number; + + @IsOptional() + @IsInt() + @AutoMap() + seatsRequested?: number; + + @IsOptional() + @IsBoolean() + @AutoMap() + strict?: boolean; + + @IsArray() + @ArrayMinSize(2) + @HasValidPositionIndexes() + @ValidateNested({ each: true }) + @AutoMap() + waypoints: WaypointDTO[]; +} diff --git a/src/modules/ad/domain/dtos/margin.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts similarity index 93% rename from src/modules/ad/domain/dtos/margin.dto.ts rename to src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts index a6d7e61..5b0e439 100644 --- a/src/modules/ad/domain/dtos/margin.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts @@ -1,7 +1,7 @@ import { AutoMap } from '@automapper/classes'; import { IsInt, IsOptional } from 'class-validator'; -export class MarginDTO { +export class MarginDurationsDTO { @IsOptional() @IsInt() @AutoMap() diff --git a/src/modules/ad/domain/dtos/schedule.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts similarity index 100% rename from src/modules/ad/domain/dtos/schedule.dto.ts rename to src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts diff --git a/src/modules/ad/domain/dtos/validators/decorators/has-passenger-seats.validator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.validator.ts similarity index 67% rename from src/modules/ad/domain/dtos/validators/decorators/has-passenger-seats.validator.ts rename to src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.validator.ts index 26dc5de..5a80a19 100644 --- a/src/modules/ad/domain/dtos/validators/decorators/has-passenger-seats.validator.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.validator.ts @@ -4,9 +4,8 @@ import { ValidationOptions, buildMessage, } from 'class-validator'; -import { hasProperPassengerSeats } from '../has-passenger-seats'; -export const HasProperPassengerSeats = ( +export const IsSchedule = ( validationOptions?: ValidationOptions, ): PropertyDecorator => ValidateBy( @@ -14,10 +13,11 @@ export const HasProperPassengerSeats = ( name: '', constraints: [], validator: { + // eslint-disable-next-line @typescript-eslint/no-unused-vars validate: (value, args: ValidationArguments): boolean => - hasProperPassengerSeats(args), + Object.keys(value).length > 0, defaultMessage: buildMessage( - () => `passenger and passenger seats are not correct`, + () => `schedule is invalid`, validationOptions, ), }, diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts new file mode 100644 index 0000000..06d178a --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts @@ -0,0 +1,22 @@ +import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator'; +import { hasValidPositionIndexes } from '../waypoint-position'; +import { WaypointDTO } from '../../waypoint.dto'; + +export const HasValidPositionIndexes = ( + validationOptions?: ValidationOptions, +): PropertyDecorator => + ValidateBy( + { + name: '', + constraints: [], + validator: { + validate: (waypoints: WaypointDTO[]): boolean => + hasValidPositionIndexes(waypoints), + defaultMessage: buildMessage( + () => `invalid waypoints positions`, + validationOptions, + ), + }, + }, + validationOptions, + ); diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping.ts new file mode 100644 index 0000000..4ec6a89 --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping.ts @@ -0,0 +1,7 @@ +import { Frequency } from '@modules/ad/core/ad.types'; + +export const intToFrequency = (frequencyAsInt: number): Frequency => { + if (frequencyAsInt == 1) return Frequency.PUNCTUAL; + if (frequencyAsInt == 2) return Frequency.RECURRENT; + throw new Error('Unknown frequency value'); +}; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts new file mode 100644 index 0000000..2bb02b1 --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts @@ -0,0 +1,15 @@ +import { WaypointDTO } from '../waypoint.dto'; + +export const hasValidPositionIndexes = (waypoints: WaypointDTO[]): boolean => { + if (!waypoints) return; + if (waypoints.every((waypoint) => waypoint.position === undefined)) + return true; + if (waypoints.every((waypoint) => typeof waypoint.position === 'number')) { + waypoints.sort((a, b) => a.position - b.position); + for (let i = 1; i < waypoints.length; i++) { + if (waypoints[i - 1].position >= waypoints[i].position) return false; + } + return true; + } + return false; +}; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts new file mode 100644 index 0000000..cb61059 --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts @@ -0,0 +1,10 @@ +import { AutoMap } from '@automapper/classes'; +import { IsInt, IsOptional } from 'class-validator'; +import { AddressDTO } from './address.dto'; + +export class WaypointDTO extends AddressDTO { + @IsOptional() + @IsInt() + @AutoMap() + position?: number; +} diff --git a/src/modules/ad/domain/dtos/find-ad-by-uuid.request.ts b/src/modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts similarity index 63% rename from src/modules/ad/domain/dtos/find-ad-by-uuid.request.ts rename to src/modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts index 9741962..e2a0d17 100644 --- a/src/modules/ad/domain/dtos/find-ad-by-uuid.request.ts +++ b/src/modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts @@ -1,7 +1,7 @@ import { IsNotEmpty, IsString } from 'class-validator'; -export class FindAdByUuidRequest { +export class FindAdByIdRequestDTO { @IsString() @IsNotEmpty() - uuid: string; + id: string; } diff --git a/src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts b/src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts new file mode 100644 index 0000000..e9427d3 --- /dev/null +++ b/src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts @@ -0,0 +1,37 @@ +import { Mapper } from '@automapper/core'; +import { InjectMapper } from '@automapper/nestjs'; +import { Controller, UsePipes } from '@nestjs/common'; +import { QueryBus } from '@nestjs/cqrs'; +import { GrpcMethod, RpcException } from '@nestjs/microservices'; +import { RpcValidationPipe } from '../../../../../utils/pipes/rpc.validation-pipe'; +import { FindAdByIdRequestDTO } from './dtos/find-ad-by-id.request.dto'; +import { AdPresenter } from '../../ad.presenter'; +import { FindAdByIdQuery } from '../../../core/queries/find-ad-by-id/find-ad-by-id.query'; +import { AdEntity } from '../../../core/ad.entity'; + +@UsePipes( + new RpcValidationPipe({ + whitelist: false, + forbidUnknownValues: false, + }), +) +@Controller() +export class FindAdByIdGrpcController { + constructor( + private readonly queryBus: QueryBus, + @InjectMapper() private readonly mapper: Mapper, + ) {} + + @GrpcMethod('AdsService', 'FindOneById') + async findOnebyId(data: FindAdByIdRequestDTO): Promise { + try { + const ad = await this.queryBus.execute(new FindAdByIdQuery(data)); + return this.mapper.map(ad, AdEntity, AdPresenter); + } catch (e) { + throw new RpcException({ + code: e.code, + message: e.message, + }); + } + } +} diff --git a/src/modules/ad/mappers/ad.profile.ts b/src/modules/ad/mappers/ad.profile.ts deleted file mode 100644 index 106b58a..0000000 --- a/src/modules/ad/mappers/ad.profile.ts +++ /dev/null @@ -1,139 +0,0 @@ -import { createMap, forMember, mapFrom, Mapper } from '@automapper/core'; -import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; -import { Injectable } from '@nestjs/common'; -import { Ad } from '../domain/entities/ad'; -import { AdPresenter } from '../adapters/primaries/ad.presenter'; -import { CreateAdRequest } from '../domain/dtos/create-ad.request'; -import { AdCreation } from '../domain/dtos/ad.creation'; -import { FrequencyNormaliser } from '../domain/entities/frequency.normaliser'; -import { Day } from '../domain/types/day.enum'; - -@Injectable() -export class AdProfile extends AutomapperProfile { - frequencyNormaliser = new FrequencyNormaliser(); - constructor(@InjectMapper() mapper: Mapper) { - super(mapper); - } - override get profile() { - return (mapper) => { - createMap(mapper, Ad, AdPresenter); - createMap( - mapper, - CreateAdRequest, - AdCreation, - forMember( - (destination) => destination.monMargin, - mapFrom((source) => source.marginDurations?.mon), - ), - forMember( - (destination) => destination.tueMargin, - mapFrom((source) => source.marginDurations?.tue), - ), - forMember( - (destination) => destination.wedMargin, - mapFrom((source) => source.marginDurations?.wed), - ), - forMember( - (destination) => destination.thuMargin, - mapFrom((source) => source.marginDurations?.thu), - ), - forMember( - (destination) => destination.friMargin, - mapFrom((source) => source.marginDurations?.fri), - ), - forMember( - (destination) => destination.satMargin, - mapFrom((source) => source.marginDurations?.sat), - ), - forMember( - (destination) => destination.sunMargin, - mapFrom((source) => source.marginDurations?.sun), - ), - forMember( - (destination) => destination.monTime, - mapFrom((source) => source.schedule.mon), - ), - forMember( - (destination) => destination.tueTime, - mapFrom((source) => source.schedule.tue), - ), - forMember( - (destination) => destination.wedTime, - mapFrom((source) => source.schedule.wed), - ), - forMember( - (destination) => destination.thuTime, - mapFrom((source) => source.schedule.thu), - ), - forMember( - (destination) => destination.friTime, - mapFrom((source) => source.schedule.fri), - ), - forMember( - (destination) => destination.satTime, - mapFrom((source) => source.schedule.sat), - ), - forMember( - (destination) => destination.sunTime, - mapFrom((source) => source.schedule.sun), - ), - forMember( - (destination) => destination.addresses.create, - mapFrom((source) => source.addresses), - ), - forMember( - (destination) => destination.fromDate, - mapFrom((source) => - this.frequencyNormaliser.fromDateResolver(source), - ), - ), - forMember( - (destination) => destination.toDate, - mapFrom((source) => this.frequencyNormaliser.toDateResolver(source)), - ), - forMember( - (destination) => destination.monTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.mon), - ), - ), - forMember( - (destination) => destination.tueTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.tue), - ), - ), - forMember( - (destination) => destination.wedTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.wed), - ), - ), - forMember( - (destination) => destination.thuTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.thu), - ), - ), - forMember( - (destination) => destination.friTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.fri), - ), - ), - forMember( - (destination) => destination.satTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.sat), - ), - ), - forMember( - (destination) => destination.sunTime, - mapFrom((source) => - this.frequencyNormaliser.scheduleResolver(source, Day.sun), - ), - ), - ); - }; - } -} diff --git a/src/modules/ad/mappers/address.profile.ts b/src/modules/ad/mappers/address.profile.ts deleted file mode 100644 index 09fa720..0000000 --- a/src/modules/ad/mappers/address.profile.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Mapper, createMap } from '@automapper/core'; -import { AutomapperProfile, InjectMapper } from '@automapper/nestjs'; -import { Injectable } from '@nestjs/common'; -import { AddressDTO } from '../domain/dtos/address.dto'; -import { Address } from '../domain/entities/address'; - -@Injectable() -export class AdProfile extends AutomapperProfile { - constructor(@InjectMapper() mapper: Mapper) { - super(mapper); - } - - override get profile() { - return (mapper) => { - createMap(mapper, AddressDTO, Address); - }; - } -} diff --git a/src/modules/ad/queries/find-ad-by-uuid.query.ts b/src/modules/ad/queries/find-ad-by-uuid.query.ts deleted file mode 100644 index 32e0257..0000000 --- a/src/modules/ad/queries/find-ad-by-uuid.query.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { FindAdByUuidRequest } from '../domain/dtos/find-ad-by-uuid.request'; - -export class FindAdByUuidQuery { - readonly uuid: string; - - constructor(findAdByUuidRequest: FindAdByUuidRequest) { - this.uuid = findAdByUuidRequest.uuid; - } -} diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 06cbc5d..62d6328 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -2,9 +2,9 @@ import { Test } from '@nestjs/testing'; import { PrismaService } from '../../../database/adapters/secondaries/prisma-service'; import { AdsRepository } from '../../adapters/secondaries/ads.repository'; import { DatabaseModule } from '../../../database/database.module'; -import { Frequency } from '../../domain/types/frequency.enum'; -import { AdCreation } from '../../domain/dtos/ad.creation'; -import { Address } from '../../domain/entities/address'; +import { Frequency } from '../../interface/commands/frequency.enum'; +import { AdDTO } from '../../core/dtos/ad.dto'; +import { Waypoint } from '../../core/entities/waypoint'; describe('Ad Repository', () => { let prismaService: PrismaService; @@ -400,7 +400,7 @@ describe('Ad Repository', () => { describe('create', () => { it('should create an punctual ad', async () => { const beforeCount = await prismaService.ad.count(); - const adToCreate: AdCreation = new AdCreation(); + const adToCreate: AdDTO = new AdDTO(); adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00'; adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; @@ -417,11 +417,11 @@ describe('Ad Repository', () => { adToCreate.friMargin = 900; adToCreate.satMargin = 900; adToCreate.sunMargin = 900; - adToCreate.seatsDriver = 3; - adToCreate.seatsPassenger = 0; + adToCreate.seatsProposed = 3; + adToCreate.seatsRequested = 0; adToCreate.strict = false; - adToCreate.addresses = { - create: [address0 as Address, address1 as Address], + adToCreate.waypoints = { + create: [address0 as Waypoint, address1 as Waypoint], }; const ad = await adsRepository.create(adToCreate); @@ -432,7 +432,7 @@ describe('Ad Repository', () => { }); it('should create an recurrent ad', async () => { const beforeCount = await prismaService.ad.count(); - const adToCreate: AdCreation = new AdCreation(); + const adToCreate: AdDTO = new AdDTO(); adToCreate.uuid = '137a26fa-4b38-48ba-aecf-1a75f6b20f3d'; adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; @@ -451,11 +451,11 @@ describe('Ad Repository', () => { adToCreate.friMargin = 900; adToCreate.satMargin = 900; adToCreate.sunMargin = 900; - adToCreate.seatsDriver = 2; - adToCreate.seatsPassenger = 0; + adToCreate.seatsProposed = 2; + adToCreate.seatsRequested = 0; adToCreate.strict = false; - adToCreate.addresses = { - create: [address0 as Address, address1 as Address], + adToCreate.waypoints = { + create: [address0 as Waypoint, address1 as Waypoint], }; const ad = await adsRepository.create(adToCreate); diff --git a/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts b/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts index 3096b7e..15918cd 100644 --- a/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts +++ b/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts @@ -1,7 +1,7 @@ import { ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; -import { DefaultParamsProvider } from '../../../../adapters/secondaries/default-params.provider'; -import { DefaultParams } from '../../../../domain/types/default-params.type'; +import { DefaultParamsProvider } from '../../../../adapters/secondaries/default-params-provider'; +import { DefaultParams } from '../../../../core/ports/default-params.type'; const mockConfigService = { get: jest.fn().mockImplementation(() => 'some_default_value'), diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts new file mode 100644 index 0000000..f6332a8 --- /dev/null +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -0,0 +1,106 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { CreateAdService } from '@modules/ad/core/commands/create-ad/create-ad.service'; +import { AD_REPOSITORY, PARAMS_PROVIDER } from '@modules/ad/ad.di-tokens'; +import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; +import { WaypointDTO } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; +import { CreateAdRequestDTO } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto'; +import { Frequency } from '@modules/ad/core/ad.types'; +import { CreateAdCommand } from '@modules/ad/core/commands/create-ad/create-ad.command'; +import { Result } from 'oxide.ts'; +import { AggregateID } from '@libs/ddd'; +import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; + +const originWaypoint: WaypointDTO = { + position: 0, + lon: 48.68944505415954, + lat: 6.176510296462267, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', +}; +const destinationWaypoint: WaypointDTO = { + position: 1, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', +}; +const punctualCreateAdRequest: CreateAdRequestDTO = { + userId: '4eb6a6af-ecfd-41c3-9118-473a507014d4', + fromDate: '2023-12-21', + toDate: '2023-12-21', + schedule: { + thu: '08:15', + }, + driver: true, + passenger: true, + seatsRequested: 1, + frequency: Frequency.PUNCTUAL, + waypoints: [originWaypoint, destinationWaypoint], +}; + +const mockAdRepository = { + insert: jest.fn().mockImplementationOnce(() => { + return Promise.resolve({ + uuid: '047a6ecf-23d4-4d3e-877c-3225d560a8da', + }); + }), +}; + +const mockDefaultParamsProvider: DefaultParamsProviderPort = { + getParams: () => { + return { + MON_MARGIN: 900, + TUE_MARGIN: 900, + WED_MARGIN: 900, + THU_MARGIN: 900, + FRI_MARGIN: 900, + SAT_MARGIN: 900, + SUN_MARGIN: 900, + DRIVER: false, + SEATS_PROPOSED: 3, + PASSENGER: true, + SEATS_REQUESTED: 1, + STRICT: false, + DEFAULT_TIMEZONE: 'Europe/Paris', + }; + }, +}; + +describe('create-ad.service', () => { + let createAdService: CreateAdService; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: AD_REPOSITORY, + useValue: mockAdRepository, + }, + { + provide: PARAMS_PROVIDER, + useValue: mockDefaultParamsProvider, + }, + CreateAdService, + ], + }).compile(); + + createAdService = module.get(CreateAdService); + }); + + it('should be defined', () => { + expect(createAdService).toBeDefined(); + }); + + describe('execution', () => { + const createAdCommand = new CreateAdCommand(punctualCreateAdRequest); + it('should create a new ad', async () => { + const result: Result = + await createAdService.execute(createAdCommand); + expect(result.unwrap()).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da'); + }); + }); +}); diff --git a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts b/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts index a816e0a..26c3176 100644 --- a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts +++ b/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts @@ -1,20 +1,19 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { CreateAdUseCase } from '../../../domain/usecases/create-ad.usecase'; -import { CreateAdRequest } from '../../../domain/dtos/create-ad.request'; +import { CreateAdUseCase } from '../../../core/usecases/create-ad.usecase'; +import { CreateAdRequestDTO } from '../../../interface/commands/create-ad.request.dto'; import { AdsRepository } from '../../../adapters/secondaries/ads.repository'; -import { CreateAdCommand } from '../../../commands/create-ad.command'; +import { CreateAdCommand } from '../../../interface/commands/create-ad.command'; import { AutomapperModule } from '@automapper/nestjs'; import { classes } from '@automapper/classes'; -import { Frequency } from '../../../domain/types/frequency.enum'; -import { Ad } from '../../../domain/entities/ad'; +import { Frequency } from '../../../interface/commands/frequency.enum'; +import { Ad } from '../../../core/entities/ad'; import { AdProfile } from '../../../mappers/ad.profile'; -import { AddressDTO } from '../../../domain/dtos/address.dto'; -import { AdCreation } from '../../../domain/dtos/ad.creation'; -import { Address } from '../../../domain/entities/address'; -import { PARAMS_PROVIDER } from '../../../ad.constants'; +import { Waypoint } from '../../../interface/commands/waypoint'; +import { AdDTO } from '../../../core/dtos/ad.dto'; +import { PARAMS_PROVIDER } from '../../../ad.di-tokens'; import { MESSAGE_PUBLISHER } from '../../../../../app.constants'; -const mockAddress1: AddressDTO = { +const originWaypoint: Waypoint = { position: 0, lon: 48.68944505415954, lat: 6.176510296462267, @@ -24,7 +23,7 @@ const mockAddress1: AddressDTO = { postalCode: '54000', country: 'France', }; -const mockAddress2: AddressDTO = { +const destinationWaypoint: Waypoint = { position: 1, lon: 48.8566, lat: 2.3522, @@ -32,38 +31,27 @@ const mockAddress2: AddressDTO = { postalCode: '75000', country: 'France', }; -const mockAddressWithoutPos1: AddressDTO = { +const originWaypointWithoutPosition: Waypoint = { lon: 43.2965, lat: 5.3698, locality: 'Marseille', postalCode: '13000', country: 'France', }; -const mockAddressWithoutPos2: AddressDTO = { +const destinationWaypointWithoutPosition: Waypoint = { lon: 43.7102, lat: 7.262, locality: 'Nice', postalCode: '06000', country: 'France', }; -const minimalRecurrentAdREquest: CreateAdRequest = { - userUuid: '224e0000-0000-4000-a000-000000000000', - frequency: Frequency.RECURRENT, - fromDate: new Date('01-05-2023'), - toDate: new Date('01-05-2024'), - schedule: { - mon: '08:00', - }, - marginDurations: {}, - addresses: [mockAddress1, mockAddress2], -}; -const newAdRequest: CreateAdRequest = { +const newAdRequest: CreateAdRequestDTO = { userUuid: '113e0000-0000-4000-a000-000000000000', driver: true, passenger: false, frequency: Frequency.RECURRENT, - fromDate: new Date('01-05-2023'), - toDate: new Date('20-08-2023'), + fromDate: new Date('2023-05-01'), + toDate: new Date('2023-08-20'), schedule: { tue: '08:00', wed: '08:30', @@ -77,8 +65,8 @@ const newAdRequest: CreateAdRequest = { sat: undefined, sun: undefined, }, - seatsDriver: 2, - addresses: [mockAddress1, mockAddress2], + seatsProposed: 2, + waypoints: [originWaypoint, destinationWaypoint], }; const mockMessagePublisher = { @@ -95,7 +83,7 @@ const mockDefaultParamsProvider = { SAT_MARGIN: 900, SUN_MARGIN: 900, DRIVER: false, - SEATS_PROVIDED: 0, + SEATS_PROPOSED: 3, PASSENGER: true, SEATS_REQUESTED: 1, STRICT: false, @@ -110,7 +98,7 @@ const mockAdRepository = { return Promise.resolve({ ...newAdRequest, uuid: 'ad000000-0000-4000-a000-000000000000', - createdAt: new Date('01-05-2023'), + createdAt: new Date('2023-05-01'), }); }) .mockImplementationOnce(() => { @@ -148,10 +136,10 @@ describe('CreateAdUseCase', () => { }); describe('execution', () => { const newAdCommand = new CreateAdCommand(newAdRequest); - it('should create an new ad', async () => { + it('should create a new ad', async () => { const newAd: Ad = await createAdUseCase.execute(newAdCommand); - expect(newAd.userUuid).toBe(newAdRequest.userUuid); - expect(newAd.uuid).toBeDefined(); + expect(newAd.userUuid).toBe('113e0000-0000-4000-a000-000000000000'); + expect(newAd.uuid).toBe('ad000000-0000-4000-a000-000000000000'); }); it('should throw an error if the ad already exists', async () => { await expect( @@ -165,68 +153,94 @@ describe('CreateAdUseCase', () => { mockAdRepository.create.mockClear(); }); - it('should define mimimal ad as 1 passager add', async () => { - const newAdCommand = new CreateAdCommand(minimalRecurrentAdREquest); - await createAdUseCase.execute(newAdCommand); - const expectedAdCreation = { - userUuid: minimalRecurrentAdREquest.userUuid, - frequency: minimalRecurrentAdREquest.frequency, - fromDate: minimalRecurrentAdREquest.fromDate, - toDate: minimalRecurrentAdREquest.toDate, - monTime: minimalRecurrentAdREquest.schedule.mon, + it('should define minimal recurrent ad as passenger', async () => { + const minimalRecurrentAdRequest: CreateAdRequestDTO = { + userUuid: '224e0000-0000-4000-a000-000000000000', + frequency: Frequency.RECURRENT, + fromDate: new Date('2023-05-01'), + toDate: new Date('2024-05-01'), + schedule: { + mon: '08:00', + }, + waypoints: [originWaypoint, destinationWaypoint], + }; + const newAdCommand = new CreateAdCommand(minimalRecurrentAdRequest); + const expectedAdCreation: AdDTO = { + userUuid: '224e0000-0000-4000-a000-000000000000', + frequency: Frequency.RECURRENT, + fromDate: new Date('2023-05-01'), + toDate: new Date('2024-05-01'), + monTime: '08:00', tueTime: undefined, wedTime: undefined, thuTime: undefined, friTime: undefined, satTime: undefined, sunTime: undefined, - monMargin: mockDefaultParamsProvider.getParams().MON_MARGIN, - tueMargin: mockDefaultParamsProvider.getParams().TUE_MARGIN, - wedMargin: mockDefaultParamsProvider.getParams().WED_MARGIN, - thuMargin: mockDefaultParamsProvider.getParams().THU_MARGIN, - friMargin: mockDefaultParamsProvider.getParams().FRI_MARGIN, - satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN, - sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN, - driver: mockDefaultParamsProvider.getParams().DRIVER, - seatsDriver: mockDefaultParamsProvider.getParams().SEATS_PROVIDED, - passenger: mockDefaultParamsProvider.getParams().PASSENGER, - seatsPassenger: mockDefaultParamsProvider.getParams().SEATS_REQUESTED, - strict: mockDefaultParamsProvider.getParams().STRICT, - addresses: { - create: minimalRecurrentAdREquest.addresses as Address[], + monMargin: 900, + tueMargin: 900, + wedMargin: 900, + thuMargin: 900, + friMargin: 900, + satMargin: 900, + sunMargin: 900, + driver: false, + seatsProposed: 3, + passenger: true, + seatsRequested: 1, + strict: false, + waypoints: { + create: [ + { + uuid: undefined, + adUuid: undefined, + position: 0, + lon: 48.68944505415954, + lat: 6.176510296462267, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + }, + { + uuid: undefined, + adUuid: undefined, + position: 1, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', + }, + ], }, createdAt: undefined, - } as AdCreation; - - expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation); + }; + await createAdUseCase.execute(newAdCommand); + expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation, { + waypoints: true, + }); }); - it('should create an passengerAd with addresses without position ', async () => { - const newPunctualPassengerAdRequest: CreateAdRequest = { + it('should create a passenger ad with waypoints without position', async () => { + const newPunctualPassengerAdRequest: CreateAdRequestDTO = { userUuid: '113e0000-0000-4000-a000-000000000000', passenger: true, frequency: Frequency.PUNCTUAL, - departure: new Date('05-22-2023 09:36'), - - marginDurations: { - mon: undefined, - tue: undefined, - wed: undefined, - thu: undefined, - fri: undefined, - sat: undefined, - sun: undefined, - }, - seatsPassenger: 1, - addresses: [mockAddressWithoutPos1, mockAddressWithoutPos2], + departureDate: new Date('2023-05-22 09:36'), + seatsRequested: 1, + waypoints: [ + originWaypointWithoutPosition, + destinationWaypointWithoutPosition, + ], schedule: {}, }; const newAdCommand = new CreateAdCommand(newPunctualPassengerAdRequest); - await createAdUseCase.execute(newAdCommand); - const expectedAdCreation = { - userUuid: newPunctualPassengerAdRequest.userUuid, - frequency: newPunctualPassengerAdRequest.frequency, - fromDate: newPunctualPassengerAdRequest.departure, - toDate: newPunctualPassengerAdRequest.departure, + const expectedAdCreation: AdDTO = { + userUuid: '113e0000-0000-4000-a000-000000000000', + frequency: Frequency.PUNCTUAL, + fromDate: new Date('2023-05-22'), + toDate: new Date('2023-05-22'), monTime: '09:36', tueTime: undefined, wedTime: undefined, @@ -234,24 +248,48 @@ describe('CreateAdUseCase', () => { friTime: undefined, satTime: undefined, sunTime: undefined, - monMargin: mockDefaultParamsProvider.getParams().MON_MARGIN, - tueMargin: mockDefaultParamsProvider.getParams().TUE_MARGIN, - wedMargin: mockDefaultParamsProvider.getParams().WED_MARGIN, - thuMargin: mockDefaultParamsProvider.getParams().THU_MARGIN, - friMargin: mockDefaultParamsProvider.getParams().FRI_MARGIN, - satMargin: mockDefaultParamsProvider.getParams().SAT_MARGIN, - sunMargin: mockDefaultParamsProvider.getParams().SUN_MARGIN, + monMargin: 900, + tueMargin: 900, + wedMargin: 900, + thuMargin: 900, + friMargin: 900, + satMargin: 900, + sunMargin: 900, driver: false, - seatsDriver: 0, - passenger: newPunctualPassengerAdRequest.passenger, - seatsPassenger: newPunctualPassengerAdRequest.seatsPassenger, - strict: mockDefaultParamsProvider.getParams().STRICT, - addresses: { - create: newPunctualPassengerAdRequest.addresses as Address[], + seatsProposed: 3, + passenger: true, + seatsRequested: 1, + strict: false, + waypoints: { + create: [ + { + uuid: undefined, + adUuid: undefined, + position: 0, + lon: 43.2965, + lat: 5.3698, + locality: 'Marseille', + postalCode: '13000', + country: 'France', + }, + { + uuid: undefined, + adUuid: undefined, + position: 1, + lon: 43.7102, + lat: 7.262, + locality: 'Nice', + postalCode: '06000', + country: 'France', + }, + ], }, createdAt: undefined, - } as AdCreation; - expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation); + }; + await createAdUseCase.execute(newAdCommand); + expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation, { + waypoints: true, + }); }); }); }); diff --git a/src/modules/ad/tests/unit/domain/find-ad-by-uuid.usecase.spec.ts b/src/modules/ad/tests/unit/domain/find-ad-by-uuid.usecase.spec.ts index d15ab75..cddd460 100644 --- a/src/modules/ad/tests/unit/domain/find-ad-by-uuid.usecase.spec.ts +++ b/src/modules/ad/tests/unit/domain/find-ad-by-uuid.usecase.spec.ts @@ -2,8 +2,8 @@ import { NotFoundException } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { FindAdByUuidQuery } from '../../../queries/find-ad-by-uuid.query'; import { AdsRepository } from '../../../adapters/secondaries/ads.repository'; -import { FindAdByUuidUseCase } from '../../../domain/usecases/find-ad-by-uuid.usecase'; -import { FindAdByUuidRequest } from '../../../domain/dtos/find-ad-by-uuid.request'; +import { FindAdByIdUseCase } from '../../../core/usecases/find-ad-by-uuid.usecase'; +import { FindAdByIdRequestDTO } from '../../../interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto'; import { MESSAGE_PUBLISHER } from '../../../../../app.constants'; const mockAd = { @@ -27,7 +27,7 @@ const mockMessagePublisher = { }; describe('FindAdByUuidUseCase', () => { - let findAdByUuidUseCase: FindAdByUuidUseCase; + let findAdByUuidUseCase: FindAdByIdUseCase; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [], @@ -40,29 +40,29 @@ describe('FindAdByUuidUseCase', () => { provide: MESSAGE_PUBLISHER, useValue: mockMessagePublisher, }, - FindAdByUuidUseCase, + FindAdByIdUseCase, ], }).compile(); - findAdByUuidUseCase = module.get(FindAdByUuidUseCase); + findAdByUuidUseCase = module.get(FindAdByIdUseCase); }); it('should be defined', () => { expect(findAdByUuidUseCase).toBeDefined(); }); describe('execute', () => { it('should return an ad', async () => { - const findAdByUuidRequest: FindAdByUuidRequest = - new FindAdByUuidRequest(); - findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a91'; + const findAdByUuidRequest: FindAdByIdRequestDTO = + new FindAdByIdRequestDTO(); + findAdByUuidRequest.id = 'bb281075-1b98-4456-89d6-c643d3044a91'; const ad = await findAdByUuidUseCase.execute( new FindAdByUuidQuery(findAdByUuidRequest), ); expect(ad).toBe(mockAd); }); it('should throw an error if ad does not exist', async () => { - const findAdByUuidRequest: FindAdByUuidRequest = - new FindAdByUuidRequest(); - findAdByUuidRequest.uuid = 'bb281075-1b98-4456-89d6-c643d3044a90'; + const findAdByUuidRequest: FindAdByIdRequestDTO = + new FindAdByIdRequestDTO(); + findAdByUuidRequest.id = 'bb281075-1b98-4456-89d6-c643d3044a90'; await expect( findAdByUuidUseCase.execute(new FindAdByUuidQuery(findAdByUuidRequest)), ).rejects.toBeInstanceOf(NotFoundException); diff --git a/src/modules/ad/tests/unit/domain/frequency-normalizer.spec.ts b/src/modules/ad/tests/unit/domain/frequency-normalizer.spec.ts new file mode 100644 index 0000000..573a749 --- /dev/null +++ b/src/modules/ad/tests/unit/domain/frequency-normalizer.spec.ts @@ -0,0 +1,140 @@ +import { Day } from '../../../core/_old/types/day.enum'; +import { CreateAdRequestDTO } from '../../../interface/commands/create-ad.request.dto'; +import { ScheduleDTO } from '../../../core/dtos/schedule.dto'; +import { FrequencyNormalizer } from '../../../core/entities/frequency.normalizer'; +import { Frequency } from '../../../interface/commands/frequency.enum'; + +describe('recurrent normalizer transformer for punctual ad ', () => { + const frequencyNormalizer = new FrequencyNormalizer(); + it('should transform punctual ad into recurrent ad', () => { + const punctualAd: CreateAdRequestDTO = { + userUuid: 'cb7ad514-ad0d-463f-9041-b79f9afe33aa', + frequency: Frequency.PUNCTUAL, + departureDate: new Date('2023-03-05T12:39:39+02:00'), + waypoints: [ + { + position: 0, + lon: 48.68944505415954, + lat: 6.176510296462267, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + }, + { + position: 1, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', + }, + ], + }; + // expect(frequencyNormalizer.fromDateResolver(punctualAd)).toStrictEqual( + // new Date('2023-03-05T12:39:39Z'), + // ); + // expect(frequencyNormalizer.toDateResolver(punctualAd)).toStrictEqual( + // new Date('2023-03-05T12:39:39Z'), + // ); + // expect( + // frequencyNormalizer.scheduleResolver(punctualAd, Day.mon), + // ).toBeUndefined(); + // expect( + // frequencyNormalizer.scheduleResolver(punctualAd, Day.tue), + // ).toBeUndefined(); + // expect( + // frequencyNormalizer.scheduleResolver(punctualAd, Day.wed), + // ).toBeUndefined(); + // expect( + // frequencyNormalizer.scheduleResolver(punctualAd, Day.thu), + // ).toBeUndefined(); + // expect( + // frequencyNormalizer.scheduleResolver(punctualAd, Day.fri), + // ).toBeUndefined(); + // expect( + // frequencyNormalizer.scheduleResolver(punctualAd, Day.sat), + // ).toBeUndefined(); + expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sun)).toBe( + '12:39', + ); + }); + // it('should leave recurrent ad as is', () => { + // const recurrentAd: CreateAdRequest = { + // userUuid: '', + // frequency: Frequency.RECURRENT, + // schedule: { + // mon: '08:30', + // tue: '08:30', + // wed: '09:00', + // fri: '09:00', + // }, + // waypoints: [], + // }; + // expect(frequencyNormalizer.fromDateResolver(recurrentAd)).toBe( + // recurrentAd.departure, + // ); + // expect(frequencyNormalizer.toDateResolver(recurrentAd)).toBe( + // recurrentAd.departure, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.mon)).toBe( + // recurrentAd.schedule.mon, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.tue)).toBe( + // recurrentAd.schedule.tue, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.wed)).toBe( + // recurrentAd.schedule.wed, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.thu)).toBe( + // recurrentAd.schedule.thu, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.fri)).toBe( + // recurrentAd.schedule.fri, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.sat)).toBe( + // recurrentAd.schedule.sat, + // ); + // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.sun)).toBe( + // recurrentAd.schedule.sun, + // ); + // }); + // it('should pass for each day of the week of a deprarture ', () => { + // const punctualAd: CreateAdRequest = { + // userUuid: '', + // frequency: Frequency.PUNCTUAL, + // departure: undefined, + // schedule: {} as ScheduleDTO, + // waypoints: [], + // }; + // punctualAd.departure = new Date('05-01-2023 '); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.mon)).toBe( + // '00:00', + // ); + // punctualAd.departure = new Date('05-02-2023 06:32:45'); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.tue)).toBe( + // '06:32', + // ); + // punctualAd.departure = new Date('05-03-2023 10:21'); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.wed)).toBe( + // '10:21', + // ); + // punctualAd.departure = new Date('05-04-2023 11:06:00'); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.thu)).toBe( + // '11:06', + // ); + // punctualAd.departure = new Date('05-05-2023 05:20'); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.fri)).toBe( + // '05:20', + // ); + // punctualAd.departure = new Date('05-06-2023'); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sat)).toBe( + // '00:00', + // ); + // punctualAd.departure = new Date('05-07-2023'); + // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sun)).toBe( + // '00:00', + // ); + // }); +}); diff --git a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts b/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts index 6a08276..a495fe0 100644 --- a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts +++ b/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts @@ -1,5 +1,5 @@ -import { intToFrequency } from '../../../domain/dtos/validators/frequency.mapping'; -import { Frequency } from '../../../domain/types/frequency.enum'; +import { intToFrequency } from '../../../core/dtos/validators/frequency.mapping'; +import { Frequency } from '../../../interface/commands/frequency.enum'; describe('frequency mapping function ', () => { it('should return punctual', () => { @@ -8,8 +8,8 @@ describe('frequency mapping function ', () => { it('should return recurrent', () => { expect(intToFrequency(2)).toBe(Frequency.RECURRENT); }); - it('should return undefined', () => { - expect(intToFrequency(0)).toBeUndefined(); - expect(intToFrequency(3)).toBeUndefined(); + it('should throw an error if frequency is unknown', () => { + expect(() => intToFrequency(0)).toThrow(); + expect(() => intToFrequency(3)).toThrow(); }); }); diff --git a/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts b/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts deleted file mode 100644 index 35bad11..0000000 --- a/src/modules/ad/tests/unit/domain/has-driver-seats-validator.spec.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { hasProperDriverSeats } from '../../../domain/dtos/validators/has-driver-seats'; - -describe('driver and/or driver seats validator', () => { - it('should validate if driver and drivers seats is not provided ', () => { - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: undefined }, - property: '', - }), - ).toBe(true); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: false }, - property: '', - }), - ).toBe(true); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: null }, - property: '', - }), - ).toBe(true); - }); - it('should not validate if driver is set to true but not the related seats is not provided or 0', () => { - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: true }, - property: '', - }), - ).toBe(false); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: true, seatsDriver: 0 }, - property: '', - }), - ).toBe(false); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: true, seatsDriver: undefined }, - property: '', - }), - ).toBe(false); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: true, seatsDriver: null }, - property: '', - }), - ).toBe(false); - }); - it('should not validate if driver seats are provided but driver is not set or set to false ', () => { - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: false, seatsDriver: 1 }, - property: '', - }), - ).toBe(false); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: undefined, seatsDriver: 1 }, - property: '', - }), - ).toBe(false); - expect( - hasProperDriverSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { driver: null, seatsDriver: 1 }, - property: '', - }), - ).toBe(false); - }); -}); diff --git a/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts b/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts deleted file mode 100644 index ae5459e..0000000 --- a/src/modules/ad/tests/unit/domain/has-passenger-seats-validator.spec.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { hasProperPassengerSeats } from '../../../domain/dtos/validators/has-passenger-seats'; - -describe('driver and/or passenger seats validator', () => { - it('should validate if passenger and passengers seats is not provided ', () => { - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: undefined }, - property: '', - }), - ).toBe(true); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: false }, - property: '', - }), - ).toBe(true); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: null }, - property: '', - }), - ).toBe(true); - }); - it('should not validate if passenger is set to true but not the related seats is not provided or 0', () => { - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: true }, - property: '', - }), - ).toBe(false); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: true, seatsPassenger: 0 }, - property: '', - }), - ).toBe(false); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: true, seatsPassenger: undefined }, - property: '', - }), - ).toBe(false); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: true, seatsPassenger: null }, - property: '', - }), - ).toBe(false); - }); - it('should not validate if passenger seats are provided but passenger is not set or set to false ', () => { - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: false, seatsPassenger: 1 }, - property: '', - }), - ).toBe(false); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: undefined, seatsPassenger: 1 }, - property: '', - }), - ).toBe(false); - expect( - hasProperPassengerSeats({ - value: undefined, - constraints: [], - targetName: '', - object: { passenger: null, seatsPassenger: 1 }, - property: '', - }), - ).toBe(false); - }); -}); diff --git a/src/modules/ad/tests/unit/domain/is-punctual-or-reccurent.spec.ts b/src/modules/ad/tests/unit/domain/is-punctual-or-recurrent.spec.ts similarity index 60% rename from src/modules/ad/tests/unit/domain/is-punctual-or-reccurent.spec.ts rename to src/modules/ad/tests/unit/domain/is-punctual-or-recurrent.spec.ts index c545407..d98e67e 100644 --- a/src/modules/ad/tests/unit/domain/is-punctual-or-reccurent.spec.ts +++ b/src/modules/ad/tests/unit/domain/is-punctual-or-recurrent.spec.ts @@ -1,10 +1,10 @@ -import { isPunctualOrRecurrent } from '../../../domain/dtos/validators/is-punctual-or-recurrent'; -import { Frequency } from '../../../domain/types/frequency.enum'; +import { isPunctualOrRecurrent } from '../../../core/dtos/validators/is-punctual-or-recurrent'; +import { Frequency } from '../../../interface/commands/frequency.enum'; describe('punctual or recurrent validators', () => { describe('punctual case ', () => { describe('valid cases', () => { - it('should validate with valid departure and empty schedule ', () => { + it('should validate with valid departure', () => { expect( isPunctualOrRecurrent({ value: undefined, @@ -12,8 +12,7 @@ describe('punctual or recurrent validators', () => { targetName: '', object: { frequency: Frequency.PUNCTUAL, - departure: new Date('01-02-2023'), - schedule: {}, + departure: new Date('2023-02-01T18:00:00+02:00'), }, property: '', }), @@ -21,7 +20,7 @@ describe('punctual or recurrent validators', () => { }); }); describe('invalid cases ', () => { - it('should not validate with invalid departure and empty schedule and margin', () => { + it('should not validate without departure', () => { expect( isPunctualOrRecurrent({ value: undefined, @@ -29,25 +28,6 @@ describe('punctual or recurrent validators', () => { targetName: '', object: { frequency: Frequency.PUNCTUAL, - fromDate: new Date('20-10-2023'), - toDate: new Date('30-10-2023'), - }, - property: '', - }), - ).toBeFalsy(); - }); - it('should not validate with no empty schedule', () => { - expect( - isPunctualOrRecurrent({ - value: undefined, - constraints: [], - targetName: '', - object: { - frequency: Frequency.PUNCTUAL, - departure: new Date('01-02-2023'), - schedule: { - mon: '08:30', - }, }, property: '', }), @@ -57,7 +37,7 @@ describe('punctual or recurrent validators', () => { }); describe('recurrent case ', () => { describe('valid cases', () => { - it('should validate with valid from date, to date and non empty schedule ', () => { + it('should validate with valid from date, to date and non empty schedule', () => { expect( isPunctualOrRecurrent({ value: undefined, @@ -65,8 +45,8 @@ describe('punctual or recurrent validators', () => { targetName: '', object: { frequency: Frequency.RECURRENT, - fromDate: new Date('01-15-2023'), - toDate: new Date('06-30-2023'), + fromDate: new Date('2023-01-15'), + toDate: new Date('2023-06-30'), schedule: { mon: '08:30', }, @@ -77,7 +57,7 @@ describe('punctual or recurrent validators', () => { }); }); describe('invalid cases ', () => { - it('should not validate with empty schedule ', () => { + it('should not validate with empty schedule', () => { expect( isPunctualOrRecurrent({ value: undefined, @@ -85,8 +65,8 @@ describe('punctual or recurrent validators', () => { targetName: '', object: { frequency: Frequency.RECURRENT, - fromDate: new Date('01-15-2023'), - toDate: new Date('06-30-2023'), + fromDate: new Date('2023-01-15'), + toDate: new Date('2023-06-30'), schedule: {}, }, property: '', @@ -101,8 +81,8 @@ describe('punctual or recurrent validators', () => { targetName: '', object: { frequency: Frequency.RECURRENT, - departure: new Date('20-10-2023'), - toDate: new Date('30-10-2023'), + departure: new Date('2023-10-20'), + toDate: new Date('2023-10-30'), }, property: '', }), diff --git a/src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts b/src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts deleted file mode 100644 index a411b2b..0000000 --- a/src/modules/ad/tests/unit/domain/recurrent-normaliser.spec.ts +++ /dev/null @@ -1,122 +0,0 @@ -import { Day } from '../../../domain/types/day.enum'; -import { CreateAdRequest } from '../../../domain/dtos/create-ad.request'; -import { ScheduleDTO } from '../../../domain/dtos/schedule.dto'; -import { FrequencyNormaliser } from '../../../domain/entities/frequency.normaliser'; -import { Frequency } from '../../../domain/types/frequency.enum'; - -describe('recurrent normalizer transformer for punctual ad ', () => { - const recurrentNormaliser = new FrequencyNormaliser(); - it('should transform punctual ad into recurrent ad ', () => { - const punctualAd: CreateAdRequest = { - userUuid: '', - frequency: Frequency.PUNCTUAL, - departure: new Date('05-03-2023 12:39:39 '), - schedule: {} as ScheduleDTO, - addresses: [], - }; - expect(recurrentNormaliser.fromDateResolver(punctualAd)).toBe( - punctualAd.departure, - ); - expect(recurrentNormaliser.toDateResolver(punctualAd)).toBe( - punctualAd.departure, - ); - expect( - recurrentNormaliser.scheduleResolver(punctualAd, Day.mon), - ).toBeUndefined(); - expect( - recurrentNormaliser.scheduleResolver(punctualAd, Day.tue), - ).toBeUndefined(); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.wed)).toBe( - '12:39', - ); - expect( - recurrentNormaliser.scheduleResolver(punctualAd, Day.thu), - ).toBeUndefined(); - expect( - recurrentNormaliser.scheduleResolver(punctualAd, Day.fri), - ).toBeUndefined(); - expect( - recurrentNormaliser.scheduleResolver(punctualAd, Day.sat), - ).toBeUndefined(); - expect( - recurrentNormaliser.scheduleResolver(punctualAd, Day.sun), - ).toBeUndefined(); - }); - it('should leave recurrent ad as is', () => { - const recurrentAd: CreateAdRequest = { - userUuid: '', - frequency: Frequency.RECURRENT, - schedule: { - mon: '08:30', - tue: '08:30', - wed: '09:00', - fri: '09:00', - }, - addresses: [], - }; - expect(recurrentNormaliser.fromDateResolver(recurrentAd)).toBe( - recurrentAd.departure, - ); - expect(recurrentNormaliser.toDateResolver(recurrentAd)).toBe( - recurrentAd.departure, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.mon)).toBe( - recurrentAd.schedule.mon, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.tue)).toBe( - recurrentAd.schedule.tue, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.wed)).toBe( - recurrentAd.schedule.wed, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.thu)).toBe( - recurrentAd.schedule.thu, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.fri)).toBe( - recurrentAd.schedule.fri, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.sat)).toBe( - recurrentAd.schedule.sat, - ); - expect(recurrentNormaliser.scheduleResolver(recurrentAd, Day.sun)).toBe( - recurrentAd.schedule.sun, - ); - }); - it('should pass for each day of the week of a deprarture ', () => { - const punctualAd: CreateAdRequest = { - userUuid: '', - frequency: Frequency.PUNCTUAL, - departure: undefined, - schedule: {} as ScheduleDTO, - addresses: [], - }; - punctualAd.departure = new Date('05-01-2023 '); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.mon)).toBe( - '00:00', - ); - punctualAd.departure = new Date('05-02-2023 06:32:45'); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.tue)).toBe( - '06:32', - ); - punctualAd.departure = new Date('05-03-2023 10:21'); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.wed)).toBe( - '10:21', - ); - punctualAd.departure = new Date('05-04-2023 11:06:00'); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.thu)).toBe( - '11:06', - ); - punctualAd.departure = new Date('05-05-2023 05:20'); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.fri)).toBe( - '05:20', - ); - punctualAd.departure = new Date('05-06-2023'); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.sat)).toBe( - '00:00', - ); - punctualAd.departure = new Date('05-07-2023'); - expect(recurrentNormaliser.scheduleResolver(punctualAd, Day.sun)).toBe( - '00:00', - ); - }); -}); diff --git a/src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts b/src/modules/ad/tests/unit/domain/valid-position-indexes.spec.ts similarity index 66% rename from src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts rename to src/modules/ad/tests/unit/domain/valid-position-indexes.spec.ts index 4a84a5e..f00d6f3 100644 --- a/src/modules/ad/tests/unit/domain/has-proper-addresses-indexes.spec.ts +++ b/src/modules/ad/tests/unit/domain/valid-position-indexes.spec.ts @@ -1,7 +1,7 @@ -import { AddressDTO } from '../../../domain/dtos/address.dto'; -import { hasProperPositionIndexes } from '../../../domain/dtos/validators/address-position'; +import { Waypoint } from '../../../interface/commands/waypoint'; +import { hasValidPositionIndexes } from '../../../core/dtos/validators/waypoint-position'; describe('addresses position validators', () => { - const mockAddress1: AddressDTO = { + const mockAddress1: Waypoint = { lon: 48.68944505415954, lat: 6.176510296462267, houseNumber: '5', @@ -10,7 +10,7 @@ describe('addresses position validators', () => { postalCode: '54000', country: 'France', }; - const mockAddress2: AddressDTO = { + const mockAddress2: Waypoint = { lon: 48.8566, lat: 2.3522, locality: 'Paris', @@ -18,7 +18,7 @@ describe('addresses position validators', () => { country: 'France', }; - const mockAddress3: AddressDTO = { + const mockAddress3: Waypoint = { lon: 49.2628, lat: 4.0347, locality: 'Reims', @@ -27,13 +27,13 @@ describe('addresses position validators', () => { }; it('should validate if none of position is definded ', () => { expect( - hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), + hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeTruthy(); }); it('should throw an error if position are partialy defined ', () => { mockAddress1.position = 0; expect( - hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), + hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeFalsy(); }); it('should throw an error if position are partialy defined ', () => { @@ -41,7 +41,7 @@ describe('addresses position validators', () => { mockAddress2.position = null; mockAddress3.position = undefined; expect( - hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), + hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeFalsy(); }); @@ -50,7 +50,7 @@ describe('addresses position validators', () => { mockAddress2.position = 1; mockAddress3.position = 1; expect( - hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), + hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeFalsy(); }); it('should validate if all positions are defined and incremented', () => { @@ -58,13 +58,13 @@ describe('addresses position validators', () => { mockAddress2.position = 1; mockAddress3.position = 2; expect( - hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), + hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeTruthy(); mockAddress1.position = 10; mockAddress2.position = 0; mockAddress3.position = 3; expect( - hasProperPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), + hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeTruthy(); }); }); diff --git a/src/modules/health/adapters/primaries/health.controller.ts b/src/modules/health/adapters/primaries/health.controller.ts index ba6ad9f..46a44a9 100644 --- a/src/modules/health/adapters/primaries/health.controller.ts +++ b/src/modules/health/adapters/primaries/health.controller.ts @@ -5,8 +5,8 @@ import { HealthCheckResult, } from '@nestjs/terminus'; import { MESSAGE_PUBLISHER } from 'src/app.constants'; -import { IPublishMessage } from 'src/interfaces/message-publisher'; import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase'; +import { MessagePublisherPort } from '@ports/message-publisher.port'; @Controller('health') export class HealthController { @@ -14,7 +14,7 @@ export class HealthController { private readonly repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase, private healthCheckService: HealthCheckService, @Inject(MESSAGE_PUBLISHER) - private readonly messagePublisher: IPublishMessage, + private readonly messagePublisher: MessagePublisherPort, ) {} @Get() diff --git a/src/modules/health/adapters/secondaries/message-publisher.ts b/src/modules/health/adapters/secondaries/message-publisher.ts index 98a963b..1350413 100644 --- a/src/modules/health/adapters/secondaries/message-publisher.ts +++ b/src/modules/health/adapters/secondaries/message-publisher.ts @@ -1,10 +1,10 @@ import { Inject, Injectable } from '@nestjs/common'; import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; -import { IPublishMessage } from 'src/interfaces/message-publisher'; +import { MessagePublisherPort } from '@ports/message-publisher.port'; @Injectable() -export class MessagePublisher implements IPublishMessage { +export class MessagePublisher implements MessagePublisherPort { constructor( @Inject(MESSAGE_BROKER_PUBLISHER) private readonly messageBrokerPublisher: MessageBrokerPublisher, diff --git a/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts b/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts index f4db828..1a6ef47 100644 --- a/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts +++ b/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts @@ -5,14 +5,14 @@ import { HealthIndicatorResult, } from '@nestjs/terminus'; import { ICheckRepository } from '../interfaces/check-repository.interface'; -import { AdsRepository } from '../../../ad/adapters/secondaries/ads.repository'; +import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; @Injectable() export class RepositoriesHealthIndicatorUseCase extends HealthIndicator { private checkRepositories: ICheckRepository[]; - constructor(private readonly adsRepository: AdsRepository) { + constructor(private readonly adRepository: AdRepository) { super(); - this.checkRepositories = [adsRepository]; + this.checkRepositories = [adRepository]; } isHealthy = async (key: string): Promise => { try { diff --git a/src/modules/health/health.module.ts b/src/modules/health/health.module.ts index de1bc71..d5ef060 100644 --- a/src/modules/health/health.module.ts +++ b/src/modules/health/health.module.ts @@ -1,6 +1,5 @@ import { Module } from '@nestjs/common'; import { HealthServerController } from './adapters/primaries/health-server.controller'; -import { AdsRepository } from '../ad/adapters/secondaries/ads.repository'; import { DatabaseModule } from '../database/database.module'; import { HealthController } from './adapters/primaries/health.controller'; import { TerminusModule } from '@nestjs/terminus'; @@ -8,13 +7,14 @@ import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; import { MessagePublisher } from './adapters/secondaries/message-publisher'; import { RepositoriesHealthIndicatorUseCase } from './domain/usecases/repositories.health-indicator.usecase'; +import { AdRepository } from '../ad/infrastructure/ad.repository'; @Module({ imports: [TerminusModule, DatabaseModule], controllers: [HealthServerController, HealthController], providers: [ RepositoriesHealthIndicatorUseCase, - AdsRepository, + AdRepository, { provide: MESSAGE_BROKER_PUBLISHER, useClass: MessageBrokerPublisher, diff --git a/src/interfaces/message-publisher.ts b/src/ports/message-publisher.port.ts similarity index 58% rename from src/interfaces/message-publisher.ts rename to src/ports/message-publisher.port.ts index 29ad456..fd35c90 100644 --- a/src/interfaces/message-publisher.ts +++ b/src/ports/message-publisher.port.ts @@ -1,3 +1,3 @@ -export interface IPublishMessage { +export interface MessagePublisherPort { publish(routingKey: string, message: string): void; } diff --git a/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts b/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts index 5b47172..6f0f365 100644 --- a/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts +++ b/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts @@ -1,6 +1,6 @@ import { ArgumentMetadata } from '@nestjs/common'; import { RpcValidationPipe } from '../../pipes/rpc.validation-pipe'; -import { FindAdByUuidRequest } from '../../../modules/ad/domain/dtos/find-ad-by-uuid.request'; +import { FindAdByIdRequestDTO } from '../../../modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto'; describe('RpcValidationPipe', () => { it('should not validate request', async () => { @@ -10,10 +10,10 @@ describe('RpcValidationPipe', () => { }); const metadata: ArgumentMetadata = { type: 'body', - metatype: FindAdByUuidRequest, + metatype: FindAdByIdRequestDTO, data: '', }; - await target.transform({}, metadata).catch((err) => { + await target.transform({}, metadata).catch((err) => { expect(err.message).toEqual('Rpc Exception'); }); }); diff --git a/tsconfig.json b/tsconfig.json index adb614c..a1f3fca 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,12 @@ "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "paths": { + "@libs/*": ["src/libs/*"], + "@modules/*": ["src/modules/*"], + "@ports/*": ["src/ports/*"], + "@utils/*": ["src/utils/*"], + } } } From b232247c93bb189e1a031c2be0aa62c816813fff Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 21 Jun 2023 11:50:36 +0200 Subject: [PATCH 02/29] working version, with basic tests --- package.json | 21 +- src/libs/db/prisma-repository.base.ts | 35 +- .../{prisma-service.ts => prisma.service.ts} | 0 src/libs/exceptions/exception.codes.ts | 1 + src/libs/exceptions/exceptions.ts | 17 + src/main.ts | 7 +- src/modules/ad/ad.module.ts | 10 +- .../ad/infrastructure/ad.repository.ts | 6 +- .../ad/infrastructure/prisma-service.ts | 15 - .../interface/{ => grpc-controllers}/ad.proto | 0 .../domain/create-ad.usecase.spec.ts | 0 .../domain/find-ad-by-uuid.usecase.spec.ts | 0 .../domain/frequency-normalizer.spec.ts | 0 .../domain/frequency.mapping.spec.ts | 0 .../domain/is-punctual-or-recurrent.spec.ts | 0 .../domain/valid-position-indexes.spec.ts | 0 .../tests/unit/core/create-ad.service.spec.ts | 10 +- .../default-param.provider.spec.ts | 32 +- .../message-publisher.spec.ts | 4 +- .../infrastructure/timezone-finder.spec.ts | 14 + .../secondaries/prisma-repository.abstract.ts | 258 -------- .../adapters/secondaries/prisma-service.ts | 15 - src/modules/database/database.module.ts | 9 - src/modules/database/domain/ad-repository.ts | 3 - .../database/exceptions/database.exception.ts | 24 - .../interfaces/collection.interface.ts | 4 - .../interfaces/repository.interface.ts | 18 - .../tests/unit/prisma-repository.spec.ts | 571 ------------------ .../adapters/primaries/health.controller.ts | 37 -- .../core/ports/check-repository.port.ts | 3 + .../repositories.health-indicator.usecase.ts | 48 ++ .../interfaces/check-repository.interface.ts | 3 - .../repositories.health-indicator.usecase.ts | 33 - src/modules/health/health.constants.ts | 1 + src/modules/health/health.di-tokens.ts | 1 + src/modules/health/health.module.ts | 19 +- .../message-publisher.ts | 2 +- .../health.grpc.controller.ts} | 4 +- .../grpc-controllers}/health.proto | 0 .../health.http.controller.ts | 24 + .../tests/unit/message-publisher.spec.ts | 4 +- ...ositories.health-indicator.usecase.spec.ts | 23 +- tsconfig.json | 1 + 43 files changed, 217 insertions(+), 1060 deletions(-) rename src/libs/db/{prisma-service.ts => prisma.service.ts} (100%) delete mode 100644 src/modules/ad/infrastructure/prisma-service.ts rename src/modules/ad/interface/{ => grpc-controllers}/ad.proto (100%) rename src/modules/ad/tests/{unit => }/domain/create-ad.usecase.spec.ts (100%) rename src/modules/ad/tests/{unit => }/domain/find-ad-by-uuid.usecase.spec.ts (100%) rename src/modules/ad/tests/{unit => }/domain/frequency-normalizer.spec.ts (100%) rename src/modules/ad/tests/{unit => }/domain/frequency.mapping.spec.ts (100%) rename src/modules/ad/tests/{unit => }/domain/is-punctual-or-recurrent.spec.ts (100%) rename src/modules/ad/tests/{unit => }/domain/valid-position-indexes.spec.ts (100%) rename src/modules/ad/tests/unit/{adapters/secondaries => infrastructure}/default-param.provider.spec.ts (51%) rename src/modules/ad/tests/unit/{adapters/secondaries => infrastructure}/message-publisher.spec.ts (84%) create mode 100644 src/modules/ad/tests/unit/infrastructure/timezone-finder.spec.ts delete mode 100644 src/modules/database/adapters/secondaries/prisma-repository.abstract.ts delete mode 100644 src/modules/database/adapters/secondaries/prisma-service.ts delete mode 100644 src/modules/database/database.module.ts delete mode 100644 src/modules/database/domain/ad-repository.ts delete mode 100644 src/modules/database/exceptions/database.exception.ts delete mode 100644 src/modules/database/interfaces/collection.interface.ts delete mode 100644 src/modules/database/interfaces/repository.interface.ts delete mode 100644 src/modules/database/tests/unit/prisma-repository.spec.ts delete mode 100644 src/modules/health/adapters/primaries/health.controller.ts create mode 100644 src/modules/health/core/ports/check-repository.port.ts create mode 100644 src/modules/health/core/usecases/repositories.health-indicator.usecase.ts delete mode 100644 src/modules/health/domain/interfaces/check-repository.interface.ts delete mode 100644 src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts create mode 100644 src/modules/health/health.constants.ts create mode 100644 src/modules/health/health.di-tokens.ts rename src/modules/health/{adapters/secondaries => infrastructure}/message-publisher.ts (88%) rename src/modules/health/{adapters/primaries/health-server.controller.ts => interface/grpc-controllers/health.grpc.controller.ts} (86%) rename src/modules/health/{adapters/primaries => interface/grpc-controllers}/health.proto (100%) create mode 100644 src/modules/health/interface/http-controllers/health.http.controller.ts diff --git a/package.json b/package.json index 310dcf9..dc70fd3 100644 --- a/package.json +++ b/package.json @@ -94,17 +94,14 @@ "ts" ], "modulePathIgnorePatterns": [ - ".controller.ts", + "libs/", ".module.ts", - ".request.ts", - ".presenter.ts", - ".profile.ts", - ".exception.ts", + ".dto.ts", ".constants.ts", "main.ts" ], "rootDir": "src", - "testRegex": ".*\\.service.spec\\.ts$", + "testRegex": ".*\\.spec\\.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, @@ -112,21 +109,17 @@ "**/*.(t|j)s" ], "coveragePathIgnorePatterns": [ - ".validator.ts", - ".controller.ts", + "libs/", ".module.ts", - ".request.ts", - ".presenter.ts", - ".profile.ts", - ".exception.ts", + ".dto.ts", ".constants.ts", - ".interfaces.ts", "main.ts" ], "coverageDirectory": "../coverage", "moduleNameMapper": { "^@libs(.*)": "/libs/$1", - "^@modules(.*)": "/modules/$1" + "^@modules(.*)": "/modules/$1", + "^@src(.*)": "$1" }, "testEnvironment": "node" } diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index 4a30d09..a6ba546 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -4,6 +4,8 @@ import { ObjectLiteral } from '../types'; import { LoggerPort } from '../ports/logger.port'; import { None, Option, Some } from 'oxide.ts'; import { PrismaRepositoryPort } from '../ports/prisma-repository.port'; +import { Prisma } from '@prisma/client'; +import { ConflictException, DatabaseErrorException } from '@libs/exceptions'; export abstract class PrismaRepositoryBase< Aggregate extends AggregateRoot, @@ -17,16 +19,11 @@ export abstract class PrismaRepositoryBase< protected readonly logger: LoggerPort, ) {} - async findOneById(uuid: string): Promise> { - try { - const entity = await this.prisma.findUnique({ - where: { uuid }, - }); - - return entity ? Some(this.mapper.toDomain(entity)) : None; - } catch (e) { - console.log('ouch on findOneById'); - } + async findOneById(id: string): Promise> { + const entity = await this.prisma.findUnique({ + where: { uuid: id }, + }); + return entity ? Some(this.mapper.toDomain(entity)) : None; } async insert(entity: Aggregate): Promise { @@ -35,12 +32,24 @@ export abstract class PrismaRepositoryBase< data: this.mapper.toPersistence(entity), }); } catch (e) { - console.log(e); - console.log('ouch on insert'); + if (e instanceof Prisma.PrismaClientKnownRequestError) { + if (e.message.includes('Already exists')) { + throw new ConflictException('Record already exists', e); + } + } + throw e; } } async healthCheck(): Promise { - return true; + try { + await this.prisma.$queryRaw`SELECT 1`; + return true; + } catch (e) { + if (e instanceof Prisma.PrismaClientKnownRequestError) { + throw new DatabaseErrorException(e.message); + } + throw new DatabaseErrorException(); + } } } diff --git a/src/libs/db/prisma-service.ts b/src/libs/db/prisma.service.ts similarity index 100% rename from src/libs/db/prisma-service.ts rename to src/libs/db/prisma.service.ts diff --git a/src/libs/exceptions/exception.codes.ts b/src/libs/exceptions/exception.codes.ts index 3291bfb..5ca541b 100644 --- a/src/libs/exceptions/exception.codes.ts +++ b/src/libs/exceptions/exception.codes.ts @@ -13,3 +13,4 @@ export const ARGUMENT_NOT_PROVIDED = 'GENERIC.ARGUMENT_NOT_PROVIDED'; export const NOT_FOUND = 'GENERIC.NOT_FOUND'; export const CONFLICT = 'GENERIC.CONFLICT'; export const INTERNAL_SERVER_ERROR = 'GENERIC.INTERNAL_SERVER_ERROR'; +export const DATABASE_ERROR = 'GENERIC.DATABASE_ERROR'; diff --git a/src/libs/exceptions/exceptions.ts b/src/libs/exceptions/exceptions.ts index 7044dbd..ceda019 100644 --- a/src/libs/exceptions/exceptions.ts +++ b/src/libs/exceptions/exceptions.ts @@ -3,6 +3,7 @@ import { ARGUMENT_NOT_PROVIDED, ARGUMENT_OUT_OF_RANGE, CONFLICT, + DATABASE_ERROR, INTERNAL_SERVER_ERROR, NOT_FOUND, } from '.'; @@ -80,3 +81,19 @@ export class InternalServerErrorException extends ExceptionBase { readonly code = INTERNAL_SERVER_ERROR; } + +/** + * Used to indicate a database error + * + * @class DatabaseErrorException + * @extends {ExceptionBase} + */ +export class DatabaseErrorException extends ExceptionBase { + static readonly message = 'Database error'; + + constructor(message = DatabaseErrorException.message) { + super(message); + } + + readonly code = DATABASE_ERROR; +} diff --git a/src/main.ts b/src/main.ts index 1c06331..505ada1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,8 +13,11 @@ async function bootstrap() { options: { package: ['ad', 'health'], protoPath: [ - join(__dirname, 'modules/ad/interface/ad.proto'), - join(__dirname, 'modules/health/adapters/primaries/health.proto'), + join(__dirname, 'modules/ad/interface/grpc-controllers/ad.proto'), + join( + __dirname, + 'modules/health/interface/grpc-controllers/health.proto', + ), ], url: process.env.SERVICE_URL + ':' + process.env.SERVICE_PORT, loader: { keepCase: true }, diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 505e5df..e81865a 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -1,24 +1,26 @@ import { Module } from '@nestjs/common'; import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller'; -import { DatabaseModule } from '../database/database.module'; import { CqrsModule } from '@nestjs/cqrs'; import { AD_REPOSITORY, PARAMS_PROVIDER, TIMEZONE_FINDER, } from './ad.di-tokens'; -import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; +import { + MESSAGE_BROKER_PUBLISHER, + MESSAGE_PUBLISHER, +} from '@src/app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; import { AdRepository } from './infrastructure/ad.repository'; import { DefaultParamsProvider } from './infrastructure/default-params-provider'; import { MessagePublisher } from './infrastructure/message-publisher'; -import { PrismaService } from './infrastructure/prisma-service'; import { AdMapper } from './ad.mapper'; import { CreateAdService } from './core/commands/create-ad/create-ad.service'; import { TimezoneFinder } from './infrastructure/timezone-finder'; +import { PrismaService } from '@libs/db/prisma.service'; @Module({ - imports: [DatabaseModule, CqrsModule], + imports: [CqrsModule], controllers: [CreateAdGrpcController], providers: [ CreateAdService, diff --git a/src/modules/ad/infrastructure/ad.repository.ts b/src/modules/ad/infrastructure/ad.repository.ts index 2b80a1e..1a187ef 100644 --- a/src/modules/ad/infrastructure/ad.repository.ts +++ b/src/modules/ad/infrastructure/ad.repository.ts @@ -1,10 +1,10 @@ import { Injectable, Logger } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; -import { AdRepositoryPort } from '../core/ports/ad.repository.port'; import { AdEntity } from '../core/ad.entity'; -import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base'; +import { AdRepositoryPort } from '../core/ports/ad.repository.port'; +import { PrismaService } from '@libs/db/prisma.service'; import { AdMapper } from '../ad.mapper'; -import { PrismaService } from './prisma-service'; +import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base'; export type AdModel = { uuid: string; diff --git a/src/modules/ad/infrastructure/prisma-service.ts b/src/modules/ad/infrastructure/prisma-service.ts deleted file mode 100644 index edf6532..0000000 --- a/src/modules/ad/infrastructure/prisma-service.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; -import { PrismaClient } from '@prisma/client'; - -@Injectable() -export class PrismaService extends PrismaClient implements OnModuleInit { - async onModuleInit() { - await this.$connect(); - } - - async enableShutdownHooks(app: INestApplication) { - this.$on('beforeExit', async () => { - await app.close(); - }); - } -} diff --git a/src/modules/ad/interface/ad.proto b/src/modules/ad/interface/grpc-controllers/ad.proto similarity index 100% rename from src/modules/ad/interface/ad.proto rename to src/modules/ad/interface/grpc-controllers/ad.proto diff --git a/src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts b/src/modules/ad/tests/domain/create-ad.usecase.spec.ts similarity index 100% rename from src/modules/ad/tests/unit/domain/create-ad.usecase.spec.ts rename to src/modules/ad/tests/domain/create-ad.usecase.spec.ts diff --git a/src/modules/ad/tests/unit/domain/find-ad-by-uuid.usecase.spec.ts b/src/modules/ad/tests/domain/find-ad-by-uuid.usecase.spec.ts similarity index 100% rename from src/modules/ad/tests/unit/domain/find-ad-by-uuid.usecase.spec.ts rename to src/modules/ad/tests/domain/find-ad-by-uuid.usecase.spec.ts diff --git a/src/modules/ad/tests/unit/domain/frequency-normalizer.spec.ts b/src/modules/ad/tests/domain/frequency-normalizer.spec.ts similarity index 100% rename from src/modules/ad/tests/unit/domain/frequency-normalizer.spec.ts rename to src/modules/ad/tests/domain/frequency-normalizer.spec.ts diff --git a/src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts b/src/modules/ad/tests/domain/frequency.mapping.spec.ts similarity index 100% rename from src/modules/ad/tests/unit/domain/frequency.mapping.spec.ts rename to src/modules/ad/tests/domain/frequency.mapping.spec.ts diff --git a/src/modules/ad/tests/unit/domain/is-punctual-or-recurrent.spec.ts b/src/modules/ad/tests/domain/is-punctual-or-recurrent.spec.ts similarity index 100% rename from src/modules/ad/tests/unit/domain/is-punctual-or-recurrent.spec.ts rename to src/modules/ad/tests/domain/is-punctual-or-recurrent.spec.ts diff --git a/src/modules/ad/tests/unit/domain/valid-position-indexes.spec.ts b/src/modules/ad/tests/domain/valid-position-indexes.spec.ts similarity index 100% rename from src/modules/ad/tests/unit/domain/valid-position-indexes.spec.ts rename to src/modules/ad/tests/domain/valid-position-indexes.spec.ts diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index f6332a8..d46d236 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -9,6 +9,7 @@ import { CreateAdCommand } from '@modules/ad/core/commands/create-ad/create-ad.c import { Result } from 'oxide.ts'; import { AggregateID } from '@libs/ddd'; import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; +import { AdEntity } from '@modules/ad/core/ad.entity'; const originWaypoint: WaypointDTO = { position: 0, @@ -43,11 +44,7 @@ const punctualCreateAdRequest: CreateAdRequestDTO = { }; const mockAdRepository = { - insert: jest.fn().mockImplementationOnce(() => { - return Promise.resolve({ - uuid: '047a6ecf-23d4-4d3e-877c-3225d560a8da', - }); - }), + insert: jest.fn(), }; const mockDefaultParamsProvider: DefaultParamsProviderPort = { @@ -98,6 +95,9 @@ describe('create-ad.service', () => { describe('execution', () => { const createAdCommand = new CreateAdCommand(punctualCreateAdRequest); it('should create a new ad', async () => { + AdEntity.create = jest.fn().mockReturnValue({ + id: '047a6ecf-23d4-4d3e-877c-3225d560a8da', + }); const result: Result = await createAdService.execute(createAdCommand); expect(result.unwrap()).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da'); diff --git a/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts b/src/modules/ad/tests/unit/infrastructure/default-param.provider.spec.ts similarity index 51% rename from src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts rename to src/modules/ad/tests/unit/infrastructure/default-param.provider.spec.ts index 15918cd..57ad3d8 100644 --- a/src/modules/ad/tests/unit/adapters/secondaries/default-param.provider.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/default-param.provider.spec.ts @@ -1,12 +1,29 @@ +import { DefaultParams } from '@modules/ad/core/ports/default-params.type'; +import { DefaultParamsProvider } from '@modules/ad/infrastructure/default-params-provider'; import { ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; -import { DefaultParamsProvider } from '../../../../adapters/secondaries/default-params-provider'; -import { DefaultParams } from '../../../../core/ports/default-params.type'; const mockConfigService = { - get: jest.fn().mockImplementation(() => 'some_default_value'), + get: jest.fn().mockImplementation((value: string) => { + switch (value) { + case 'DEPARTURE_MARGIN': + return 900; + case 'ROLE': + return 'passenger'; + case 'SEATS_PROPOSED': + return 3; + case 'SEATS_REQUESTED': + return 1; + case 'STRICT_FREQUENCY': + return 'false'; + case 'DEFAULT_TIMEZONE': + return 'Europe/Paris'; + default: + return 'some_default_value'; + } + }), }; -//TODO complete coverage + describe('DefaultParamsProvider', () => { let defaultParamsProvider: DefaultParamsProvider; @@ -33,8 +50,9 @@ describe('DefaultParamsProvider', () => { it('should provide default params', async () => { const params: DefaultParams = defaultParamsProvider.getParams(); - expect(params.SUN_MARGIN).toBeNaN(); - expect(params.PASSENGER).toBe(false); - expect(params.DRIVER).toBe(false); + expect(params.SUN_MARGIN).toBe(900); + expect(params.PASSENGER).toBeTruthy(); + expect(params.DRIVER).toBeFalsy(); + expect(params.DEFAULT_TIMEZONE).toBe('Europe/Paris'); }); }); diff --git a/src/modules/ad/tests/unit/adapters/secondaries/message-publisher.spec.ts b/src/modules/ad/tests/unit/infrastructure/message-publisher.spec.ts similarity index 84% rename from src/modules/ad/tests/unit/adapters/secondaries/message-publisher.spec.ts rename to src/modules/ad/tests/unit/infrastructure/message-publisher.spec.ts index d32a536..54dab1f 100644 --- a/src/modules/ad/tests/unit/adapters/secondaries/message-publisher.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/message-publisher.spec.ts @@ -1,6 +1,6 @@ +import { MessagePublisher } from '@modules/ad/infrastructure/message-publisher'; import { Test, TestingModule } from '@nestjs/testing'; -import { MessagePublisher } from '../../../../adapters/secondaries/message-publisher'; -import { MESSAGE_BROKER_PUBLISHER } from '../../../../../../app.constants'; +import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants'; const mockMessageBrokerPublisher = { publish: jest.fn().mockImplementation(), diff --git a/src/modules/ad/tests/unit/infrastructure/timezone-finder.spec.ts b/src/modules/ad/tests/unit/infrastructure/timezone-finder.spec.ts new file mode 100644 index 0000000..46e3ab8 --- /dev/null +++ b/src/modules/ad/tests/unit/infrastructure/timezone-finder.spec.ts @@ -0,0 +1,14 @@ +import { TimezoneFinder } from '@modules/ad/infrastructure/timezone-finder'; + +describe('Timezone Finder', () => { + it('should be defined', () => { + const timezoneFinder: TimezoneFinder = new TimezoneFinder(); + expect(timezoneFinder).toBeDefined(); + }); + it('should get timezone for Nancy(France) as Europe/Paris', () => { + const timezoneFinder: TimezoneFinder = new TimezoneFinder(); + const timezones = timezoneFinder.timezones(6.179373, 48.687913); + expect(timezones.length).toBe(1); + expect(timezones[0]).toBe('Europe/Paris'); + }); +}); diff --git a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts b/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts deleted file mode 100644 index b5b9700..0000000 --- a/src/modules/database/adapters/secondaries/prisma-repository.abstract.ts +++ /dev/null @@ -1,258 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { Prisma } from '@prisma/client'; -import { DatabaseException } from '../../exceptions/database.exception'; -import { ICollection } from '../../interfaces/collection.interface'; -import { IRepository } from '../../interfaces/repository.interface'; -import { PrismaService } from './prisma-service'; - -/** - * Child classes MUST redefined model property with appropriate model name - */ -@Injectable() -export abstract class PrismaRepository implements IRepository { - protected model: string; - - constructor(protected readonly _prisma: PrismaService) {} - - async findAll( - page = 1, - perPage = 10, - where?: any, - include?: any, - ): Promise> { - const [data, total] = await this._prisma.$transaction([ - this._prisma[this.model].findMany({ - where, - include, - skip: (page - 1) * perPage, - take: perPage, - }), - this._prisma[this.model].count({ - where, - }), - ]); - return Promise.resolve({ - data, - total, - }); - } - - async findOneByUuid(uuid: string): Promise { - try { - const entity = await this._prisma[this.model].findUnique({ - where: { uuid }, - }); - - return entity; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async findOne(where: any, include?: any): Promise { - try { - const entity = await this._prisma[this.model].findFirst({ - where: where, - include: include, - }); - - return entity; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - ); - } else { - throw new DatabaseException(); - } - } - } - - // TODO : using any is not good, but needed for nested entities - // TODO : Refactor for good clean architecture ? - async create(entity: Partial | any, include?: any): Promise { - try { - const res = await this._prisma[this.model].create({ - data: entity, - include: include, - }); - return res; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async update(uuid: string, entity: Partial): Promise { - try { - const updatedEntity = await this._prisma[this.model].update({ - where: { uuid }, - data: entity, - }); - return updatedEntity; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async updateWhere( - where: any, - entity: Partial | any, - include?: any, - ): Promise { - try { - const updatedEntity = await this._prisma[this.model].update({ - where: where, - data: entity, - include: include, - }); - - return updatedEntity; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async delete(uuid: string): Promise { - try { - const entity = await this._prisma[this.model].delete({ - where: { uuid }, - }); - - return entity; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async deleteMany(where: any): Promise { - try { - const entity = await this._prisma[this.model].deleteMany({ - where: where, - }); - - return entity; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async findAllByQuery( - include: string[], - where: string[], - ): Promise> { - const query = `SELECT ${include.join(',')} FROM ${ - this.model - } WHERE ${where.join(' AND ')}`; - const data: T[] = await this._prisma.$queryRawUnsafe(query); - return Promise.resolve({ - data, - total: data.length, - }); - } - - async createWithFields(fields: object): Promise { - try { - const command = `INSERT INTO ${this.model} ("${Object.keys(fields).join( - '","', - )}") VALUES (${Object.values(fields).join(',')})`; - return await this._prisma.$executeRawUnsafe(command); - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async updateWithFields(uuid: string, entity: object): Promise { - entity['"updatedAt"'] = `to_timestamp(${Date.now()} / 1000.0)`; - const values = Object.keys(entity).map((key) => `${key} = ${entity[key]}`); - try { - const command = `UPDATE ${this.model} SET ${values.join( - ', ', - )} WHERE uuid = '${uuid}'`; - return await this._prisma.$executeRawUnsafe(command); - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } - - async healthCheck(): Promise { - try { - await this._prisma.$queryRaw`SELECT 1`; - return true; - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - throw new DatabaseException( - Prisma.PrismaClientKnownRequestError.name, - e.code, - e.message, - ); - } else { - throw new DatabaseException(); - } - } - } -} diff --git a/src/modules/database/adapters/secondaries/prisma-service.ts b/src/modules/database/adapters/secondaries/prisma-service.ts deleted file mode 100644 index edf6532..0000000 --- a/src/modules/database/adapters/secondaries/prisma-service.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { INestApplication, Injectable, OnModuleInit } from '@nestjs/common'; -import { PrismaClient } from '@prisma/client'; - -@Injectable() -export class PrismaService extends PrismaClient implements OnModuleInit { - async onModuleInit() { - await this.$connect(); - } - - async enableShutdownHooks(app: INestApplication) { - this.$on('beforeExit', async () => { - await app.close(); - }); - } -} diff --git a/src/modules/database/database.module.ts b/src/modules/database/database.module.ts deleted file mode 100644 index f1defa7..0000000 --- a/src/modules/database/database.module.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Module } from '@nestjs/common'; -import { PrismaService } from './adapters/secondaries/prisma-service'; -import { AdRepository } from './domain/ad-repository'; - -@Module({ - providers: [PrismaService, AdRepository], - exports: [PrismaService, AdRepository], -}) -export class DatabaseModule {} diff --git a/src/modules/database/domain/ad-repository.ts b/src/modules/database/domain/ad-repository.ts deleted file mode 100644 index edbaf5f..0000000 --- a/src/modules/database/domain/ad-repository.ts +++ /dev/null @@ -1,3 +0,0 @@ -import { PrismaRepository } from '../adapters/secondaries/prisma-repository.abstract'; - -export class AdRepository extends PrismaRepository {} diff --git a/src/modules/database/exceptions/database.exception.ts b/src/modules/database/exceptions/database.exception.ts deleted file mode 100644 index b0782a6..0000000 --- a/src/modules/database/exceptions/database.exception.ts +++ /dev/null @@ -1,24 +0,0 @@ -export class DatabaseException implements Error { - name: string; - message: string; - - constructor( - private _type: string = 'unknown', - private _code: string = '', - message?: string, - ) { - this.name = 'DatabaseException'; - this.message = message ?? 'An error occured with the database.'; - if (this.message.includes('Unique constraint failed')) { - this.message = 'Already exists.'; - } - } - - get type(): string { - return this._type; - } - - get code(): string { - return this._code; - } -} diff --git a/src/modules/database/interfaces/collection.interface.ts b/src/modules/database/interfaces/collection.interface.ts deleted file mode 100644 index 6e9a96d..0000000 --- a/src/modules/database/interfaces/collection.interface.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface ICollection { - data: T[]; - total: number; -} diff --git a/src/modules/database/interfaces/repository.interface.ts b/src/modules/database/interfaces/repository.interface.ts deleted file mode 100644 index 1e23984..0000000 --- a/src/modules/database/interfaces/repository.interface.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { ICollection } from './collection.interface'; - -export interface IRepository { - findAll( - page: number, - perPage: number, - params?: any, - include?: any, - ): Promise>; - findOne(where: any, include?: any): Promise; - findOneByUuid(uuid: string, include?: any): Promise; - create(entity: Partial | any, include?: any): Promise; - update(uuid: string, entity: Partial, include?: any): Promise; - updateWhere(where: any, entity: Partial | any, include?: any): Promise; - delete(uuid: string): Promise; - deleteMany(where: any): Promise; - healthCheck(): Promise; -} diff --git a/src/modules/database/tests/unit/prisma-repository.spec.ts b/src/modules/database/tests/unit/prisma-repository.spec.ts deleted file mode 100644 index eb3bad0..0000000 --- a/src/modules/database/tests/unit/prisma-repository.spec.ts +++ /dev/null @@ -1,571 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { Test, TestingModule } from '@nestjs/testing'; -import { PrismaService } from '../../adapters/secondaries/prisma-service'; -import { PrismaRepository } from '../../adapters/secondaries/prisma-repository.abstract'; -import { DatabaseException } from '../../exceptions/database.exception'; -import { Prisma } from '@prisma/client'; - -class FakeEntity { - uuid?: string; - name: string; -} - -let entityId = 2; -const entityUuid = 'uuid-'; -const entityName = 'name-'; - -const createRandomEntity = (): FakeEntity => { - const entity: FakeEntity = { - uuid: `${entityUuid}${entityId}`, - name: `${entityName}${entityId}`, - }; - - entityId++; - - return entity; -}; - -const fakeEntityToCreate: FakeEntity = { - name: 'test', -}; - -const fakeEntityCreated: FakeEntity = { - ...fakeEntityToCreate, - uuid: 'some-uuid', -}; - -const fakeEntities: FakeEntity[] = []; -Array.from({ length: 10 }).forEach(() => { - fakeEntities.push(createRandomEntity()); -}); - -@Injectable() -class FakePrismaRepository extends PrismaRepository { - protected model = 'fake'; -} - -class FakePrismaService extends PrismaService { - fake: any; -} - -const mockPrismaService = { - $transaction: jest.fn().mockImplementation(async (data: any) => { - const entities = await data[0]; - if (entities.length == 1) { - return Promise.resolve([[fakeEntityCreated], 1]); - } - - return Promise.resolve([fakeEntities, fakeEntities.length]); - }), - // eslint-disable-next-line @typescript-eslint/no-unused-vars - $queryRawUnsafe: jest.fn().mockImplementation((query?: string) => { - return Promise.resolve(fakeEntities); - }), - $executeRawUnsafe: jest - .fn() - .mockResolvedValueOnce(fakeEntityCreated) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((fields: object) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((fields: object) => { - throw new Error('an unknown error'); - }) - .mockResolvedValueOnce(fakeEntityCreated) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((fields: object) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((fields: object) => { - throw new Error('an unknown error'); - }), - $queryRaw: jest - .fn() - .mockImplementationOnce(() => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - .mockImplementationOnce(() => { - return true; - }) - .mockImplementation(() => { - throw new Prisma.PrismaClientKnownRequestError('Database unavailable', { - code: 'code', - clientVersion: 'version', - }); - }), - fake: { - create: jest - .fn() - .mockResolvedValueOnce(fakeEntityCreated) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Error('an unknown error'); - }), - - findMany: jest.fn().mockImplementation((params?: any) => { - if (params?.where?.limit == 1) { - return Promise.resolve([fakeEntityCreated]); - } - - return Promise.resolve(fakeEntities); - }), - count: jest.fn().mockResolvedValue(fakeEntities.length), - - findUnique: jest.fn().mockImplementation(async (params?: any) => { - let entity; - - if (params?.where?.uuid) { - entity = fakeEntities.find( - (entity) => entity.uuid === params?.where?.uuid, - ); - } - - if (!entity && params?.where?.uuid == 'unknown') { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - } else if (!entity) { - throw new Error('no entity'); - } - - return entity; - }), - - findFirst: jest - .fn() - .mockImplementationOnce((params?: any) => { - if (params?.where?.name) { - return Promise.resolve( - fakeEntities.find((entity) => entity.name === params?.where?.name), - ); - } - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Error('an unknown error'); - }), - - update: jest - .fn() - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - .mockImplementationOnce((params: any) => { - const entity = fakeEntities.find( - (entity) => entity.name === params.where.name, - ); - Object.entries(params.data).map(([key, value]) => { - entity[key] = value; - }); - - return Promise.resolve(entity); - }) - .mockImplementation((params: any) => { - const entity = fakeEntities.find( - (entity) => entity.uuid === params.where.uuid, - ); - Object.entries(params.data).map(([key, value]) => { - entity[key] = value; - }); - - return Promise.resolve(entity); - }), - - delete: jest - .fn() - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - .mockImplementation((params: any) => { - let found = false; - - fakeEntities.forEach((entity, index) => { - if (entity.uuid === params?.where?.uuid) { - found = true; - fakeEntities.splice(index, 1); - } - }); - - if (!found) { - throw new Error(); - } - }), - - deleteMany: jest - .fn() - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((params?: any) => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) - .mockImplementation((params: any) => { - let found = false; - - fakeEntities.forEach((entity, index) => { - if (entity.uuid === params?.where?.uuid) { - found = true; - fakeEntities.splice(index, 1); - } - }); - - if (!found) { - throw new Error(); - } - }), - }, -}; - -describe('PrismaRepository', () => { - let fakeRepository: FakePrismaRepository; - let prisma: FakePrismaService; - - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [ - FakePrismaRepository, - { - provide: PrismaService, - useValue: mockPrismaService, - }, - ], - }).compile(); - - fakeRepository = module.get(FakePrismaRepository); - prisma = module.get(PrismaService) as FakePrismaService; - }); - - it('should be defined', () => { - expect(fakeRepository).toBeDefined(); - expect(prisma).toBeDefined(); - }); - - describe('findAll', () => { - it('should return an array of entities', async () => { - jest.spyOn(prisma.fake, 'findMany'); - jest.spyOn(prisma.fake, 'count'); - jest.spyOn(prisma, '$transaction'); - - const entities = await fakeRepository.findAll(); - expect(entities).toStrictEqual({ - data: fakeEntities, - total: fakeEntities.length, - }); - }); - - it('should return an array containing only one entity', async () => { - const entities = await fakeRepository.findAll(1, 10, { limit: 1 }); - - expect(prisma.fake.findMany).toHaveBeenCalledWith({ - skip: 0, - take: 10, - where: { limit: 1 }, - }); - expect(entities).toEqual({ - data: [fakeEntityCreated], - total: 1, - }); - }); - }); - - describe('create', () => { - it('should create an entity', async () => { - jest.spyOn(prisma.fake, 'create'); - - const newEntity = await fakeRepository.create(fakeEntityToCreate); - expect(newEntity).toBe(fakeEntityCreated); - expect(prisma.fake.create).toHaveBeenCalledTimes(1); - }); - - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.create(fakeEntityToCreate), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should throw a DatabaseException if uuid is not found', async () => { - await expect( - fakeRepository.create(fakeEntityToCreate), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('findOneByUuid', () => { - it('should find an entity by uuid', async () => { - const entity = await fakeRepository.findOneByUuid(fakeEntities[0].uuid); - expect(entity).toBe(fakeEntities[0]); - }); - - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.findOneByUuid('unknown'), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should throw a DatabaseException if uuid is not found', async () => { - await expect( - fakeRepository.findOneByUuid('wrong-uuid'), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('findOne', () => { - it('should find one entity', async () => { - const entity = await fakeRepository.findOne({ - name: fakeEntities[0].name, - }); - - expect(entity.name).toBe(fakeEntities[0].name); - }); - - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.findOne({ - name: fakeEntities[0].name, - }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should throw a DatabaseException for unknown error', async () => { - await expect( - fakeRepository.findOne({ - name: fakeEntities[0].name, - }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('update', () => { - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.update('fake-uuid', { name: 'error' }), - ).rejects.toBeInstanceOf(DatabaseException); - await expect( - fakeRepository.updateWhere({ name: 'error' }, { name: 'new error' }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should update an entity with name', async () => { - const newName = 'new-random-name'; - - await fakeRepository.updateWhere( - { name: fakeEntities[0].name }, - { - name: newName, - }, - ); - expect(fakeEntities[0].name).toBe(newName); - }); - - it('should update an entity with uuid', async () => { - const newName = 'random-name'; - - await fakeRepository.update(fakeEntities[0].uuid, { - name: newName, - }); - expect(fakeEntities[0].name).toBe(newName); - }); - - it("should throw an exception if an entity doesn't exist", async () => { - await expect( - fakeRepository.update('fake-uuid', { name: 'error' }), - ).rejects.toBeInstanceOf(DatabaseException); - await expect( - fakeRepository.updateWhere({ name: 'error' }, { name: 'new error' }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('delete', () => { - it('should throw a DatabaseException for client error', async () => { - await expect(fakeRepository.delete('fake-uuid')).rejects.toBeInstanceOf( - DatabaseException, - ); - }); - - it('should delete an entity', async () => { - const savedUuid = fakeEntities[0].uuid; - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const res = await fakeRepository.delete(savedUuid); - - const deletedEntity = fakeEntities.find( - (entity) => entity.uuid === savedUuid, - ); - expect(deletedEntity).toBeUndefined(); - }); - - it("should throw an exception if an entity doesn't exist", async () => { - await expect(fakeRepository.delete('fake-uuid')).rejects.toBeInstanceOf( - DatabaseException, - ); - }); - }); - - describe('deleteMany', () => { - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.deleteMany({ uuid: 'fake-uuid' }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should delete entities based on their uuid', async () => { - const savedUuid = fakeEntities[0].uuid; - - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const res = await fakeRepository.deleteMany({ uuid: savedUuid }); - - const deletedEntity = fakeEntities.find( - (entity) => entity.uuid === savedUuid, - ); - expect(deletedEntity).toBeUndefined(); - }); - - it("should throw an exception if an entity doesn't exist", async () => { - await expect( - fakeRepository.deleteMany({ uuid: 'fake-uuid' }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('findAllByquery', () => { - it('should return an array of entities', async () => { - const entities = await fakeRepository.findAllByQuery( - ['uuid', 'name'], - ['name is not null'], - ); - expect(entities).toStrictEqual({ - data: fakeEntities, - total: fakeEntities.length, - }); - }); - }); - - describe('createWithFields', () => { - it('should create an entity', async () => { - jest.spyOn(prisma, '$queryRawUnsafe'); - - const newEntity = await fakeRepository.createWithFields({ - uuid: '804319b3-a09b-4491-9f82-7976bfce0aff', - name: 'my-name', - }); - expect(newEntity).toBe(fakeEntityCreated); - expect(prisma.$queryRawUnsafe).toHaveBeenCalledTimes(1); - }); - - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.createWithFields({ - uuid: '804319b3-a09b-4491-9f82-7976bfce0aff', - name: 'my-name', - }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should throw a DatabaseException if uuid is not found', async () => { - await expect( - fakeRepository.createWithFields({ - name: 'my-name', - }), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('updateWithFields', () => { - it('should update an entity', async () => { - jest.spyOn(prisma, '$queryRawUnsafe'); - - const updatedEntity = await fakeRepository.updateWithFields( - '804319b3-a09b-4491-9f82-7976bfce0aff', - { - name: 'my-name', - }, - ); - expect(updatedEntity).toBe(fakeEntityCreated); - expect(prisma.$queryRawUnsafe).toHaveBeenCalledTimes(1); - }); - - it('should throw a DatabaseException for client error', async () => { - await expect( - fakeRepository.updateWithFields( - '804319b3-a09b-4491-9f82-7976bfce0aff', - { - name: 'my-name', - }, - ), - ).rejects.toBeInstanceOf(DatabaseException); - }); - - it('should throw a DatabaseException if uuid is not found', async () => { - await expect( - fakeRepository.updateWithFields( - '804319b3-a09b-4491-9f82-7976bfce0aff', - { - name: 'my-name', - }, - ), - ).rejects.toBeInstanceOf(DatabaseException); - }); - }); - - describe('healthCheck', () => { - it('should throw a DatabaseException for client error', async () => { - await expect(fakeRepository.healthCheck()).rejects.toBeInstanceOf( - DatabaseException, - ); - }); - - it('should return a healthy result', async () => { - const res = await fakeRepository.healthCheck(); - expect(res).toBeTruthy(); - }); - - it('should throw an exception if database is not available', async () => { - await expect(fakeRepository.healthCheck()).rejects.toBeInstanceOf( - DatabaseException, - ); - }); - }); -}); diff --git a/src/modules/health/adapters/primaries/health.controller.ts b/src/modules/health/adapters/primaries/health.controller.ts deleted file mode 100644 index 46a44a9..0000000 --- a/src/modules/health/adapters/primaries/health.controller.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Controller, Get, Inject } from '@nestjs/common'; -import { - HealthCheckService, - HealthCheck, - HealthCheckResult, -} from '@nestjs/terminus'; -import { MESSAGE_PUBLISHER } from 'src/app.constants'; -import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase'; -import { MessagePublisherPort } from '@ports/message-publisher.port'; - -@Controller('health') -export class HealthController { - constructor( - private readonly repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase, - private healthCheckService: HealthCheckService, - @Inject(MESSAGE_PUBLISHER) - private readonly messagePublisher: MessagePublisherPort, - ) {} - - @Get() - @HealthCheck() - async check() { - try { - return await this.healthCheckService.check([ - async () => - this.repositoriesHealthIndicatorUseCase.isHealthy('repositories'), - ]); - } catch (error) { - const healthCheckResult: HealthCheckResult = error.response; - this.messagePublisher.publish( - 'logging.user.health.crit', - JSON.stringify(healthCheckResult.error), - ); - throw error; - } - } -} diff --git a/src/modules/health/core/ports/check-repository.port.ts b/src/modules/health/core/ports/check-repository.port.ts new file mode 100644 index 0000000..64d8980 --- /dev/null +++ b/src/modules/health/core/ports/check-repository.port.ts @@ -0,0 +1,3 @@ +export interface CheckRepositoryPort { + healthCheck(): Promise; +} diff --git a/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts b/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts new file mode 100644 index 0000000..9600c31 --- /dev/null +++ b/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts @@ -0,0 +1,48 @@ +import { Inject, Injectable } from '@nestjs/common'; +import { + HealthCheckError, + HealthCheckResult, + HealthIndicator, + HealthIndicatorResult, +} from '@nestjs/terminus'; +import { CheckRepositoryPort } from '../ports/check-repository.port'; +import { AD_REPOSITORY } from '@modules/health/health.di-tokens'; +import { AdRepositoryPort } from '@modules/ad/core/ports/ad.repository.port'; +import { MESSAGE_PUBLISHER } from '@src/app.constants'; +import { MessagePublisherPort } from '@ports/message-publisher.port'; +import { LOGGING_AD_HEALTH_CRIT } from '@modules/health/health.constants'; + +@Injectable() +export class RepositoriesHealthIndicatorUseCase extends HealthIndicator { + private checkRepositories: CheckRepositoryPort[]; + constructor( + @Inject(AD_REPOSITORY) + private readonly adRepository: AdRepositoryPort, + @Inject(MESSAGE_PUBLISHER) + private readonly messagePublisher: MessagePublisherPort, + ) { + super(); + this.checkRepositories = [adRepository]; + } + isHealthy = async (key: string): Promise => { + try { + await Promise.all( + this.checkRepositories.map( + async (checkRepository: CheckRepositoryPort) => { + await checkRepository.healthCheck(); + }, + ), + ); + return this.getStatus(key, true); + } catch (error) { + const healthCheckResult: HealthCheckResult = error; + this.messagePublisher.publish( + LOGGING_AD_HEALTH_CRIT, + JSON.stringify(healthCheckResult.error), + ); + throw new HealthCheckError('Repository', { + repository: error.message, + }); + } + }; +} diff --git a/src/modules/health/domain/interfaces/check-repository.interface.ts b/src/modules/health/domain/interfaces/check-repository.interface.ts deleted file mode 100644 index 68c3178..0000000 --- a/src/modules/health/domain/interfaces/check-repository.interface.ts +++ /dev/null @@ -1,3 +0,0 @@ -export interface ICheckRepository { - healthCheck(): Promise; -} diff --git a/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts b/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts deleted file mode 100644 index 1a6ef47..0000000 --- a/src/modules/health/domain/usecases/repositories.health-indicator.usecase.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { Injectable } from '@nestjs/common'; -import { - HealthCheckError, - HealthIndicator, - HealthIndicatorResult, -} from '@nestjs/terminus'; -import { ICheckRepository } from '../interfaces/check-repository.interface'; -import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; - -@Injectable() -export class RepositoriesHealthIndicatorUseCase extends HealthIndicator { - private checkRepositories: ICheckRepository[]; - constructor(private readonly adRepository: AdRepository) { - super(); - this.checkRepositories = [adRepository]; - } - isHealthy = async (key: string): Promise => { - try { - await Promise.all( - this.checkRepositories.map( - async (checkRepository: ICheckRepository) => { - await checkRepository.healthCheck(); - }, - ), - ); - return this.getStatus(key, true); - } catch (e: any) { - throw new HealthCheckError('Repository', { - repository: e.message, - }); - } - }; -} diff --git a/src/modules/health/health.constants.ts b/src/modules/health/health.constants.ts new file mode 100644 index 0000000..3f29432 --- /dev/null +++ b/src/modules/health/health.constants.ts @@ -0,0 +1 @@ +export const LOGGING_AD_HEALTH_CRIT = 'logging.ad.health.crit'; diff --git a/src/modules/health/health.di-tokens.ts b/src/modules/health/health.di-tokens.ts new file mode 100644 index 0000000..2706306 --- /dev/null +++ b/src/modules/health/health.di-tokens.ts @@ -0,0 +1 @@ +export const AD_REPOSITORY = Symbol('AD_REPOSITORY'); diff --git a/src/modules/health/health.module.ts b/src/modules/health/health.module.ts index d5ef060..945c281 100644 --- a/src/modules/health/health.module.ts +++ b/src/modules/health/health.module.ts @@ -1,20 +1,23 @@ import { Module } from '@nestjs/common'; -import { HealthServerController } from './adapters/primaries/health-server.controller'; -import { DatabaseModule } from '../database/database.module'; -import { HealthController } from './adapters/primaries/health.controller'; +import { HealthHttpController } from './interface/http-controllers/health.http.controller'; import { TerminusModule } from '@nestjs/terminus'; import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; -import { MessagePublisher } from './adapters/secondaries/message-publisher'; -import { RepositoriesHealthIndicatorUseCase } from './domain/usecases/repositories.health-indicator.usecase'; +import { MessagePublisher } from './infrastructure/message-publisher'; +import { RepositoriesHealthIndicatorUseCase } from './core/usecases/repositories.health-indicator.usecase'; import { AdRepository } from '../ad/infrastructure/ad.repository'; +import { AD_REPOSITORY } from './health.di-tokens'; +import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller'; @Module({ - imports: [TerminusModule, DatabaseModule], - controllers: [HealthServerController, HealthController], + imports: [TerminusModule], + controllers: [HealthGrpcController, HealthHttpController], providers: [ RepositoriesHealthIndicatorUseCase, - AdRepository, + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, { provide: MESSAGE_BROKER_PUBLISHER, useClass: MessageBrokerPublisher, diff --git a/src/modules/health/adapters/secondaries/message-publisher.ts b/src/modules/health/infrastructure/message-publisher.ts similarity index 88% rename from src/modules/health/adapters/secondaries/message-publisher.ts rename to src/modules/health/infrastructure/message-publisher.ts index 1350413..070f5b1 100644 --- a/src/modules/health/adapters/secondaries/message-publisher.ts +++ b/src/modules/health/infrastructure/message-publisher.ts @@ -1,5 +1,5 @@ import { Inject, Injectable } from '@nestjs/common'; -import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants'; +import { MESSAGE_BROKER_PUBLISHER } from '../../../app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; import { MessagePublisherPort } from '@ports/message-publisher.port'; diff --git a/src/modules/health/adapters/primaries/health-server.controller.ts b/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts similarity index 86% rename from src/modules/health/adapters/primaries/health-server.controller.ts rename to src/modules/health/interface/grpc-controllers/health.grpc.controller.ts index 3cdc70d..5aa260e 100644 --- a/src/modules/health/adapters/primaries/health-server.controller.ts +++ b/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts @@ -1,6 +1,6 @@ import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; -import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase'; +import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; enum ServingStatus { UNKNOWN = 0, @@ -17,7 +17,7 @@ interface HealthCheckResponse { } @Controller() -export class HealthServerController { +export class HealthGrpcController { constructor( private readonly repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase, ) {} diff --git a/src/modules/health/adapters/primaries/health.proto b/src/modules/health/interface/grpc-controllers/health.proto similarity index 100% rename from src/modules/health/adapters/primaries/health.proto rename to src/modules/health/interface/grpc-controllers/health.proto diff --git a/src/modules/health/interface/http-controllers/health.http.controller.ts b/src/modules/health/interface/http-controllers/health.http.controller.ts new file mode 100644 index 0000000..0e43fee --- /dev/null +++ b/src/modules/health/interface/http-controllers/health.http.controller.ts @@ -0,0 +1,24 @@ +import { Controller, Get } from '@nestjs/common'; +import { HealthCheckService, HealthCheck } from '@nestjs/terminus'; +import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; + +@Controller('health') +export class HealthHttpController { + constructor( + private readonly repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase, + private healthCheckService: HealthCheckService, + ) {} + + @Get() + @HealthCheck() + async check() { + try { + return await this.healthCheckService.check([ + async () => + this.repositoriesHealthIndicatorUseCase.isHealthy('repositories'), + ]); + } catch (error) { + throw error; + } + } +} diff --git a/src/modules/health/tests/unit/message-publisher.spec.ts b/src/modules/health/tests/unit/message-publisher.spec.ts index eec02ea..7b3e9a9 100644 --- a/src/modules/health/tests/unit/message-publisher.spec.ts +++ b/src/modules/health/tests/unit/message-publisher.spec.ts @@ -1,6 +1,6 @@ +import { MessagePublisher } from '@modules/health/infrastructure/message-publisher'; import { Test, TestingModule } from '@nestjs/testing'; -import { MessagePublisher } from '../../adapters/secondaries/message-publisher'; -import { MESSAGE_BROKER_PUBLISHER } from '../../../../app.constants'; +import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants'; const mockMessageBrokerPublisher = { publish: jest.fn().mockImplementation(), diff --git a/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts b/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts index 0353505..6e5642f 100644 --- a/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts +++ b/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts @@ -1,19 +1,25 @@ import { Test, TestingModule } from '@nestjs/testing'; import { HealthCheckError, HealthIndicatorResult } from '@nestjs/terminus'; -import { RepositoriesHealthIndicatorUseCase } from '../../domain/usecases/repositories.health-indicator.usecase'; -import { AdsRepository } from '../../../ad/adapters/secondaries/ads.repository'; +import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; +import { AD_REPOSITORY } from '@modules/health/health.di-tokens'; +import { MESSAGE_PUBLISHER } from '@src/app.constants'; +import { DatabaseErrorException } from '@libs/exceptions'; -const mockAdsRepository = { +const mockAdRepository = { healthCheck: jest .fn() .mockImplementationOnce(() => { return Promise.resolve(true); }) .mockImplementation(() => { - throw new Error('an error occured in the repository'); + throw new DatabaseErrorException('an error occured in the database'); }), }; +const mockMessagePublisher = { + publish: jest.fn().mockImplementation(), +}; + describe('RepositoriesHealthIndicatorUseCase', () => { let repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase; @@ -22,8 +28,12 @@ describe('RepositoriesHealthIndicatorUseCase', () => { providers: [ RepositoriesHealthIndicatorUseCase, { - provide: AdsRepository, - useValue: mockAdsRepository, + provide: AD_REPOSITORY, + useValue: mockAdRepository, + }, + { + provide: MESSAGE_PUBLISHER, + useValue: mockMessagePublisher, }, ], }).compile(); @@ -42,7 +52,6 @@ describe('RepositoriesHealthIndicatorUseCase', () => { it('should check health successfully', async () => { const healthIndicatorResult: HealthIndicatorResult = await repositoriesHealthIndicatorUseCase.isHealthy('repositories'); - expect(healthIndicatorResult['repositories'].status).toBe('up'); }); diff --git a/tsconfig.json b/tsconfig.json index a1f3fca..c42aa05 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -22,6 +22,7 @@ "@modules/*": ["src/modules/*"], "@ports/*": ["src/ports/*"], "@utils/*": ["src/utils/*"], + "@src/*": ["src/*"], } } } From 1989ff6e67bcebbc757153a782908428718352fa Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 21 Jun 2023 12:31:59 +0200 Subject: [PATCH 03/29] refactor health module, improve code coverage --- src/app.module.ts | 3 +- src/libs/db/prisma-repository.base.ts | 9 +- src/libs/ports/prisma-repository.port.ts | 7 + src/modules/ad/ad.module.ts | 7 + .../ad/infrastructure/ad.repository.ts | 8 +- .../tests/domain/frequency-normalizer.spec.ts | 140 ------------------ .../ad/tests/domain/frequency.mapping.spec.ts | 15 -- .../domain/is-punctual-or-recurrent.spec.ts | 93 ------------ .../unit/interface/frequency.mapping.spec.ts | 15 ++ .../interface}/valid-position-indexes.spec.ts | 25 ++-- src/modules/health/health.module.ts | 3 +- ...ositories.health-indicator.usecase.spec.ts | 4 +- 12 files changed, 63 insertions(+), 266 deletions(-) delete mode 100644 src/modules/ad/tests/domain/frequency-normalizer.spec.ts delete mode 100644 src/modules/ad/tests/domain/frequency.mapping.spec.ts delete mode 100644 src/modules/ad/tests/domain/is-punctual-or-recurrent.spec.ts create mode 100644 src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts rename src/modules/ad/tests/{domain => unit/interface}/valid-position-indexes.spec.ts (68%) diff --git a/src/app.module.ts b/src/app.module.ts index ee0f470..f11aeaf 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -14,6 +14,7 @@ import { } from '@mobicoop/configuration-module'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { RequestContextModule } from 'nestjs-request-context'; +import { HealthModule } from '@modules/health/health.module'; @Module({ imports: [ @@ -53,7 +54,7 @@ import { RequestContextModule } from 'nestjs-request-context'; propagateConfigurationQueue: 'ad-configuration-propagate', }), }), - // HealthModule, + HealthModule, AdModule, ], controllers: [], diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index a6ba546..b27b41e 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -3,7 +3,10 @@ import { AggregateRoot, Mapper, RepositoryPort } from '../ddd'; import { ObjectLiteral } from '../types'; import { LoggerPort } from '../ports/logger.port'; import { None, Option, Some } from 'oxide.ts'; -import { PrismaRepositoryPort } from '../ports/prisma-repository.port'; +import { + PrismaRawRepositoryPort, + PrismaRepositoryPort, +} from '../ports/prisma-repository.port'; import { Prisma } from '@prisma/client'; import { ConflictException, DatabaseErrorException } from '@libs/exceptions'; @@ -14,6 +17,7 @@ export abstract class PrismaRepositoryBase< { protected constructor( protected readonly prisma: PrismaRepositoryPort | any, + protected readonly prismaRaw: PrismaRawRepositoryPort, protected readonly mapper: Mapper, protected readonly eventEmitter: EventEmitter2, protected readonly logger: LoggerPort, @@ -43,9 +47,10 @@ export abstract class PrismaRepositoryBase< async healthCheck(): Promise { try { - await this.prisma.$queryRaw`SELECT 1`; + await this.prismaRaw.$queryRaw`SELECT 1`; return true; } catch (e) { + console.log(e); if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseErrorException(e.message); } diff --git a/src/libs/ports/prisma-repository.port.ts b/src/libs/ports/prisma-repository.port.ts index 70cfcee..79b86fe 100644 --- a/src/libs/ports/prisma-repository.port.ts +++ b/src/libs/ports/prisma-repository.port.ts @@ -2,3 +2,10 @@ export interface PrismaRepositoryPort { findUnique(options: any): Promise; create(entity: any): Promise; } + +export interface PrismaRawRepositoryPort { + $queryRaw( + query: TemplateStringsArray, + ...values: any[] + ): Promise; +} diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index e81865a..2211535 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -47,5 +47,12 @@ import { PrismaService } from '@libs/db/prisma.service'; useClass: TimezoneFinder, }, ], + exports: [ + PrismaService, + AdMapper, + AD_REPOSITORY, + PARAMS_PROVIDER, + TIMEZONE_FINDER, + ], }) export class AdModule {} diff --git a/src/modules/ad/infrastructure/ad.repository.ts b/src/modules/ad/infrastructure/ad.repository.ts index 1a187ef..3bddba8 100644 --- a/src/modules/ad/infrastructure/ad.repository.ts +++ b/src/modules/ad/infrastructure/ad.repository.ts @@ -66,6 +66,12 @@ export class AdRepository mapper: AdMapper, eventEmitter: EventEmitter2, ) { - super(prisma.ad, mapper, eventEmitter, new Logger(AdRepository.name)); + super( + prisma.ad, + prisma, + mapper, + eventEmitter, + new Logger(AdRepository.name), + ); } } diff --git a/src/modules/ad/tests/domain/frequency-normalizer.spec.ts b/src/modules/ad/tests/domain/frequency-normalizer.spec.ts deleted file mode 100644 index 573a749..0000000 --- a/src/modules/ad/tests/domain/frequency-normalizer.spec.ts +++ /dev/null @@ -1,140 +0,0 @@ -import { Day } from '../../../core/_old/types/day.enum'; -import { CreateAdRequestDTO } from '../../../interface/commands/create-ad.request.dto'; -import { ScheduleDTO } from '../../../core/dtos/schedule.dto'; -import { FrequencyNormalizer } from '../../../core/entities/frequency.normalizer'; -import { Frequency } from '../../../interface/commands/frequency.enum'; - -describe('recurrent normalizer transformer for punctual ad ', () => { - const frequencyNormalizer = new FrequencyNormalizer(); - it('should transform punctual ad into recurrent ad', () => { - const punctualAd: CreateAdRequestDTO = { - userUuid: 'cb7ad514-ad0d-463f-9041-b79f9afe33aa', - frequency: Frequency.PUNCTUAL, - departureDate: new Date('2023-03-05T12:39:39+02:00'), - waypoints: [ - { - position: 0, - lon: 48.68944505415954, - lat: 6.176510296462267, - houseNumber: '5', - street: 'Avenue Foch', - locality: 'Nancy', - postalCode: '54000', - country: 'France', - }, - { - position: 1, - lon: 48.8566, - lat: 2.3522, - locality: 'Paris', - postalCode: '75000', - country: 'France', - }, - ], - }; - // expect(frequencyNormalizer.fromDateResolver(punctualAd)).toStrictEqual( - // new Date('2023-03-05T12:39:39Z'), - // ); - // expect(frequencyNormalizer.toDateResolver(punctualAd)).toStrictEqual( - // new Date('2023-03-05T12:39:39Z'), - // ); - // expect( - // frequencyNormalizer.scheduleResolver(punctualAd, Day.mon), - // ).toBeUndefined(); - // expect( - // frequencyNormalizer.scheduleResolver(punctualAd, Day.tue), - // ).toBeUndefined(); - // expect( - // frequencyNormalizer.scheduleResolver(punctualAd, Day.wed), - // ).toBeUndefined(); - // expect( - // frequencyNormalizer.scheduleResolver(punctualAd, Day.thu), - // ).toBeUndefined(); - // expect( - // frequencyNormalizer.scheduleResolver(punctualAd, Day.fri), - // ).toBeUndefined(); - // expect( - // frequencyNormalizer.scheduleResolver(punctualAd, Day.sat), - // ).toBeUndefined(); - expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sun)).toBe( - '12:39', - ); - }); - // it('should leave recurrent ad as is', () => { - // const recurrentAd: CreateAdRequest = { - // userUuid: '', - // frequency: Frequency.RECURRENT, - // schedule: { - // mon: '08:30', - // tue: '08:30', - // wed: '09:00', - // fri: '09:00', - // }, - // waypoints: [], - // }; - // expect(frequencyNormalizer.fromDateResolver(recurrentAd)).toBe( - // recurrentAd.departure, - // ); - // expect(frequencyNormalizer.toDateResolver(recurrentAd)).toBe( - // recurrentAd.departure, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.mon)).toBe( - // recurrentAd.schedule.mon, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.tue)).toBe( - // recurrentAd.schedule.tue, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.wed)).toBe( - // recurrentAd.schedule.wed, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.thu)).toBe( - // recurrentAd.schedule.thu, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.fri)).toBe( - // recurrentAd.schedule.fri, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.sat)).toBe( - // recurrentAd.schedule.sat, - // ); - // expect(frequencyNormalizer.scheduleResolver(recurrentAd, Day.sun)).toBe( - // recurrentAd.schedule.sun, - // ); - // }); - // it('should pass for each day of the week of a deprarture ', () => { - // const punctualAd: CreateAdRequest = { - // userUuid: '', - // frequency: Frequency.PUNCTUAL, - // departure: undefined, - // schedule: {} as ScheduleDTO, - // waypoints: [], - // }; - // punctualAd.departure = new Date('05-01-2023 '); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.mon)).toBe( - // '00:00', - // ); - // punctualAd.departure = new Date('05-02-2023 06:32:45'); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.tue)).toBe( - // '06:32', - // ); - // punctualAd.departure = new Date('05-03-2023 10:21'); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.wed)).toBe( - // '10:21', - // ); - // punctualAd.departure = new Date('05-04-2023 11:06:00'); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.thu)).toBe( - // '11:06', - // ); - // punctualAd.departure = new Date('05-05-2023 05:20'); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.fri)).toBe( - // '05:20', - // ); - // punctualAd.departure = new Date('05-06-2023'); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sat)).toBe( - // '00:00', - // ); - // punctualAd.departure = new Date('05-07-2023'); - // expect(frequencyNormalizer.scheduleResolver(punctualAd, Day.sun)).toBe( - // '00:00', - // ); - // }); -}); diff --git a/src/modules/ad/tests/domain/frequency.mapping.spec.ts b/src/modules/ad/tests/domain/frequency.mapping.spec.ts deleted file mode 100644 index a495fe0..0000000 --- a/src/modules/ad/tests/domain/frequency.mapping.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { intToFrequency } from '../../../core/dtos/validators/frequency.mapping'; -import { Frequency } from '../../../interface/commands/frequency.enum'; - -describe('frequency mapping function ', () => { - it('should return punctual', () => { - expect(intToFrequency(1)).toBe(Frequency.PUNCTUAL); - }); - it('should return recurrent', () => { - expect(intToFrequency(2)).toBe(Frequency.RECURRENT); - }); - it('should throw an error if frequency is unknown', () => { - expect(() => intToFrequency(0)).toThrow(); - expect(() => intToFrequency(3)).toThrow(); - }); -}); diff --git a/src/modules/ad/tests/domain/is-punctual-or-recurrent.spec.ts b/src/modules/ad/tests/domain/is-punctual-or-recurrent.spec.ts deleted file mode 100644 index d98e67e..0000000 --- a/src/modules/ad/tests/domain/is-punctual-or-recurrent.spec.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { isPunctualOrRecurrent } from '../../../core/dtos/validators/is-punctual-or-recurrent'; -import { Frequency } from '../../../interface/commands/frequency.enum'; - -describe('punctual or recurrent validators', () => { - describe('punctual case ', () => { - describe('valid cases', () => { - it('should validate with valid departure', () => { - expect( - isPunctualOrRecurrent({ - value: undefined, - constraints: [], - targetName: '', - object: { - frequency: Frequency.PUNCTUAL, - departure: new Date('2023-02-01T18:00:00+02:00'), - }, - property: '', - }), - ).toBeTruthy(); - }); - }); - describe('invalid cases ', () => { - it('should not validate without departure', () => { - expect( - isPunctualOrRecurrent({ - value: undefined, - constraints: [], - targetName: '', - object: { - frequency: Frequency.PUNCTUAL, - }, - property: '', - }), - ).toBeFalsy(); - }); - }); - }); - describe('recurrent case ', () => { - describe('valid cases', () => { - it('should validate with valid from date, to date and non empty schedule', () => { - expect( - isPunctualOrRecurrent({ - value: undefined, - constraints: [], - targetName: '', - object: { - frequency: Frequency.RECURRENT, - fromDate: new Date('2023-01-15'), - toDate: new Date('2023-06-30'), - schedule: { - mon: '08:30', - }, - }, - property: '', - }), - ).toBeTruthy(); - }); - }); - describe('invalid cases ', () => { - it('should not validate with empty schedule', () => { - expect( - isPunctualOrRecurrent({ - value: undefined, - constraints: [], - targetName: '', - object: { - frequency: Frequency.RECURRENT, - fromDate: new Date('2023-01-15'), - toDate: new Date('2023-06-30'), - schedule: {}, - }, - property: '', - }), - ).toBeFalsy(); - }); - it('should not validate with invalid from date to date and empty schedule and margin', () => { - expect( - isPunctualOrRecurrent({ - value: undefined, - constraints: [], - targetName: '', - object: { - frequency: Frequency.RECURRENT, - departure: new Date('2023-10-20'), - toDate: new Date('2023-10-30'), - }, - property: '', - }), - ).toBeFalsy(); - }); - }); - }); -}); diff --git a/src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts b/src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts new file mode 100644 index 0000000..bd8928d --- /dev/null +++ b/src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts @@ -0,0 +1,15 @@ +import { Frequency } from '@modules/ad/core/ad.types'; +import { intToFrequency } from '@modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping'; + +describe('frequency mapping', () => { + it('should return punctual if frequency is 1', () => { + expect(intToFrequency(1)).toBe(Frequency.PUNCTUAL); + }); + it('should return recurrent if frequency is 2', () => { + expect(intToFrequency(2)).toBe(Frequency.RECURRENT); + }); + it('should throw an error if frequency is unknown', () => { + expect(() => intToFrequency(0)).toThrow(); + expect(() => intToFrequency(3)).toThrow(); + }); +}); diff --git a/src/modules/ad/tests/domain/valid-position-indexes.spec.ts b/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts similarity index 68% rename from src/modules/ad/tests/domain/valid-position-indexes.spec.ts rename to src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts index f00d6f3..d90f99e 100644 --- a/src/modules/ad/tests/domain/valid-position-indexes.spec.ts +++ b/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts @@ -1,7 +1,8 @@ -import { Waypoint } from '../../../interface/commands/waypoint'; -import { hasValidPositionIndexes } from '../../../core/dtos/validators/waypoint-position'; -describe('addresses position validators', () => { - const mockAddress1: Waypoint = { +import { hasValidPositionIndexes } from '@modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position'; +import { WaypointDTO } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; + +describe('addresses position validator', () => { + const mockAddress1: WaypointDTO = { lon: 48.68944505415954, lat: 6.176510296462267, houseNumber: '5', @@ -10,33 +11,33 @@ describe('addresses position validators', () => { postalCode: '54000', country: 'France', }; - const mockAddress2: Waypoint = { + const mockAddress2: WaypointDTO = { lon: 48.8566, lat: 2.3522, locality: 'Paris', postalCode: '75000', country: 'France', }; - - const mockAddress3: Waypoint = { + const mockAddress3: WaypointDTO = { lon: 49.2628, lat: 4.0347, locality: 'Reims', postalCode: '51454', country: 'France', }; - it('should validate if none of position is definded ', () => { + + it('should validate if no position is defined', () => { expect( hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeTruthy(); }); - it('should throw an error if position are partialy defined ', () => { + it('should not validate if only one position is defined', () => { mockAddress1.position = 0; expect( hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeFalsy(); }); - it('should throw an error if position are partialy defined ', () => { + it('should not validate if positions are partially defined', () => { mockAddress1.position = 0; mockAddress2.position = null; mockAddress3.position = undefined; @@ -45,7 +46,7 @@ describe('addresses position validators', () => { ).toBeFalsy(); }); - it('should throw an error if positions are not incremented ', () => { + it('should not validate if multiple positions have same value', () => { mockAddress1.position = 0; mockAddress2.position = 1; mockAddress3.position = 1; @@ -53,7 +54,7 @@ describe('addresses position validators', () => { hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeFalsy(); }); - it('should validate if all positions are defined and incremented', () => { + it('should validate if all positions are ordered', () => { mockAddress1.position = 0; mockAddress2.position = 1; mockAddress3.position = 2; diff --git a/src/modules/health/health.module.ts b/src/modules/health/health.module.ts index 945c281..6b04c26 100644 --- a/src/modules/health/health.module.ts +++ b/src/modules/health/health.module.ts @@ -8,9 +8,10 @@ import { RepositoriesHealthIndicatorUseCase } from './core/usecases/repositories import { AdRepository } from '../ad/infrastructure/ad.repository'; import { AD_REPOSITORY } from './health.di-tokens'; import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller'; +import { AdModule } from '@modules/ad/ad.module'; @Module({ - imports: [TerminusModule], + imports: [TerminusModule, AdModule], controllers: [HealthGrpcController, HealthHttpController], providers: [ RepositoriesHealthIndicatorUseCase, diff --git a/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts b/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts index 6e5642f..cf9a7fc 100644 --- a/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts +++ b/src/modules/health/tests/unit/repositories.health-indicator.usecase.spec.ts @@ -12,7 +12,7 @@ const mockAdRepository = { return Promise.resolve(true); }) .mockImplementation(() => { - throw new DatabaseErrorException('an error occured in the database'); + throw new DatabaseErrorException('An error occured in the database'); }), }; @@ -56,9 +56,11 @@ describe('RepositoriesHealthIndicatorUseCase', () => { }); it('should throw an error if database is unavailable', async () => { + jest.spyOn(mockMessagePublisher, 'publish'); await expect( repositoriesHealthIndicatorUseCase.isHealthy('repositories'), ).rejects.toBeInstanceOf(HealthCheckError); + expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1); }); }); }); From 65f9ed41721c28327a945d6659e895e169da671c Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 21 Jun 2023 16:19:41 +0200 Subject: [PATCH 04/29] divide admodel to support different read-write models --- src/libs/db/prisma-repository.base.ts | 10 +- src/libs/ddd/mapper.interface.ts | 7 +- src/modules/ad/ad.mapper.ts | 32 +- .../ad/infrastructure/ad.repository.ts | 15 +- .../ad/tests/domain/create-ad.usecase.spec.ts | 295 ------- .../domain/find-ad-by-uuid.usecase.spec.ts | 71 -- .../tests/integration/ad.repository.spec.ts | 727 +++++++++--------- .../ad/tests/unit/core/ad.entity.spec.ts | 409 ++++++++++ .../tests/unit/core/create-ad.service.spec.ts | 27 +- 9 files changed, 853 insertions(+), 740 deletions(-) delete mode 100644 src/modules/ad/tests/domain/create-ad.usecase.spec.ts delete mode 100644 src/modules/ad/tests/domain/find-ad-by-uuid.usecase.spec.ts create mode 100644 src/modules/ad/tests/unit/core/ad.entity.spec.ts diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index b27b41e..031262c 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -12,20 +12,22 @@ import { ConflictException, DatabaseErrorException } from '@libs/exceptions'; export abstract class PrismaRepositoryBase< Aggregate extends AggregateRoot, - DbModel extends ObjectLiteral, + DbReadModel extends ObjectLiteral, + DbWriteModel extends ObjectLiteral, > implements RepositoryPort { protected constructor( protected readonly prisma: PrismaRepositoryPort | any, protected readonly prismaRaw: PrismaRawRepositoryPort, - protected readonly mapper: Mapper, + protected readonly mapper: Mapper, protected readonly eventEmitter: EventEmitter2, protected readonly logger: LoggerPort, ) {} - async findOneById(id: string): Promise> { + async findOneById(id: string, include?: any): Promise> { const entity = await this.prisma.findUnique({ where: { uuid: id }, + include, }); return entity ? Some(this.mapper.toDomain(entity)) : None; } @@ -47,7 +49,7 @@ export abstract class PrismaRepositoryBase< async healthCheck(): Promise { try { - await this.prismaRaw.$queryRaw`SELECT 1`; + await this.prisma.$queryRaw`SELECT 1`; return true; } catch (e) { console.log(e); diff --git a/src/libs/ddd/mapper.interface.ts b/src/libs/ddd/mapper.interface.ts index 3ce9520..8e9e3a4 100644 --- a/src/libs/ddd/mapper.interface.ts +++ b/src/libs/ddd/mapper.interface.ts @@ -2,10 +2,11 @@ import { Entity } from './entity.base'; export interface Mapper< DomainEntity extends Entity, - DbRecord, + DbReadRecord, + DbWriteRecord, Response = any, > { - toPersistence(entity: DomainEntity): DbRecord; - toDomain(record: any): DomainEntity; + toPersistence(entity: DomainEntity): DbWriteRecord; + toDomain(record: DbReadRecord): DomainEntity; toResponse(entity: DomainEntity): Response; } diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index 40d4f45..2b98e79 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -2,7 +2,11 @@ import { Mapper } from '@libs/ddd'; import { AdResponseDto } from './interface/dtos/ad.response.dto'; import { Inject, Injectable } from '@nestjs/common'; import { AdEntity } from './core/ad.entity'; -import { AdModel, WaypointModel } from './infrastructure/ad.repository'; +import { + AdWriteModel, + AdReadModel, + WaypointModel, +} from './infrastructure/ad.repository'; import { Frequency } from './core/ad.types'; import { WaypointProps } from './core/value-objects/waypoint.value-object'; import { v4 } from 'uuid'; @@ -21,7 +25,9 @@ import { DateTime, TimeZone } from 'timezonecomplete'; */ @Injectable() -export class AdMapper implements Mapper { +export class AdMapper + implements Mapper +{ private timezone: string; private readonly defaultParams: DefaultParams; @@ -34,11 +40,11 @@ export class AdMapper implements Mapper { this.defaultParams = defaultParamsProvider.getParams(); } - toPersistence = (entity: AdEntity): AdModel => { + toPersistence = (entity: AdEntity): AdWriteModel => { const copy = entity.getProps(); const timezone = this.getTimezone(copy.waypoints[0].address.coordinates); const now = new Date(); - const record: AdModel = { + const record: AdWriteModel = { uuid: copy.id, userUuid: copy.userId, driver: copy.driver, @@ -127,7 +133,7 @@ export class AdMapper implements Mapper { return record; }; - toDomain = (record: AdModel): AdEntity => { + toDomain = (record: AdReadModel): AdEntity => { const entity = new AdEntity({ id: record.uuid, createdAt: new Date(record.createdAt), @@ -140,13 +146,13 @@ export class AdMapper implements Mapper { fromDate: record.fromDate.toISOString(), toDate: record.toDate.toISOString(), schedule: { - mon: record.monTime.toISOString(), - tue: record.tueTime.toISOString(), - wed: record.wedTime.toISOString(), - thu: record.thuTime.toISOString(), - fri: record.friTime.toISOString(), - sat: record.satTime.toISOString(), - sun: record.sunTime.toISOString(), + mon: record.monTime?.toISOString(), + tue: record.tueTime?.toISOString(), + wed: record.wedTime?.toISOString(), + thu: record.thuTime?.toISOString(), + fri: record.friTime?.toISOString(), + sat: record.satTime?.toISOString(), + sun: record.sunTime?.toISOString(), }, marginDurations: { mon: record.monMargin, @@ -160,7 +166,7 @@ export class AdMapper implements Mapper { seatsProposed: record.seatsProposed, seatsRequested: record.seatsRequested, strict: record.strict, - waypoints: record.waypoints.create.map((waypoint: WaypointModel) => ({ + waypoints: record.waypoints.map((waypoint: WaypointModel) => ({ position: waypoint.position, address: { name: waypoint.name, diff --git a/src/modules/ad/infrastructure/ad.repository.ts b/src/modules/ad/infrastructure/ad.repository.ts index 3bddba8..29a4dca 100644 --- a/src/modules/ad/infrastructure/ad.repository.ts +++ b/src/modules/ad/infrastructure/ad.repository.ts @@ -6,7 +6,7 @@ import { PrismaService } from '@libs/db/prisma.service'; import { AdMapper } from '../ad.mapper'; import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base'; -export type AdModel = { +export type AdBaseModel = { uuid: string; userUuid: string; driver: boolean; @@ -31,11 +31,18 @@ export type AdModel = { seatsProposed: number; seatsRequested: number; strict: boolean; + createdAt: Date; + updatedAt: Date; +}; + +export type AdReadModel = AdBaseModel & { + waypoints: WaypointModel[]; +}; + +export type AdWriteModel = AdBaseModel & { waypoints: { create: WaypointModel[]; }; - createdAt: Date; - updatedAt: Date; }; export type WaypointModel = { @@ -58,7 +65,7 @@ export type WaypointModel = { * */ @Injectable() export class AdRepository - extends PrismaRepositoryBase + extends PrismaRepositoryBase implements AdRepositoryPort { constructor( diff --git a/src/modules/ad/tests/domain/create-ad.usecase.spec.ts b/src/modules/ad/tests/domain/create-ad.usecase.spec.ts deleted file mode 100644 index 26c3176..0000000 --- a/src/modules/ad/tests/domain/create-ad.usecase.spec.ts +++ /dev/null @@ -1,295 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { CreateAdUseCase } from '../../../core/usecases/create-ad.usecase'; -import { CreateAdRequestDTO } from '../../../interface/commands/create-ad.request.dto'; -import { AdsRepository } from '../../../adapters/secondaries/ads.repository'; -import { CreateAdCommand } from '../../../interface/commands/create-ad.command'; -import { AutomapperModule } from '@automapper/nestjs'; -import { classes } from '@automapper/classes'; -import { Frequency } from '../../../interface/commands/frequency.enum'; -import { Ad } from '../../../core/entities/ad'; -import { AdProfile } from '../../../mappers/ad.profile'; -import { Waypoint } from '../../../interface/commands/waypoint'; -import { AdDTO } from '../../../core/dtos/ad.dto'; -import { PARAMS_PROVIDER } from '../../../ad.di-tokens'; -import { MESSAGE_PUBLISHER } from '../../../../../app.constants'; - -const originWaypoint: Waypoint = { - position: 0, - lon: 48.68944505415954, - lat: 6.176510296462267, - houseNumber: '5', - street: 'Avenue Foch', - locality: 'Nancy', - postalCode: '54000', - country: 'France', -}; -const destinationWaypoint: Waypoint = { - position: 1, - lon: 48.8566, - lat: 2.3522, - locality: 'Paris', - postalCode: '75000', - country: 'France', -}; -const originWaypointWithoutPosition: Waypoint = { - lon: 43.2965, - lat: 5.3698, - locality: 'Marseille', - postalCode: '13000', - country: 'France', -}; -const destinationWaypointWithoutPosition: Waypoint = { - lon: 43.7102, - lat: 7.262, - locality: 'Nice', - postalCode: '06000', - country: 'France', -}; -const newAdRequest: CreateAdRequestDTO = { - userUuid: '113e0000-0000-4000-a000-000000000000', - driver: true, - passenger: false, - frequency: Frequency.RECURRENT, - fromDate: new Date('2023-05-01'), - toDate: new Date('2023-08-20'), - schedule: { - tue: '08:00', - wed: '08:30', - }, - marginDurations: { - mon: undefined, - tue: undefined, - wed: undefined, - thu: undefined, - fri: undefined, - sat: undefined, - sun: undefined, - }, - seatsProposed: 2, - waypoints: [originWaypoint, destinationWaypoint], -}; - -const mockMessagePublisher = { - publish: jest.fn().mockImplementation(), -}; -const mockDefaultParamsProvider = { - getParams: () => { - return { - MON_MARGIN: 900, - TUE_MARGIN: 900, - WED_MARGIN: 900, - THU_MARGIN: 900, - FRI_MARGIN: 900, - SAT_MARGIN: 900, - SUN_MARGIN: 900, - DRIVER: false, - SEATS_PROPOSED: 3, - PASSENGER: true, - SEATS_REQUESTED: 1, - STRICT: false, - }; - }, -}; -const mockAdRepository = { - create: jest - .fn() - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((command?: CreateAdCommand) => { - return Promise.resolve({ - ...newAdRequest, - uuid: 'ad000000-0000-4000-a000-000000000000', - createdAt: new Date('2023-05-01'), - }); - }) - .mockImplementationOnce(() => { - throw new Error('Already exists'); - }) - .mockImplementation(), -}; -describe('CreateAdUseCase', () => { - let createAdUseCase: CreateAdUseCase; - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - imports: [AutomapperModule.forRoot({ strategyInitializer: classes() })], - providers: [ - { - provide: AdsRepository, - useValue: mockAdRepository, - }, - { - provide: MESSAGE_PUBLISHER, - useValue: mockMessagePublisher, - }, - CreateAdUseCase, - AdProfile, - { - provide: PARAMS_PROVIDER, - useValue: mockDefaultParamsProvider, - }, - ], - }).compile(); - - createAdUseCase = module.get(CreateAdUseCase); - }); - it('should be defined', () => { - expect(createAdUseCase).toBeDefined(); - }); - describe('execution', () => { - const newAdCommand = new CreateAdCommand(newAdRequest); - it('should create a new ad', async () => { - const newAd: Ad = await createAdUseCase.execute(newAdCommand); - expect(newAd.userUuid).toBe('113e0000-0000-4000-a000-000000000000'); - expect(newAd.uuid).toBe('ad000000-0000-4000-a000-000000000000'); - }); - it('should throw an error if the ad already exists', async () => { - await expect( - createAdUseCase.execute(newAdCommand), - ).rejects.toBeInstanceOf(Error); - }); - }); - - describe('Ad parameter default setting ', () => { - beforeEach(() => { - mockAdRepository.create.mockClear(); - }); - - it('should define minimal recurrent ad as passenger', async () => { - const minimalRecurrentAdRequest: CreateAdRequestDTO = { - userUuid: '224e0000-0000-4000-a000-000000000000', - frequency: Frequency.RECURRENT, - fromDate: new Date('2023-05-01'), - toDate: new Date('2024-05-01'), - schedule: { - mon: '08:00', - }, - waypoints: [originWaypoint, destinationWaypoint], - }; - const newAdCommand = new CreateAdCommand(minimalRecurrentAdRequest); - const expectedAdCreation: AdDTO = { - userUuid: '224e0000-0000-4000-a000-000000000000', - frequency: Frequency.RECURRENT, - fromDate: new Date('2023-05-01'), - toDate: new Date('2024-05-01'), - monTime: '08:00', - tueTime: undefined, - wedTime: undefined, - thuTime: undefined, - friTime: undefined, - satTime: undefined, - sunTime: undefined, - monMargin: 900, - tueMargin: 900, - wedMargin: 900, - thuMargin: 900, - friMargin: 900, - satMargin: 900, - sunMargin: 900, - driver: false, - seatsProposed: 3, - passenger: true, - seatsRequested: 1, - strict: false, - waypoints: { - create: [ - { - uuid: undefined, - adUuid: undefined, - position: 0, - lon: 48.68944505415954, - lat: 6.176510296462267, - houseNumber: '5', - street: 'Avenue Foch', - locality: 'Nancy', - postalCode: '54000', - country: 'France', - }, - { - uuid: undefined, - adUuid: undefined, - position: 1, - lon: 48.8566, - lat: 2.3522, - locality: 'Paris', - postalCode: '75000', - country: 'France', - }, - ], - }, - createdAt: undefined, - }; - await createAdUseCase.execute(newAdCommand); - expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation, { - waypoints: true, - }); - }); - it('should create a passenger ad with waypoints without position', async () => { - const newPunctualPassengerAdRequest: CreateAdRequestDTO = { - userUuid: '113e0000-0000-4000-a000-000000000000', - passenger: true, - frequency: Frequency.PUNCTUAL, - departureDate: new Date('2023-05-22 09:36'), - seatsRequested: 1, - waypoints: [ - originWaypointWithoutPosition, - destinationWaypointWithoutPosition, - ], - schedule: {}, - }; - const newAdCommand = new CreateAdCommand(newPunctualPassengerAdRequest); - const expectedAdCreation: AdDTO = { - userUuid: '113e0000-0000-4000-a000-000000000000', - frequency: Frequency.PUNCTUAL, - fromDate: new Date('2023-05-22'), - toDate: new Date('2023-05-22'), - monTime: '09:36', - tueTime: undefined, - wedTime: undefined, - thuTime: undefined, - friTime: undefined, - satTime: undefined, - sunTime: undefined, - monMargin: 900, - tueMargin: 900, - wedMargin: 900, - thuMargin: 900, - friMargin: 900, - satMargin: 900, - sunMargin: 900, - driver: false, - seatsProposed: 3, - passenger: true, - seatsRequested: 1, - strict: false, - waypoints: { - create: [ - { - uuid: undefined, - adUuid: undefined, - position: 0, - lon: 43.2965, - lat: 5.3698, - locality: 'Marseille', - postalCode: '13000', - country: 'France', - }, - { - uuid: undefined, - adUuid: undefined, - position: 1, - lon: 43.7102, - lat: 7.262, - locality: 'Nice', - postalCode: '06000', - country: 'France', - }, - ], - }, - createdAt: undefined, - }; - await createAdUseCase.execute(newAdCommand); - expect(mockAdRepository.create).toBeCalledWith(expectedAdCreation, { - waypoints: true, - }); - }); - }); -}); diff --git a/src/modules/ad/tests/domain/find-ad-by-uuid.usecase.spec.ts b/src/modules/ad/tests/domain/find-ad-by-uuid.usecase.spec.ts deleted file mode 100644 index cddd460..0000000 --- a/src/modules/ad/tests/domain/find-ad-by-uuid.usecase.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { NotFoundException } from '@nestjs/common'; -import { Test, TestingModule } from '@nestjs/testing'; -import { FindAdByUuidQuery } from '../../../queries/find-ad-by-uuid.query'; -import { AdsRepository } from '../../../adapters/secondaries/ads.repository'; -import { FindAdByIdUseCase } from '../../../core/usecases/find-ad-by-uuid.usecase'; -import { FindAdByIdRequestDTO } from '../../../interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto'; -import { MESSAGE_PUBLISHER } from '../../../../../app.constants'; - -const mockAd = { - uuid: 'bb281075-1b98-4456-89d6-c643d3044a91', -}; - -const mockAdRepository = { - findOneByUuid: jest - .fn() - // eslint-disable-next-line @typescript-eslint/no-unused-vars - .mockImplementationOnce((query?: FindAdByUuidQuery) => { - return Promise.resolve(mockAd); - }) - .mockImplementation(() => { - return Promise.resolve(null); - }), -}; - -const mockMessagePublisher = { - publish: jest.fn().mockImplementation(), -}; - -describe('FindAdByUuidUseCase', () => { - let findAdByUuidUseCase: FindAdByIdUseCase; - beforeAll(async () => { - const module: TestingModule = await Test.createTestingModule({ - imports: [], - providers: [ - { - provide: AdsRepository, - useValue: mockAdRepository, - }, - { - provide: MESSAGE_PUBLISHER, - useValue: mockMessagePublisher, - }, - FindAdByIdUseCase, - ], - }).compile(); - - findAdByUuidUseCase = module.get(FindAdByIdUseCase); - }); - it('should be defined', () => { - expect(findAdByUuidUseCase).toBeDefined(); - }); - describe('execute', () => { - it('should return an ad', async () => { - const findAdByUuidRequest: FindAdByIdRequestDTO = - new FindAdByIdRequestDTO(); - findAdByUuidRequest.id = 'bb281075-1b98-4456-89d6-c643d3044a91'; - const ad = await findAdByUuidUseCase.execute( - new FindAdByUuidQuery(findAdByUuidRequest), - ); - expect(ad).toBe(mockAd); - }); - it('should throw an error if ad does not exist', async () => { - const findAdByUuidRequest: FindAdByIdRequestDTO = - new FindAdByIdRequestDTO(); - findAdByUuidRequest.id = 'bb281075-1b98-4456-89d6-c643d3044a90'; - await expect( - findAdByUuidUseCase.execute(new FindAdByUuidQuery(findAdByUuidRequest)), - ).rejects.toBeInstanceOf(NotFoundException); - }); - }); -}); diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 62d6328..319af8d 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -1,14 +1,20 @@ +import { PrismaService } from '@libs/db/prisma.service'; +import { + AD_REPOSITORY, + PARAMS_PROVIDER, + TIMEZONE_FINDER, +} from '@modules/ad/ad.di-tokens'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; +import { DefaultParamsProvider } from '@modules/ad/infrastructure/default-params-provider'; +import { TimezoneFinder } from '@modules/ad/infrastructure/timezone-finder'; +import { ConfigModule } from '@nestjs/config'; +import { EventEmitterModule } from '@nestjs/event-emitter'; import { Test } from '@nestjs/testing'; -import { PrismaService } from '../../../database/adapters/secondaries/prisma-service'; -import { AdsRepository } from '../../adapters/secondaries/ads.repository'; -import { DatabaseModule } from '../../../database/database.module'; -import { Frequency } from '../../interface/commands/frequency.enum'; -import { AdDTO } from '../../core/dtos/ad.dto'; -import { Waypoint } from '../../core/entities/waypoint'; describe('Ad Repository', () => { let prismaService: PrismaService; - let adsRepository: AdsRepository; + let adRepository: AdRepository; const executeInsertCommand = async (table: string, object: any) => { const command = `INSERT INTO ${table} ("${Object.keys(object).join( @@ -20,13 +26,14 @@ describe('Ad Repository', () => { const getSeed = (index: number, uuid: string): string => { return `'${uuid.slice(0, -2)}${index.toString(16).padStart(2, '0')}'`; }; + const baseUuid = { uuid: 'be459a29-7a41-4c0b-b371-abe90bfb6f00', }; - const baseAdress0Uuid = { + const baseOriginWaypointUuid = { uuid: 'bad5e786-3b15-4e51-a8fc-926fa9327ff1', }; - const baseAdress1Uuid = { + const baseDestinationWaypointUuid = { uuid: '4d200eb6-7389-487f-a1ca-dbc0e40381c9', }; const baseUserUuid = { @@ -35,24 +42,24 @@ describe('Ad Repository', () => { const driverAd = { driver: 'true', passenger: 'false', - seatsDriver: 3, - seatsPassenger: 0, - strict: 'false', - }; - const passengerAd = { - driver: 'false', - passenger: 'true', - seatsDriver: 0, - seatsPassenger: 1, - strict: 'false', - }; - const driverAndPassengerAd = { - driver: 'true', - passenger: 'true', - seatsDriver: 3, - seatsPassenger: 1, + seatsProposed: 3, + seatsRequested: 0, strict: 'false', }; + // const passengerAd = { + // driver: 'false', + // passenger: 'true', + // seatsProposed: 0, + // seatsRequested: 1, + // strict: 'false', + // }; + // const driverAndPassengerAd = { + // driver: 'true', + // passenger: 'true', + // seatsProposed: 3, + // seatsRequested: 1, + // strict: 'false', + // }; const punctualAd = { frequency: `'PUNCTUAL'`, fromDate: `'2023-01-01'`, @@ -63,7 +70,7 @@ describe('Ad Repository', () => { thuTime: 'NULL', friTime: 'NULL', satTime: 'NULL', - sunTime: `'07:00'`, + sunTime: `'2023-01-01T07:00:00Z'`, monMargin: 900, tueMargin: 900, wedMargin: 900, @@ -72,27 +79,26 @@ describe('Ad Repository', () => { satMargin: 900, sunMargin: 900, }; - const recurrentAd = { - frequency: `'RECURRENT'`, - fromDate: `'2023-01-01'`, - toDate: `'2023-12-31'`, - monTime: `'07:00'`, - tueTime: `'07:00'`, - wedTime: `'07:00'`, - thuTime: `'07:00'`, - friTime: `'07:00'`, - satTime: 'NULL', - sunTime: 'NULL', - monMargin: 900, - tueMargin: 900, - wedMargin: 900, - thuMargin: 900, - friMargin: 900, - satMargin: 900, - sunMargin: 900, - }; - - const address0 = { + // const recurrentAd = { + // frequency: `'RECURRENT'`, + // fromDate: `'2023-01-01'`, + // toDate: `'2023-12-31'`, + // monTime: `'2023-01-01T07:15:00Z'`, + // tueTime: `'2023-01-01T07:15:00Z'`, + // wedTime: `'2023-01-01T07:05:00Z'`, + // thuTime: `'2023-01-01T07:15:00Z'`, + // friTime: `'2023-01-01T07:15:00Z'`, + // satTime: 'NULL', + // sunTime: 'NULL', + // monMargin: 900, + // tueMargin: 900, + // wedMargin: 900, + // thuMargin: 900, + // friMargin: 900, + // satMargin: 900, + // sunMargin: 900, + // }; + const originWaypoint = { position: 0, lon: 43.7102, lat: 7.262, @@ -100,7 +106,7 @@ describe('Ad Repository', () => { postalCode: "'06000'", country: "'France'", }; - const address1 = { + const destinationWaypoint = { position: 1, lon: 43.2965, lat: 5.3698, @@ -108,6 +114,7 @@ describe('Ad Repository', () => { postalCode: "'13000'", country: "'France'", }; + const createPunctualDriverAds = async (nbToCreate = 10) => { const adToCreate = { ...baseUuid, @@ -118,139 +125,159 @@ describe('Ad Repository', () => { for (let i = 0; i < nbToCreate; i++) { adToCreate.uuid = getSeed(i, baseUuid.uuid); await executeInsertCommand('ad', adToCreate); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress0Uuid.uuid), + await executeInsertCommand('waypoint', { + uuid: getSeed(i, baseOriginWaypointUuid.uuid), adUuid: adToCreate.uuid, - ...address0, + ...originWaypoint, }); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress1Uuid.uuid), + await executeInsertCommand('waypoint', { + uuid: getSeed(i, baseDestinationWaypointUuid.uuid), adUuid: adToCreate.uuid, - ...address1, - }); - } - }; - const createRecurrentDriverAds = async (nbToCreate = 10) => { - const adToCreate = { - ...baseUuid, - ...baseUserUuid, - ...driverAd, - ...punctualAd, - }; - for (let i = 0; i < nbToCreate; i++) { - adToCreate.uuid = getSeed(i, baseUuid.uuid); - await executeInsertCommand('ad', adToCreate); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress0Uuid.uuid), - adUuid: adToCreate.uuid, - ...address0, - }); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress1Uuid.uuid), - adUuid: adToCreate.uuid, - ...address1, + ...destinationWaypoint, }); } }; - const createPunctualPassengerAds = async (nbToCreate = 10) => { - const adToCreate = { - ...baseUuid, - ...baseUserUuid, - ...passengerAd, - ...punctualAd, - }; - for (let i = 0; i < nbToCreate; i++) { - adToCreate.uuid = getSeed(i, baseUuid.uuid); - await executeInsertCommand('ad', adToCreate); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress0Uuid.uuid), - adUuid: adToCreate.uuid, - ...address0, - }); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress1Uuid.uuid), - adUuid: adToCreate.uuid, - ...address1, - }); - } - }; + // const createRecurrentDriverAds = async (nbToCreate = 10) => { + // const adToCreate = { + // ...baseUuid, + // ...baseUserUuid, + // ...driverAd, + // ...punctualAd, + // }; + // for (let i = 0; i < nbToCreate; i++) { + // adToCreate.uuid = getSeed(i, baseUuid.uuid); + // await executeInsertCommand('ad', adToCreate); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseOriginWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...originWaypoint, + // }); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseDestinationWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...destinationWaypoint, + // }); + // } + // }; - const createRecurrentPassengerAds = async (nbToCreate = 10) => { - const adToCreate = { - ...baseUuid, - ...baseUserUuid, - ...passengerAd, - ...recurrentAd, - }; - for (let i = 0; i < nbToCreate; i++) { - adToCreate.uuid = getSeed(i, baseUuid.uuid); - await executeInsertCommand('ad', adToCreate); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress0Uuid.uuid), - adUuid: adToCreate.uuid, - ...address0, - }); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress1Uuid.uuid), - adUuid: adToCreate.uuid, - ...address1, - }); - } - }; + // const createPunctualPassengerAds = async (nbToCreate = 10) => { + // const adToCreate = { + // ...baseUuid, + // ...baseUserUuid, + // ...passengerAd, + // ...punctualAd, + // }; + // for (let i = 0; i < nbToCreate; i++) { + // adToCreate.uuid = getSeed(i, baseUuid.uuid); + // await executeInsertCommand('ad', adToCreate); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseOriginWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...originWaypoint, + // }); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseDestinationWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...destinationWaypoint, + // }); + // } + // }; - const createPunctualDriverPassengerAds = async (nbToCreate = 10) => { - const adToCreate = { - ...baseUuid, - ...baseUserUuid, - ...driverAndPassengerAd, - ...punctualAd, - }; - for (let i = 0; i < nbToCreate; i++) { - adToCreate.uuid = getSeed(i, baseUuid.uuid); - await executeInsertCommand('ad', adToCreate); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress0Uuid.uuid), - adUuid: adToCreate.uuid, - ...address0, - }); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress1Uuid.uuid), - adUuid: adToCreate.uuid, - ...address1, - }); - } - }; + // const createRecurrentPassengerAds = async (nbToCreate = 10) => { + // const adToCreate = { + // ...baseUuid, + // ...baseUserUuid, + // ...passengerAd, + // ...recurrentAd, + // }; + // for (let i = 0; i < nbToCreate; i++) { + // adToCreate.uuid = getSeed(i, baseUuid.uuid); + // await executeInsertCommand('ad', adToCreate); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseOriginWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...originWaypoint, + // }); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseDestinationWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...destinationWaypoint, + // }); + // } + // }; + + // const createPunctualDriverPassengerAds = async (nbToCreate = 10) => { + // const adToCreate = { + // ...baseUuid, + // ...baseUserUuid, + // ...driverAndPassengerAd, + // ...punctualAd, + // }; + // for (let i = 0; i < nbToCreate; i++) { + // adToCreate.uuid = getSeed(i, baseUuid.uuid); + // await executeInsertCommand('ad', adToCreate); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseOriginWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...originWaypoint, + // }); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseDestinationWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...destinationWaypoint, + // }); + // } + // }; + + // const createRecurrentDriverPassengerAds = async (nbToCreate = 10) => { + // const adToCreate = { + // ...baseUuid, + // ...baseUserUuid, + // ...driverAndPassengerAd, + // ...recurrentAd, + // }; + // for (let i = 0; i < nbToCreate; i++) { + // adToCreate.uuid = getSeed(i, baseUuid.uuid); + // await executeInsertCommand('ad', adToCreate); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseOriginWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...originWaypoint, + // }); + // await executeInsertCommand('waypoint', { + // uuid: getSeed(i, baseDestinationWaypointUuid.uuid), + // adUuid: adToCreate.uuid, + // ...destinationWaypoint, + // }); + // } + // }; - const createRecurrentDriverPassengerAds = async (nbToCreate = 10) => { - const adToCreate = { - ...baseUuid, - ...baseUserUuid, - ...driverAndPassengerAd, - ...recurrentAd, - }; - for (let i = 0; i < nbToCreate; i++) { - adToCreate.uuid = getSeed(i, baseUuid.uuid); - await executeInsertCommand('ad', adToCreate); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress0Uuid.uuid), - adUuid: adToCreate.uuid, - ...address0, - }); - await executeInsertCommand('address', { - uuid: getSeed(i, baseAdress1Uuid.uuid), - adUuid: adToCreate.uuid, - ...address1, - }); - } - }; beforeAll(async () => { const module = await Test.createTestingModule({ - imports: [DatabaseModule], - providers: [PrismaService, AdsRepository], + imports: [ + EventEmitterModule.forRoot(), + ConfigModule.forRoot({ isGlobal: true }), + ], + providers: [ + PrismaService, + AdMapper, + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, + { + provide: PARAMS_PROVIDER, + useClass: DefaultParamsProvider, + }, + { + provide: TIMEZONE_FINDER, + useClass: TimezoneFinder, + }, + ], }).compile(); prismaService = module.get(PrismaService); - adsRepository = module.get(AdsRepository); + adRepository = module.get(AD_REPOSITORY); }); afterAll(async () => { await prismaService.$disconnect(); @@ -259,210 +286,212 @@ describe('Ad Repository', () => { beforeEach(async () => { await prismaService.ad.deleteMany(); }); - describe('findAll', () => { - it('should return an empty data array', async () => { - const res = await adsRepository.findAll(); - expect(res).toEqual({ - data: [], - total: 0, - }); - }); + // describe('findAll', () => { + // it('should return an empty data array', async () => { + // const res = await adRepository.findAll(); + // expect(res).toEqual({ + // data: [], + // total: 0, + // }); + // }); - describe('drivers', () => { - it('should return a data array with 8 punctual driver ads', async () => { - await createPunctualDriverAds(8); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(8); - expect(ads.total).toBe(8); - expect(ads.data[0].driver).toBeTruthy(); - expect(ads.data[0].passenger).toBeFalsy(); - }); + // describe('drivers', () => { + // it('should return a data array with 8 punctual driver ads', async () => { + // await createPunctualDriverAds(8); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(8); + // expect(ads.total).toBe(8); + // expect(ads.data[0].driver).toBeTruthy(); + // expect(ads.data[0].passenger).toBeFalsy(); + // }); - it('should return a data array limited to 10 punctual driver ads', async () => { - await createPunctualDriverAds(20); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(10); - expect(ads.total).toBe(20); - expect(ads.data[1].driver).toBeTruthy(); - expect(ads.data[1].passenger).toBeFalsy(); - }); + // it('should return a data array limited to 10 punctual driver ads', async () => { + // await createPunctualDriverAds(20); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(10); + // expect(ads.total).toBe(20); + // expect(ads.data[1].driver).toBeTruthy(); + // expect(ads.data[1].passenger).toBeFalsy(); + // }); - it('should return a data array with 8 recurrent driver ads', async () => { - await createRecurrentDriverAds(8); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(8); - expect(ads.total).toBe(8); - expect(ads.data[2].driver).toBeTruthy(); - expect(ads.data[2].passenger).toBeFalsy(); - }); + // it('should return a data array with 8 recurrent driver ads', async () => { + // await createRecurrentDriverAds(8); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(8); + // expect(ads.total).toBe(8); + // expect(ads.data[2].driver).toBeTruthy(); + // expect(ads.data[2].passenger).toBeFalsy(); + // }); - it('should return a data array limited to 10 recurrent driver ads', async () => { - await createRecurrentDriverAds(20); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(10); - expect(ads.total).toBe(20); - expect(ads.data[3].driver).toBeTruthy(); - expect(ads.data[3].passenger).toBeFalsy(); - }); - }); + // it('should return a data array limited to 10 recurrent driver ads', async () => { + // await createRecurrentDriverAds(20); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(10); + // expect(ads.total).toBe(20); + // expect(ads.data[3].driver).toBeTruthy(); + // expect(ads.data[3].passenger).toBeFalsy(); + // }); + // }); - describe('passengers', () => { - it('should return a data array with 7 punctual passenger ads', async () => { - await createPunctualPassengerAds(7); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(7); - expect(ads.total).toBe(7); - expect(ads.data[0].passenger).toBeTruthy(); - expect(ads.data[0].driver).toBeFalsy(); - }); + // describe('passengers', () => { + // it('should return a data array with 7 punctual passenger ads', async () => { + // await createPunctualPassengerAds(7); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(7); + // expect(ads.total).toBe(7); + // expect(ads.data[0].passenger).toBeTruthy(); + // expect(ads.data[0].driver).toBeFalsy(); + // }); - it('should return a data array limited to 10 punctual passenger ads', async () => { - await createPunctualPassengerAds(15); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(10); - expect(ads.total).toBe(15); - expect(ads.data[1].passenger).toBeTruthy(); - expect(ads.data[1].driver).toBeFalsy(); - }); + // it('should return a data array limited to 10 punctual passenger ads', async () => { + // await createPunctualPassengerAds(15); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(10); + // expect(ads.total).toBe(15); + // expect(ads.data[1].passenger).toBeTruthy(); + // expect(ads.data[1].driver).toBeFalsy(); + // }); - it('should return a data array with 7 recurrent passenger ads', async () => { - await createRecurrentPassengerAds(7); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(7); - expect(ads.total).toBe(7); - expect(ads.data[2].passenger).toBeTruthy(); - expect(ads.data[2].driver).toBeFalsy(); - }); + // it('should return a data array with 7 recurrent passenger ads', async () => { + // await createRecurrentPassengerAds(7); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(7); + // expect(ads.total).toBe(7); + // expect(ads.data[2].passenger).toBeTruthy(); + // expect(ads.data[2].driver).toBeFalsy(); + // }); - it('should return a data array limited to 10 recurrent passenger ads', async () => { - await createRecurrentPassengerAds(15); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(10); - expect(ads.total).toBe(15); - expect(ads.data[3].passenger).toBeTruthy(); - expect(ads.data[3].driver).toBeFalsy(); - }); - }); + // it('should return a data array limited to 10 recurrent passenger ads', async () => { + // await createRecurrentPassengerAds(15); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(10); + // expect(ads.total).toBe(15); + // expect(ads.data[3].passenger).toBeTruthy(); + // expect(ads.data[3].driver).toBeFalsy(); + // }); + // }); - describe('drivers and passengers', () => { - it('should return a data array with 6 punctual driver and passenger ads', async () => { - await createPunctualDriverPassengerAds(6); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(6); - expect(ads.total).toBe(6); - expect(ads.data[0].passenger).toBeTruthy(); - expect(ads.data[0].driver).toBeTruthy(); - }); + // describe('drivers and passengers', () => { + // it('should return a data array with 6 punctual driver and passenger ads', async () => { + // await createPunctualDriverPassengerAds(6); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(6); + // expect(ads.total).toBe(6); + // expect(ads.data[0].passenger).toBeTruthy(); + // expect(ads.data[0].driver).toBeTruthy(); + // }); - it('should return a data array limited to 10 punctual driver and passenger ads', async () => { - await createPunctualDriverPassengerAds(16); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(10); - expect(ads.total).toBe(16); - expect(ads.data[1].passenger).toBeTruthy(); - expect(ads.data[1].driver).toBeTruthy(); - }); + // it('should return a data array limited to 10 punctual driver and passenger ads', async () => { + // await createPunctualDriverPassengerAds(16); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(10); + // expect(ads.total).toBe(16); + // expect(ads.data[1].passenger).toBeTruthy(); + // expect(ads.data[1].driver).toBeTruthy(); + // }); - it('should return a data array with 6 recurrent driver and passenger ads', async () => { - await createRecurrentDriverPassengerAds(6); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(6); - expect(ads.total).toBe(6); - expect(ads.data[2].passenger).toBeTruthy(); - expect(ads.data[2].driver).toBeTruthy(); - }); + // it('should return a data array with 6 recurrent driver and passenger ads', async () => { + // await createRecurrentDriverPassengerAds(6); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(6); + // expect(ads.total).toBe(6); + // expect(ads.data[2].passenger).toBeTruthy(); + // expect(ads.data[2].driver).toBeTruthy(); + // }); - it('should return a data array limited to 10 recurrent driver and passenger ads', async () => { - await createRecurrentDriverPassengerAds(16); - const ads = await adsRepository.findAll(); - expect(ads.data.length).toBe(10); - expect(ads.total).toBe(16); - expect(ads.data[3].passenger).toBeTruthy(); - expect(ads.data[3].driver).toBeTruthy(); - }); - }); - }); - describe('findOneByUuid', () => { + // it('should return a data array limited to 10 recurrent driver and passenger ads', async () => { + // await createRecurrentDriverPassengerAds(16); + // const ads = await adRepository.findAll(); + // expect(ads.data.length).toBe(10); + // expect(ads.total).toBe(16); + // expect(ads.data[3].passenger).toBeTruthy(); + // expect(ads.data[3].driver).toBeTruthy(); + // }); + // }); + // }); + describe('findOneById', () => { it('should return an ad', async () => { await createPunctualDriverAds(1); - const ad = await adsRepository.findOneByUuid(baseUuid.uuid); + const result = await adRepository.findOneById(baseUuid.uuid, { + waypoints: true, + }); - expect(ad.uuid).toBe(baseUuid.uuid); + expect(result.unwrap().id).toBe(baseUuid.uuid); }); - it('should return null', async () => { - const ad = await adsRepository.findOneByUuid( - '544572be-11fb-4244-8235-587221fc9104', - ); - expect(ad).toBeNull(); - }); + // it('should return null', async () => { + // const ad = await adRepository.findOneById( + // '544572be-11fb-4244-8235-587221fc9104', + // ); + // expect(ad).toBeNull(); + // }); }); - describe('create', () => { - it('should create an punctual ad', async () => { - const beforeCount = await prismaService.ad.count(); - const adToCreate: AdDTO = new AdDTO(); + // describe('create', () => { + // it('should create a punctual ad', async () => { + // const beforeCount = await prismaService.ad.count(); + // const adToCreate: AdDTO = new AdDTO(); - adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00'; - adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; - adToCreate.driver = true; - adToCreate.passenger = false; - adToCreate.frequency = Frequency.PUNCTUAL; - adToCreate.fromDate = new Date('05-22-2023 09:36'); - adToCreate.toDate = new Date('05-22-2023 09:36'); - adToCreate.monTime = '09:36'; - adToCreate.monMargin = 900; - adToCreate.tueMargin = 900; - adToCreate.wedMargin = 900; - adToCreate.thuMargin = 900; - adToCreate.friMargin = 900; - adToCreate.satMargin = 900; - adToCreate.sunMargin = 900; - adToCreate.seatsProposed = 3; - adToCreate.seatsRequested = 0; - adToCreate.strict = false; - adToCreate.waypoints = { - create: [address0 as Waypoint, address1 as Waypoint], - }; - const ad = await adsRepository.create(adToCreate); + // adToCreate.uuid = 'be459a29-7a41-4c0b-b371-abe90bfb6f00'; + // adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; + // adToCreate.driver = true; + // adToCreate.passenger = false; + // adToCreate.frequency = Frequency.PUNCTUAL; + // adToCreate.fromDate = new Date('05-22-2023 09:36'); + // adToCreate.toDate = new Date('05-22-2023 09:36'); + // adToCreate.monTime = '09:36'; + // adToCreate.monMargin = 900; + // adToCreate.tueMargin = 900; + // adToCreate.wedMargin = 900; + // adToCreate.thuMargin = 900; + // adToCreate.friMargin = 900; + // adToCreate.satMargin = 900; + // adToCreate.sunMargin = 900; + // adToCreate.seatsProposed = 3; + // adToCreate.seatsRequested = 0; + // adToCreate.strict = false; + // adToCreate.waypoints = { + // create: [originWaypoint as Waypoint, destinationWaypoint as Waypoint], + // }; + // const ad = await adRepository.create(adToCreate); - const afterCount = await prismaService.ad.count(); + // const afterCount = await prismaService.ad.count(); - expect(afterCount - beforeCount).toBe(1); - expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00'); - }); - it('should create an recurrent ad', async () => { - const beforeCount = await prismaService.ad.count(); - const adToCreate: AdDTO = new AdDTO(); + // expect(afterCount - beforeCount).toBe(1); + // expect(ad.uuid).toBe('be459a29-7a41-4c0b-b371-abe90bfb6f00'); + // }); + // it('should create a recurrent ad', async () => { + // const beforeCount = await prismaService.ad.count(); + // const adToCreate: AdDTO = new AdDTO(); - adToCreate.uuid = '137a26fa-4b38-48ba-aecf-1a75f6b20f3d'; - adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; - adToCreate.driver = true; - adToCreate.passenger = false; - adToCreate.frequency = Frequency.RECURRENT; - adToCreate.fromDate = new Date('01-15-2023 '); - adToCreate.toDate = new Date('10-31-2023'); - adToCreate.monTime = '07:30'; - adToCreate.friTime = '07:45'; - adToCreate.thuTime = '08:00'; - adToCreate.monMargin = 900; - adToCreate.tueMargin = 900; - adToCreate.wedMargin = 900; - adToCreate.thuMargin = 900; - adToCreate.friMargin = 900; - adToCreate.satMargin = 900; - adToCreate.sunMargin = 900; - adToCreate.seatsProposed = 2; - adToCreate.seatsRequested = 0; - adToCreate.strict = false; - adToCreate.waypoints = { - create: [address0 as Waypoint, address1 as Waypoint], - }; - const ad = await adsRepository.create(adToCreate); + // adToCreate.uuid = '137a26fa-4b38-48ba-aecf-1a75f6b20f3d'; + // adToCreate.userUuid = '4e52b54d-a729-4dbd-9283-f84a11bb2200'; + // adToCreate.driver = true; + // adToCreate.passenger = false; + // adToCreate.frequency = Frequency.RECURRENT; + // adToCreate.fromDate = new Date('01-15-2023 '); + // adToCreate.toDate = new Date('10-31-2023'); + // adToCreate.monTime = '07:30'; + // adToCreate.friTime = '07:45'; + // adToCreate.thuTime = '08:00'; + // adToCreate.monMargin = 900; + // adToCreate.tueMargin = 900; + // adToCreate.wedMargin = 900; + // adToCreate.thuMargin = 900; + // adToCreate.friMargin = 900; + // adToCreate.satMargin = 900; + // adToCreate.sunMargin = 900; + // adToCreate.seatsProposed = 2; + // adToCreate.seatsRequested = 0; + // adToCreate.strict = false; + // adToCreate.waypoints = { + // create: [originWaypoint as Waypoint, destinationWaypoint as Waypoint], + // }; + // const ad = await adRepository.create(adToCreate); - const afterCount = await prismaService.ad.count(); + // const afterCount = await prismaService.ad.count(); - expect(afterCount - beforeCount).toBe(1); - expect(ad.uuid).toBe('137a26fa-4b38-48ba-aecf-1a75f6b20f3d'); - }); - }); + // expect(afterCount - beforeCount).toBe(1); + // expect(ad.uuid).toBe('137a26fa-4b38-48ba-aecf-1a75f6b20f3d'); + // }); + // }); }); diff --git a/src/modules/ad/tests/unit/core/ad.entity.spec.ts b/src/modules/ad/tests/unit/core/ad.entity.spec.ts new file mode 100644 index 0000000..55d2be6 --- /dev/null +++ b/src/modules/ad/tests/unit/core/ad.entity.spec.ts @@ -0,0 +1,409 @@ +import { AdEntity } from '@modules/ad/core/ad.entity'; +import { + CreateAdProps, + DefaultAdProps, + Frequency, +} from '@modules/ad/core/ad.types'; +import { MarginDurationsProps } from '@modules/ad/core/value-objects/margin-durations.value-object'; +import { WaypointProps } from '@modules/ad/core/value-objects/waypoint.value-object'; + +const originWaypointProps: WaypointProps = { + position: 0, + address: { + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.68944505415954, + lat: 6.176510296462267, + }, + }, +}; +const destinationWaypointProps: WaypointProps = { + position: 1, + address: { + locality: 'Paris', + postalCode: '75000', + country: 'France', + coordinates: { + lon: 48.8566, + lat: 2.3522, + }, + }, +}; +const marginDurationsProps: MarginDurationsProps = { + mon: 600, + tue: 600, + wed: 600, + thu: 600, + fri: 600, + sat: 600, + sun: 600, +}; +const baseCreateAdProps = { + userId: 'e8fe64b1-4c33-49e1-9f69-4db48b21df36', + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [originWaypointProps, destinationWaypointProps], +}; +const punctualCreateAdProps = { + fromDate: '2023-06-21', + toDate: '2023-06-21', + schedule: { + wed: '08:30', + }, + frequency: Frequency.PUNCTUAL, +}; +const recurrentCreateAdProps = { + fromDate: '2023-06-21', + toDate: '2024-06-20', + schedule: { + mon: '08:30', + tue: '08:30', + wed: '08:00', + thu: '08:30', + fri: '08:30', + }, + frequency: Frequency.RECURRENT, +}; +const punctualPassengerCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: true, +}; +const recurrentPassengerCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...recurrentCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: true, +}; +const punctualDriverCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: true, + passenger: false, +}; +const recurrentDriverCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...recurrentCreateAdProps, + marginDurations: marginDurationsProps, + driver: true, + passenger: false, +}; +const punctualDriverPassengerCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: true, + passenger: true, +}; +const recurrentDriverPassengerCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...recurrentCreateAdProps, + marginDurations: marginDurationsProps, + driver: true, + passenger: true, +}; +const defaultAdProps: DefaultAdProps = { + driver: false, + passenger: true, + marginDurations: { + mon: 900, + tue: 900, + wed: 900, + thu: 900, + fri: 900, + sat: 900, + sun: 900, + }, + seatsProposed: 3, + seatsRequested: 1, + strict: false, +}; + +describe('Ad entity create', () => { + describe('With complete props', () => { + it('should create a new punctual passenger ad entity', async () => { + const punctualPassengerAd: AdEntity = AdEntity.create( + punctualPassengerCreateAdProps, + defaultAdProps, + ); + expect(punctualPassengerAd.id.length).toBe(36); + expect(punctualPassengerAd.getProps().schedule.mon).toBeUndefined(); + expect(punctualPassengerAd.getProps().schedule.wed).toBe('08:30'); + expect(punctualPassengerAd.getProps().driver).toBeFalsy(); + expect(punctualPassengerAd.getProps().passenger).toBeTruthy(); + }); + it('should create a new punctual driver ad entity', async () => { + const punctualDriverAd: AdEntity = AdEntity.create( + punctualDriverCreateAdProps, + defaultAdProps, + ); + expect(punctualDriverAd.id.length).toBe(36); + expect(punctualDriverAd.getProps().schedule.mon).toBeUndefined(); + expect(punctualDriverAd.getProps().schedule.wed).toBe('08:30'); + expect(punctualDriverAd.getProps().driver).toBeTruthy(); + expect(punctualDriverAd.getProps().passenger).toBeFalsy(); + }); + it('should create a new punctual driver and passenger ad entity', async () => { + const punctualDriverPassengerAd: AdEntity = AdEntity.create( + punctualDriverPassengerCreateAdProps, + defaultAdProps, + ); + expect(punctualDriverPassengerAd.id.length).toBe(36); + expect(punctualDriverPassengerAd.getProps().schedule.mon).toBeUndefined(); + expect(punctualDriverPassengerAd.getProps().schedule.wed).toBe('08:30'); + expect(punctualDriverPassengerAd.getProps().driver).toBeTruthy(); + expect(punctualDriverPassengerAd.getProps().passenger).toBeTruthy(); + }); + it('should create a new recurrent passenger ad entity', async () => { + const recurrentPassengerAd: AdEntity = AdEntity.create( + recurrentPassengerCreateAdProps, + defaultAdProps, + ); + expect(recurrentPassengerAd.id.length).toBe(36); + expect(recurrentPassengerAd.getProps().schedule.mon).toBe('08:30'); + expect(recurrentPassengerAd.getProps().schedule.sat).toBeUndefined(); + expect(recurrentPassengerAd.getProps().driver).toBeFalsy(); + expect(recurrentPassengerAd.getProps().passenger).toBeTruthy(); + }); + it('should create a new recurrent driver ad entity', async () => { + const recurrentDriverAd: AdEntity = AdEntity.create( + recurrentDriverCreateAdProps, + defaultAdProps, + ); + expect(recurrentDriverAd.id.length).toBe(36); + expect(recurrentDriverAd.getProps().schedule.mon).toBe('08:30'); + expect(recurrentDriverAd.getProps().schedule.sat).toBeUndefined(); + expect(recurrentDriverAd.getProps().driver).toBeTruthy(); + expect(recurrentDriverAd.getProps().passenger).toBeFalsy(); + }); + it('should create a new recurrent driver and passenger ad entity', async () => { + const recurrentDriverPassengerAd: AdEntity = AdEntity.create( + recurrentDriverPassengerCreateAdProps, + defaultAdProps, + ); + expect(recurrentDriverPassengerAd.id.length).toBe(36); + expect(recurrentDriverPassengerAd.getProps().schedule.mon).toBe('08:30'); + expect( + recurrentDriverPassengerAd.getProps().schedule.sat, + ).toBeUndefined(); + expect(recurrentDriverPassengerAd.getProps().driver).toBeTruthy(); + expect(recurrentDriverPassengerAd.getProps().passenger).toBeTruthy(); + }); + }); + + describe('With incomplete props', () => { + it('should create a new punctual passenger ad entity if no role is given', async () => { + const punctualWithoutRoleCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: false, + }; + const punctualWithoutRoleAd: AdEntity = AdEntity.create( + punctualWithoutRoleCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutRoleAd.id.length).toBe(36); + expect(punctualWithoutRoleAd.getProps().schedule.mon).toBeUndefined(); + expect(punctualWithoutRoleAd.getProps().schedule.wed).toBe('08:30'); + expect(punctualWithoutRoleAd.getProps().driver).toBeFalsy(); + expect(punctualWithoutRoleAd.getProps().passenger).toBeTruthy(); + }); + it('should create a new strict punctual passenger ad entity if no strict param is given', async () => { + const punctualWithoutStrictCreateAdProps: CreateAdProps = { + userId: 'e8fe64b1-4c33-49e1-9f69-4db48b21df36', + seatsProposed: 3, + seatsRequested: 1, + strict: undefined, + waypoints: [originWaypointProps, destinationWaypointProps], + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: true, + }; + const punctualWithoutStrictAd: AdEntity = AdEntity.create( + punctualWithoutStrictCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutStrictAd.id.length).toBe(36); + expect(punctualWithoutStrictAd.getProps().schedule.mon).toBeUndefined(); + expect(punctualWithoutStrictAd.getProps().schedule.wed).toBe('08:30'); + expect(punctualWithoutStrictAd.getProps().driver).toBeFalsy(); + expect(punctualWithoutStrictAd.getProps().passenger).toBeTruthy(); + expect(punctualWithoutStrictAd.getProps().strict).toBeFalsy(); + }); + it('should create a new punctual passenger ad entity with seats requested if no seats requested param is given', async () => { + const punctualWithoutSeatsRequestedCreateAdProps: CreateAdProps = { + userId: 'e8fe64b1-4c33-49e1-9f69-4db48b21df36', + seatsProposed: 3, + seatsRequested: undefined, + strict: false, + waypoints: [originWaypointProps, destinationWaypointProps], + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: true, + }; + const punctualWithoutSeatsRequestedAd: AdEntity = AdEntity.create( + punctualWithoutSeatsRequestedCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutSeatsRequestedAd.id.length).toBe(36); + expect( + punctualWithoutSeatsRequestedAd.getProps().schedule.mon, + ).toBeUndefined(); + expect(punctualWithoutSeatsRequestedAd.getProps().schedule.wed).toBe( + '08:30', + ); + expect(punctualWithoutSeatsRequestedAd.getProps().driver).toBeFalsy(); + expect(punctualWithoutSeatsRequestedAd.getProps().passenger).toBeTruthy(); + expect(punctualWithoutSeatsRequestedAd.getProps().seatsRequested).toBe(1); + }); + it('should create a new punctual driver ad entity with seats proposed if no seats proposed param is given', async () => { + const punctualWithoutSeatsProposedCreateAdProps: CreateAdProps = { + userId: 'e8fe64b1-4c33-49e1-9f69-4db48b21df36', + seatsProposed: undefined, + seatsRequested: 1, + strict: false, + waypoints: [originWaypointProps, destinationWaypointProps], + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: true, + passenger: false, + }; + const punctualWithoutSeatsProposedAd: AdEntity = AdEntity.create( + punctualWithoutSeatsProposedCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutSeatsProposedAd.id.length).toBe(36); + expect( + punctualWithoutSeatsProposedAd.getProps().schedule.mon, + ).toBeUndefined(); + expect(punctualWithoutSeatsProposedAd.getProps().schedule.wed).toBe( + '08:30', + ); + expect(punctualWithoutSeatsProposedAd.getProps().driver).toBeTruthy(); + expect(punctualWithoutSeatsProposedAd.getProps().passenger).toBeFalsy(); + expect(punctualWithoutSeatsProposedAd.getProps().seatsProposed).toBe(3); + }); + it('should create a new punctual driver ad entity with margin durations if margin durations are empty', async () => { + const punctualWithoutMarginDurationsCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + waypoints: [originWaypointProps, destinationWaypointProps], + ...punctualCreateAdProps, + marginDurations: {}, + driver: true, + passenger: false, + }; + const punctualWithoutMarginDurationsAd: AdEntity = AdEntity.create( + punctualWithoutMarginDurationsCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutMarginDurationsAd.id.length).toBe(36); + expect( + punctualWithoutMarginDurationsAd.getProps().schedule.mon, + ).toBeUndefined(); + expect(punctualWithoutMarginDurationsAd.getProps().schedule.wed).toBe( + '08:30', + ); + expect(punctualWithoutMarginDurationsAd.getProps().driver).toBeTruthy(); + expect(punctualWithoutMarginDurationsAd.getProps().passenger).toBeFalsy(); + expect( + punctualWithoutMarginDurationsAd.getProps().marginDurations.mon, + ).toBe(900); + }); + it('should create a new punctual driver ad entity with margin durations if margin durations are undefined', async () => { + const punctualWithoutMarginDurationsCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + waypoints: [originWaypointProps, destinationWaypointProps], + ...punctualCreateAdProps, + marginDurations: undefined, + driver: true, + passenger: false, + }; + const punctualWithoutMarginDurationsAd: AdEntity = AdEntity.create( + punctualWithoutMarginDurationsCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutMarginDurationsAd.id.length).toBe(36); + expect( + punctualWithoutMarginDurationsAd.getProps().schedule.mon, + ).toBeUndefined(); + expect(punctualWithoutMarginDurationsAd.getProps().schedule.wed).toBe( + '08:30', + ); + expect(punctualWithoutMarginDurationsAd.getProps().driver).toBeTruthy(); + expect(punctualWithoutMarginDurationsAd.getProps().passenger).toBeFalsy(); + expect( + punctualWithoutMarginDurationsAd.getProps().marginDurations.mon, + ).toBe(900); + }); + it('should create a new punctual passenger ad entity with valid positions if positions are missing', async () => { + const punctualWithoutPositionsCreateAdProps: CreateAdProps = { + userId: 'e8fe64b1-4c33-49e1-9f69-4db48b21df36', + seatsProposed: 3, + seatsRequested: 1, + strict: undefined, + waypoints: [ + { + position: undefined, + address: { + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.68944505415954, + lat: 6.176510296462267, + }, + }, + }, + { + position: undefined, + address: { + locality: 'Paris', + postalCode: '75000', + country: 'France', + coordinates: { + lon: 48.8566, + lat: 2.3522, + }, + }, + }, + ], + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: false, + }; + const punctualWithoutPositionsAd: AdEntity = AdEntity.create( + punctualWithoutPositionsCreateAdProps, + defaultAdProps, + ); + expect(punctualWithoutPositionsAd.id.length).toBe(36); + expect( + punctualWithoutPositionsAd.getProps().schedule.mon, + ).toBeUndefined(); + expect(punctualWithoutPositionsAd.getProps().schedule.wed).toBe('08:30'); + expect(punctualWithoutPositionsAd.getProps().driver).toBeFalsy(); + expect(punctualWithoutPositionsAd.getProps().passenger).toBeTruthy(); + expect(punctualWithoutPositionsAd.getProps().waypoints[0].position).toBe( + 0, + ); + expect(punctualWithoutPositionsAd.getProps().waypoints[1].position).toBe( + 1, + ); + }); + }); +}); diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index d46d236..149fff5 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -10,6 +10,7 @@ import { Result } from 'oxide.ts'; import { AggregateID } from '@libs/ddd'; import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; import { AdEntity } from '@modules/ad/core/ad.entity'; +import { ConflictException } from '@libs/exceptions'; const originWaypoint: WaypointDTO = { position: 0, @@ -44,7 +45,15 @@ const punctualCreateAdRequest: CreateAdRequestDTO = { }; const mockAdRepository = { - insert: jest.fn(), + insert: jest + .fn() + .mockImplementationOnce(() => ({})) + .mockImplementationOnce(() => { + throw new Error(); + }) + .mockImplementationOnce(() => { + throw new ConflictException('already exists'); + }), }; const mockDefaultParamsProvider: DefaultParamsProviderPort = { @@ -102,5 +111,21 @@ describe('create-ad.service', () => { await createAdService.execute(createAdCommand); expect(result.unwrap()).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da'); }); + it('should throw an error if something bad happens', async () => { + AdEntity.create = jest.fn().mockReturnValue({ + id: '047a6ecf-23d4-4d3e-877c-3225d560a8da', + }); + await expect( + createAdService.execute(createAdCommand), + ).rejects.toBeInstanceOf(Error); + }); + it('should return an Err if Ad already exists', async () => { + AdEntity.create = jest.fn().mockReturnValue({ + id: '047a6ecf-23d4-4d3e-877c-3225d560a8da', + }); + const result: Result = + await createAdService.execute(createAdCommand); + expect(result.isErr()).toBeTruthy(); + }); }); }); From 22565eb253c6d7ae215802767c83514d703a0271 Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 21 Jun 2023 17:00:02 +0200 Subject: [PATCH 05/29] improve code coverage --- src/modules/ad/ad.mapper.ts | 1 - src/modules/ad/tests/unit/ad.mapper.spec.ts | 186 ++++++++++++++++++++ 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/modules/ad/tests/unit/ad.mapper.spec.ts diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index 2b98e79..5cc8ad0 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -28,7 +28,6 @@ import { DateTime, TimeZone } from 'timezonecomplete'; export class AdMapper implements Mapper { - private timezone: string; private readonly defaultParams: DefaultParams; constructor( diff --git a/src/modules/ad/tests/unit/ad.mapper.spec.ts b/src/modules/ad/tests/unit/ad.mapper.spec.ts new file mode 100644 index 0000000..f7e76ed --- /dev/null +++ b/src/modules/ad/tests/unit/ad.mapper.spec.ts @@ -0,0 +1,186 @@ +import { PARAMS_PROVIDER, TIMEZONE_FINDER } from '@modules/ad/ad.di-tokens'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { AdEntity } from '@modules/ad/core/ad.entity'; +import { Frequency } from '@modules/ad/core/ad.types'; +import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; +import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port'; +import { + AdReadModel, + AdWriteModel, +} from '@modules/ad/infrastructure/ad.repository'; +import { Test } from '@nestjs/testing'; + +const now = new Date('2023-06-21 06:00:00'); +const adEntity: AdEntity = new AdEntity({ + id: 'c160cf8c-f057-4962-841f-3ad68346df44', + props: { + userId: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + driver: false, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: '2023-06-21', + toDate: '2023-06-21', + schedule: { + wed: '07:15', + }, + waypoints: [ + { + position: 0, + address: { + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.68944505415954, + lat: 6.176510296462267, + }, + }, + }, + { + position: 1, + address: { + locality: 'Paris', + postalCode: '75000', + country: 'France', + coordinates: { + lon: 48.8566, + lat: 2.3522, + }, + }, + }, + ], + marginDurations: { + mon: 600, + tue: 600, + wed: 600, + thu: 600, + fri: 600, + sat: 600, + sun: 600, + }, + strict: false, + seatsProposed: 3, + seatsRequested: 1, + }, + createdAt: now, + updatedAt: now, +}); +const adReadModel: AdReadModel = { + uuid: 'c160cf8c-f057-4962-841f-3ad68346df44', + userUuid: '7ca2490b-d04d-4ac5-8d6c-5c416fab922e', + driver: false, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: new Date('2023-06-21'), + toDate: new Date('2023-06-21'), + monTime: undefined, + tueTime: undefined, + wedTime: new Date('2023-06-21T07:15:00Z'), + thuTime: undefined, + friTime: undefined, + satTime: undefined, + sunTime: undefined, + waypoints: [ + { + uuid: '6f53f55e-2bdb-4c23-b6a9-6d7b498e47b9', + position: 0, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + lon: 48.68944505415954, + lat: 6.176510296462267, + createdAt: now, + updatedAt: now, + }, + { + uuid: 'e18c6a84-0ab7-4e44-af1d-829d0b0d0573', + position: 1, + locality: 'Paris', + postalCode: '75000', + country: 'France', + lon: 48.8566, + lat: 2.3522, + createdAt: now, + updatedAt: now, + }, + ], + monMargin: 600, + tueMargin: 600, + wedMargin: 600, + thuMargin: 600, + friMargin: 600, + satMargin: 600, + sunMargin: 600, + strict: false, + seatsProposed: 3, + seatsRequested: 1, + createdAt: now, + updatedAt: now, +}; + +const mockDefaultParamsProvider: DefaultParamsProviderPort = { + getParams: () => { + return { + MON_MARGIN: 900, + TUE_MARGIN: 900, + WED_MARGIN: 900, + THU_MARGIN: 900, + FRI_MARGIN: 900, + SAT_MARGIN: 900, + SUN_MARGIN: 900, + DRIVER: false, + SEATS_PROPOSED: 3, + PASSENGER: true, + SEATS_REQUESTED: 1, + STRICT: false, + DEFAULT_TIMEZONE: 'Europe/Paris', + }; + }, +}; + +const mockTimezoneFinder: TimezoneFinderPort = { + timezones: jest.fn(), +}; + +describe('Ad Mapper', () => { + let adMapper: AdMapper; + + beforeAll(async () => { + const module = await Test.createTestingModule({ + providers: [ + AdMapper, + { + provide: PARAMS_PROVIDER, + useValue: mockDefaultParamsProvider, + }, + { + provide: TIMEZONE_FINDER, + useValue: mockTimezoneFinder, + }, + ], + }).compile(); + adMapper = module.get(AdMapper); + }); + + it('should be defined', () => { + expect(adMapper).toBeDefined(); + }); + + it('should map domain entity to persistence data', async () => { + const mapped: AdWriteModel = adMapper.toPersistence(adEntity); + expect(mapped.waypoints.create[0].uuid.length).toBe(36); + expect(mapped.waypoints.create[1].uuid.length).toBe(36); + }); + + it('should map persisted data to domain entity', async () => { + const mapped: AdEntity = adMapper.toDomain(adReadModel); + expect(mapped.getProps().waypoints[0].address.coordinates.lon).toBe( + 48.68944505415954, + ); + expect(mapped.getProps().waypoints[1].address.coordinates.lat).toBe(2.3522); + }); +}); From 4ad00b96c0348bd288afaadab1e38b9c6dc76b9e Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 22 Jun 2023 11:40:31 +0200 Subject: [PATCH 06/29] extract timezone and timeconverter to infrastructure --- src/modules/ad/ad.di-tokens.ts | 1 + src/modules/ad/ad.mapper.ts | 77 +++++++------------ src/modules/ad/ad.module.ts | 6 ++ .../ad/core/ports/time-converter.port.ts | 8 ++ .../ad/core/ports/timezone-finder.port.ts | 2 +- .../ad/infrastructure/time-converter.ts | 24 ++++++ .../ad/infrastructure/timezone-finder.ts | 10 ++- src/modules/ad/tests/unit/ad.mapper.spec.ts | 58 +++++++++++--- .../infrastructure/time-converter.spec.ts | 52 +++++++++++++ 9 files changed, 175 insertions(+), 63 deletions(-) create mode 100644 src/modules/ad/core/ports/time-converter.port.ts create mode 100644 src/modules/ad/infrastructure/time-converter.ts create mode 100644 src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts diff --git a/src/modules/ad/ad.di-tokens.ts b/src/modules/ad/ad.di-tokens.ts index b2942e0..5d06236 100644 --- a/src/modules/ad/ad.di-tokens.ts +++ b/src/modules/ad/ad.di-tokens.ts @@ -1,3 +1,4 @@ export const PARAMS_PROVIDER = Symbol('PARAMS_PROVIDER'); export const TIMEZONE_FINDER = Symbol('TIMEZONE_FINDER'); +export const TIME_CONVERTER = Symbol('TIME_CONVERTER'); export const AD_REPOSITORY = Symbol('AD_REPOSITORY'); diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index 5cc8ad0..571cdc9 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -10,12 +10,15 @@ import { import { Frequency } from './core/ad.types'; import { WaypointProps } from './core/value-objects/waypoint.value-object'; import { v4 } from 'uuid'; -import { PARAMS_PROVIDER, TIMEZONE_FINDER } from './ad.di-tokens'; +import { + PARAMS_PROVIDER, + TIMEZONE_FINDER, + TIME_CONVERTER, +} from './ad.di-tokens'; import { TimezoneFinderPort } from './core/ports/timezone-finder.port'; -import { Coordinates } from './core/types/coordinates'; import { DefaultParamsProviderPort } from './core/ports/default-params-provider.port'; import { DefaultParams } from './core/ports/default-params.type'; -import { DateTime, TimeZone } from 'timezonecomplete'; +import { TimeConverterPort } from './core/ports/time-converter.port'; /** * Mapper constructs objects that are used in different layers: @@ -35,13 +38,20 @@ export class AdMapper private readonly defaultParamsProvider: DefaultParamsProviderPort, @Inject(TIMEZONE_FINDER) private readonly timezoneFinder: TimezoneFinderPort, + @Inject(TIME_CONVERTER) + private readonly timeConverter: TimeConverterPort, ) { this.defaultParams = defaultParamsProvider.getParams(); } toPersistence = (entity: AdEntity): AdWriteModel => { const copy = entity.getProps(); - const timezone = this.getTimezone(copy.waypoints[0].address.coordinates); + const { lon, lat } = copy.waypoints[0].address.coordinates; + const timezone = this.timezoneFinder.timezones( + lon, + lat, + this.defaultParams.DEFAULT_TIMEZONE, + )[0]; const now = new Date(); const record: AdWriteModel = { uuid: copy.id, @@ -52,50 +62,50 @@ export class AdMapper fromDate: new Date(copy.fromDate), toDate: new Date(copy.toDate), monTime: copy.schedule.mon - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.mon, timezone, ) : undefined, tueTime: copy.schedule.tue - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.tue, timezone, ) : undefined, wedTime: copy.schedule.wed - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.wed, timezone, ) : undefined, thuTime: copy.schedule.thu - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.thu, timezone, ) : undefined, friTime: copy.schedule.fri - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.fri, timezone, ) : undefined, satTime: copy.schedule.sat - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.sat, timezone, ) : undefined, sunTime: copy.schedule.sun - ? AdMapper.toUtcDatetime( - new Date(copy.fromDate), + ? this.timeConverter.dateTimeToUtc( + copy.fromDate, copy.schedule.sun, timezone, ) @@ -199,35 +209,4 @@ export class AdMapper (avoid blacklisting, which will return everything but blacklisted items, which can lead to a data leak). */ - - private getTimezone = (coordinates: Coordinates): string => { - try { - const timezones = this.timezoneFinder.timezones( - coordinates.lon, - coordinates.lat, - ); - if (timezones.length > 0) return timezones[0]; - } catch (e) {} - return this.defaultParams.DEFAULT_TIMEZONE; - }; - - private static toUtcDatetime = ( - date: Date, - time: string, - timezone: string, - ): Date => { - try { - if (!date || !time || !timezone) throw new Error(); - return new Date( - new DateTime( - `${date.toISOString().split('T')[0]}T${time}:00`, - TimeZone.zone(timezone, false), - ) - .convert(TimeZone.zone('UTC')) - .toIsoString(), - ); - } catch (e) { - return undefined; - } - }; } diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 2211535..602d973 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -5,6 +5,7 @@ import { AD_REPOSITORY, PARAMS_PROVIDER, TIMEZONE_FINDER, + TIME_CONVERTER, } from './ad.di-tokens'; import { MESSAGE_BROKER_PUBLISHER, @@ -18,6 +19,7 @@ import { AdMapper } from './ad.mapper'; import { CreateAdService } from './core/commands/create-ad/create-ad.service'; import { TimezoneFinder } from './infrastructure/timezone-finder'; import { PrismaService } from '@libs/db/prisma.service'; +import { TimeConverter } from './infrastructure/time-converter'; @Module({ imports: [CqrsModule], @@ -46,6 +48,10 @@ import { PrismaService } from '@libs/db/prisma.service'; provide: TIMEZONE_FINDER, useClass: TimezoneFinder, }, + { + provide: TIME_CONVERTER, + useClass: TimeConverter, + }, ], exports: [ PrismaService, diff --git a/src/modules/ad/core/ports/time-converter.port.ts b/src/modules/ad/core/ports/time-converter.port.ts new file mode 100644 index 0000000..feb4d2c --- /dev/null +++ b/src/modules/ad/core/ports/time-converter.port.ts @@ -0,0 +1,8 @@ +export interface TimeConverterPort { + dateTimeToUtc( + date: string, + time: string, + timezone: string, + dst?: boolean, + ): Date; +} diff --git a/src/modules/ad/core/ports/timezone-finder.port.ts b/src/modules/ad/core/ports/timezone-finder.port.ts index ddfbc8b..72ba115 100644 --- a/src/modules/ad/core/ports/timezone-finder.port.ts +++ b/src/modules/ad/core/ports/timezone-finder.port.ts @@ -1,3 +1,3 @@ export interface TimezoneFinderPort { - timezones(lon: number, lat: number): string[]; + timezones(lon: number, lat: number, defaultTimezone?: string): string[]; } diff --git a/src/modules/ad/infrastructure/time-converter.ts b/src/modules/ad/infrastructure/time-converter.ts new file mode 100644 index 0000000..ef41158 --- /dev/null +++ b/src/modules/ad/infrastructure/time-converter.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@nestjs/common'; +import { TimeConverterPort } from '../core/ports/time-converter.port'; +import { DateTime, TimeZone } from 'timezonecomplete'; + +@Injectable() +export class TimeConverter implements TimeConverterPort { + dateTimeToUtc = ( + date: string, + time: string, + timezone: string, + dst?: boolean, + ): Date => { + try { + if (!date || !time || !timezone) throw new Error(); + return new Date( + new DateTime(`${date}T${time}`, TimeZone.zone(timezone, dst)) + .convert(TimeZone.zone('UTC')) + .toIsoString(), + ); + } catch (e) { + return undefined; + } + }; +} diff --git a/src/modules/ad/infrastructure/timezone-finder.ts b/src/modules/ad/infrastructure/timezone-finder.ts index 7c1ec9a..be990b3 100644 --- a/src/modules/ad/infrastructure/timezone-finder.ts +++ b/src/modules/ad/infrastructure/timezone-finder.ts @@ -4,5 +4,13 @@ import { find } from 'geo-tz'; @Injectable() export class TimezoneFinder implements TimezoneFinderPort { - timezones = (lon: number, lat: number): string[] => find(lat, lon); + timezones = ( + lon: number, + lat: number, + defaultTimezone?: string, + ): string[] => { + const foundTimezones = find(lat, lon); + if (defaultTimezone && foundTimezones.length == 0) return [defaultTimezone]; + return foundTimezones; + }; } diff --git a/src/modules/ad/tests/unit/ad.mapper.spec.ts b/src/modules/ad/tests/unit/ad.mapper.spec.ts index f7e76ed..78e659d 100644 --- a/src/modules/ad/tests/unit/ad.mapper.spec.ts +++ b/src/modules/ad/tests/unit/ad.mapper.spec.ts @@ -1,13 +1,19 @@ -import { PARAMS_PROVIDER, TIMEZONE_FINDER } from '@modules/ad/ad.di-tokens'; +import { + PARAMS_PROVIDER, + TIMEZONE_FINDER, + TIME_CONVERTER, +} from '@modules/ad/ad.di-tokens'; import { AdMapper } from '@modules/ad/ad.mapper'; import { AdEntity } from '@modules/ad/core/ad.entity'; import { Frequency } from '@modules/ad/core/ad.types'; import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; +import { TimeConverterPort } from '@modules/ad/core/ports/time-converter.port'; import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port'; import { AdReadModel, AdWriteModel, } from '@modules/ad/infrastructure/ad.repository'; +import { AdResponseDto } from '@modules/ad/interface/dtos/ad.response.dto'; import { Test } from '@nestjs/testing'; const now = new Date('2023-06-21 06:00:00'); @@ -21,7 +27,13 @@ const adEntity: AdEntity = new AdEntity({ fromDate: '2023-06-21', toDate: '2023-06-21', schedule: { + mon: '07:15', + tue: '07:15', wed: '07:15', + thu: '07:15', + fri: '07:15', + sat: '07:15', + sun: '07:15', }, waypoints: [ { @@ -33,8 +45,8 @@ const adEntity: AdEntity = new AdEntity({ postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lat: 48.68944505415954, + lon: 6.176510296462267, }, }, }, @@ -45,8 +57,8 @@ const adEntity: AdEntity = new AdEntity({ postalCode: '75000', country: 'France', coordinates: { - lon: 48.8566, - lat: 2.3522, + lat: 48.8566, + lon: 2.3522, }, }, }, @@ -91,8 +103,8 @@ const adReadModel: AdReadModel = { locality: 'Nancy', postalCode: '54000', country: 'France', - lon: 48.68944505415954, - lat: 6.176510296462267, + lat: 48.68944505415954, + lon: 6.176510296462267, createdAt: now, updatedAt: now, }, @@ -102,8 +114,8 @@ const adReadModel: AdReadModel = { locality: 'Paris', postalCode: '75000', country: 'France', - lon: 48.8566, - lat: 2.3522, + lat: 48.8566, + lon: 2.3522, createdAt: now, updatedAt: now, }, @@ -143,7 +155,20 @@ const mockDefaultParamsProvider: DefaultParamsProviderPort = { }; const mockTimezoneFinder: TimezoneFinderPort = { - timezones: jest.fn(), + // eslint-disable-next-line @typescript-eslint/no-unused-vars + timezones: jest.fn().mockImplementation((lon: number, lat: number) => { + if (lon < 60) return 'Europe/Paris'; + return 'America/New_York'; + }), +}; + +const mockTimeConverter: TimeConverterPort = { + dateTimeToUtc: jest + .fn() + // eslint-disable-next-line @typescript-eslint/no-unused-vars + .mockImplementation((datetime: Date, timezone: string, dst?: boolean) => { + return datetime; + }), }; describe('Ad Mapper', () => { @@ -161,6 +186,10 @@ describe('Ad Mapper', () => { provide: TIMEZONE_FINDER, useValue: mockTimezoneFinder, }, + { + provide: TIME_CONVERTER, + useValue: mockTimeConverter, + }, ], }).compile(); adMapper = module.get(AdMapper); @@ -178,9 +207,14 @@ describe('Ad Mapper', () => { it('should map persisted data to domain entity', async () => { const mapped: AdEntity = adMapper.toDomain(adReadModel); - expect(mapped.getProps().waypoints[0].address.coordinates.lon).toBe( + expect(mapped.getProps().waypoints[0].address.coordinates.lat).toBe( 48.68944505415954, ); - expect(mapped.getProps().waypoints[1].address.coordinates.lat).toBe(2.3522); + expect(mapped.getProps().waypoints[1].address.coordinates.lon).toBe(2.3522); + }); + + it('should map domain entity to response', async () => { + const mapped: AdResponseDto = adMapper.toResponse(adEntity); + expect(mapped.uuid).toBe('c160cf8c-f057-4962-841f-3ad68346df44'); }); }); diff --git a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts new file mode 100644 index 0000000..4bd8a4d --- /dev/null +++ b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts @@ -0,0 +1,52 @@ +import { TimeConverter } from '@modules/ad/infrastructure/time-converter'; + +describe('Time Converter', () => { + it('should be defined', () => { + const timeConverter: TimeConverter = new TimeConverter(); + expect(timeConverter).toBeDefined(); + }); + it('should convert a paris datetime to utc', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-06-22'; + const parisTime = '08:00'; + const utcDatetime = timeConverter.dateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime.toISOString()).toEqual('2023-06-22T06:00:00.000Z'); + }); + it('should return undefined if date is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-16-22'; + const parisTime = '08:00'; + const utcDatetime = timeConverter.dateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime).toBeUndefined(); + }); + it('should return undefined if time is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-06-22'; + const parisTime = '28:00'; + const utcDatetime = timeConverter.dateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime).toBeUndefined(); + }); + it('should return undefined if timezone is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-06-22'; + const parisTime = '08:00'; + const utcDatetime = timeConverter.dateTimeToUtc( + parisDate, + parisTime, + 'Foo/Bar', + ); + expect(utcDatetime).toBeUndefined(); + }); +}); From 211bee2c70785d1464b63c7dc3a9e93e417879ec Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 23 Jun 2023 11:37:26 +0200 Subject: [PATCH 07/29] improve tests, handle coordinates precision --- .../migration.sql | 4 +- prisma/schema.prisma | 4 +- src/libs/api/paginated.response.base.ts | 4 +- src/libs/db/prisma-repository.base.ts | 13 +- src/libs/ddd/query.base.ts | 31 +++++ src/libs/ddd/repository.port.ts | 14 +-- src/modules/ad/ad.mapper.ts | 59 +++++++-- src/modules/ad/ad.module.ts | 5 +- .../ad/core/ports/time-converter.port.ts | 3 +- .../find-ad-by-id.query-handler.ts | 17 +++ .../find-ad-by-id/find-ad-by-id.query.ts | 9 +- .../ad/infrastructure/time-converter.ts | 14 ++- src/modules/ad/interface/ad.presenter.ts | 6 - .../ad/interface/dtos/ad.response.dto.ts | 40 +++++- .../create-ad.grpc.controller.ts | 5 +- .../grpc-controllers/dtos/address.dto.ts | 4 +- .../grpc-controllers/dtos/coordinates.dto.ts | 10 +- .../dtos/create-ad.request.dto.ts | 19 +-- .../dtos/find-ad-by-id.request.dto.ts | 2 +- .../dtos/margin-durations.dto.ts | 2 +- .../grpc-controllers/dtos/schedule.dto.ts | 2 +- .../valid-position-indexes.validator.ts | 4 +- .../dtos/validators/to-precision.ts | 4 + .../dtos/validators/waypoint-position.ts | 4 +- .../grpc-controllers/dtos/waypoint.dto.ts | 4 +- .../find-ad-by-id.grpc.controller.ts | 38 ++++++ .../find-ad-by-id.grpc.controller.ts | 37 ------ .../tests/integration/ad.repository.spec.ts | 8 +- src/modules/ad/tests/unit/ad.mapper.spec.ts | 15 +-- .../tests/unit/core/create-ad.service.spec.ts | 10 +- .../infrastructure/time-converter.spec.ts | 117 +++++++++++------- .../interface/valid-position-indexes.spec.ts | 8 +- .../unit/rpc-validation-pipe.usecase.spec.ts | 6 +- 33 files changed, 354 insertions(+), 168 deletions(-) rename prisma/migrations/{20230609141640_init => 20230623091500_init}/migration.sql (95%) create mode 100644 src/libs/ddd/query.base.ts create mode 100644 src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query-handler.ts delete mode 100644 src/modules/ad/interface/ad.presenter.ts rename src/modules/ad/interface/{queries/find-ad-by-id => grpc-controllers}/dtos/find-ad-by-id.request.dto.ts (74%) create mode 100644 src/modules/ad/interface/grpc-controllers/dtos/validators/to-precision.ts create mode 100644 src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts delete mode 100644 src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts diff --git a/prisma/migrations/20230609141640_init/migration.sql b/prisma/migrations/20230623091500_init/migration.sql similarity index 95% rename from prisma/migrations/20230609141640_init/migration.sql rename to prisma/migrations/20230623091500_init/migration.sql index a413ac9..f354d9a 100644 --- a/prisma/migrations/20230609141640_init/migration.sql +++ b/prisma/migrations/20230623091500_init/migration.sql @@ -38,8 +38,8 @@ CREATE TABLE "waypoint" ( "uuid" UUID NOT NULL, "adUuid" UUID NOT NULL, "position" SMALLINT NOT NULL, - "lon" DOUBLE PRECISION NOT NULL, - "lat" DOUBLE PRECISION NOT NULL, + "lon" DECIMAL(9,6) NOT NULL, + "lat" DECIMAL(8,6) NOT NULL, "name" TEXT, "houseNumber" TEXT, "street" TEXT, diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ea20ac2..91e2e9a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -47,8 +47,8 @@ model Waypoint { uuid String @id @default(uuid()) @db.Uuid adUuid String @db.Uuid position Int @db.SmallInt - lon Float - lat Float + lon Decimal @db.Decimal(9, 6) + lat Decimal @db.Decimal(8, 6) name String? houseNumber String? street String? diff --git a/src/libs/api/paginated.response.base.ts b/src/libs/api/paginated.response.base.ts index 8e8c3d3..f11d849 100644 --- a/src/libs/api/paginated.response.base.ts +++ b/src/libs/api/paginated.response.base.ts @@ -1,8 +1,8 @@ import { Paginated } from '../ddd'; export abstract class PaginatedResponseDto extends Paginated { - readonly count: number; - readonly limit: number; + readonly total: number; + readonly perPage: number; readonly page: number; abstract readonly data: readonly T[]; } diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index 031262c..9f8983d 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -2,13 +2,16 @@ import { EventEmitter2 } from '@nestjs/event-emitter'; import { AggregateRoot, Mapper, RepositoryPort } from '../ddd'; import { ObjectLiteral } from '../types'; import { LoggerPort } from '../ports/logger.port'; -import { None, Option, Some } from 'oxide.ts'; import { PrismaRawRepositoryPort, PrismaRepositoryPort, } from '../ports/prisma-repository.port'; import { Prisma } from '@prisma/client'; -import { ConflictException, DatabaseErrorException } from '@libs/exceptions'; +import { + ConflictException, + DatabaseErrorException, + NotFoundException, +} from '@libs/exceptions'; export abstract class PrismaRepositoryBase< Aggregate extends AggregateRoot, @@ -24,12 +27,13 @@ export abstract class PrismaRepositoryBase< protected readonly logger: LoggerPort, ) {} - async findOneById(id: string, include?: any): Promise> { + async findOneById(id: string, include?: any): Promise { const entity = await this.prisma.findUnique({ where: { uuid: id }, include, }); - return entity ? Some(this.mapper.toDomain(entity)) : None; + if (entity) return this.mapper.toDomain(entity); + throw new NotFoundException('Record not found'); } async insert(entity: Aggregate): Promise { @@ -52,7 +56,6 @@ export abstract class PrismaRepositoryBase< await this.prisma.$queryRaw`SELECT 1`; return true; } catch (e) { - console.log(e); if (e instanceof Prisma.PrismaClientKnownRequestError) { throw new DatabaseErrorException(e.message); } diff --git a/src/libs/ddd/query.base.ts b/src/libs/ddd/query.base.ts new file mode 100644 index 0000000..7aba63c --- /dev/null +++ b/src/libs/ddd/query.base.ts @@ -0,0 +1,31 @@ +import { OrderBy, PaginatedQueryParams } from './repository.port'; + +/** + * Base class for regular queries + */ +export abstract class QueryBase {} + +/** + * Base class for paginated queries + */ +export abstract class PaginatedQueryBase extends QueryBase { + perPage: number; + offset: number; + orderBy: OrderBy; + page: number; + + constructor(props: PaginatedParams) { + super(); + this.perPage = props.perPage || 10; + this.offset = props.page ? props.page * this.perPage : 0; + this.page = props.page || 0; + this.orderBy = props.orderBy || { field: true, param: 'desc' }; + } +} + +// Paginated query parameters +export type PaginatedParams = Omit< + T, + 'perPage' | 'offset' | 'orderBy' | 'page' +> & + Partial>; diff --git a/src/libs/ddd/repository.port.ts b/src/libs/ddd/repository.port.ts index 8d675ce..66d8450 100644 --- a/src/libs/ddd/repository.port.ts +++ b/src/libs/ddd/repository.port.ts @@ -1,5 +1,3 @@ -import { Option } from 'oxide.ts'; - /* Most of repositories will probably need generic save/find/delete operations, so it's easier to have some shared interfaces. @@ -8,14 +6,14 @@ import { Option } from 'oxide.ts'; */ export class Paginated { - readonly count: number; - readonly limit: number; + readonly total: number; + readonly perPage: number; readonly page: number; readonly data: readonly T[]; constructor(props: Paginated) { - this.count = props.count; - this.limit = props.limit; + this.total = props.total; + this.perPage = props.perPage; this.page = props.page; this.data = props.data; } @@ -24,7 +22,7 @@ export class Paginated { export type OrderBy = { field: string | true; param: 'asc' | 'desc' }; export type PaginatedQueryParams = { - limit: number; + perPage: number; page: number; offset: number; orderBy: OrderBy; @@ -32,7 +30,7 @@ export type PaginatedQueryParams = { export interface RepositoryPort { insert(entity: Entity | Entity[]): Promise; - findOneById(id: string): Promise>; + findOneById(id: string, include?: any): Promise; healthCheck(): Promise; // findAll(): Promise; // findAllPaginated(params: PaginatedQueryParams): Promise>; diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index 571cdc9..aa21320 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -62,49 +62,49 @@ export class AdMapper fromDate: new Date(copy.fromDate), toDate: new Date(copy.toDate), monTime: copy.schedule.mon - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.mon, timezone, ) : undefined, tueTime: copy.schedule.tue - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.tue, timezone, ) : undefined, wedTime: copy.schedule.wed - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.wed, timezone, ) : undefined, thuTime: copy.schedule.thu - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.thu, timezone, ) : undefined, friTime: copy.schedule.fri - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.fri, timezone, ) : undefined, satTime: copy.schedule.sat - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.sat, timezone, ) : undefined, sunTime: copy.schedule.sun - ? this.timeConverter.dateTimeToUtc( + ? this.timeConverter.localDateTimeToUtc( copy.fromDate, copy.schedule.sun, timezone, @@ -143,6 +143,11 @@ export class AdMapper }; toDomain = (record: AdReadModel): AdEntity => { + const timezone = this.timezoneFinder.timezones( + record.waypoints[0].lon, + record.waypoints[0].lat, + this.defaultParams.DEFAULT_TIMEZONE, + )[0]; const entity = new AdEntity({ id: record.uuid, createdAt: new Date(record.createdAt), @@ -152,13 +157,23 @@ export class AdMapper driver: record.driver, passenger: record.passenger, frequency: Frequency[record.frequency], - fromDate: record.fromDate.toISOString(), - toDate: record.toDate.toISOString(), + fromDate: record.fromDate.toISOString().split('T')[0], + toDate: record.toDate.toISOString().split('T')[0], schedule: { mon: record.monTime?.toISOString(), tue: record.tueTime?.toISOString(), - wed: record.wedTime?.toISOString(), - thu: record.thuTime?.toISOString(), + wed: record.wedTime + ? this.timeConverter.utcDatetimeToLocalTime( + record.wedTime.toISOString(), + timezone, + ) + : undefined, + thu: record.thuTime + ? this.timeConverter.utcDatetimeToLocalTime( + record.thuTime.toISOString(), + timezone, + ) + : undefined, fri: record.friTime?.toISOString(), sat: record.satTime?.toISOString(), sun: record.sunTime?.toISOString(), @@ -198,7 +213,27 @@ export class AdMapper toResponse = (entity: AdEntity): AdResponseDto => { const props = entity.getProps(); const response = new AdResponseDto(entity); - response.uuid = props.id; + response.userId = props.userId; + response.driver = props.driver; + response.passenger = props.passenger; + response.frequency = props.frequency; + response.fromDate = props.fromDate; + response.toDate = props.toDate; + response.schedule = { ...props.schedule }; + response.marginDurations = { ...props.marginDurations }; + response.seatsProposed = props.seatsProposed; + response.seatsRequested = props.seatsRequested; + response.waypoints = props.waypoints.map((waypoint: WaypointProps) => ({ + position: waypoint.position, + name: waypoint.address.name, + houseNumber: waypoint.address.houseNumber, + street: waypoint.address.street, + postalCode: waypoint.address.postalCode, + locality: waypoint.address.locality, + country: waypoint.address.country, + lon: waypoint.address.coordinates.lon, + lat: waypoint.address.coordinates.lat, + })); return response; }; diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 602d973..40dd238 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -20,12 +20,15 @@ import { CreateAdService } from './core/commands/create-ad/create-ad.service'; import { TimezoneFinder } from './infrastructure/timezone-finder'; import { PrismaService } from '@libs/db/prisma.service'; import { TimeConverter } from './infrastructure/time-converter'; +import { FindAdByIdGrpcController } from './interface/grpc-controllers/find-ad-by-id.grpc.controller'; +import { FindAdByIdQueryHandler } from './core/queries/find-ad-by-id/find-ad-by-id.query-handler'; @Module({ imports: [CqrsModule], - controllers: [CreateAdGrpcController], + controllers: [CreateAdGrpcController, FindAdByIdGrpcController], providers: [ CreateAdService, + FindAdByIdQueryHandler, PrismaService, AdMapper, { diff --git a/src/modules/ad/core/ports/time-converter.port.ts b/src/modules/ad/core/ports/time-converter.port.ts index feb4d2c..e48dbd0 100644 --- a/src/modules/ad/core/ports/time-converter.port.ts +++ b/src/modules/ad/core/ports/time-converter.port.ts @@ -1,8 +1,9 @@ export interface TimeConverterPort { - dateTimeToUtc( + localDateTimeToUtc( date: string, time: string, timezone: string, dst?: boolean, ): Date; + utcDatetimeToLocalTime(isoString: string, timezone: string): string; } diff --git a/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query-handler.ts b/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query-handler.ts new file mode 100644 index 0000000..547c572 --- /dev/null +++ b/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query-handler.ts @@ -0,0 +1,17 @@ +import { IQueryHandler, QueryHandler } from '@nestjs/cqrs'; +import { FindAdByIdQuery } from './find-ad-by-id.query'; +import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { AdRepositoryPort } from '../../ports/ad.repository.port'; +import { Inject } from '@nestjs/common'; +import { AdEntity } from '../../ad.entity'; + +@QueryHandler(FindAdByIdQuery) +export class FindAdByIdQueryHandler implements IQueryHandler { + constructor( + @Inject(AD_REPOSITORY) + private readonly repository: AdRepositoryPort, + ) {} + async execute(query: FindAdByIdQuery): Promise { + return await this.repository.findOneById(query.id, { waypoints: true }); + } +} diff --git a/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts b/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts index 97c8ea0..defce96 100644 --- a/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts +++ b/src/modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query.ts @@ -1,9 +1,10 @@ -import { FindAdByIdRequestDTO } from '../../../interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto'; +import { QueryBase } from '@libs/ddd/query.base'; -export class FindAdByIdQuery { +export class FindAdByIdQuery extends QueryBase { readonly id: string; - constructor(findAdByIdRequestDTO: FindAdByIdRequestDTO) { - this.id = findAdByIdRequestDTO.id; + constructor(id: string) { + super(); + this.id = id; } } diff --git a/src/modules/ad/infrastructure/time-converter.ts b/src/modules/ad/infrastructure/time-converter.ts index ef41158..b94b14a 100644 --- a/src/modules/ad/infrastructure/time-converter.ts +++ b/src/modules/ad/infrastructure/time-converter.ts @@ -4,7 +4,7 @@ import { DateTime, TimeZone } from 'timezonecomplete'; @Injectable() export class TimeConverter implements TimeConverterPort { - dateTimeToUtc = ( + localDateTimeToUtc = ( date: string, time: string, timezone: string, @@ -21,4 +21,16 @@ export class TimeConverter implements TimeConverterPort { return undefined; } }; + + utcDatetimeToLocalTime = (isoString: string, timezone: string): string => { + try { + return new DateTime(isoString) + .convert(TimeZone.zone(timezone)) + .toString() + .split('T')[1] + .substring(0, 5); + } catch (e) { + return undefined; + } + }; } diff --git a/src/modules/ad/interface/ad.presenter.ts b/src/modules/ad/interface/ad.presenter.ts deleted file mode 100644 index 9f19a71..0000000 --- a/src/modules/ad/interface/ad.presenter.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { AutoMap } from '@automapper/classes'; - -export class AdPresenter { - @AutoMap() - id: string; -} diff --git a/src/modules/ad/interface/dtos/ad.response.dto.ts b/src/modules/ad/interface/dtos/ad.response.dto.ts index 1ce10fd..61ec321 100644 --- a/src/modules/ad/interface/dtos/ad.response.dto.ts +++ b/src/modules/ad/interface/dtos/ad.response.dto.ts @@ -1,5 +1,43 @@ import { ResponseBase } from '@libs/api/response.base'; +import { Frequency } from '@modules/ad/core/ad.types'; export class AdResponseDto extends ResponseBase { - uuid: string; + userId: string; + driver: boolean; + passenger: boolean; + frequency: Frequency; + fromDate: string; + toDate: string; + schedule: { + mon?: string; + tue?: string; + wed?: string; + thu?: string; + fri?: string; + sat?: string; + sun?: string; + }; + marginDurations: { + mon?: number; + tue?: number; + wed?: number; + thu?: number; + fri?: number; + sat?: number; + sun?: number; + }; + seatsProposed: number; + seatsRequested: number; + strict: boolean; + waypoints: { + position: number; + name?: string; + houseNumber?: string; + street?: string; + postalCode?: string; + locality?: string; + country: string; + lon: number; + lat: number; + }[]; } diff --git a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts index 033263a..2e5ba6c 100644 --- a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts @@ -2,8 +2,7 @@ import { Controller, UsePipes } from '@nestjs/common'; import { CommandBus } from '@nestjs/cqrs'; import { GrpcMethod, RpcException } from '@nestjs/microservices'; import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; -import { AdPresenter } from '../ad.presenter'; -import { CreateAdRequestDTO } from './dtos/create-ad.request.dto'; +import { CreateAdRequestDto } from './dtos/create-ad.request.dto'; import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command'; import { Result, match } from 'oxide.ts'; import { AggregateID } from '@libs/ddd'; @@ -21,7 +20,7 @@ export class CreateAdGrpcController { constructor(private readonly commandBus: CommandBus) {} @GrpcMethod('AdsService', 'Create') - async create(data: CreateAdRequestDTO): Promise { + async create(data: CreateAdRequestDto): Promise { const result: Result = await this.commandBus.execute(new CreateAdCommand(data)); diff --git a/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts index fc110a8..cdd5384 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts @@ -1,8 +1,8 @@ import { AutoMap } from '@automapper/classes'; import { IsOptional, IsString } from 'class-validator'; -import { CoordinatesDTO } from './coordinates.dto'; +import { CoordinatesDto as CoordinatesDto } from './coordinates.dto'; -export class AddressDTO extends CoordinatesDTO { +export class AddressDto extends CoordinatesDto { @IsOptional() @AutoMap() name?: string; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts index 7bce3b1..54b4654 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts @@ -1,11 +1,19 @@ import { AutoMap } from '@automapper/classes'; +import { Transform } from 'class-transformer'; import { IsLatitude, IsLongitude } from 'class-validator'; +import { toPrecision } from './validators/to-precision'; -export class CoordinatesDTO { +export class CoordinatesDto { + @Transform(({ value }) => toPrecision(value, 6), { + toClassOnly: true, + }) @IsLongitude() @AutoMap() lon: number; + @Transform(({ value }) => toPrecision(value, 6), { + toClassOnly: true, + }) @IsLatitude() @AutoMap() lat: number; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts index 45ba314..4bd2559 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts @@ -11,15 +11,15 @@ import { IsISO8601, } from 'class-validator'; import { Transform, Type } from 'class-transformer'; -import { ScheduleDTO } from './schedule.dto'; -import { MarginDurationsDTO } from './margin-durations.dto'; -import { WaypointDTO } from './waypoint.dto'; +import { ScheduleDto } from './schedule.dto'; +import { MarginDurationsDto } from './margin-durations.dto'; +import { WaypointDto } from './waypoint.dto'; import { intToFrequency } from './validators/frequency.mapping'; import { IsSchedule } from './validators/decorators/is-schedule.validator'; import { HasValidPositionIndexes } from './validators/decorators/valid-position-indexes.validator'; import { Frequency } from '@modules/ad/core/ad.types'; -export class CreateAdRequestDTO { +export class CreateAdRequestDto { @IsUUID(4) @AutoMap() userId: string; @@ -55,17 +55,17 @@ export class CreateAdRequestDTO { @AutoMap() toDate: string; - @Type(() => ScheduleDTO) + @Type(() => ScheduleDto) @IsSchedule() @ValidateNested({ each: true }) @AutoMap() - schedule: ScheduleDTO; + schedule: ScheduleDto; @IsOptional() - @Type(() => MarginDurationsDTO) + @Type(() => MarginDurationsDto) @ValidateNested({ each: true }) @AutoMap() - marginDurations?: MarginDurationsDTO; + marginDurations?: MarginDurationsDto; @IsOptional() @IsInt() @@ -82,10 +82,11 @@ export class CreateAdRequestDTO { @AutoMap() strict?: boolean; + @Type(() => WaypointDto) @IsArray() @ArrayMinSize(2) @HasValidPositionIndexes() @ValidateNested({ each: true }) @AutoMap() - waypoints: WaypointDTO[]; + waypoints: WaypointDto[]; } diff --git a/src/modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/find-ad-by-id.request.dto.ts similarity index 74% rename from src/modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts rename to src/modules/ad/interface/grpc-controllers/dtos/find-ad-by-id.request.dto.ts index e2a0d17..ad485fc 100644 --- a/src/modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/find-ad-by-id.request.dto.ts @@ -1,6 +1,6 @@ import { IsNotEmpty, IsString } from 'class-validator'; -export class FindAdByIdRequestDTO { +export class FindAdByIdRequestDto { @IsString() @IsNotEmpty() id: string; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts index 5b0e439..564f23d 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts @@ -1,7 +1,7 @@ import { AutoMap } from '@automapper/classes'; import { IsInt, IsOptional } from 'class-validator'; -export class MarginDurationsDTO { +export class MarginDurationsDto { @IsOptional() @IsInt() @AutoMap() diff --git a/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts index 3918c57..9b9e9d0 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts @@ -1,7 +1,7 @@ import { AutoMap } from '@automapper/classes'; import { IsOptional, IsMilitaryTime } from 'class-validator'; -export class ScheduleDTO { +export class ScheduleDto { @IsOptional() @IsMilitaryTime() @AutoMap() diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts index 06d178a..5dede34 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts @@ -1,6 +1,6 @@ import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator'; import { hasValidPositionIndexes } from '../waypoint-position'; -import { WaypointDTO } from '../../waypoint.dto'; +import { WaypointDto } from '../../waypoint.dto'; export const HasValidPositionIndexes = ( validationOptions?: ValidationOptions, @@ -10,7 +10,7 @@ export const HasValidPositionIndexes = ( name: '', constraints: [], validator: { - validate: (waypoints: WaypointDTO[]): boolean => + validate: (waypoints: WaypointDto[]): boolean => hasValidPositionIndexes(waypoints), defaultMessage: buildMessage( () => `invalid waypoints positions`, diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/to-precision.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/to-precision.ts new file mode 100644 index 0000000..997e89c --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/to-precision.ts @@ -0,0 +1,4 @@ +export const toPrecision = (input: number, precision: number): number => { + const multiplier = 10 ** precision; + return Math.round((input + Number.EPSILON) * multiplier) / multiplier; +}; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts index 2bb02b1..efd7300 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts @@ -1,6 +1,6 @@ -import { WaypointDTO } from '../waypoint.dto'; +import { WaypointDto } from '../waypoint.dto'; -export const hasValidPositionIndexes = (waypoints: WaypointDTO[]): boolean => { +export const hasValidPositionIndexes = (waypoints: WaypointDto[]): boolean => { if (!waypoints) return; if (waypoints.every((waypoint) => waypoint.position === undefined)) return true; diff --git a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts index cb61059..40c5b52 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts @@ -1,8 +1,8 @@ import { AutoMap } from '@automapper/classes'; import { IsInt, IsOptional } from 'class-validator'; -import { AddressDTO } from './address.dto'; +import { AddressDto } from './address.dto'; -export class WaypointDTO extends AddressDTO { +export class WaypointDto extends AddressDto { @IsOptional() @IsInt() @AutoMap() diff --git a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts new file mode 100644 index 0000000..b60008f --- /dev/null +++ b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts @@ -0,0 +1,38 @@ +import { Controller, UsePipes } from '@nestjs/common'; +import { QueryBus } from '@nestjs/cqrs'; +import { GrpcMethod, RpcException } from '@nestjs/microservices'; +import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe'; +import { FindAdByIdRequestDto } from './dtos/find-ad-by-id.request.dto'; +import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query'; +import { AdResponseDto } from '../dtos/ad.response.dto'; +import { AdEntity } from '@modules/ad/core/ad.entity'; +import { AdMapper } from '@modules/ad/ad.mapper'; + +@UsePipes( + new RpcValidationPipe({ + whitelist: false, + forbidUnknownValues: false, + }), +) +@Controller() +export class FindAdByIdGrpcController { + constructor( + protected readonly mapper: AdMapper, + private readonly queryBus: QueryBus, + ) {} + + @GrpcMethod('AdsService', 'FindOneById') + async findOnebyId(data: FindAdByIdRequestDto): Promise { + try { + const ad: AdEntity = await this.queryBus.execute( + new FindAdByIdQuery(data.id), + ); + return this.mapper.toResponse(ad); + } catch (e) { + throw new RpcException({ + code: e.code, + message: e.message, + }); + } + } +} diff --git a/src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts b/src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts deleted file mode 100644 index e9427d3..0000000 --- a/src/modules/ad/interface/queries/find-ad-by-id/find-ad-by-id.grpc.controller.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { Mapper } from '@automapper/core'; -import { InjectMapper } from '@automapper/nestjs'; -import { Controller, UsePipes } from '@nestjs/common'; -import { QueryBus } from '@nestjs/cqrs'; -import { GrpcMethod, RpcException } from '@nestjs/microservices'; -import { RpcValidationPipe } from '../../../../../utils/pipes/rpc.validation-pipe'; -import { FindAdByIdRequestDTO } from './dtos/find-ad-by-id.request.dto'; -import { AdPresenter } from '../../ad.presenter'; -import { FindAdByIdQuery } from '../../../core/queries/find-ad-by-id/find-ad-by-id.query'; -import { AdEntity } from '../../../core/ad.entity'; - -@UsePipes( - new RpcValidationPipe({ - whitelist: false, - forbidUnknownValues: false, - }), -) -@Controller() -export class FindAdByIdGrpcController { - constructor( - private readonly queryBus: QueryBus, - @InjectMapper() private readonly mapper: Mapper, - ) {} - - @GrpcMethod('AdsService', 'FindOneById') - async findOnebyId(data: FindAdByIdRequestDTO): Promise { - try { - const ad = await this.queryBus.execute(new FindAdByIdQuery(data)); - return this.mapper.map(ad, AdEntity, AdPresenter); - } catch (e) { - throw new RpcException({ - code: e.code, - message: e.message, - }); - } - } -} diff --git a/src/modules/ad/tests/integration/ad.repository.spec.ts b/src/modules/ad/tests/integration/ad.repository.spec.ts index 319af8d..ffd1da3 100644 --- a/src/modules/ad/tests/integration/ad.repository.spec.ts +++ b/src/modules/ad/tests/integration/ad.repository.spec.ts @@ -3,10 +3,12 @@ import { AD_REPOSITORY, PARAMS_PROVIDER, TIMEZONE_FINDER, + TIME_CONVERTER, } from '@modules/ad/ad.di-tokens'; import { AdMapper } from '@modules/ad/ad.mapper'; import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; import { DefaultParamsProvider } from '@modules/ad/infrastructure/default-params-provider'; +import { TimeConverter } from '@modules/ad/infrastructure/time-converter'; import { TimezoneFinder } from '@modules/ad/infrastructure/timezone-finder'; import { ConfigModule } from '@nestjs/config'; import { EventEmitterModule } from '@nestjs/event-emitter'; @@ -274,6 +276,10 @@ describe('Ad Repository', () => { provide: TIMEZONE_FINDER, useClass: TimezoneFinder, }, + { + provide: TIME_CONVERTER, + useClass: TimeConverter, + }, ], }).compile(); prismaService = module.get(PrismaService); @@ -416,7 +422,7 @@ describe('Ad Repository', () => { waypoints: true, }); - expect(result.unwrap().id).toBe(baseUuid.uuid); + expect(result.id).toBe(baseUuid.uuid); }); // it('should return null', async () => { diff --git a/src/modules/ad/tests/unit/ad.mapper.spec.ts b/src/modules/ad/tests/unit/ad.mapper.spec.ts index 78e659d..2a6a13d 100644 --- a/src/modules/ad/tests/unit/ad.mapper.spec.ts +++ b/src/modules/ad/tests/unit/ad.mapper.spec.ts @@ -45,8 +45,8 @@ const adEntity: AdEntity = new AdEntity({ postalCode: '54000', country: 'France', coordinates: { - lat: 48.68944505415954, - lon: 6.176510296462267, + lat: 48.689445, + lon: 6.1765102, }, }, }, @@ -103,8 +103,8 @@ const adReadModel: AdReadModel = { locality: 'Nancy', postalCode: '54000', country: 'France', - lat: 48.68944505415954, - lon: 6.176510296462267, + lat: 48.689445, + lon: 6.1765102, createdAt: now, updatedAt: now, }, @@ -163,12 +163,13 @@ const mockTimezoneFinder: TimezoneFinderPort = { }; const mockTimeConverter: TimeConverterPort = { - dateTimeToUtc: jest + localDateTimeToUtc: jest .fn() // eslint-disable-next-line @typescript-eslint/no-unused-vars .mockImplementation((datetime: Date, timezone: string, dst?: boolean) => { return datetime; }), + utcDatetimeToLocalTime: jest.fn(), }; describe('Ad Mapper', () => { @@ -208,13 +209,13 @@ describe('Ad Mapper', () => { it('should map persisted data to domain entity', async () => { const mapped: AdEntity = adMapper.toDomain(adReadModel); expect(mapped.getProps().waypoints[0].address.coordinates.lat).toBe( - 48.68944505415954, + 48.689445, ); expect(mapped.getProps().waypoints[1].address.coordinates.lon).toBe(2.3522); }); it('should map domain entity to response', async () => { const mapped: AdResponseDto = adMapper.toResponse(adEntity); - expect(mapped.uuid).toBe('c160cf8c-f057-4962-841f-3ad68346df44'); + expect(mapped.id).toBe('c160cf8c-f057-4962-841f-3ad68346df44'); }); }); diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index 149fff5..2080706 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -2,8 +2,8 @@ import { Test, TestingModule } from '@nestjs/testing'; import { CreateAdService } from '@modules/ad/core/commands/create-ad/create-ad.service'; import { AD_REPOSITORY, PARAMS_PROVIDER } from '@modules/ad/ad.di-tokens'; import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; -import { WaypointDTO } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; -import { CreateAdRequestDTO } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto'; +import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; +import { CreateAdRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto'; import { Frequency } from '@modules/ad/core/ad.types'; import { CreateAdCommand } from '@modules/ad/core/commands/create-ad/create-ad.command'; import { Result } from 'oxide.ts'; @@ -12,7 +12,7 @@ import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; import { AdEntity } from '@modules/ad/core/ad.entity'; import { ConflictException } from '@libs/exceptions'; -const originWaypoint: WaypointDTO = { +const originWaypoint: WaypointDto = { position: 0, lon: 48.68944505415954, lat: 6.176510296462267, @@ -22,7 +22,7 @@ const originWaypoint: WaypointDTO = { postalCode: '54000', country: 'France', }; -const destinationWaypoint: WaypointDTO = { +const destinationWaypoint: WaypointDto = { position: 1, lon: 48.8566, lat: 2.3522, @@ -30,7 +30,7 @@ const destinationWaypoint: WaypointDTO = { postalCode: '75000', country: 'France', }; -const punctualCreateAdRequest: CreateAdRequestDTO = { +const punctualCreateAdRequest: CreateAdRequestDto = { userId: '4eb6a6af-ecfd-41c3-9118-473a507014d4', fromDate: '2023-12-21', toDate: '2023-12-21', diff --git a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts index 4bd8a4d..136e272 100644 --- a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts @@ -5,48 +5,81 @@ describe('Time Converter', () => { const timeConverter: TimeConverter = new TimeConverter(); expect(timeConverter).toBeDefined(); }); - it('should convert a paris datetime to utc', () => { - const timeConverter: TimeConverter = new TimeConverter(); - const parisDate = '2023-06-22'; - const parisTime = '08:00'; - const utcDatetime = timeConverter.dateTimeToUtc( - parisDate, - parisTime, - 'Europe/Paris', - ); - expect(utcDatetime.toISOString()).toEqual('2023-06-22T06:00:00.000Z'); + + describe('localDateTimeToUtc', () => { + it('should convert a paris datetime to utc', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-06-22'; + const parisTime = '08:00'; + const utcDatetime = timeConverter.localDateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime.toISOString()).toEqual('2023-06-22T06:00:00.000Z'); + }); + it('should return undefined if date is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-16-22'; + const parisTime = '08:00'; + const utcDatetime = timeConverter.localDateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime).toBeUndefined(); + }); + it('should return undefined if time is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-06-22'; + const parisTime = '28:00'; + const utcDatetime = timeConverter.localDateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime).toBeUndefined(); + }); + it('should return undefined if timezone is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = '2023-06-22'; + const parisTime = '08:00'; + const utcDatetime = timeConverter.localDateTimeToUtc( + parisDate, + parisTime, + 'Foo/Bar', + ); + expect(utcDatetime).toBeUndefined(); + }); }); - it('should return undefined if date is invalid', () => { - const timeConverter: TimeConverter = new TimeConverter(); - const parisDate = '2023-16-22'; - const parisTime = '08:00'; - const utcDatetime = timeConverter.dateTimeToUtc( - parisDate, - parisTime, - 'Europe/Paris', - ); - expect(utcDatetime).toBeUndefined(); - }); - it('should return undefined if time is invalid', () => { - const timeConverter: TimeConverter = new TimeConverter(); - const parisDate = '2023-06-22'; - const parisTime = '28:00'; - const utcDatetime = timeConverter.dateTimeToUtc( - parisDate, - parisTime, - 'Europe/Paris', - ); - expect(utcDatetime).toBeUndefined(); - }); - it('should return undefined if timezone is invalid', () => { - const timeConverter: TimeConverter = new TimeConverter(); - const parisDate = '2023-06-22'; - const parisTime = '08:00'; - const utcDatetime = timeConverter.dateTimeToUtc( - parisDate, - parisTime, - 'Foo/Bar', - ); - expect(utcDatetime).toBeUndefined(); + + describe('utcDatetimeToLocalTime', () => { + it('should convert an utc datetime isostring to a paris local time', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const utcDatetimeIsostring = '2023-06-22T06:25:00.000Z'; + const parisTime = timeConverter.utcDatetimeToLocalTime( + utcDatetimeIsostring, + 'Europe/Paris', + ); + expect(parisTime).toBe('08:25'); + }); + it('should return undefined if isostring input is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const utcDatetimeIsostring = 'not_an_isostring'; + const parisTime = timeConverter.utcDatetimeToLocalTime( + utcDatetimeIsostring, + 'Europe/Paris', + ); + expect(parisTime).toBeUndefined(); + }); + it('should return undefined if timezone input is invalid', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const utcDatetimeIsostring = '2023-06-22T06:25:00.000Z'; + const parisTime = timeConverter.utcDatetimeToLocalTime( + utcDatetimeIsostring, + 'Foo/Bar', + ); + expect(parisTime).toBeUndefined(); + }); }); }); diff --git a/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts b/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts index d90f99e..f7d5272 100644 --- a/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts +++ b/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts @@ -1,8 +1,8 @@ import { hasValidPositionIndexes } from '@modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position'; -import { WaypointDTO } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; +import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; describe('addresses position validator', () => { - const mockAddress1: WaypointDTO = { + const mockAddress1: WaypointDto = { lon: 48.68944505415954, lat: 6.176510296462267, houseNumber: '5', @@ -11,14 +11,14 @@ describe('addresses position validator', () => { postalCode: '54000', country: 'France', }; - const mockAddress2: WaypointDTO = { + const mockAddress2: WaypointDto = { lon: 48.8566, lat: 2.3522, locality: 'Paris', postalCode: '75000', country: 'France', }; - const mockAddress3: WaypointDTO = { + const mockAddress3: WaypointDto = { lon: 49.2628, lat: 4.0347, locality: 'Reims', diff --git a/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts b/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts index 6f0f365..5b56535 100644 --- a/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts +++ b/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts @@ -1,6 +1,6 @@ import { ArgumentMetadata } from '@nestjs/common'; import { RpcValidationPipe } from '../../pipes/rpc.validation-pipe'; -import { FindAdByIdRequestDTO } from '../../../modules/ad/interface/queries/find-ad-by-id/dtos/find-ad-by-id.request.dto'; +import { FindAdByIdRequestDto } from '../../../modules/ad/interface/grpc-controllers/dtos/find-ad-by-id.request.dto'; describe('RpcValidationPipe', () => { it('should not validate request', async () => { @@ -10,10 +10,10 @@ describe('RpcValidationPipe', () => { }); const metadata: ArgumentMetadata = { type: 'body', - metatype: FindAdByIdRequestDTO, + metatype: FindAdByIdRequestDto, data: '', }; - await target.transform({}, metadata).catch((err) => { + await target.transform({}, metadata).catch((err) => { expect(err.message).toEqual('Rpc Exception'); }); }); From 0409670eec3d8f3b60d76959b3d80381bebc6c02 Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 23 Jun 2023 14:41:06 +0200 Subject: [PATCH 08/29] validator tests --- .../grpc-controllers/dtos/coordinates.dto.ts | 2 +- .../dtos/create-ad.request.dto.ts | 6 +- .../int-to-frequency.ts} | 0 .../to-precision.ts | 0 ...> has-valid-position-indexes.decorator.ts} | 2 +- ....validator.ts => is-schedule.decorator.ts} | 0 ...> has-valid-position-indexes.validator.ts} | 14 +++-- ...s-valid-position-indexes.decorator.spec.ts | 62 +++++++++++++++++++ ...-valid-position-indexes.validator.spec.ts} | 14 +++-- ...pping.spec.ts => int-to-frequency.spec.ts} | 2 +- .../interface/is-schedule.decorator.spec.ts | 30 +++++++++ .../tests/unit/interface/to-precision.spec.ts | 14 +++++ 12 files changed, 129 insertions(+), 17 deletions(-) rename src/modules/ad/interface/grpc-controllers/dtos/{validators/frequency.mapping.ts => transformers/int-to-frequency.ts} (100%) rename src/modules/ad/interface/grpc-controllers/dtos/{validators => transformers}/to-precision.ts (100%) rename src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/{valid-position-indexes.validator.ts => has-valid-position-indexes.decorator.ts} (87%) rename src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/{is-schedule.validator.ts => is-schedule.decorator.ts} (100%) rename src/modules/ad/interface/grpc-controllers/dtos/validators/{waypoint-position.ts => has-valid-position-indexes.validator.ts} (51%) create mode 100644 src/modules/ad/tests/unit/interface/has-valid-position-indexes.decorator.spec.ts rename src/modules/ad/tests/unit/interface/{valid-position-indexes.spec.ts => has-valid-position-indexes.validator.spec.ts} (83%) rename src/modules/ad/tests/unit/interface/{frequency.mapping.spec.ts => int-to-frequency.spec.ts} (92%) create mode 100644 src/modules/ad/tests/unit/interface/is-schedule.decorator.spec.ts create mode 100644 src/modules/ad/tests/unit/interface/to-precision.spec.ts diff --git a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts index 54b4654..99f5783 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts @@ -1,7 +1,7 @@ import { AutoMap } from '@automapper/classes'; import { Transform } from 'class-transformer'; import { IsLatitude, IsLongitude } from 'class-validator'; -import { toPrecision } from './validators/to-precision'; +import { toPrecision } from './transformers/to-precision'; export class CoordinatesDto { @Transform(({ value }) => toPrecision(value, 6), { diff --git a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts index 4bd2559..f7f31b5 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts @@ -14,9 +14,9 @@ import { Transform, Type } from 'class-transformer'; import { ScheduleDto } from './schedule.dto'; import { MarginDurationsDto } from './margin-durations.dto'; import { WaypointDto } from './waypoint.dto'; -import { intToFrequency } from './validators/frequency.mapping'; -import { IsSchedule } from './validators/decorators/is-schedule.validator'; -import { HasValidPositionIndexes } from './validators/decorators/valid-position-indexes.validator'; +import { intToFrequency } from './transformers/int-to-frequency'; +import { IsSchedule } from './validators/decorators/is-schedule.decorator'; +import { HasValidPositionIndexes } from './validators/decorators/has-valid-position-indexes.decorator'; import { Frequency } from '@modules/ad/core/ad.types'; export class CreateAdRequestDto { diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping.ts b/src/modules/ad/interface/grpc-controllers/dtos/transformers/int-to-frequency.ts similarity index 100% rename from src/modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping.ts rename to src/modules/ad/interface/grpc-controllers/dtos/transformers/int-to-frequency.ts diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/to-precision.ts b/src/modules/ad/interface/grpc-controllers/dtos/transformers/to-precision.ts similarity index 100% rename from src/modules/ad/interface/grpc-controllers/dtos/validators/to-precision.ts rename to src/modules/ad/interface/grpc-controllers/dtos/transformers/to-precision.ts diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-valid-position-indexes.decorator.ts similarity index 87% rename from src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts rename to src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-valid-position-indexes.decorator.ts index 5dede34..87e3a36 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/valid-position-indexes.validator.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-valid-position-indexes.decorator.ts @@ -1,5 +1,5 @@ import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator'; -import { hasValidPositionIndexes } from '../waypoint-position'; +import { hasValidPositionIndexes } from '../has-valid-position-indexes.validator'; import { WaypointDto } from '../../waypoint.dto'; export const HasValidPositionIndexes = ( diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.validator.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.decorator.ts similarity index 100% rename from src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.validator.ts rename to src/modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.decorator.ts diff --git a/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts b/src/modules/ad/interface/grpc-controllers/dtos/validators/has-valid-position-indexes.validator.ts similarity index 51% rename from src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts rename to src/modules/ad/interface/grpc-controllers/dtos/validators/has-valid-position-indexes.validator.ts index efd7300..04504a8 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/validators/has-valid-position-indexes.validator.ts @@ -1,14 +1,16 @@ import { WaypointDto } from '../waypoint.dto'; export const hasValidPositionIndexes = (waypoints: WaypointDto[]): boolean => { - if (!waypoints) return; + if (!waypoints) return false; + if (waypoints.length == 0) return false; if (waypoints.every((waypoint) => waypoint.position === undefined)) - return true; + return false; if (waypoints.every((waypoint) => typeof waypoint.position === 'number')) { - waypoints.sort((a, b) => a.position - b.position); - for (let i = 1; i < waypoints.length; i++) { - if (waypoints[i - 1].position >= waypoints[i].position) return false; - } + const positions = Array.from(waypoints, (waypoint) => waypoint.position); + positions.sort(); + for (let i = 1; i < positions.length; i++) + if (positions[i] != positions[i - 1] + 1) return false; + return true; } return false; diff --git a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.decorator.spec.ts b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.decorator.spec.ts new file mode 100644 index 0000000..bf61ce6 --- /dev/null +++ b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.decorator.spec.ts @@ -0,0 +1,62 @@ +import { HasValidPositionIndexes } from '@modules/ad/interface/grpc-controllers/dtos/validators/decorators/has-valid-position-indexes.decorator'; +import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; +import { Validator } from 'class-validator'; + +describe('valid position indexes decorator', () => { + class MyClass { + @HasValidPositionIndexes() + waypoints: WaypointDto[]; + } + it('should return a property decorator has a function', () => { + const hasValidPositionIndexes = HasValidPositionIndexes(); + expect(typeof hasValidPositionIndexes).toBe('function'); + }); + it('should validate an array of waypoints with valid positions', async () => { + const myClassInstance = new MyClass(); + myClassInstance.waypoints = [ + { + position: 0, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', + }, + { + position: 1, + lon: 49.2628, + lat: 4.0347, + locality: 'Reims', + postalCode: '51454', + country: 'France', + }, + ]; + const validator = new Validator(); + const validation = await validator.validate(myClassInstance); + expect(validation.length).toBe(0); + }); + it('should not validate an array of waypoints with invalid positions', async () => { + const myClassInstance = new MyClass(); + myClassInstance.waypoints = [ + { + position: 1, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', + }, + { + position: 1, + lon: 49.2628, + lat: 4.0347, + locality: 'Reims', + postalCode: '51454', + country: 'France', + }, + ]; + const validator = new Validator(); + const validation = await validator.validate(myClassInstance); + expect(validation.length).toBe(1); + }); +}); diff --git a/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts similarity index 83% rename from src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts rename to src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts index f7d5272..ffc0272 100644 --- a/src/modules/ad/tests/unit/interface/valid-position-indexes.spec.ts +++ b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts @@ -1,4 +1,4 @@ -import { hasValidPositionIndexes } from '@modules/ad/interface/grpc-controllers/dtos/validators/waypoint-position'; +import { hasValidPositionIndexes } from '@modules/ad/interface/grpc-controllers/dtos/validators/has-valid-position-indexes.validator'; import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; describe('addresses position validator', () => { @@ -26,10 +26,10 @@ describe('addresses position validator', () => { country: 'France', }; - it('should validate if no position is defined', () => { + it('should not validate if no position is defined', () => { expect( hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), - ).toBeTruthy(); + ).toBeFalsy(); }); it('should not validate if only one position is defined', () => { mockAddress1.position = 0; @@ -61,11 +61,15 @@ describe('addresses position validator', () => { expect( hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeTruthy(); - mockAddress1.position = 10; - mockAddress2.position = 0; + mockAddress1.position = 1; + mockAddress2.position = 2; mockAddress3.position = 3; expect( hasValidPositionIndexes([mockAddress1, mockAddress2, mockAddress3]), ).toBeTruthy(); }); + it('should not validate if no waypoints are defined', () => { + expect(hasValidPositionIndexes(undefined)).toBeFalsy(); + expect(hasValidPositionIndexes([])).toBeFalsy(); + }); }); diff --git a/src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts b/src/modules/ad/tests/unit/interface/int-to-frequency.spec.ts similarity index 92% rename from src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts rename to src/modules/ad/tests/unit/interface/int-to-frequency.spec.ts index bd8928d..08f2729 100644 --- a/src/modules/ad/tests/unit/interface/frequency.mapping.spec.ts +++ b/src/modules/ad/tests/unit/interface/int-to-frequency.spec.ts @@ -1,5 +1,5 @@ import { Frequency } from '@modules/ad/core/ad.types'; -import { intToFrequency } from '@modules/ad/interface/grpc-controllers/dtos/validators/frequency.mapping'; +import { intToFrequency } from '@modules/ad/interface/grpc-controllers/dtos/transformers/int-to-frequency'; describe('frequency mapping', () => { it('should return punctual if frequency is 1', () => { diff --git a/src/modules/ad/tests/unit/interface/is-schedule.decorator.spec.ts b/src/modules/ad/tests/unit/interface/is-schedule.decorator.spec.ts new file mode 100644 index 0000000..613d0b4 --- /dev/null +++ b/src/modules/ad/tests/unit/interface/is-schedule.decorator.spec.ts @@ -0,0 +1,30 @@ +import { ScheduleDto } from '@modules/ad/interface/grpc-controllers/dtos/schedule.dto'; +import { IsSchedule } from '@modules/ad/interface/grpc-controllers/dtos/validators/decorators/is-schedule.decorator'; +import { Validator } from 'class-validator'; + +describe('schedule decorator', () => { + class MyClass { + @IsSchedule() + schedule: ScheduleDto; + } + it('should return a property decorator has a function', () => { + const isSchedule = IsSchedule(); + expect(typeof isSchedule).toBe('function'); + }); + it('should validate a valid schedule', async () => { + const myClassInstance = new MyClass(); + myClassInstance.schedule = { + mon: '07:15', + }; + const validator = new Validator(); + const validation = await validator.validate(myClassInstance); + expect(validation.length).toBe(0); + }); + it('should not validate a invalid schedule', async () => { + const myClassInstance = new MyClass(); + myClassInstance.schedule = {}; + const validator = new Validator(); + const validation = await validator.validate(myClassInstance); + expect(validation.length).toBe(1); + }); +}); diff --git a/src/modules/ad/tests/unit/interface/to-precision.spec.ts b/src/modules/ad/tests/unit/interface/to-precision.spec.ts new file mode 100644 index 0000000..2da0933 --- /dev/null +++ b/src/modules/ad/tests/unit/interface/to-precision.spec.ts @@ -0,0 +1,14 @@ +import { toPrecision } from '@modules/ad/interface/grpc-controllers/dtos/transformers/to-precision'; + +describe('precision handler', () => { + it('should return a 6 digits float number for a 10 digits float input number and 6 as precision', () => { + const precised = toPrecision(1.1234567891, 6); + const stringPrecised = precised.toString().split('.')[1]; + expect(stringPrecised.length).toBe(6); + }); + it('should return a 2 digits float number for a 2 digits float input number and 4 as precision', () => { + const precised = toPrecision(1.12, 4); + const stringPrecised = precised.toString().split('.')[1]; + expect(stringPrecised.length).toBe(2); + }); +}); From b7bb656f10b1449e12543b1074f65a5fdcc30a21 Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 23 Jun 2023 15:35:37 +0200 Subject: [PATCH 09/29] test find-by-id query --- README.md | 59 ++++---- src/app.module.ts | 3 - .../grpc-controllers/dtos/address.dto.ts | 7 - .../grpc-controllers/dtos/coordinates.dto.ts | 3 - .../dtos/create-ad.request.dto.ts | 13 -- .../dtos/margin-durations.dto.ts | 8 -- .../grpc-controllers/dtos/schedule.dto.ts | 8 -- .../grpc-controllers/dtos/waypoint.dto.ts | 2 - .../core/find-ad-by-id.query-handler.spec.ts | 132 ++++++++++++++++++ 9 files changed, 165 insertions(+), 70 deletions(-) create mode 100644 src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts diff --git a/README.md b/README.md index 92c67cb..c683b97 100644 --- a/README.md +++ b/README.md @@ -48,30 +48,34 @@ npm run migrate The app exposes the following [gRPC](https://grpc.io/) services : -- **FindByUuid** : find an ad by its uuid +- **FindById** : find an ad by its id ```json { - "uuid": "80126a61-d128-4f96-afdb-92e33c75a3e1" + "id": "80126a61-d128-4f96-afdb-92e33c75a3e1" } ``` -- **Create** : create an ad (note that uuid is optional, a uuid will be automatically attributed if it is not provided) +- **Create** : create an ad (note that id is optional, an id (as a uuid) will be automatically attributed if it is not provided) Punctual driver ad : ```json { - "userUuid": "80c9bb02-0931-4a1d-bea6-22d358992245", + "userId": "80c9bb02-0931-4a1d-bea6-22d358992245", "driver": true, - "seatsDriver": 3, + "seatsProposed": 3, "frequency": "PUNCTUAL", - "departure": "2023-01-15 09:00", - "addresses": [ + "fromDate": "2023-01-15", + "toDate": "2023-01-15", + "schedule": { + "thu": "09:00" + }, + "waypoints": [ { "position": 0, - "lon": 48.68944505415954, - "lat": 6.176510296462267, + "lon": 48.689445, + "lat": 6.17651, "houseNumber": "5", "street": "Avenue Foch", "locality": "Nancy", @@ -94,14 +98,18 @@ The app exposes the following [gRPC](https://grpc.io/) services : ```json { - "userUuid": "80c9bb02-0931-4a1d-bea6-22d358992245", + "userId": "80c9bb02-0931-4a1d-bea6-22d358992245", "driver": true, "pasenger": true, - "seatsDriver": 3, - "seatsPassenger": 1, + "seatsProposed": 3, + "seatsRequested": 1, "frequency": "PUNCTUAL", - "departure": "2023-01-15 09:00", - "addresses": [ + "fromDate": "2023-01-15", + "toDate": "2023-01-15", + "schedule": { + "thu": "09:00" + }, + "waypoints": [ { "position": 0, "lon": 48.68944505415954, @@ -139,11 +147,11 @@ The app exposes the following [gRPC](https://grpc.io/) services : "tue": "07:05", "fri": "07:10" }, - "addresses": [ + "waypoints": [ { "position": 0, - "lon": 48.68944505415954, - "lat": 6.176510296462267, + "lon": 48.689445, + "lat": 6.17651, "houseNumber": "5", "street": "Avenue Foch", "locality": "Nancy", @@ -164,15 +172,14 @@ The app exposes the following [gRPC](https://grpc.io/) services : The list of possible options when creating an ad : - - uuid (optional): the uuid of the ad - - userUuid: the user uuid + - id (optional): the id of the ad (as a uuid) + - userId: the user id (as a uuid) - driver (boolean, optional): if the ad is a driver ad - passenger (boolean, optional): if the ad is a passenger ad - frequency: `PUNCTUAL` or `RECURRENT` - - departure (required if punctual): departure date and hour/minute for a punctual ad - - fromDate (required if recurrent): start date for recurrent ad - - toDate (required if recurrent): end date for recurrent ad - - schedule (required if recurrent): an object with the departure time for each carpooled day in the week + - fromDate: start date for recurrent ad, carpool date for punctual ad + - toDate: end date for recurrent ad, same as fromDate for punctual ad + - schedule: an object with the departure time for each carpooled day in the week - marginDurations (optional): an object with the margin duration (in seconds) for each carpooled day in the week, eg: { @@ -181,10 +188,10 @@ The app exposes the following [gRPC](https://grpc.io/) services : "fri": 950 } - - seatsDriver (optional): number of seats proposed as driver; - - seatsPassenger (optional): number of seats requested as passenger; + - seatsProposed (optional): number of seats proposed as driver + - seatsRequested (optional): number of seats requested as passenger - strict (boolean, optional): if set to true, allow matching only with similar frequency ads - - addresses: an array of adresses that represent the waypoints of the journey (only first and last waypoints are used for passenger ads) + - waypoints: an array of addresses that represent the waypoints of the journey (only first and last waypoints are used for passenger ads). Note that positions **must** be consecutives Default values must be set in `.env` file. diff --git a/src/app.module.ts b/src/app.module.ts index f11aeaf..ebb535c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,8 +2,6 @@ import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; // import { HealthModule } from './modules/health/health.module'; import { AdModule } from './modules/ad/ad.module'; -import { AutomapperModule } from '@automapper/nestjs'; -import { classes } from '@automapper/classes'; import { MessageBrokerModule, MessageBrokerModuleOptions, @@ -21,7 +19,6 @@ import { HealthModule } from '@modules/health/health.module'; ConfigModule.forRoot({ isGlobal: true }), EventEmitterModule.forRoot(), RequestContextModule, - AutomapperModule.forRoot({ strategyInitializer: classes() }), MessageBrokerModule.forRootAsync({ imports: [ConfigModule], inject: [ConfigService], diff --git a/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts index cdd5384..9659d96 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/address.dto.ts @@ -1,33 +1,26 @@ -import { AutoMap } from '@automapper/classes'; import { IsOptional, IsString } from 'class-validator'; import { CoordinatesDto as CoordinatesDto } from './coordinates.dto'; export class AddressDto extends CoordinatesDto { @IsOptional() - @AutoMap() name?: string; @IsOptional() @IsString() - @AutoMap() houseNumber?: string; @IsOptional() @IsString() - @AutoMap() street?: string; @IsOptional() @IsString() - @AutoMap() locality?: string; @IsOptional() @IsString() - @AutoMap() postalCode?: string; @IsString() - @AutoMap() country: string; } diff --git a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts index 99f5783..3e622be 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/coordinates.dto.ts @@ -1,4 +1,3 @@ -import { AutoMap } from '@automapper/classes'; import { Transform } from 'class-transformer'; import { IsLatitude, IsLongitude } from 'class-validator'; import { toPrecision } from './transformers/to-precision'; @@ -8,13 +7,11 @@ export class CoordinatesDto { toClassOnly: true, }) @IsLongitude() - @AutoMap() lon: number; @Transform(({ value }) => toPrecision(value, 6), { toClassOnly: true, }) @IsLatitude() - @AutoMap() lat: number; } diff --git a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts index f7f31b5..6d07d97 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto.ts @@ -1,4 +1,3 @@ -import { AutoMap } from '@automapper/classes'; import { IsOptional, IsBoolean, @@ -21,65 +20,54 @@ import { Frequency } from '@modules/ad/core/ad.types'; export class CreateAdRequestDto { @IsUUID(4) - @AutoMap() userId: string; @IsOptional() @IsBoolean() - @AutoMap() driver?: boolean; @IsOptional() @IsBoolean() - @AutoMap() passenger?: boolean; @Transform(({ value }) => intToFrequency(value), { toClassOnly: true, }) @IsEnum(Frequency) - @AutoMap() frequency: Frequency; @IsISO8601({ strict: true, strictSeparator: true, }) - @AutoMap() fromDate: string; @IsISO8601({ strict: true, strictSeparator: true, }) - @AutoMap() toDate: string; @Type(() => ScheduleDto) @IsSchedule() @ValidateNested({ each: true }) - @AutoMap() schedule: ScheduleDto; @IsOptional() @Type(() => MarginDurationsDto) @ValidateNested({ each: true }) - @AutoMap() marginDurations?: MarginDurationsDto; @IsOptional() @IsInt() - @AutoMap() seatsProposed?: number; @IsOptional() @IsInt() - @AutoMap() seatsRequested?: number; @IsOptional() @IsBoolean() - @AutoMap() strict?: boolean; @Type(() => WaypointDto) @@ -87,6 +75,5 @@ export class CreateAdRequestDto { @ArrayMinSize(2) @HasValidPositionIndexes() @ValidateNested({ each: true }) - @AutoMap() waypoints: WaypointDto[]; } diff --git a/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts index 564f23d..5637707 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/margin-durations.dto.ts @@ -1,39 +1,31 @@ -import { AutoMap } from '@automapper/classes'; import { IsInt, IsOptional } from 'class-validator'; export class MarginDurationsDto { @IsOptional() @IsInt() - @AutoMap() mon?: number; @IsOptional() @IsInt() - @AutoMap() tue?: number; @IsOptional() @IsInt() - @AutoMap() wed?: number; @IsOptional() @IsInt() - @AutoMap() thu?: number; @IsOptional() @IsInt() - @AutoMap() fri?: number; @IsOptional() @IsInt() - @AutoMap() sat?: number; @IsOptional() @IsInt() - @AutoMap() sun?: number; } diff --git a/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts index 9b9e9d0..7316a64 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/schedule.dto.ts @@ -1,39 +1,31 @@ -import { AutoMap } from '@automapper/classes'; import { IsOptional, IsMilitaryTime } from 'class-validator'; export class ScheduleDto { @IsOptional() @IsMilitaryTime() - @AutoMap() mon?: string; @IsOptional() @IsMilitaryTime() - @AutoMap() tue?: string; @IsOptional() @IsMilitaryTime() - @AutoMap() wed?: string; @IsOptional() @IsMilitaryTime() - @AutoMap() thu?: string; @IsOptional() @IsMilitaryTime() - @AutoMap() fri?: string; @IsOptional() @IsMilitaryTime() - @AutoMap() sat?: string; @IsOptional() @IsMilitaryTime() - @AutoMap() sun?: string; } diff --git a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts index 40c5b52..ded5386 100644 --- a/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts +++ b/src/modules/ad/interface/grpc-controllers/dtos/waypoint.dto.ts @@ -1,10 +1,8 @@ -import { AutoMap } from '@automapper/classes'; import { IsInt, IsOptional } from 'class-validator'; import { AddressDto } from './address.dto'; export class WaypointDto extends AddressDto { @IsOptional() @IsInt() - @AutoMap() position?: number; } diff --git a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts new file mode 100644 index 0000000..37f294c --- /dev/null +++ b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts @@ -0,0 +1,132 @@ +import { AD_REPOSITORY } from '@modules/ad/ad.di-tokens'; +import { AdEntity } from '@modules/ad/core/ad.entity'; +import { + CreateAdProps, + DefaultAdProps, + Frequency, +} from '@modules/ad/core/ad.types'; +import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query'; +import { FindAdByIdQueryHandler } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query-handler'; +import { MarginDurationsProps } from '@modules/ad/core/value-objects/margin-durations.value-object'; +import { WaypointProps } from '@modules/ad/core/value-objects/waypoint.value-object'; +import { Test, TestingModule } from '@nestjs/testing'; + +const originWaypointProps: WaypointProps = { + position: 0, + address: { + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.68944505415954, + lat: 6.176510296462267, + }, + }, +}; +const destinationWaypointProps: WaypointProps = { + position: 1, + address: { + locality: 'Paris', + postalCode: '75000', + country: 'France', + coordinates: { + lon: 48.8566, + lat: 2.3522, + }, + }, +}; +const marginDurationsProps: MarginDurationsProps = { + mon: 600, + tue: 600, + wed: 600, + thu: 600, + fri: 600, + sat: 600, + sun: 600, +}; +const baseCreateAdProps = { + userId: 'e8fe64b1-4c33-49e1-9f69-4db48b21df36', + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [originWaypointProps, destinationWaypointProps], +}; +const punctualCreateAdProps = { + fromDate: '2023-06-22', + toDate: '2023-06-22', + schedule: { + wed: '08:30', + }, + frequency: Frequency.PUNCTUAL, +}; +const punctualPassengerCreateAdProps: CreateAdProps = { + ...baseCreateAdProps, + ...punctualCreateAdProps, + marginDurations: marginDurationsProps, + driver: false, + passenger: true, +}; + +const defaultAdProps: DefaultAdProps = { + driver: false, + passenger: true, + marginDurations: { + mon: 900, + tue: 900, + wed: 900, + thu: 900, + fri: 900, + sat: 900, + sun: 900, + }, + seatsProposed: 3, + seatsRequested: 1, + strict: false, +}; + +const ad: AdEntity = AdEntity.create( + punctualPassengerCreateAdProps, + defaultAdProps, +); + +const mockAdRepository = { + findOneById: jest.fn().mockImplementation(() => ad), +}; + +describe('find-ad-by-id.query-handler', () => { + let findAdByIdQueryHandler: FindAdByIdQueryHandler; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: AD_REPOSITORY, + useValue: mockAdRepository, + }, + FindAdByIdQueryHandler, + ], + }).compile(); + + findAdByIdQueryHandler = module.get( + FindAdByIdQueryHandler, + ); + }); + + it('should be defined', () => { + expect(findAdByIdQueryHandler).toBeDefined(); + }); + + describe('execution', () => { + it('should return an ad', async () => { + const findAdbyIdQuery = new FindAdByIdQuery( + 'dd264806-13b4-4226-9b18-87adf0ad5dd1', + ); + const ad: AdEntity = await findAdByIdQueryHandler.execute( + findAdbyIdQuery, + ); + expect(ad.getProps().fromDate).toBe('2023-06-22'); + }); + }); +}); From d22de7c448f119668cd994a75c30a288a939f6bb Mon Sep 17 00:00:00 2001 From: sbriat Date: Fri, 23 Jun 2023 16:34:32 +0200 Subject: [PATCH 10/29] improve test coverage --- README.md | 4 +- .../ad/tests/unit/core/ad.entity.spec.ts | 8 +- .../unit/core/address.value-object.spec.ts | 24 +++++ .../core/coordinates.value-object.spec.ts | 12 +++ .../tests/unit/core/create-ad.service.spec.ts | 4 +- .../core/find-ad-by-id.query-handler.spec.ts | 4 +- .../margin-durations.value-object.spec.ts | 47 ++++++++++ .../unit/core/schedule.value-object.spec.ts | 22 +++++ .../unit/core/waypoint.value-object.spec.ts | 22 +++++ .../unit/infrastructure/ad.repository.spec.ts | 88 +++++++++++++++++++ .../infrastructure/time-converter.spec.ts | 11 +++ ...s-valid-position-indexes.validator.spec.ts | 4 +- 12 files changed, 238 insertions(+), 12 deletions(-) create mode 100644 src/modules/ad/tests/unit/core/address.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/schedule.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts create mode 100644 src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts diff --git a/README.md b/README.md index c683b97..5fe1dc6 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,8 @@ The app exposes the following [gRPC](https://grpc.io/) services : "waypoints": [ { "position": 0, - "lon": 48.68944505415954, - "lat": 6.176510296462267, + "lon": 48.689445, + "lat": 6.17651, "houseNumber": "5", "street": "Avenue Foch", "locality": "Nancy", diff --git a/src/modules/ad/tests/unit/core/ad.entity.spec.ts b/src/modules/ad/tests/unit/core/ad.entity.spec.ts index 55d2be6..8d71ac6 100644 --- a/src/modules/ad/tests/unit/core/ad.entity.spec.ts +++ b/src/modules/ad/tests/unit/core/ad.entity.spec.ts @@ -16,8 +16,8 @@ const originWaypointProps: WaypointProps = { postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, }, }, }; @@ -364,8 +364,8 @@ describe('Ad entity create', () => { postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, }, }, }, diff --git a/src/modules/ad/tests/unit/core/address.value-object.spec.ts b/src/modules/ad/tests/unit/core/address.value-object.spec.ts new file mode 100644 index 0000000..5475680 --- /dev/null +++ b/src/modules/ad/tests/unit/core/address.value-object.spec.ts @@ -0,0 +1,24 @@ +import { Address } from '@modules/ad/core/value-objects/address.value-object'; + +describe('Address value object', () => { + it('should create an address value object', () => { + const addressVO = new Address({ + houseNumber: '5', + street: 'rue de la monnaie', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.689445, + lat: 6.17651, + }, + }); + expect(addressVO.houseNumber).toBe('5'); + expect(addressVO.street).toBe('rue de la monnaie'); + expect(addressVO.locality).toBe('Nancy'); + expect(addressVO.postalCode).toBe('54000'); + expect(addressVO.country).toBe('France'); + expect(addressVO.coordinates.lon).toBe(48.689445); + expect(addressVO.name).toBeUndefined(); + }); +}); diff --git a/src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts b/src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts new file mode 100644 index 0000000..4718ac5 --- /dev/null +++ b/src/modules/ad/tests/unit/core/coordinates.value-object.spec.ts @@ -0,0 +1,12 @@ +import { Coordinates } from '@modules/ad/core/value-objects/coordinates.value-object'; + +describe('Coordinates value object', () => { + it('should create a coordinates value object', () => { + const coordinatesVO = new Coordinates({ + lon: 48.689445, + lat: 6.17651, + }); + expect(coordinatesVO.lon).toBe(48.689445); + expect(coordinatesVO.lat).toBe(6.17651); + }); +}); diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index 2080706..e90dea6 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -14,8 +14,8 @@ import { ConflictException } from '@libs/exceptions'; const originWaypoint: WaypointDto = { position: 0, - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, houseNumber: '5', street: 'Avenue Foch', locality: 'Nancy', diff --git a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts index 37f294c..96bcabf 100644 --- a/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts +++ b/src/modules/ad/tests/unit/core/find-ad-by-id.query-handler.spec.ts @@ -20,8 +20,8 @@ const originWaypointProps: WaypointProps = { postalCode: '54000', country: 'France', coordinates: { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, }, }, }; diff --git a/src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts b/src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts new file mode 100644 index 0000000..c7e99d4 --- /dev/null +++ b/src/modules/ad/tests/unit/core/margin-durations.value-object.spec.ts @@ -0,0 +1,47 @@ +import { MarginDurations } from '@modules/ad/core/value-objects/margin-durations.value-object'; + +describe('Margin durations value object', () => { + it('should create a margin durations value object', () => { + const marginDurationsVO = new MarginDurations({ + mon: 600, + tue: 610, + wed: 620, + thu: 630, + fri: 640, + sat: 650, + sun: 660, + }); + expect(marginDurationsVO.mon).toBe(600); + expect(marginDurationsVO.tue).toBe(610); + expect(marginDurationsVO.wed).toBe(620); + expect(marginDurationsVO.thu).toBe(630); + expect(marginDurationsVO.fri).toBe(640); + expect(marginDurationsVO.sat).toBe(650); + expect(marginDurationsVO.sun).toBe(660); + }); + it('should update margin durations value object values', () => { + const marginDurationsVO = new MarginDurations({ + mon: 600, + tue: 610, + wed: 620, + thu: 630, + fri: 640, + sat: 650, + sun: 660, + }); + marginDurationsVO.mon = 700; + marginDurationsVO.tue = 710; + marginDurationsVO.wed = 720; + marginDurationsVO.thu = 730; + marginDurationsVO.fri = 740; + marginDurationsVO.sat = 750; + marginDurationsVO.sun = 760; + expect(marginDurationsVO.mon).toBe(700); + expect(marginDurationsVO.tue).toBe(710); + expect(marginDurationsVO.wed).toBe(720); + expect(marginDurationsVO.thu).toBe(730); + expect(marginDurationsVO.fri).toBe(740); + expect(marginDurationsVO.sat).toBe(750); + expect(marginDurationsVO.sun).toBe(760); + }); +}); diff --git a/src/modules/ad/tests/unit/core/schedule.value-object.spec.ts b/src/modules/ad/tests/unit/core/schedule.value-object.spec.ts new file mode 100644 index 0000000..b4d14bd --- /dev/null +++ b/src/modules/ad/tests/unit/core/schedule.value-object.spec.ts @@ -0,0 +1,22 @@ +import { Schedule } from '@modules/ad/core/value-objects/schedule.value-object'; + +describe('Schedule value object', () => { + it('should create a schedule value object', () => { + const scheduleVO = new Schedule({ + mon: '07:00', + tue: '07:05', + wed: '07:10', + thu: '07:15', + fri: '07:20', + sat: '07:25', + sun: '07:30', + }); + expect(scheduleVO.mon).toBe('07:00'); + expect(scheduleVO.tue).toBe('07:05'); + expect(scheduleVO.wed).toBe('07:10'); + expect(scheduleVO.thu).toBe('07:15'); + expect(scheduleVO.fri).toBe('07:20'); + expect(scheduleVO.sat).toBe('07:25'); + expect(scheduleVO.sun).toBe('07:30'); + }); +}); diff --git a/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts b/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts new file mode 100644 index 0000000..ed4b842 --- /dev/null +++ b/src/modules/ad/tests/unit/core/waypoint.value-object.spec.ts @@ -0,0 +1,22 @@ +import { Waypoint } from '@modules/ad/core/value-objects/waypoint.value-object'; + +describe('Waypoint value object', () => { + it('should create a waypoint value object', () => { + const waypointVO = new Waypoint({ + position: 0, + address: { + houseNumber: '5', + street: 'rue de la monnaie', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + coordinates: { + lon: 48.689445, + lat: 6.17651, + }, + }, + }); + expect(waypointVO.position).toBe(0); + expect(waypointVO.address.country).toBe('France'); + }); +}); diff --git a/src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts b/src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts new file mode 100644 index 0000000..88b9e48 --- /dev/null +++ b/src/modules/ad/tests/unit/infrastructure/ad.repository.spec.ts @@ -0,0 +1,88 @@ +import { PrismaService } from '@libs/db/prisma.service'; +import { + PARAMS_PROVIDER, + TIMEZONE_FINDER, + TIME_CONVERTER, +} from '@modules/ad/ad.di-tokens'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; +import { TimeConverterPort } from '@modules/ad/core/ports/time-converter.port'; +import { TimezoneFinderPort } from '@modules/ad/core/ports/timezone-finder.port'; +import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; +import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter'; +import { Test, TestingModule } from '@nestjs/testing'; + +const mockDefaultParamsProvider: DefaultParamsProviderPort = { + getParams: () => { + return { + MON_MARGIN: 900, + TUE_MARGIN: 900, + WED_MARGIN: 900, + THU_MARGIN: 900, + FRI_MARGIN: 900, + SAT_MARGIN: 900, + SUN_MARGIN: 900, + DRIVER: false, + SEATS_PROPOSED: 3, + PASSENGER: true, + SEATS_REQUESTED: 1, + STRICT: false, + DEFAULT_TIMEZONE: 'Europe/Paris', + }; + }, +}; + +const mockTimezoneFinder: TimezoneFinderPort = { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + timezones: jest.fn().mockImplementation((lon: number, lat: number) => { + if (lon < 60) return 'Europe/Paris'; + return 'America/New_York'; + }), +}; + +const mockTimeConverter: TimeConverterPort = { + localDateTimeToUtc: jest + .fn() + // eslint-disable-next-line @typescript-eslint/no-unused-vars + .mockImplementation((datetime: Date, timezone: string, dst?: boolean) => { + return datetime; + }), + utcDatetimeToLocalTime: jest.fn(), +}; + +describe('Ad repository', () => { + let prismaService: PrismaService; + let adMapper: AdMapper; + let eventEmitter: EventEmitter2; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + imports: [EventEmitterModule.forRoot()], + providers: [ + PrismaService, + AdMapper, + { + provide: PARAMS_PROVIDER, + useValue: mockDefaultParamsProvider, + }, + { + provide: TIMEZONE_FINDER, + useValue: mockTimezoneFinder, + }, + { + provide: TIME_CONVERTER, + useValue: mockTimeConverter, + }, + ], + }).compile(); + + prismaService = module.get(PrismaService); + adMapper = module.get(AdMapper); + eventEmitter = module.get(EventEmitter2); + }); + it('should be defined', () => { + expect( + new AdRepository(prismaService, adMapper, eventEmitter), + ).toBeDefined(); + }); +}); diff --git a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts index 136e272..f5634e0 100644 --- a/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts +++ b/src/modules/ad/tests/unit/infrastructure/time-converter.spec.ts @@ -51,6 +51,17 @@ describe('Time Converter', () => { ); expect(utcDatetime).toBeUndefined(); }); + it('should return undefined if date is undefined', () => { + const timeConverter: TimeConverter = new TimeConverter(); + const parisDate = undefined; + const parisTime = '08:00'; + const utcDatetime = timeConverter.localDateTimeToUtc( + parisDate, + parisTime, + 'Europe/Paris', + ); + expect(utcDatetime).toBeUndefined(); + }); }); describe('utcDatetimeToLocalTime', () => { diff --git a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts index ffc0272..c2a52b0 100644 --- a/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts +++ b/src/modules/ad/tests/unit/interface/has-valid-position-indexes.validator.spec.ts @@ -3,8 +3,8 @@ import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoin describe('addresses position validator', () => { const mockAddress1: WaypointDto = { - lon: 48.68944505415954, - lat: 6.176510296462267, + lon: 48.689445, + lat: 6.17651, houseNumber: '5', street: 'Avenue Foch', locality: 'Nancy', From f644d34068d8a180f5a69f26566386b83c7bb34e Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 11:02:16 +0200 Subject: [PATCH 11/29] removed oxide dependecy, added create ad grpc controller test --- package-lock.json | 226 +++++++++--------- package.json | 1 - .../exceptions/rpc-exception.codes.enum.ts | 19 ++ src/modules/ad/core/ad.errors.ts | 4 +- .../commands/create-ad/create-ad.service.ts | 11 +- .../create-ad.grpc.controller.ts | 34 ++- .../tests/unit/core/create-ad.service.spec.ts | 18 +- .../create-ad.grpc.controller.spec.ts | 106 ++++++++ 8 files changed, 265 insertions(+), 154 deletions(-) create mode 100644 src/libs/exceptions/rpc-exception.codes.enum.ts create mode 100644 src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts diff --git a/package-lock.json b/package-lock.json index 45f60b1..7d0b76b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,7 +31,6 @@ "geo-tz": "^7.0.7", "ioredis": "^5.3.2", "nestjs-request-context": "^2.1.0", - "oxide.ts": "^1.1.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.2.0", "timezonecomplete": "^5.12.4" @@ -215,42 +214,42 @@ } }, "node_modules/@babel/code-frame": { - "version": "7.21.4", - "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.21.4.tgz", - "integrity": "sha512-LYvhNKfwWSPpocw8GI7gpK2nq3HSDuEPC/uSYaALSJu9xjsalaaYFOq0Pwt5KmVqwEbZlDu81aLXwBOmD/Fv9g==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.5.tgz", + "integrity": "sha512-Xmwn266vad+6DAqEB2A6V/CcZVp62BbwVmcOJc2RPuwih1kw02TjQvWVWlcKGbBPd+8/0V5DEkOcizRGYsspYQ==", "dev": true, "dependencies": { - "@babel/highlight": "^7.18.6" + "@babel/highlight": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/compat-data": { - "version": "7.22.3", - "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.22.3.tgz", - "integrity": "sha512-aNtko9OPOwVESUFp3MZfD8Uzxl7JzSeJpd7npIoxCasU37PFbAQRpKglkaKwlHOyeJdrREpo8TW8ldrkYWwvIQ==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/compat-data/-/compat-data-7.22.5.tgz", + "integrity": "sha512-4Jc/YuIaYqKnDDz892kPIledykKg12Aw1PYX5i/TY28anJtacvM1Rrr8wbieB9GfEJwlzqT0hUEao0CxEebiDA==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/core": { - "version": "7.22.1", - "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.22.1.tgz", - "integrity": "sha512-Hkqu7J4ynysSXxmAahpN1jjRwVJ+NdpraFLIWflgjpVob3KNyK3/tIUc7Q7szed8WMp0JNa7Qtd1E9Oo22F9gA==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/core/-/core-7.22.5.tgz", + "integrity": "sha512-SBuTAjg91A3eKOvD+bPEz3LlhHZRNu1nFOVts9lzDJTXshHTjII0BAtDS3Y2DAkdZdDKWVZGVwkDfc4Clxn1dg==", "dev": true, "dependencies": { "@ampproject/remapping": "^2.2.0", - "@babel/code-frame": "^7.21.4", - "@babel/generator": "^7.22.0", - "@babel/helper-compilation-targets": "^7.22.1", - "@babel/helper-module-transforms": "^7.22.1", - "@babel/helpers": "^7.22.0", - "@babel/parser": "^7.22.0", - "@babel/template": "^7.21.9", - "@babel/traverse": "^7.22.1", - "@babel/types": "^7.22.0", + "@babel/code-frame": "^7.22.5", + "@babel/generator": "^7.22.5", + "@babel/helper-compilation-targets": "^7.22.5", + "@babel/helper-module-transforms": "^7.22.5", + "@babel/helpers": "^7.22.5", + "@babel/parser": "^7.22.5", + "@babel/template": "^7.22.5", + "@babel/traverse": "^7.22.5", + "@babel/types": "^7.22.5", "convert-source-map": "^1.7.0", "debug": "^4.1.0", "gensync": "^1.0.0-beta.2", @@ -281,12 +280,12 @@ } }, "node_modules/@babel/generator": { - "version": "7.22.3", - "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.22.3.tgz", - "integrity": "sha512-C17MW4wlk//ES/CJDL51kPNwl+qiBQyN7b9SKyVp11BLGFeSPoVaHrv+MNt8jwQFhQWowW88z1eeBx3pFz9v8A==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.22.5.tgz", + "integrity": "sha512-+lcUbnTRhd0jOewtFSedLyiPsD5tswKkbgcezOqqWFUVNEwoUTlpPOBmvhG7OXWLR4jMdv0czPGH5XbflnD1EA==", "dev": true, "dependencies": { - "@babel/types": "^7.22.3", + "@babel/types": "^7.22.5", "@jridgewell/gen-mapping": "^0.3.2", "@jridgewell/trace-mapping": "^0.3.17", "jsesc": "^2.5.1" @@ -296,13 +295,13 @@ } }, "node_modules/@babel/helper-compilation-targets": { - "version": "7.22.1", - "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.22.1.tgz", - "integrity": "sha512-Rqx13UM3yVB5q0D/KwQ8+SPfX/+Rnsy1Lw1k/UwOC4KC6qrzIQoY3lYnBu5EHKBlEHHcj0M0W8ltPSkD8rqfsQ==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-compilation-targets/-/helper-compilation-targets-7.22.5.tgz", + "integrity": "sha512-Ji+ywpHeuqxB8WDxraCiqR0xfhYjiDE/e6k7FuIaANnoOFxAHskHChz4vA1mJC9Lbm01s1PVAGhQY4FUKSkGZw==", "dev": true, "dependencies": { - "@babel/compat-data": "^7.22.0", - "@babel/helper-validator-option": "^7.21.0", + "@babel/compat-data": "^7.22.5", + "@babel/helper-validator-option": "^7.22.5", "browserslist": "^4.21.3", "lru-cache": "^5.1.1", "semver": "^6.3.0" @@ -324,65 +323,65 @@ } }, "node_modules/@babel/helper-environment-visitor": { - "version": "7.22.1", - "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.1.tgz", - "integrity": "sha512-Z2tgopurB/kTbidvzeBrc2To3PUP/9i5MUe+fU6QJCQDyPwSH2oRapkLw3KGECDYSjhQZCNxEvNvZlLw8JjGwA==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-environment-visitor/-/helper-environment-visitor-7.22.5.tgz", + "integrity": "sha512-XGmhECfVA/5sAt+H+xpSg0mfrHq6FzNr9Oxh7PSEBBRUb/mL7Kz3NICXb194rCqAEdxkhPT1a88teizAFyvk8Q==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-function-name": { - "version": "7.21.0", - "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.21.0.tgz", - "integrity": "sha512-HfK1aMRanKHpxemaY2gqBmL04iAPOPRj7DxtNbiDOrJK+gdwkiNRVpCpUJYbUT+aZyemKN8brqTOxzCaG6ExRg==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.22.5.tgz", + "integrity": "sha512-wtHSq6jMRE3uF2otvfuD3DIvVhOsSNshQl0Qrd7qC9oQJzHvOL4qQXlQn2916+CXGywIjpGuIkoyZRRxHPiNQQ==", "dev": true, "dependencies": { - "@babel/template": "^7.20.7", - "@babel/types": "^7.21.0" + "@babel/template": "^7.22.5", + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-hoist-variables": { - "version": "7.18.6", - "resolved": "https://registry.npmjs.org/@babel/helper-hoist-variables/-/helper-hoist-variables-7.18.6.tgz", - "integrity": "sha512-UlJQPkFqFULIcyW5sbzgbkxn2FKRgwWiRexcuaR8RNJRy8+LLveqPjwZV/bwrLZCN0eUHD/x8D0heK1ozuoo6Q==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-hoist-variables/-/helper-hoist-variables-7.22.5.tgz", + "integrity": "sha512-wGjk9QZVzvknA6yKIUURb8zY3grXCcOZt+/7Wcy8O2uctxhplmUPkOdlgoNhmdVee2c92JXbf1xpMtVNbfoxRw==", "dev": true, "dependencies": { - "@babel/types": "^7.18.6" + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-module-imports": { - "version": "7.21.4", - "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.21.4.tgz", - "integrity": "sha512-orajc5T2PsRYUN3ZryCEFeMDYwyw09c/pZeaQEZPH0MpKzSvn3e0uXsDBu3k03VI+9DBiRo+l22BfKTpKwa/Wg==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-module-imports/-/helper-module-imports-7.22.5.tgz", + "integrity": "sha512-8Dl6+HD/cKifutF5qGd/8ZJi84QeAKh+CEe1sBzz8UayBBGg1dAIJrdHOcOM5b2MpzWL2yuotJTtGjETq0qjXg==", "dev": true, "dependencies": { - "@babel/types": "^7.21.4" + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-module-transforms": { - "version": "7.22.1", - "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.22.1.tgz", - "integrity": "sha512-dxAe9E7ySDGbQdCVOY/4+UcD8M9ZFqZcZhSPsPacvCG4M+9lwtDDQfI2EoaSvmf7W/8yCBkGU0m7Pvt1ru3UZw==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.22.5.tgz", + "integrity": "sha512-+hGKDt/Ze8GFExiVHno/2dvG5IdstpzCq0y4Qc9OJ25D4q3pKfiIP/4Vp3/JvhDkLKsDK2api3q3fpIgiIF5bw==", "dev": true, "dependencies": { - "@babel/helper-environment-visitor": "^7.22.1", - "@babel/helper-module-imports": "^7.21.4", - "@babel/helper-simple-access": "^7.21.5", - "@babel/helper-split-export-declaration": "^7.18.6", - "@babel/helper-validator-identifier": "^7.19.1", - "@babel/template": "^7.21.9", - "@babel/traverse": "^7.22.1", - "@babel/types": "^7.22.0" + "@babel/helper-environment-visitor": "^7.22.5", + "@babel/helper-module-imports": "^7.22.5", + "@babel/helper-simple-access": "^7.22.5", + "@babel/helper-split-export-declaration": "^7.22.5", + "@babel/helper-validator-identifier": "^7.22.5", + "@babel/template": "^7.22.5", + "@babel/traverse": "^7.22.5", + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" @@ -398,77 +397,77 @@ } }, "node_modules/@babel/helper-simple-access": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.21.5.tgz", - "integrity": "sha512-ENPDAMC1wAjR0uaCUwliBdiSl1KBJAVnMTzXqi64c2MG8MPR6ii4qf7bSXDqSFbr4W6W028/rf5ivoHop5/mkg==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-simple-access/-/helper-simple-access-7.22.5.tgz", + "integrity": "sha512-n0H99E/K+Bika3++WNL17POvo4rKWZ7lZEp1Q+fStVbUi8nxPQEBOlTmCOxW/0JsS56SKKQ+ojAe2pHKJHN35w==", "dev": true, "dependencies": { - "@babel/types": "^7.21.5" + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-split-export-declaration": { - "version": "7.18.6", - "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.18.6.tgz", - "integrity": "sha512-bde1etTx6ZyTmobl9LLMMQsaizFVZrquTEHOqKeQESMKo4PlObf+8+JA25ZsIpZhT/WEd39+vOdLXAFG/nELpA==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.22.5.tgz", + "integrity": "sha512-thqK5QFghPKWLhAV321lxF95yCg2K3Ob5yw+M3VHWfdia0IkPXUtoLH8x/6Fh486QUvzhb8YOWHChTVen2/PoQ==", "dev": true, "dependencies": { - "@babel/types": "^7.18.6" + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-string-parser": { - "version": "7.21.5", - "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.21.5.tgz", - "integrity": "sha512-5pTUx3hAJaZIdW99sJ6ZUUgWq/Y+Hja7TowEnLNMm1VivRgZQL3vpBY3qUACVsvw+yQU6+YgfBVmcbLaZtrA1w==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-string-parser/-/helper-string-parser-7.22.5.tgz", + "integrity": "sha512-mM4COjgZox8U+JcXQwPijIZLElkgEpO5rsERVDJTc2qfCDfERyob6k5WegS14SX18IIjv+XD+GrqNumY5JRCDw==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-validator-identifier": { - "version": "7.19.1", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.19.1.tgz", - "integrity": "sha512-awrNfaMtnHUr653GgGEs++LlAvW6w+DcPrOliSMXWCKo597CwL5Acf/wWdNkf/tfEQE3mjkeD1YOVZOUV/od1w==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.5.tgz", + "integrity": "sha512-aJXu+6lErq8ltp+JhkJUfk1MTGyuA4v7f3pA+BJ5HLfNC6nAQ0Cpi9uOquUj8Hehg0aUiHzWQbOVJGao6ztBAQ==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helper-validator-option": { - "version": "7.21.0", - "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.21.0.tgz", - "integrity": "sha512-rmL/B8/f0mKS2baE9ZpyTcTavvEuWhTTW8amjzXNvYG4AwBsqTLikfXsEofsJEfKHf+HQVQbFOHy6o+4cnC/fQ==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helper-validator-option/-/helper-validator-option-7.22.5.tgz", + "integrity": "sha512-R3oB6xlIVKUnxNUxbmgq7pKjxpru24zlimpE8WK47fACIlM0II/Hm1RS8IaOI7NgCr6LNS+jl5l75m20npAziw==", "dev": true, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/helpers": { - "version": "7.22.3", - "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.22.3.tgz", - "integrity": "sha512-jBJ7jWblbgr7r6wYZHMdIqKc73ycaTcCaWRq4/2LpuPHcx7xMlZvpGQkOYc9HeSjn6rcx15CPlgVcBtZ4WZJ2w==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.22.5.tgz", + "integrity": "sha512-pSXRmfE1vzcUIDFQcSGA5Mr+GxBV9oiRKDuDxXvWQQBCh8HoIjs/2DlDB7H8smac1IVrB9/xdXj2N3Wol9Cr+Q==", "dev": true, "dependencies": { - "@babel/template": "^7.21.9", - "@babel/traverse": "^7.22.1", - "@babel/types": "^7.22.3" + "@babel/template": "^7.22.5", + "@babel/traverse": "^7.22.5", + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/highlight": { - "version": "7.18.6", - "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.18.6.tgz", - "integrity": "sha512-u7stbOuYjaPezCuLj29hNW1v64M2Md2qupEKP1fHc7WdOA3DgLh37suiSrZYY7haUB7iBeQZ9P1uiRF359do3g==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.22.5.tgz", + "integrity": "sha512-BSKlD1hgnedS5XRnGOljZawtag7H1yPfQp0tdNJCHoH6AZ+Pcm9VvkrK59/Yy593Ypg0zMxH2BxD1VPYUQ7UIw==", "dev": true, "dependencies": { - "@babel/helper-validator-identifier": "^7.18.6", + "@babel/helper-validator-identifier": "^7.22.5", "chalk": "^2.0.0", "js-tokens": "^4.0.0" }, @@ -548,9 +547,9 @@ } }, "node_modules/@babel/parser": { - "version": "7.22.4", - "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.4.tgz", - "integrity": "sha512-VLLsx06XkEYqBtE5YGPwfSGwfrjnyPP5oiGty3S8pQLFDFLaS8VwWSIxkTXpcvr5zeYLE6+MBNl2npl/YnfofA==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.22.5.tgz", + "integrity": "sha512-DFZMC9LJUG9PLOclRC32G63UXwzqS2koQC8dkx+PLdmt1xSePYpbT/NbsrJy8Q/muXz7o/h/d4A7Fuyixm559Q==", "dev": true, "bin": { "parser": "bin/babel-parser.js" @@ -737,33 +736,33 @@ } }, "node_modules/@babel/template": { - "version": "7.21.9", - "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.21.9.tgz", - "integrity": "sha512-MK0X5k8NKOuWRamiEfc3KEJiHMTkGZNUjzMipqCGDDc6ijRl/B7RGSKVGncu4Ro/HdyzzY6cmoXuKI2Gffk7vQ==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.22.5.tgz", + "integrity": "sha512-X7yV7eiwAxdj9k94NEylvbVHLiVG1nvzCV2EAowhxLTwODV1jl9UzZ48leOC0sH7OnuHrIkllaBgneUykIcZaw==", "dev": true, "dependencies": { - "@babel/code-frame": "^7.21.4", - "@babel/parser": "^7.21.9", - "@babel/types": "^7.21.5" + "@babel/code-frame": "^7.22.5", + "@babel/parser": "^7.22.5", + "@babel/types": "^7.22.5" }, "engines": { "node": ">=6.9.0" } }, "node_modules/@babel/traverse": { - "version": "7.22.4", - "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.4.tgz", - "integrity": "sha512-Tn1pDsjIcI+JcLKq1AVlZEr4226gpuAQTsLMorsYg9tuS/kG7nuwwJ4AB8jfQuEgb/COBwR/DqJxmoiYFu5/rQ==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.22.5.tgz", + "integrity": "sha512-7DuIjPgERaNo6r+PZwItpjCZEa5vyw4eJGufeLxrPdBXBoLcCJCIasvK6pK/9DVNrLZTLFhUGqaC6X/PA007TQ==", "dev": true, "dependencies": { - "@babel/code-frame": "^7.21.4", - "@babel/generator": "^7.22.3", - "@babel/helper-environment-visitor": "^7.22.1", - "@babel/helper-function-name": "^7.21.0", - "@babel/helper-hoist-variables": "^7.18.6", - "@babel/helper-split-export-declaration": "^7.18.6", - "@babel/parser": "^7.22.4", - "@babel/types": "^7.22.4", + "@babel/code-frame": "^7.22.5", + "@babel/generator": "^7.22.5", + "@babel/helper-environment-visitor": "^7.22.5", + "@babel/helper-function-name": "^7.22.5", + "@babel/helper-hoist-variables": "^7.22.5", + "@babel/helper-split-export-declaration": "^7.22.5", + "@babel/parser": "^7.22.5", + "@babel/types": "^7.22.5", "debug": "^4.1.0", "globals": "^11.1.0" }, @@ -781,13 +780,13 @@ } }, "node_modules/@babel/types": { - "version": "7.22.4", - "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.4.tgz", - "integrity": "sha512-Tx9x3UBHTTsMSW85WB2kphxYQVvrZ/t1FxD88IpSgIjiUJlCm9z+xWIDwyo1vffTwSqteqyznB8ZE9vYYk16zA==", + "version": "7.22.5", + "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.22.5.tgz", + "integrity": "sha512-zo3MIHGOkPOfoRXitsgHLjEXmlDaD/5KU1Uzuc9GNiZPhSqVxVRtxuPaSBZDsYZ9qV88AjtMtWW7ww98loJ9KA==", "dev": true, "dependencies": { - "@babel/helper-string-parser": "^7.21.5", - "@babel/helper-validator-identifier": "^7.19.1", + "@babel/helper-string-parser": "^7.22.5", + "@babel/helper-validator-identifier": "^7.22.5", "to-fast-properties": "^2.0.0" }, "engines": { @@ -6918,11 +6917,6 @@ "node": ">=0.10.0" } }, - "node_modules/oxide.ts": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/oxide.ts/-/oxide.ts-1.1.0.tgz", - "integrity": "sha512-+MkqFRQVHEe/x4/cJ6KuYz2m2VpnoBi7aKLbttGYTxmpNZalQ2RbKH2HxyfsTqXJhjh9DYxulPWfQV/hWMmzCg==" - }, "node_modules/p-limit": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-3.1.0.tgz", @@ -7788,9 +7782,9 @@ "dev": true }, "node_modules/semver": { - "version": "7.5.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.1.tgz", - "integrity": "sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==", + "version": "7.5.3", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.3.tgz", + "integrity": "sha512-QBlUtyVk/5EeHbi7X0fw6liDZc7BBmEaSYn01fMU1OUYbf6GPsbTtd8WmnqbI20SeycoHSeiybkE/q1Q+qlThQ==", "dev": true, "dependencies": { "lru-cache": "^6.0.0" diff --git a/package.json b/package.json index dc70fd3..bd1ef20 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "geo-tz": "^7.0.7", "ioredis": "^5.3.2", "nestjs-request-context": "^2.1.0", - "oxide.ts": "^1.1.0", "reflect-metadata": "^0.1.13", "rxjs": "^7.2.0", "timezonecomplete": "^5.12.4" diff --git a/src/libs/exceptions/rpc-exception.codes.enum.ts b/src/libs/exceptions/rpc-exception.codes.enum.ts new file mode 100644 index 0000000..034391c --- /dev/null +++ b/src/libs/exceptions/rpc-exception.codes.enum.ts @@ -0,0 +1,19 @@ +export enum RpcExceptionCode { + OK = 0, + CANCELLED = 1, + UNKNOWN = 2, + INVALID_ARGUMENT = 3, + DEADLINE_EXCEEDED = 4, + NOT_FOUND = 5, + ALREADY_EXISTS = 6, + PERMISSION_DENIED = 7, + RESOURCE_EXHAUSTED = 8, + FAILED_PRECONDITION = 9, + ABORTED = 10, + OUT_OF_RANGE = 11, + UNIMPLEMENTED = 12, + INTERNAL = 13, + UNAVAILABLE = 14, + DATA_LOSS = 15, + UNAUTHENTICATED = 16, +} diff --git a/src/modules/ad/core/ad.errors.ts b/src/modules/ad/core/ad.errors.ts index 474edf9..ec53993 100644 --- a/src/modules/ad/core/ad.errors.ts +++ b/src/modules/ad/core/ad.errors.ts @@ -1,11 +1,11 @@ import { ExceptionBase } from '@libs/exceptions'; -export class AdAlreadyExistsError extends ExceptionBase { +export class AdAlreadyExistsException extends ExceptionBase { static readonly message = 'Ad already exists'; public readonly code = 'AD.ALREADY_EXISTS'; constructor(cause?: Error, metadata?: unknown) { - super(AdAlreadyExistsError.message, cause, metadata); + super(AdAlreadyExistsException.message, cause, metadata); } } diff --git a/src/modules/ad/core/commands/create-ad/create-ad.service.ts b/src/modules/ad/core/commands/create-ad/create-ad.service.ts index 1a618e9..b85affe 100644 --- a/src/modules/ad/core/commands/create-ad/create-ad.service.ts +++ b/src/modules/ad/core/commands/create-ad/create-ad.service.ts @@ -5,9 +5,8 @@ import { Inject } from '@nestjs/common'; import { AD_REPOSITORY, PARAMS_PROVIDER } from '@modules/ad/ad.di-tokens'; import { AdRepositoryPort } from '@modules/ad/core/ports/ad.repository.port'; import { DefaultParamsProviderPort } from '@modules/ad/core/ports/default-params-provider.port'; -import { Err, Ok, Result } from 'oxide.ts'; import { AggregateID } from '@libs/ddd'; -import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; +import { AdAlreadyExistsException } from '@modules/ad/core/ad.errors'; import { AdEntity } from '@modules/ad/core/ad.entity'; import { ConflictException } from '@libs/exceptions'; import { Waypoint } from '../../types/waypoint'; @@ -25,9 +24,7 @@ export class CreateAdService implements ICommandHandler { this.defaultParams = defaultParamsProvider.getParams(); } - async execute( - command: CreateAdCommand, - ): Promise> { + async execute(command: CreateAdCommand): Promise { const ad = AdEntity.create( { userId: command.userId, @@ -77,10 +74,10 @@ export class CreateAdService implements ICommandHandler { try { await this.repository.insert(ad); - return Ok(ad.id); + return ad.id; } catch (error: any) { if (error instanceof ConflictException) { - return Err(new AdAlreadyExistsError(error)); + throw new AdAlreadyExistsException(error); } throw error; } diff --git a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts index 2e5ba6c..65a7112 100644 --- a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts @@ -4,10 +4,10 @@ import { GrpcMethod, RpcException } from '@nestjs/microservices'; import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; import { CreateAdRequestDto } from './dtos/create-ad.request.dto'; import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command'; -import { Result, match } from 'oxide.ts'; import { AggregateID } from '@libs/ddd'; -import { AdAlreadyExistsError } from '../../core/ad.errors'; +import { AdAlreadyExistsException } from '../../core/ad.errors'; import { IdResponse } from '@libs/api/id.response.dto'; +import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; @UsePipes( new RpcValidationPipe({ @@ -21,22 +21,18 @@ export class CreateAdGrpcController { @GrpcMethod('AdsService', 'Create') async create(data: CreateAdRequestDto): Promise { - const result: Result = - await this.commandBus.execute(new CreateAdCommand(data)); - - // Deciding what to do with a Result (similar to Rust matching) - // if Ok we return a response with an id - // if Error decide what to do with it depending on its type - return match(result, { - Ok: (id: string) => new IdResponse(id), - Err: (error: Error) => { - if (error instanceof AdAlreadyExistsError) - throw new RpcException({ - code: 6, - message: 'Ad already exists', - }); - throw new RpcException({}); - }, - }); + try { + const aggregateID: AggregateID = await this.commandBus.execute( + new CreateAdCommand(data), + ); + return new IdResponse(aggregateID); + } catch (error: any) { + if (error instanceof AdAlreadyExistsException) + throw new RpcException({ + code: RpcExceptionCode.ALREADY_EXISTS, + message: error.message, + }); + throw new RpcException({}); + } } } diff --git a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts index e90dea6..f2577f1 100644 --- a/src/modules/ad/tests/unit/core/create-ad.service.spec.ts +++ b/src/modules/ad/tests/unit/core/create-ad.service.spec.ts @@ -6,9 +6,8 @@ import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoin import { CreateAdRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto'; import { Frequency } from '@modules/ad/core/ad.types'; import { CreateAdCommand } from '@modules/ad/core/commands/create-ad/create-ad.command'; -import { Result } from 'oxide.ts'; import { AggregateID } from '@libs/ddd'; -import { AdAlreadyExistsError } from '@modules/ad/core/ad.errors'; +import { AdAlreadyExistsException } from '@modules/ad/core/ad.errors'; import { AdEntity } from '@modules/ad/core/ad.entity'; import { ConflictException } from '@libs/exceptions'; @@ -107,9 +106,10 @@ describe('create-ad.service', () => { AdEntity.create = jest.fn().mockReturnValue({ id: '047a6ecf-23d4-4d3e-877c-3225d560a8da', }); - const result: Result = - await createAdService.execute(createAdCommand); - expect(result.unwrap()).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da'); + const result: AggregateID = await createAdService.execute( + createAdCommand, + ); + expect(result).toBe('047a6ecf-23d4-4d3e-877c-3225d560a8da'); }); it('should throw an error if something bad happens', async () => { AdEntity.create = jest.fn().mockReturnValue({ @@ -119,13 +119,13 @@ describe('create-ad.service', () => { createAdService.execute(createAdCommand), ).rejects.toBeInstanceOf(Error); }); - it('should return an Err if Ad already exists', async () => { + it('should throw an exception if Ad already exists', async () => { AdEntity.create = jest.fn().mockReturnValue({ id: '047a6ecf-23d4-4d3e-877c-3225d560a8da', }); - const result: Result = - await createAdService.execute(createAdCommand); - expect(result.isErr()).toBeTruthy(); + await expect( + createAdService.execute(createAdCommand), + ).rejects.toBeInstanceOf(AdAlreadyExistsException); }); }); }); diff --git a/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts b/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts new file mode 100644 index 0000000..49c4f8e --- /dev/null +++ b/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts @@ -0,0 +1,106 @@ +import { IdResponse } from '@libs/api/id.response.dto'; +import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; +import { AdAlreadyExistsException } from '@modules/ad/core/ad.errors'; +import { Frequency } from '@modules/ad/core/ad.types'; +import { CreateAdGrpcController } from '@modules/ad/interface/grpc-controllers/create-ad.grpc.controller'; +import { CreateAdRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/create-ad.request.dto'; +import { WaypointDto } from '@modules/ad/interface/grpc-controllers/dtos/waypoint.dto'; +import { CommandBus } from '@nestjs/cqrs'; +import { RpcException } from '@nestjs/microservices'; +import { Test, TestingModule } from '@nestjs/testing'; + +const originWaypoint: WaypointDto = { + position: 0, + lon: 48.689445, + lat: 6.17651, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', +}; +const destinationWaypoint: WaypointDto = { + position: 1, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', +}; +const punctualCreateAdRequest: CreateAdRequestDto = { + userId: '4eb6a6af-ecfd-41c3-9118-473a507014d4', + fromDate: '2023-12-21', + toDate: '2023-12-21', + schedule: { + thu: '08:15', + }, + driver: false, + passenger: true, + seatsRequested: 1, + frequency: Frequency.PUNCTUAL, + waypoints: [originWaypoint, destinationWaypoint], +}; + +const mockCommandBus = { + execute: jest + .fn() + .mockImplementationOnce(() => '200d61a8-d878-4378-a609-c19ea71633d2') + .mockImplementationOnce(() => { + throw new AdAlreadyExistsException(); + }) + .mockImplementationOnce(() => { + throw new Error(); + }), +}; + +describe('Create Ad Grpc Controller', () => { + let createAdGrpcController: CreateAdGrpcController; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: CommandBus, + useValue: mockCommandBus, + }, + CreateAdGrpcController, + ], + }).compile(); + + createAdGrpcController = module.get( + CreateAdGrpcController, + ); + }); + + it('should be defined', () => { + expect(createAdGrpcController).toBeDefined(); + }); + + it('should create a new ad', async () => { + const result: IdResponse = await createAdGrpcController.create( + punctualCreateAdRequest, + ); + expect(result).toBeInstanceOf(IdResponse); + expect(result.id).toBe('200d61a8-d878-4378-a609-c19ea71633d2'); + }); + + it('should throw an dedicated RpcException if ad already exists', async () => { + expect.assertions(2); + try { + await createAdGrpcController.create(punctualCreateAdRequest); + } catch (e: any) { + expect(e).toBeInstanceOf(RpcException); + expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS); + } + }); + + it('should throw a generic RpcException', async () => { + expect.assertions(2); + try { + await createAdGrpcController.create(punctualCreateAdRequest); + } catch (e: any) { + expect(e).toBeInstanceOf(RpcException); + expect(e.error.code).toBeUndefined(); + } + }); +}); From d294049a284f58c9e1995bb4167cb5e3fbfb2013 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 11:57:01 +0200 Subject: [PATCH 12/29] improve tests, added findAdById grpc controller test --- package.json | 3 +- .../create-ad.grpc.controller.ts | 7 +- .../find-ad-by-id.grpc.controller.ts | 10 +- .../create-ad.grpc.controller.spec.ts | 18 ++- .../find-ad-by-id.grpc.controller.spec.ts | 140 ++++++++++++++++++ 5 files changed, 170 insertions(+), 8 deletions(-) create mode 100644 src/modules/ad/tests/unit/interface/find-ad-by-id.grpc.controller.spec.ts diff --git a/package.json b/package.json index bd1ef20..26bd1fb 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,8 @@ "moduleNameMapper": { "^@libs(.*)": "/libs/$1", "^@modules(.*)": "/modules/$1", - "^@src(.*)": "$1" + "^@src(.*)": "$1", + "^@utils(.*)": "utils/$1" }, "testEnvironment": "node" } diff --git a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts index 65a7112..91158b7 100644 --- a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts @@ -1,13 +1,13 @@ import { Controller, UsePipes } from '@nestjs/common'; import { CommandBus } from '@nestjs/cqrs'; import { GrpcMethod, RpcException } from '@nestjs/microservices'; -import { RpcValidationPipe } from '../../../../utils/pipes/rpc.validation-pipe'; import { CreateAdRequestDto } from './dtos/create-ad.request.dto'; import { CreateAdCommand } from '../../core/commands/create-ad/create-ad.command'; import { AggregateID } from '@libs/ddd'; import { AdAlreadyExistsException } from '../../core/ad.errors'; import { IdResponse } from '@libs/api/id.response.dto'; import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; +import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe'; @UsePipes( new RpcValidationPipe({ @@ -32,7 +32,10 @@ export class CreateAdGrpcController { code: RpcExceptionCode.ALREADY_EXISTS, message: error.message, }); - throw new RpcException({}); + throw new RpcException({ + code: RpcExceptionCode.UNKNOWN, + message: error.message, + }); } } } diff --git a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts index b60008f..f7d5be3 100644 --- a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts @@ -7,6 +7,8 @@ import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad- import { AdResponseDto } from '../dtos/ad.response.dto'; import { AdEntity } from '@modules/ad/core/ad.entity'; import { AdMapper } from '@modules/ad/ad.mapper'; +import { NotFoundException } from '@libs/exceptions'; +import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; @UsePipes( new RpcValidationPipe({ @@ -29,8 +31,14 @@ export class FindAdByIdGrpcController { ); return this.mapper.toResponse(ad); } catch (e) { + if (e instanceof NotFoundException) { + throw new RpcException({ + code: RpcExceptionCode.NOT_FOUND, + message: e.message, + }); + } throw new RpcException({ - code: e.code, + code: RpcExceptionCode.UNKNOWN, message: e.message, }); } diff --git a/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts b/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts index 49c4f8e..e0bc8e3 100644 --- a/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts +++ b/src/modules/ad/tests/unit/interface/create-ad.grpc.controller.spec.ts @@ -72,35 +72,45 @@ describe('Create Ad Grpc Controller', () => { ); }); + afterEach(async () => { + jest.clearAllMocks(); + }); + it('should be defined', () => { expect(createAdGrpcController).toBeDefined(); }); it('should create a new ad', async () => { + jest.spyOn(mockCommandBus, 'execute'); const result: IdResponse = await createAdGrpcController.create( punctualCreateAdRequest, ); expect(result).toBeInstanceOf(IdResponse); expect(result.id).toBe('200d61a8-d878-4378-a609-c19ea71633d2'); + expect(mockCommandBus.execute).toHaveBeenCalledTimes(1); }); - it('should throw an dedicated RpcException if ad already exists', async () => { - expect.assertions(2); + it('should throw a dedicated RpcException if ad already exists', async () => { + jest.spyOn(mockCommandBus, 'execute'); + expect.assertions(3); try { await createAdGrpcController.create(punctualCreateAdRequest); } catch (e: any) { expect(e).toBeInstanceOf(RpcException); expect(e.error.code).toBe(RpcExceptionCode.ALREADY_EXISTS); } + expect(mockCommandBus.execute).toHaveBeenCalledTimes(1); }); it('should throw a generic RpcException', async () => { - expect.assertions(2); + jest.spyOn(mockCommandBus, 'execute'); + expect.assertions(3); try { await createAdGrpcController.create(punctualCreateAdRequest); } catch (e: any) { expect(e).toBeInstanceOf(RpcException); - expect(e.error.code).toBeUndefined(); + expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN); } + expect(mockCommandBus.execute).toHaveBeenCalledTimes(1); }); }); diff --git a/src/modules/ad/tests/unit/interface/find-ad-by-id.grpc.controller.spec.ts b/src/modules/ad/tests/unit/interface/find-ad-by-id.grpc.controller.spec.ts new file mode 100644 index 0000000..b86dd3e --- /dev/null +++ b/src/modules/ad/tests/unit/interface/find-ad-by-id.grpc.controller.spec.ts @@ -0,0 +1,140 @@ +import { NotFoundException } from '@libs/exceptions'; +import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; +import { AdMapper } from '@modules/ad/ad.mapper'; +import { Frequency } from '@modules/ad/core/ad.types'; +import { FindAdByIdGrpcController } from '@modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller'; +import { QueryBus } from '@nestjs/cqrs'; +import { RpcException } from '@nestjs/microservices'; +import { Test, TestingModule } from '@nestjs/testing'; + +const mockQueryBus = { + execute: jest + .fn() + .mockImplementationOnce(() => '200d61a8-d878-4378-a609-c19ea71633d2') + .mockImplementationOnce(() => { + throw new NotFoundException(); + }) + .mockImplementationOnce(() => { + throw new Error(); + }), +}; + +const mockAdMapper = { + toResponse: jest.fn().mockImplementationOnce(() => ({ + userId: '8cc90d1a-4a59-4289-a7d8-078f9db7857f', + driver: true, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: '2023-06-27', + toDate: '2023-06-27', + schedule: { + tue: '07:15', + }, + marginDurations: { + mon: 900, + tue: 900, + wed: 900, + thu: 900, + fri: 900, + sat: 900, + sun: 900, + }, + seatsProposed: 3, + seatsRequested: 1, + waypoints: [ + { + position: 0, + lon: 48.689445, + lat: 6.17651, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + }, + { + position: 1, + lon: 48.8566, + lat: 2.3522, + locality: 'Paris', + postalCode: '75000', + country: 'France', + }, + ], + })), +}; + +describe('Find Ad By Id Grpc Controller', () => { + let findAdbyIdGrpcController: FindAdByIdGrpcController; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: QueryBus, + useValue: mockQueryBus, + }, + { + provide: AdMapper, + useValue: mockAdMapper, + }, + FindAdByIdGrpcController, + ], + }).compile(); + + findAdbyIdGrpcController = module.get( + FindAdByIdGrpcController, + ); + }); + + afterEach(async () => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(findAdbyIdGrpcController).toBeDefined(); + }); + + it('should return an ad', async () => { + jest.spyOn(mockQueryBus, 'execute'); + jest.spyOn(mockAdMapper, 'toResponse'); + const response = await findAdbyIdGrpcController.findOnebyId({ + id: '6dcf093c-c7db-4dae-8e9c-c715cebf83c7', + }); + expect(response.userId).toBe('8cc90d1a-4a59-4289-a7d8-078f9db7857f'); + expect(mockQueryBus.execute).toHaveBeenCalledTimes(1); + expect(mockAdMapper.toResponse).toHaveBeenCalledTimes(1); + }); + + it('should throw a dedicated RpcException if ad is not found', async () => { + jest.spyOn(mockQueryBus, 'execute'); + jest.spyOn(mockAdMapper, 'toResponse'); + expect.assertions(4); + try { + await findAdbyIdGrpcController.findOnebyId({ + id: 'ac85f5f4-41cd-4c5d-9aee-0a1acb176fb8', + }); + } catch (e: any) { + expect(e).toBeInstanceOf(RpcException); + expect(e.error.code).toBe(RpcExceptionCode.NOT_FOUND); + } + expect(mockQueryBus.execute).toHaveBeenCalledTimes(1); + expect(mockAdMapper.toResponse).toHaveBeenCalledTimes(0); + }); + + it('should throw a generic RpcException', async () => { + jest.spyOn(mockQueryBus, 'execute'); + jest.spyOn(mockAdMapper, 'toResponse'); + expect.assertions(4); + try { + await findAdbyIdGrpcController.findOnebyId({ + id: '53c8e7ec-ef68-42bc-ba4c-5ef3effa60a6', + }); + } catch (e: any) { + expect(e).toBeInstanceOf(RpcException); + expect(e.error.code).toBe(RpcExceptionCode.UNKNOWN); + } + expect(mockQueryBus.execute).toHaveBeenCalledTimes(1); + expect(mockAdMapper.toResponse).toHaveBeenCalledTimes(0); + }); +}); From e407e915fa0a614aeb5694d0e0d424eb45b2536e Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 12:18:22 +0200 Subject: [PATCH 13/29] fixed queryRaw for healthcheck, added health grpc controller test --- src/libs/db/prisma-repository.base.ts | 2 +- .../health.grpc.controller.ts | 6 +- .../tests/unit/health.grpc.controller.spec.ts | 72 +++++++++++++++++++ 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 src/modules/health/tests/unit/health.grpc.controller.spec.ts diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index 9f8983d..897834a 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -53,7 +53,7 @@ export abstract class PrismaRepositoryBase< async healthCheck(): Promise { try { - await this.prisma.$queryRaw`SELECT 1`; + await this.prismaRaw.$queryRaw`SELECT 1`; return true; } catch (e) { if (e instanceof Prisma.PrismaClientKnownRequestError) { diff --git a/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts b/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts index 5aa260e..f939dd2 100644 --- a/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts +++ b/src/modules/health/interface/grpc-controllers/health.grpc.controller.ts @@ -2,7 +2,7 @@ import { Controller } from '@nestjs/common'; import { GrpcMethod } from '@nestjs/microservices'; import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; -enum ServingStatus { +export enum ServingStatus { UNKNOWN = 0, SERVING = 1, NOT_SERVING = 2, @@ -25,9 +25,9 @@ export class HealthGrpcController { @GrpcMethod('Health', 'Check') async check( // eslint-disable-next-line @typescript-eslint/no-unused-vars - data: HealthCheckRequest, + data?: HealthCheckRequest, // eslint-disable-next-line @typescript-eslint/no-unused-vars - metadata: any, + metadata?: any, ): Promise { const healthCheck = await this.repositoriesHealthIndicatorUseCase.isHealthy( 'repositories', diff --git a/src/modules/health/tests/unit/health.grpc.controller.spec.ts b/src/modules/health/tests/unit/health.grpc.controller.spec.ts new file mode 100644 index 0000000..0157218 --- /dev/null +++ b/src/modules/health/tests/unit/health.grpc.controller.spec.ts @@ -0,0 +1,72 @@ +import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase'; +import { + HealthGrpcController, + ServingStatus, +} from '@modules/health/interface/grpc-controllers/health.grpc.controller'; +import { Test, TestingModule } from '@nestjs/testing'; + +const mockRepositoriesHealthIndicatorUseCase = { + isHealthy: jest + .fn() + .mockImplementationOnce(() => ({ + repositories: { + status: 'up', + }, + })) + .mockImplementationOnce(() => ({ + repositories: { + status: 'down', + }, + })), +}; + +describe('Health Grpc Controller', () => { + let healthGrpcController: HealthGrpcController; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: RepositoriesHealthIndicatorUseCase, + useValue: mockRepositoriesHealthIndicatorUseCase, + }, + HealthGrpcController, + ], + }).compile(); + + healthGrpcController = + module.get(HealthGrpcController); + }); + + afterEach(async () => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(healthGrpcController).toBeDefined(); + }); + + it('should return a Serving status ', async () => { + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + const servingStatus: { status: ServingStatus } = + await healthGrpcController.check(); + expect(servingStatus).toEqual({ + status: ServingStatus.SERVING, + }); + expect( + mockRepositoriesHealthIndicatorUseCase.isHealthy, + ).toHaveBeenCalledTimes(1); + }); + + it('should return a Not Serving status ', async () => { + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + const servingStatus: { status: ServingStatus } = + await healthGrpcController.check(); + expect(servingStatus).toEqual({ + status: ServingStatus.NOT_SERVING, + }); + expect( + mockRepositoriesHealthIndicatorUseCase.isHealthy, + ).toHaveBeenCalledTimes(1); + }); +}); From 6f2305ae6a33d75a3304f4d048b30cf7ffda16f9 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 14:02:10 +0200 Subject: [PATCH 14/29] add health tests; move utils to libs --- package.json | 3 +- .../unit/rpc-validation-pipe.usecase.spec.ts | 0 .../utils/pipes/rpc.validation-pipe.ts | 3 +- .../create-ad.grpc.controller.ts | 2 +- .../find-ad-by-id.grpc.controller.ts | 2 +- .../health.http.controller.ts | 10 ++- .../tests/unit/health.http.controller.spec.ts | 90 +++++++++++++++++++ tsconfig.json | 1 - 8 files changed, 102 insertions(+), 9 deletions(-) rename src/{utils => libs}/tests/unit/rpc-validation-pipe.usecase.spec.ts (100%) rename src/{ => libs}/utils/pipes/rpc.validation-pipe.ts (74%) create mode 100644 src/modules/health/tests/unit/health.http.controller.spec.ts diff --git a/package.json b/package.json index 26bd1fb..bd1ef20 100644 --- a/package.json +++ b/package.json @@ -118,8 +118,7 @@ "moduleNameMapper": { "^@libs(.*)": "/libs/$1", "^@modules(.*)": "/modules/$1", - "^@src(.*)": "$1", - "^@utils(.*)": "utils/$1" + "^@src(.*)": "$1" }, "testEnvironment": "node" } diff --git a/src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts b/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts similarity index 100% rename from src/utils/tests/unit/rpc-validation-pipe.usecase.spec.ts rename to src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts diff --git a/src/utils/pipes/rpc.validation-pipe.ts b/src/libs/utils/pipes/rpc.validation-pipe.ts similarity index 74% rename from src/utils/pipes/rpc.validation-pipe.ts rename to src/libs/utils/pipes/rpc.validation-pipe.ts index f2b8c19..75b1852 100644 --- a/src/utils/pipes/rpc.validation-pipe.ts +++ b/src/libs/utils/pipes/rpc.validation-pipe.ts @@ -1,3 +1,4 @@ +import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; import { Injectable, ValidationPipe } from '@nestjs/common'; import { RpcException } from '@nestjs/microservices'; @@ -6,7 +7,7 @@ export class RpcValidationPipe extends ValidationPipe { createExceptionFactory() { return (validationErrors = []) => { return new RpcException({ - code: 3, + code: RpcExceptionCode.INVALID_ARGUMENT, message: this.flattenValidationErrors(validationErrors), }); }; diff --git a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts index 91158b7..6a14291 100644 --- a/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/create-ad.grpc.controller.ts @@ -7,7 +7,7 @@ import { AggregateID } from '@libs/ddd'; import { AdAlreadyExistsException } from '../../core/ad.errors'; import { IdResponse } from '@libs/api/id.response.dto'; import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; -import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe'; +import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe'; @UsePipes( new RpcValidationPipe({ diff --git a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts index f7d5be3..37f937a 100644 --- a/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts +++ b/src/modules/ad/interface/grpc-controllers/find-ad-by-id.grpc.controller.ts @@ -1,7 +1,6 @@ import { Controller, UsePipes } from '@nestjs/common'; import { QueryBus } from '@nestjs/cqrs'; import { GrpcMethod, RpcException } from '@nestjs/microservices'; -import { RpcValidationPipe } from '@utils/pipes/rpc.validation-pipe'; import { FindAdByIdRequestDto } from './dtos/find-ad-by-id.request.dto'; import { FindAdByIdQuery } from '@modules/ad/core/queries/find-ad-by-id/find-ad-by-id.query'; import { AdResponseDto } from '../dtos/ad.response.dto'; @@ -9,6 +8,7 @@ import { AdEntity } from '@modules/ad/core/ad.entity'; import { AdMapper } from '@modules/ad/ad.mapper'; import { NotFoundException } from '@libs/exceptions'; import { RpcExceptionCode } from '@libs/exceptions/rpc-exception.codes.enum'; +import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe'; @UsePipes( new RpcValidationPipe({ diff --git a/src/modules/health/interface/http-controllers/health.http.controller.ts b/src/modules/health/interface/http-controllers/health.http.controller.ts index 0e43fee..fbc9e87 100644 --- a/src/modules/health/interface/http-controllers/health.http.controller.ts +++ b/src/modules/health/interface/http-controllers/health.http.controller.ts @@ -1,6 +1,10 @@ +import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase'; import { Controller, Get } from '@nestjs/common'; -import { HealthCheckService, HealthCheck } from '@nestjs/terminus'; -import { RepositoriesHealthIndicatorUseCase } from '../../core/usecases/repositories.health-indicator.usecase'; +import { + HealthCheckService, + HealthCheck, + HealthCheckResult, +} from '@nestjs/terminus'; @Controller('health') export class HealthHttpController { @@ -11,7 +15,7 @@ export class HealthHttpController { @Get() @HealthCheck() - async check() { + async check(): Promise { try { return await this.healthCheckService.check([ async () => diff --git a/src/modules/health/tests/unit/health.http.controller.spec.ts b/src/modules/health/tests/unit/health.http.controller.spec.ts new file mode 100644 index 0000000..8982358 --- /dev/null +++ b/src/modules/health/tests/unit/health.http.controller.spec.ts @@ -0,0 +1,90 @@ +import { RepositoriesHealthIndicatorUseCase } from '@modules/health/core/usecases/repositories.health-indicator.usecase'; +import { HealthHttpController } from '@modules/health/interface/http-controllers/health.http.controller'; +import { HealthCheckResult, HealthCheckService } from '@nestjs/terminus'; +import { Test, TestingModule } from '@nestjs/testing'; + +const mockHealthCheckService = { + check: jest + .fn() + .mockImplementationOnce(() => ({ + status: 'ok', + info: { + repositories: { + status: 'up', + }, + }, + error: {}, + details: { + repositories: { + status: 'up', + }, + }, + })) + .mockImplementationOnce(() => ({ + status: 'error', + info: {}, + error: { + repository: + "\nInvalid `prisma.$queryRaw()` invocation:\n\n\nCan't reach database server at `v3-db`:`5432`\n\nPlease make sure your database server is running at `v3-db`:`5432`.", + }, + details: { + repository: + "\nInvalid `prisma.$queryRaw()` invocation:\n\n\nCan't reach database server at `v3-db`:`5432`\n\nPlease make sure your database server is running at `v3-db`:`5432`.", + }, + })), +}; + +const mockRepositoriesHealthIndicatorUseCase = { + isHealthy: jest.fn(), +}; + +describe('Health Http Controller', () => { + let healthHttpController: HealthHttpController; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: HealthCheckService, + useValue: mockHealthCheckService, + }, + { + provide: RepositoriesHealthIndicatorUseCase, + useValue: mockRepositoriesHealthIndicatorUseCase, + }, + HealthHttpController, + ], + }).compile(); + + healthHttpController = + module.get(HealthHttpController); + }); + + afterEach(async () => { + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(healthHttpController).toBeDefined(); + }); + + it('should return an HealthCheckResult with Ok status ', async () => { + jest.spyOn(mockHealthCheckService, 'check'); + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + + const healthCheckResult: HealthCheckResult = + await healthHttpController.check(); + expect(healthCheckResult.status).toBe('ok'); + expect(mockHealthCheckService.check).toHaveBeenCalledTimes(1); + }); + + it('should return an HealthCheckResult with Error status ', async () => { + jest.spyOn(mockHealthCheckService, 'check'); + jest.spyOn(mockRepositoriesHealthIndicatorUseCase, 'isHealthy'); + + const healthCheckResult: HealthCheckResult = + await healthHttpController.check(); + expect(healthCheckResult.status).toBe('error'); + expect(mockHealthCheckService.check).toHaveBeenCalledTimes(1); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index c42aa05..e082b4e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -21,7 +21,6 @@ "@libs/*": ["src/libs/*"], "@modules/*": ["src/modules/*"], "@ports/*": ["src/ports/*"], - "@utils/*": ["src/utils/*"], "@src/*": ["src/*"], } } From 3a90345d3e60303205cfc2421631243804ea8d4b Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 14:10:56 +0200 Subject: [PATCH 15/29] simplify path --- src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts b/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts index 5b56535..fffdad4 100644 --- a/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts +++ b/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts @@ -1,6 +1,6 @@ import { ArgumentMetadata } from '@nestjs/common'; -import { RpcValidationPipe } from '../../pipes/rpc.validation-pipe'; -import { FindAdByIdRequestDto } from '../../../modules/ad/interface/grpc-controllers/dtos/find-ad-by-id.request.dto'; +import { FindAdByIdRequestDto } from '@modules/ad/interface/grpc-controllers/dtos/find-ad-by-id.request.dto'; +import { RpcValidationPipe } from '@libs/utils/pipes/rpc.validation-pipe'; describe('RpcValidationPipe', () => { it('should not validate request', async () => { From 6c94330003c9502158e955740e288b191270940d Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 14:14:14 +0200 Subject: [PATCH 16/29] update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5fe1dc6..84f8a3a 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ The app exposes the following [gRPC](https://grpc.io/) services : ```json { - "userUuid": "80c9bb02-0931-4a1d-bea6-22d358992245", + "userId": "80c9bb02-0931-4a1d-bea6-22d358992245", "passenger": true, "seatsPassenger": 1, "frequency": "RECURRRENT", @@ -179,7 +179,7 @@ The app exposes the following [gRPC](https://grpc.io/) services : - frequency: `PUNCTUAL` or `RECURRENT` - fromDate: start date for recurrent ad, carpool date for punctual ad - toDate: end date for recurrent ad, same as fromDate for punctual ad - - schedule: an object with the departure time for each carpooled day in the week + - schedule: an object with the departure time for each carpooled day in the week (only the carpooled day for punctual ad) - marginDurations (optional): an object with the margin duration (in seconds) for each carpooled day in the week, eg: { @@ -191,7 +191,7 @@ The app exposes the following [gRPC](https://grpc.io/) services : - seatsProposed (optional): number of seats proposed as driver - seatsRequested (optional): number of seats requested as passenger - strict (boolean, optional): if set to true, allow matching only with similar frequency ads - - waypoints: an array of addresses that represent the waypoints of the journey (only first and last waypoints are used for passenger ads). Note that positions **must** be consecutives + - waypoints: an array of addresses that represent the waypoints of the journey (only first and last waypoints are used for passenger ads). Note that positions are **required** and **must** be consecutives Default values must be set in `.env` file. From 2c6e0458411e9daad61e6c1a50ab7132bff086ad Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 14:27:21 +0200 Subject: [PATCH 17/29] move ports to libs --- src/{ => libs}/ports/message-publisher.port.ts | 0 src/modules/ad/infrastructure/message-publisher.ts | 2 +- .../core/usecases/repositories.health-indicator.usecase.ts | 2 +- src/modules/health/infrastructure/message-publisher.ts | 2 +- tsconfig.json | 1 - 5 files changed, 3 insertions(+), 4 deletions(-) rename src/{ => libs}/ports/message-publisher.port.ts (100%) diff --git a/src/ports/message-publisher.port.ts b/src/libs/ports/message-publisher.port.ts similarity index 100% rename from src/ports/message-publisher.port.ts rename to src/libs/ports/message-publisher.port.ts diff --git a/src/modules/ad/infrastructure/message-publisher.ts b/src/modules/ad/infrastructure/message-publisher.ts index 10b2a0d..69122e5 100644 --- a/src/modules/ad/infrastructure/message-publisher.ts +++ b/src/modules/ad/infrastructure/message-publisher.ts @@ -1,7 +1,7 @@ import { Inject, Injectable } from '@nestjs/common'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; -import { MessagePublisherPort } from '@ports/message-publisher.port'; import { MESSAGE_BROKER_PUBLISHER } from '@src/app.constants'; +import { MessagePublisherPort } from '@libs/ports/message-publisher.port'; @Injectable() export class MessagePublisher implements MessagePublisherPort { diff --git a/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts b/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts index 9600c31..36e95c1 100644 --- a/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts +++ b/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts @@ -9,8 +9,8 @@ import { CheckRepositoryPort } from '../ports/check-repository.port'; import { AD_REPOSITORY } from '@modules/health/health.di-tokens'; import { AdRepositoryPort } from '@modules/ad/core/ports/ad.repository.port'; import { MESSAGE_PUBLISHER } from '@src/app.constants'; -import { MessagePublisherPort } from '@ports/message-publisher.port'; import { LOGGING_AD_HEALTH_CRIT } from '@modules/health/health.constants'; +import { MessagePublisherPort } from '@libs/ports/message-publisher.port'; @Injectable() export class RepositoriesHealthIndicatorUseCase extends HealthIndicator { diff --git a/src/modules/health/infrastructure/message-publisher.ts b/src/modules/health/infrastructure/message-publisher.ts index 070f5b1..036743b 100644 --- a/src/modules/health/infrastructure/message-publisher.ts +++ b/src/modules/health/infrastructure/message-publisher.ts @@ -1,7 +1,7 @@ import { Inject, Injectable } from '@nestjs/common'; import { MESSAGE_BROKER_PUBLISHER } from '../../../app.constants'; import { MessageBrokerPublisher } from '@mobicoop/message-broker-module'; -import { MessagePublisherPort } from '@ports/message-publisher.port'; +import { MessagePublisherPort } from '@libs/ports/message-publisher.port'; @Injectable() export class MessagePublisher implements MessagePublisherPort { diff --git a/tsconfig.json b/tsconfig.json index e082b4e..546b2f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,6 @@ "paths": { "@libs/*": ["src/libs/*"], "@modules/*": ["src/modules/*"], - "@ports/*": ["src/ports/*"], "@src/*": ["src/*"], } } From 8d1d0f82cfefbc16177a436b3f9ce8d0f8516eb5 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 14:49:08 +0200 Subject: [PATCH 18/29] refactor --- src/modules/ad/ad.mapper.ts | 8 +++--- .../commands/create-ad/create-ad.service.ts | 28 +++++++++---------- .../infrastructure/default-params-provider.ts | 28 +++++++++---------- .../health.http.controller.ts | 2 +- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/modules/ad/ad.mapper.ts b/src/modules/ad/ad.mapper.ts index aa21320..61f75d4 100644 --- a/src/modules/ad/ad.mapper.ts +++ b/src/modules/ad/ad.mapper.ts @@ -31,7 +31,7 @@ import { TimeConverterPort } from './core/ports/time-converter.port'; export class AdMapper implements Mapper { - private readonly defaultParams: DefaultParams; + private readonly _defaultParams: DefaultParams; constructor( @Inject(PARAMS_PROVIDER) @@ -41,7 +41,7 @@ export class AdMapper @Inject(TIME_CONVERTER) private readonly timeConverter: TimeConverterPort, ) { - this.defaultParams = defaultParamsProvider.getParams(); + this._defaultParams = defaultParamsProvider.getParams(); } toPersistence = (entity: AdEntity): AdWriteModel => { @@ -50,7 +50,7 @@ export class AdMapper const timezone = this.timezoneFinder.timezones( lon, lat, - this.defaultParams.DEFAULT_TIMEZONE, + this._defaultParams.DEFAULT_TIMEZONE, )[0]; const now = new Date(); const record: AdWriteModel = { @@ -146,7 +146,7 @@ export class AdMapper const timezone = this.timezoneFinder.timezones( record.waypoints[0].lon, record.waypoints[0].lat, - this.defaultParams.DEFAULT_TIMEZONE, + this._defaultParams.DEFAULT_TIMEZONE, )[0]; const entity = new AdEntity({ id: record.uuid, diff --git a/src/modules/ad/core/commands/create-ad/create-ad.service.ts b/src/modules/ad/core/commands/create-ad/create-ad.service.ts index b85affe..1fc16fc 100644 --- a/src/modules/ad/core/commands/create-ad/create-ad.service.ts +++ b/src/modules/ad/core/commands/create-ad/create-ad.service.ts @@ -13,7 +13,7 @@ import { Waypoint } from '../../types/waypoint'; @CommandHandler(CreateAdCommand) export class CreateAdService implements ICommandHandler { - private readonly defaultParams: DefaultParams; + private readonly _defaultParams: DefaultParams; constructor( @Inject(AD_REPOSITORY) @@ -21,7 +21,7 @@ export class CreateAdService implements ICommandHandler { @Inject(PARAMS_PROVIDER) private readonly defaultParamsProvider: DefaultParamsProviderPort, ) { - this.defaultParams = defaultParamsProvider.getParams(); + this._defaultParams = defaultParamsProvider.getParams(); } async execute(command: CreateAdCommand): Promise { @@ -55,20 +55,20 @@ export class CreateAdService implements ICommandHandler { })), }, { - driver: this.defaultParams.DRIVER, - passenger: this.defaultParams.PASSENGER, + driver: this._defaultParams.DRIVER, + passenger: this._defaultParams.PASSENGER, marginDurations: { - mon: this.defaultParams.MON_MARGIN, - tue: this.defaultParams.TUE_MARGIN, - wed: this.defaultParams.WED_MARGIN, - thu: this.defaultParams.THU_MARGIN, - fri: this.defaultParams.FRI_MARGIN, - sat: this.defaultParams.SAT_MARGIN, - sun: this.defaultParams.SUN_MARGIN, + mon: this._defaultParams.MON_MARGIN, + tue: this._defaultParams.TUE_MARGIN, + wed: this._defaultParams.WED_MARGIN, + thu: this._defaultParams.THU_MARGIN, + fri: this._defaultParams.FRI_MARGIN, + sat: this._defaultParams.SAT_MARGIN, + sun: this._defaultParams.SUN_MARGIN, }, - strict: this.defaultParams.STRICT, - seatsProposed: this.defaultParams.SEATS_PROPOSED, - seatsRequested: this.defaultParams.SEATS_REQUESTED, + strict: this._defaultParams.STRICT, + seatsProposed: this._defaultParams.SEATS_PROPOSED, + seatsRequested: this._defaultParams.SEATS_REQUESTED, }, ); diff --git a/src/modules/ad/infrastructure/default-params-provider.ts b/src/modules/ad/infrastructure/default-params-provider.ts index 3e1ee57..c27f5c1 100644 --- a/src/modules/ad/infrastructure/default-params-provider.ts +++ b/src/modules/ad/infrastructure/default-params-provider.ts @@ -5,20 +5,20 @@ import { DefaultParams } from '../core/ports/default-params.type'; @Injectable() export class DefaultParamsProvider implements DefaultParamsProviderPort { - constructor(private readonly configService: ConfigService) {} + constructor(private readonly _configService: ConfigService) {} getParams = (): DefaultParams => ({ - MON_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - TUE_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - WED_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - THU_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - FRI_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - SAT_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - SUN_MARGIN: parseInt(this.configService.get('DEPARTURE_MARGIN')), - DRIVER: this.configService.get('ROLE') == 'driver', - SEATS_PROPOSED: parseInt(this.configService.get('SEATS_PROPOSED')), - PASSENGER: this.configService.get('ROLE') == 'passenger', - SEATS_REQUESTED: parseInt(this.configService.get('SEATS_REQUESTED')), - STRICT: this.configService.get('STRICT_FREQUENCY') == 'true', - DEFAULT_TIMEZONE: this.configService.get('DEFAULT_TIMEZONE'), + MON_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + TUE_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + WED_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + THU_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + FRI_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + SAT_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + SUN_MARGIN: parseInt(this._configService.get('DEPARTURE_MARGIN')), + DRIVER: this._configService.get('ROLE') == 'driver', + SEATS_PROPOSED: parseInt(this._configService.get('SEATS_PROPOSED')), + PASSENGER: this._configService.get('ROLE') == 'passenger', + SEATS_REQUESTED: parseInt(this._configService.get('SEATS_REQUESTED')), + STRICT: this._configService.get('STRICT_FREQUENCY') == 'true', + DEFAULT_TIMEZONE: this._configService.get('DEFAULT_TIMEZONE'), }); } diff --git a/src/modules/health/interface/http-controllers/health.http.controller.ts b/src/modules/health/interface/http-controllers/health.http.controller.ts index fbc9e87..256dae5 100644 --- a/src/modules/health/interface/http-controllers/health.http.controller.ts +++ b/src/modules/health/interface/http-controllers/health.http.controller.ts @@ -10,7 +10,7 @@ import { export class HealthHttpController { constructor( private readonly repositoriesHealthIndicatorUseCase: RepositoriesHealthIndicatorUseCase, - private healthCheckService: HealthCheckService, + private readonly healthCheckService: HealthCheckService, ) {} @Get() From 26dab584b23917cd33992725fa0f6503fed5c40a Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 27 Jun 2023 17:51:20 +0200 Subject: [PATCH 19/29] libs tests --- package.json | 14 +- src/libs/exceptions/index.ts | 1 + .../unit/db/prisma-repository.base.spec.ts | 250 ++++++++++++++++++ src/libs/tests/unit/guard.spec.ts | 65 +++++ .../rpc-validation-pipe.usecase.spec.ts | 0 5 files changed, 328 insertions(+), 2 deletions(-) create mode 100644 src/libs/tests/unit/db/prisma-repository.base.spec.ts create mode 100644 src/libs/tests/unit/guard.spec.ts rename src/libs/tests/unit/{ => utils}/rpc-validation-pipe.usecase.spec.ts (100%) diff --git a/package.json b/package.json index bd1ef20..bbabe30 100644 --- a/package.json +++ b/package.json @@ -93,10 +93,15 @@ "ts" ], "modulePathIgnorePatterns": [ - "libs/", ".module.ts", ".dto.ts", ".constants.ts", + ".response.ts", + ".response.base.ts", + ".port.ts", + "libs/exceptions", + "libs/types", + "prisma-service.base.ts", "main.ts" ], "rootDir": "src", @@ -108,10 +113,15 @@ "**/*.(t|j)s" ], "coveragePathIgnorePatterns": [ - "libs/", ".module.ts", ".dto.ts", ".constants.ts", + ".response.ts", + ".response.base.ts", + ".port.ts", + "libs/exceptions", + "libs/types", + "prisma-service.base.ts", "main.ts" ], "coverageDirectory": "../coverage", diff --git a/src/libs/exceptions/index.ts b/src/libs/exceptions/index.ts index f14fac5..4445442 100644 --- a/src/libs/exceptions/index.ts +++ b/src/libs/exceptions/index.ts @@ -1,3 +1,4 @@ export * from './exception.base'; export * from './exception.codes'; export * from './exceptions'; +export * from './rpc-exception.codes.enum'; diff --git a/src/libs/tests/unit/db/prisma-repository.base.spec.ts b/src/libs/tests/unit/db/prisma-repository.base.spec.ts new file mode 100644 index 0000000..9217dfe --- /dev/null +++ b/src/libs/tests/unit/db/prisma-repository.base.spec.ts @@ -0,0 +1,250 @@ +import { ResponseBase } from '@libs/api/response.base'; +import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base'; +import { PrismaService } from '@libs/db/prisma.service'; +import { AggregateID, AggregateRoot, Mapper, RepositoryPort } from '@libs/ddd'; +import { ConflictException } from '@libs/exceptions'; +import { Injectable, Logger } from '@nestjs/common'; +import { EventEmitter2 } from '@nestjs/event-emitter'; +import { Test, TestingModule } from '@nestjs/testing'; +import { Prisma } from '@prisma/client'; +import { v4 } from 'uuid'; + +export interface FakeProps { + name: string; +} + +export interface CreateFakeProps { + name: string; +} + +class FakeEntity extends AggregateRoot { + protected readonly _id: AggregateID; + + static create = (create: CreateFakeProps): FakeEntity => { + const id = v4(); + const props: FakeProps = { ...create }; + const fake = new FakeEntity({ id, props }); + return fake; + }; + + validate(): void { + // not implemented + } +} + +type FakeModel = { + uuid: string; + name: string; + createdAt: Date; + updatedAt: Date; +}; + +type FakeRepositoryPort = RepositoryPort; + +class FakeResponseDto extends ResponseBase { + name: string; +} + +const fakeDbWriteModel: FakeModel = { + uuid: 'd567ea3b-4981-43c9-9449-a409b5fa9fed', + name: 'fakeName', + createdAt: new Date('2023-06-28T16:02:00Z'), + updatedAt: new Date('2023-06-28T16:02:00Z'), +}; + +let modelId = 2; +const modelUuid = 'uuid-'; +const modelName = 'name-'; + +const createRandomModel = (): FakeModel => { + const fakeModel: FakeModel = { + uuid: `${modelUuid}${modelId}`, + name: `${modelName}${modelId}`, + createdAt: new Date('2023-06-30T08:00:00Z'), + updatedAt: new Date('2023-06-30T08:00:00Z'), + }; + + modelId++; + + return fakeModel; +}; + +const fakeModels: FakeModel[] = []; +Array.from({ length: 10 }).forEach(() => { + fakeModels.push(createRandomModel()); +}); + +@Injectable() +class FakeMapper + implements Mapper +{ + toPersistence = (entity: FakeEntity): FakeModel => { + const copy = entity.getProps(); + const record: FakeModel = { + uuid: copy.id, + name: copy.name, + createdAt: copy.createdAt, + updatedAt: copy.updatedAt, + }; + return record; + }; + + toDomain = (record: FakeModel): FakeEntity => { + const entity = new FakeEntity({ + id: record.uuid, + createdAt: new Date(record.createdAt), + updatedAt: new Date(record.updatedAt), + props: { + name: record.name, + }, + }); + return entity; + }; + + toResponse = (entity: FakeEntity): FakeResponseDto => { + const props = entity.getProps(); + const response = new FakeResponseDto(entity); + response.name = props.name; + return response; + }; +} + +@Injectable() +class FakePrismaService extends PrismaService { + fake: any; +} + +const mockPrismaService = { + $queryRaw: jest + .fn() + .mockImplementationOnce(() => { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { + code: 'code', + clientVersion: 'version', + }); + }) + .mockImplementationOnce(() => { + return true; + }) + .mockImplementation(() => { + throw new Prisma.PrismaClientKnownRequestError('Database unavailable', { + code: 'code', + clientVersion: 'version', + }); + }), + fake: { + create: jest + .fn() + .mockResolvedValueOnce(fakeDbWriteModel) + .mockImplementationOnce(() => { + throw new Prisma.PrismaClientKnownRequestError('Already exists', { + code: 'code', + clientVersion: 'version', + }); + }) + .mockImplementationOnce(() => { + throw new Error('an unknown error'); + }), + + findUnique: jest.fn().mockImplementation(async (params?: any) => { + let model: FakeModel; + + if (params?.where?.uuid) { + model = fakeModels.find( + (entity) => entity.uuid === params?.where?.uuid, + ); + } + + if (!model && params?.where?.uuid == 'unknown') { + throw new Prisma.PrismaClientKnownRequestError('unknown request', { + code: 'code', + clientVersion: 'version', + }); + } else if (!model) { + throw new Error('No record found'); + } + + return model; + }), + }, +}; + +@Injectable() +class FakeRepository + extends PrismaRepositoryBase + implements FakeRepositoryPort +{ + constructor( + prisma: FakePrismaService, + mapper: FakeMapper, + eventEmitter: EventEmitter2, + ) { + super( + prisma.fake, + prisma, + mapper, + eventEmitter, + new Logger(FakeRepository.name), + ); + } +} + +describe('PrismaRepositoryBase', () => { + let fakeRepository: FakeRepository; + let prisma: FakePrismaService; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + EventEmitter2, + FakeRepository, + FakeMapper, + { + provide: FakePrismaService, + useValue: mockPrismaService, + }, + ], + }).compile(); + + fakeRepository = module.get(FakeRepository); + prisma = module.get(FakePrismaService); + }); + + it('should be defined', () => { + expect(fakeRepository).toBeDefined(); + expect(prisma).toBeDefined(); + }); + + describe('insert', () => { + it('should create a model', async () => { + jest.spyOn(prisma.fake, 'create'); + + await fakeRepository.insert( + FakeEntity.create({ + name: 'someFakeName', + }), + ); + expect(prisma.fake.create).toHaveBeenCalledTimes(1); + }); + + it('should throw a ConflictException if model already exists', async () => { + await expect( + fakeRepository.insert( + FakeEntity.create({ + name: 'someFakeName', + }), + ), + ).rejects.toBeInstanceOf(ConflictException); + }); + + it('should throw an exception an error occurs', async () => { + await expect( + fakeRepository.insert( + FakeEntity.create({ + name: 'someFakeName', + }), + ), + ).rejects.toBeInstanceOf(Error); + }); + }); +}); diff --git a/src/libs/tests/unit/guard.spec.ts b/src/libs/tests/unit/guard.spec.ts new file mode 100644 index 0000000..8a4b543 --- /dev/null +++ b/src/libs/tests/unit/guard.spec.ts @@ -0,0 +1,65 @@ +import { Guard } from '@libs/guard'; + +describe('Guard', () => { + describe('isEmpty', () => { + it('should return false for a number', () => { + expect(Guard.isEmpty(1)).toBeFalsy(); + }); + it('should return false for a falsy boolean', () => { + expect(Guard.isEmpty(false)).toBeFalsy(); + }); + it('should return false for a truthy boolean', () => { + expect(Guard.isEmpty(true)).toBeFalsy(); + }); + it('should return true for undefined', () => { + expect(Guard.isEmpty(undefined)).toBeTruthy(); + }); + it('should return true for null', () => { + expect(Guard.isEmpty(null)).toBeTruthy(); + }); + it('should return false for a Date', () => { + expect(Guard.isEmpty(new Date('2023-06-28'))).toBeFalsy(); + }); + it('should return false for an object with keys', () => { + expect(Guard.isEmpty({ key: 'value' })).toBeFalsy(); + }); + it('should return true for an object without keys', () => { + expect(Guard.isEmpty({})).toBeTruthy(); + }); + it('should return true for an array without values', () => { + expect(Guard.isEmpty([])).toBeTruthy(); + }); + it('should return true for an array with only empty values', () => { + expect(Guard.isEmpty([null, undefined])).toBeTruthy(); + }); + it('should return false for an array with some empty values', () => { + expect(Guard.isEmpty([1, null, undefined])).toBeFalsy(); + }); + it('should return true for an empty string', () => { + expect(Guard.isEmpty('')).toBeTruthy(); + }); + }); + describe('lengthIsBetween', () => { + it('should return true for a string in the range', () => { + expect(Guard.lengthIsBetween('test', 0, 4)).toBeTruthy(); + }); + it('should return true for a number in the range', () => { + expect(Guard.lengthIsBetween(2, 0, 4)).toBeTruthy(); + }); + it('should return true for an array with number of elements in the range', () => { + expect(Guard.lengthIsBetween([1, 2, 3], 0, 4)).toBeTruthy(); + }); + it('should return false for a string not in the range', () => { + expect(Guard.lengthIsBetween('test', 5, 9)).toBeFalsy(); + }); + it('should return false for a number not in the range', () => { + expect(Guard.lengthIsBetween(2, 3, 6)).toBeFalsy(); + }); + it('should return false for an array with number of elements not in the range', () => { + expect(Guard.lengthIsBetween([1, 2, 3], 10, 12)).toBeFalsy(); + }); + it('should throw an exception if value is empty', () => { + expect(() => Guard.lengthIsBetween(undefined, 0, 4)).toThrow(); + }); + }); +}); diff --git a/src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts b/src/libs/tests/unit/utils/rpc-validation-pipe.usecase.spec.ts similarity index 100% rename from src/libs/tests/unit/rpc-validation-pipe.usecase.spec.ts rename to src/libs/tests/unit/utils/rpc-validation-pipe.usecase.spec.ts From ef48e8ae68b76194252446888265f117f618ed0c Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 28 Jun 2023 10:02:13 +0200 Subject: [PATCH 20/29] tests for prisma --- package.json | 4 +- .../unit/db/prisma-repository.base.spec.ts | 99 +++++++++++++------ 2 files changed, 70 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index bbabe30..46efac1 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,7 @@ ".port.ts", "libs/exceptions", "libs/types", - "prisma-service.base.ts", + "prisma.service.ts", "main.ts" ], "rootDir": "src", @@ -121,7 +121,7 @@ ".port.ts", "libs/exceptions", "libs/types", - "prisma-service.base.ts", + "prisma.service.ts", "main.ts" ], "coverageDirectory": "../coverage", diff --git a/src/libs/tests/unit/db/prisma-repository.base.spec.ts b/src/libs/tests/unit/db/prisma-repository.base.spec.ts index 9217dfe..49eca17 100644 --- a/src/libs/tests/unit/db/prisma-repository.base.spec.ts +++ b/src/libs/tests/unit/db/prisma-repository.base.spec.ts @@ -2,7 +2,11 @@ import { ResponseBase } from '@libs/api/response.base'; import { PrismaRepositoryBase } from '@libs/db/prisma-repository.base'; import { PrismaService } from '@libs/db/prisma.service'; import { AggregateID, AggregateRoot, Mapper, RepositoryPort } from '@libs/ddd'; -import { ConflictException } from '@libs/exceptions'; +import { + ConflictException, + DatabaseErrorException, + NotFoundException, +} from '@libs/exceptions'; import { Injectable, Logger } from '@nestjs/common'; import { EventEmitter2 } from '@nestjs/event-emitter'; import { Test, TestingModule } from '@nestjs/testing'; @@ -45,33 +49,33 @@ class FakeResponseDto extends ResponseBase { name: string; } -const fakeDbWriteModel: FakeModel = { +const fakeRecord: FakeModel = { uuid: 'd567ea3b-4981-43c9-9449-a409b5fa9fed', name: 'fakeName', createdAt: new Date('2023-06-28T16:02:00Z'), updatedAt: new Date('2023-06-28T16:02:00Z'), }; -let modelId = 2; -const modelUuid = 'uuid-'; -const modelName = 'name-'; +let recordId = 2; +const recordUuid = 'uuid-'; +const recordName = 'fakeName-'; -const createRandomModel = (): FakeModel => { - const fakeModel: FakeModel = { - uuid: `${modelUuid}${modelId}`, - name: `${modelName}${modelId}`, +const createRandomRecord = (): FakeModel => { + const fakeRecord: FakeModel = { + uuid: `${recordUuid}${recordId}`, + name: `${recordName}${recordId}`, createdAt: new Date('2023-06-30T08:00:00Z'), updatedAt: new Date('2023-06-30T08:00:00Z'), }; - modelId++; + recordId++; - return fakeModel; + return fakeRecord; }; -const fakeModels: FakeModel[] = []; +const fakeRecords: FakeModel[] = []; Array.from({ length: 10 }).forEach(() => { - fakeModels.push(createRandomModel()); + fakeRecords.push(createRandomRecord()); }); @Injectable() @@ -117,12 +121,6 @@ class FakePrismaService extends PrismaService { const mockPrismaService = { $queryRaw: jest .fn() - .mockImplementationOnce(() => { - throw new Prisma.PrismaClientKnownRequestError('unknown request', { - code: 'code', - clientVersion: 'version', - }); - }) .mockImplementationOnce(() => { return true; }) @@ -131,11 +129,14 @@ const mockPrismaService = { code: 'code', clientVersion: 'version', }); + }) + .mockImplementationOnce(() => { + throw new Error(); }), fake: { create: jest .fn() - .mockResolvedValueOnce(fakeDbWriteModel) + .mockResolvedValueOnce(fakeRecord) .mockImplementationOnce(() => { throw new Prisma.PrismaClientKnownRequestError('Already exists', { code: 'code', @@ -143,28 +144,26 @@ const mockPrismaService = { }); }) .mockImplementationOnce(() => { - throw new Error('an unknown error'); + throw new Error('An unknown error'); }), findUnique: jest.fn().mockImplementation(async (params?: any) => { - let model: FakeModel; + let record: FakeModel; if (params?.where?.uuid) { - model = fakeModels.find( - (entity) => entity.uuid === params?.where?.uuid, + record = fakeRecords.find( + (record) => record.uuid === params?.where?.uuid, ); } - if (!model && params?.where?.uuid == 'unknown') { + if (!record && params?.where?.uuid == 'uuid-triggering-error') { throw new Prisma.PrismaClientKnownRequestError('unknown request', { code: 'code', clientVersion: 'version', }); - } else if (!model) { - throw new Error('No record found'); } - return model; + return record; }), }, }; @@ -216,7 +215,7 @@ describe('PrismaRepositoryBase', () => { }); describe('insert', () => { - it('should create a model', async () => { + it('should create a record', async () => { jest.spyOn(prisma.fake, 'create'); await fakeRepository.insert( @@ -227,7 +226,7 @@ describe('PrismaRepositoryBase', () => { expect(prisma.fake.create).toHaveBeenCalledTimes(1); }); - it('should throw a ConflictException if model already exists', async () => { + it('should throw a ConflictException if record already exists', async () => { await expect( fakeRepository.insert( FakeEntity.create({ @@ -237,7 +236,7 @@ describe('PrismaRepositoryBase', () => { ).rejects.toBeInstanceOf(ConflictException); }); - it('should throw an exception an error occurs', async () => { + it('should throw an Error if an error occurs', async () => { await expect( fakeRepository.insert( FakeEntity.create({ @@ -247,4 +246,42 @@ describe('PrismaRepositoryBase', () => { ).rejects.toBeInstanceOf(Error); }); }); + + describe('findOneById', () => { + it('should find a record by its id', async () => { + const record = await fakeRepository.findOneById('uuid-3'); + expect(record.getProps().name).toBe('fakeName-3'); + }); + + it('should throw an Error for client error', async () => { + await expect( + fakeRepository.findOneById('uuid-triggering-error'), + ).rejects.toBeInstanceOf(Error); + }); + + it('should throw a NotFoundException if id is not found', async () => { + await expect( + fakeRepository.findOneById('wrong-id'), + ).rejects.toBeInstanceOf(NotFoundException); + }); + }); + + describe('healthCheck', () => { + it('should return a healthy result', async () => { + const res = await fakeRepository.healthCheck(); + expect(res).toBeTruthy(); + }); + + it('should throw an exception if database is not available', async () => { + await expect(fakeRepository.healthCheck()).rejects.toBeInstanceOf( + DatabaseErrorException, + ); + }); + + it('should throw a DatabaseErrorException if an error occurs', async () => { + await expect(fakeRepository.healthCheck()).rejects.toBeInstanceOf( + DatabaseErrorException, + ); + }); + }); }); From b12ba842c0a257c7628382c6d0ca2d7ab5c8fac4 Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 28 Jun 2023 11:02:55 +0200 Subject: [PATCH 21/29] tests ddd lib --- .../unit/db/prisma-repository.base.spec.ts | 4 +- .../unit/ddd/aggregate-root.base.spec.ts | 76 +++++++++++++++++++ src/libs/tests/unit/ddd/command.base.spec.ts | 47 ++++++++++++ 3 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/libs/tests/unit/ddd/aggregate-root.base.spec.ts create mode 100644 src/libs/tests/unit/ddd/command.base.spec.ts diff --git a/src/libs/tests/unit/db/prisma-repository.base.spec.ts b/src/libs/tests/unit/db/prisma-repository.base.spec.ts index 49eca17..e3cc272 100644 --- a/src/libs/tests/unit/db/prisma-repository.base.spec.ts +++ b/src/libs/tests/unit/db/prisma-repository.base.spec.ts @@ -13,11 +13,11 @@ import { Test, TestingModule } from '@nestjs/testing'; import { Prisma } from '@prisma/client'; import { v4 } from 'uuid'; -export interface FakeProps { +interface FakeProps { name: string; } -export interface CreateFakeProps { +interface CreateFakeProps { name: string; } diff --git a/src/libs/tests/unit/ddd/aggregate-root.base.spec.ts b/src/libs/tests/unit/ddd/aggregate-root.base.spec.ts new file mode 100644 index 0000000..1b10ec6 --- /dev/null +++ b/src/libs/tests/unit/ddd/aggregate-root.base.spec.ts @@ -0,0 +1,76 @@ +import { + AggregateID, + AggregateRoot, + DomainEvent, + DomainEventProps, +} from '@libs/ddd'; +import { EventEmitter2 } from '@nestjs/event-emitter'; +import { v4 } from 'uuid'; + +interface FakeProps { + name: string; +} + +interface CreateFakeProps { + name: string; +} + +class FakeRecordCreatedDomainEvent extends DomainEvent { + readonly name: string; + + constructor(props: DomainEventProps) { + super(props); + this.name = props.name; + } +} + +class FakeEntity extends AggregateRoot { + protected readonly _id: AggregateID; + + static create = (create: CreateFakeProps): FakeEntity => { + const id = v4(); + const props: FakeProps = { ...create }; + const fake = new FakeEntity({ id, props }); + fake.addEvent( + new FakeRecordCreatedDomainEvent({ + aggregateId: id, + name: props.name, + }), + ); + return fake; + }; + + validate(): void { + // not implemented + } +} + +const mockLogger = { + debug: jest.fn(), + log: jest.fn(), + error: jest.fn(), + warn: jest.fn(), +}; + +describe('AggregateRoot Base', () => { + it('should define an aggregate root based object instance', () => { + const fakeInstance = FakeEntity.create({ + name: 'someFakeName', + }); + expect(fakeInstance).toBeDefined(); + expect(fakeInstance.domainEvents.length).toBe(1); + }); + + it('should publish domain events', async () => { + jest.spyOn(mockLogger, 'debug'); + const eventEmitter = new EventEmitter2(); + jest.spyOn(eventEmitter, 'emitAsync'); + const fakeInstance = FakeEntity.create({ + name: 'someFakeName', + }); + await fakeInstance.publishEvents(mockLogger, eventEmitter); + expect(mockLogger.debug).toHaveBeenCalledTimes(1); + expect(eventEmitter.emitAsync).toHaveBeenCalledTimes(1); + expect(fakeInstance.domainEvents.length).toBe(0); + }); +}); diff --git a/src/libs/tests/unit/ddd/command.base.spec.ts b/src/libs/tests/unit/ddd/command.base.spec.ts new file mode 100644 index 0000000..5ea8f42 --- /dev/null +++ b/src/libs/tests/unit/ddd/command.base.spec.ts @@ -0,0 +1,47 @@ +import { Command, CommandProps } from '@libs/ddd'; +import { ArgumentNotProvidedException } from '@libs/exceptions'; + +class FakeCommand extends Command { + readonly name: string; + + constructor(props: CommandProps) { + super(props); + this.name = props.name; + } +} + +class BadFakeCommand extends Command { + constructor(props: CommandProps) { + super(props); + } +} + +describe('Command Base', () => { + it('should define a command based object instance', () => { + const fakeCommand = new FakeCommand({ name: 'fakeName' }); + expect(fakeCommand).toBeDefined(); + expect(fakeCommand.id.length).toBe(36); + }); + + it('should define a command based object instance with a provided id', () => { + const fakeCommand = new FakeCommand({ id: 'some-id', name: 'fakeName' }); + expect(fakeCommand.id).toBe('some-id'); + }); + + it('should define a command based object instance with metadata', () => { + const fakeCommand = new FakeCommand({ + name: 'fakeName', + metadata: { + correlationId: 'some-correlation-id', + causationId: 'some-causation-id', + userId: 'some-user-id', + timestamp: new Date('2023-06-28T05:00:00Z').getTime(), + }, + }); + expect(fakeCommand.metadata.timestamp).toBe(1687928400000); + }); + + it('should throw an exception if props are empty', () => { + expect(() => new BadFakeCommand({})).toThrow(ArgumentNotProvidedException); + }); +}); From 6fafbbd10986b190d1d2c4955384773f467e6f5e Mon Sep 17 00:00:00 2001 From: sbriat Date: Wed, 28 Jun 2023 14:24:34 +0200 Subject: [PATCH 22/29] improve tests --- package.json | 2 + .../tests/unit/ddd/domain-event.base.spec.ts | 42 ++++ src/libs/tests/unit/ddd/entity.base.spec.ts | 209 ++++++++++++++++++ src/libs/tests/unit/ddd/query.base.spec.ts | 40 ++++ .../tests/unit/ddd/value-object.base.spec.ts | 42 ++++ 5 files changed, 335 insertions(+) create mode 100644 src/libs/tests/unit/ddd/domain-event.base.spec.ts create mode 100644 src/libs/tests/unit/ddd/entity.base.spec.ts create mode 100644 src/libs/tests/unit/ddd/query.base.spec.ts create mode 100644 src/libs/tests/unit/ddd/value-object.base.spec.ts diff --git a/package.json b/package.json index 46efac1..ad046cd 100644 --- a/package.json +++ b/package.json @@ -102,6 +102,7 @@ "libs/exceptions", "libs/types", "prisma.service.ts", + "convert-props-to-object.util.ts", "main.ts" ], "rootDir": "src", @@ -122,6 +123,7 @@ "libs/exceptions", "libs/types", "prisma.service.ts", + "convert-props-to-object.util.ts", "main.ts" ], "coverageDirectory": "../coverage", diff --git a/src/libs/tests/unit/ddd/domain-event.base.spec.ts b/src/libs/tests/unit/ddd/domain-event.base.spec.ts new file mode 100644 index 0000000..d4751b2 --- /dev/null +++ b/src/libs/tests/unit/ddd/domain-event.base.spec.ts @@ -0,0 +1,42 @@ +import { DomainEvent, DomainEventProps } from '@libs/ddd'; +import { ArgumentNotProvidedException } from '@libs/exceptions'; + +class FakeDomainEvent extends DomainEvent { + readonly name: string; + + constructor(props: DomainEventProps) { + super(props); + this.name = props.name; + } +} + +describe('DomainEvent Base', () => { + it('should define a domain event based object instance', () => { + const fakeDomainEvent = new FakeDomainEvent({ + aggregateId: 'some-id', + name: 'some-name', + }); + expect(fakeDomainEvent).toBeDefined(); + expect(fakeDomainEvent.id.length).toBe(36); + }); + + it('should define a domain event based object instance with metadata', () => { + const fakeDomainEvent = new FakeDomainEvent({ + aggregateId: 'some-id', + name: 'some-name', + metadata: { + correlationId: 'some-correlation-id', + causationId: 'some-causation-id', + userId: 'some-user-id', + timestamp: new Date('2023-06-28T05:00:00Z').getTime(), + }, + }); + expect(fakeDomainEvent.metadata.timestamp).toBe(1687928400000); + }); + it('should throw an exception if props are empty', () => { + const emptyProps: DomainEventProps = undefined; + expect(() => new FakeDomainEvent(emptyProps)).toThrow( + ArgumentNotProvidedException, + ); + }); +}); diff --git a/src/libs/tests/unit/ddd/entity.base.spec.ts b/src/libs/tests/unit/ddd/entity.base.spec.ts new file mode 100644 index 0000000..38f90f2 --- /dev/null +++ b/src/libs/tests/unit/ddd/entity.base.spec.ts @@ -0,0 +1,209 @@ +import { Entity } from '@libs/ddd'; +import { ArgumentOutOfRangeException } from '@libs/exceptions'; + +interface FakeProps { + name: string; +} + +class FakeEntity extends Entity { + protected _id: string; + + validate(): void { + // not implemented + } +} + +describe('Entity Base', () => { + it('should define an entity based object instance', () => { + const fakeInstance = new FakeEntity({ + id: 'some-id', + props: { + name: 'some-name', + }, + }); + expect(fakeInstance).toBeDefined(); + expect(fakeInstance.id).toBe('some-id'); + expect(fakeInstance.createdAt).toBeInstanceOf(Date); + expect(fakeInstance.updatedAt).toBeInstanceOf(Date); + expect(FakeEntity.isEntity(fakeInstance)).toBeTruthy(); + }); + + it('should define an entity with given created and updated dates', () => { + const fakeInstance = new FakeEntity({ + id: 'some-id', + createdAt: new Date('2023-06-28T05:00:00Z'), + updatedAt: new Date('2023-06-28T06:00:00Z'), + props: { + name: 'some-name', + }, + }); + expect(fakeInstance.createdAt.getUTCHours()).toBe(5); + expect(fakeInstance.updatedAt.getUTCHours()).toBe(6); + }); + + it('should compare entities', () => { + const fakeInstance = new FakeEntity({ + id: 'some-id', + props: { + name: 'some-name', + }, + }); + const fakeInstanceClone = new FakeEntity({ + id: 'some-id', + props: { + name: 'some-name', + }, + }); + const fakeInstanceNotReallyClone = new FakeEntity({ + id: 'some-slightly-different-id', + props: { + name: 'some-name', + }, + }); + const undefinedFakeInstance: FakeEntity = undefined; + expect(fakeInstance.equals(undefinedFakeInstance)).toBeFalsy(); + expect(fakeInstance.equals(fakeInstance)).toBeTruthy(); + expect(fakeInstance.equals(fakeInstanceClone)).toBeTruthy(); + expect(fakeInstance.equals(fakeInstanceNotReallyClone)).toBeFalsy(); + }); + + it('should convert entity to plain object', () => { + const fakeInstance = new FakeEntity({ + id: 'some-id', + createdAt: new Date('2023-06-28T05:00:00Z'), + updatedAt: new Date('2023-06-28T06:00:00Z'), + props: { + name: 'some-name', + }, + }); + expect(fakeInstance.toObject()).toEqual({ + id: 'some-id', + createdAt: new Date('2023-06-28T05:00:00.000Z'), + updatedAt: new Date('2023-06-28T06:00:00.000Z'), + name: 'some-name', + }); + }); + + it('should throw an exception if props number is too high', () => { + interface BigFakeProps { + prop1: string; + prop2: string; + prop3: string; + prop4: string; + prop5: string; + prop6: string; + prop7: string; + prop8: string; + prop9: string; + prop10: string; + prop11: string; + prop12: string; + prop13: string; + prop14: string; + prop15: string; + prop16: string; + prop17: string; + prop18: string; + prop19: string; + prop20: string; + prop21: string; + prop22: string; + prop23: string; + prop24: string; + prop25: string; + prop26: string; + prop27: string; + prop28: string; + prop29: string; + prop30: string; + prop31: string; + prop32: string; + prop33: string; + prop34: string; + prop35: string; + prop36: string; + prop37: string; + prop38: string; + prop39: string; + prop40: string; + prop41: string; + prop42: string; + prop43: string; + prop44: string; + prop45: string; + prop46: string; + prop47: string; + prop48: string; + prop49: string; + prop50: string; + prop51: string; + } + + class BigFakeEntity extends Entity { + protected _id: string; + + validate(): void { + // not implemented + } + } + expect( + () => + new BigFakeEntity({ + id: 'some-id', + props: { + prop1: 'some-name', + prop2: 'some-name', + prop3: 'some-name', + prop4: 'some-name', + prop5: 'some-name', + prop6: 'some-name', + prop7: 'some-name', + prop8: 'some-name', + prop9: 'some-name', + prop10: 'some-name', + prop11: 'some-name', + prop12: 'some-name', + prop13: 'some-name', + prop14: 'some-name', + prop15: 'some-name', + prop16: 'some-name', + prop17: 'some-name', + prop18: 'some-name', + prop19: 'some-name', + prop20: 'some-name', + prop21: 'some-name', + prop22: 'some-name', + prop23: 'some-name', + prop24: 'some-name', + prop25: 'some-name', + prop26: 'some-name', + prop27: 'some-name', + prop28: 'some-name', + prop29: 'some-name', + prop30: 'some-name', + prop31: 'some-name', + prop32: 'some-name', + prop33: 'some-name', + prop34: 'some-name', + prop35: 'some-name', + prop36: 'some-name', + prop37: 'some-name', + prop38: 'some-name', + prop39: 'some-name', + prop40: 'some-name', + prop41: 'some-name', + prop42: 'some-name', + prop43: 'some-name', + prop44: 'some-name', + prop45: 'some-name', + prop46: 'some-name', + prop47: 'some-name', + prop48: 'some-name', + prop49: 'some-name', + prop50: 'some-name', + prop51: 'some-name', + }, + }), + ).toThrow(ArgumentOutOfRangeException); + }); +}); diff --git a/src/libs/tests/unit/ddd/query.base.spec.ts b/src/libs/tests/unit/ddd/query.base.spec.ts new file mode 100644 index 0000000..9d93d8f --- /dev/null +++ b/src/libs/tests/unit/ddd/query.base.spec.ts @@ -0,0 +1,40 @@ +import { + PaginatedParams, + PaginatedQueryBase, + QueryBase, +} from '@libs/ddd/query.base'; + +class FakeQuery extends QueryBase { + readonly id: string; + + constructor(id: string) { + super(); + this.id = id; + } +} + +describe('Query Base', () => { + it('should define a query based object instance', () => { + const fakeQuery = new FakeQuery('some-id'); + expect(fakeQuery).toBeDefined(); + }); +}); + +class FakePaginatedQuery extends PaginatedQueryBase { + readonly id: string; + + constructor(props: PaginatedParams) { + super(props); + this.id = props.id; + } +} + +describe('Paginated Query Base', () => { + it('should define a paginated query based object instance', () => { + const fakePaginatedQuery = new FakePaginatedQuery({ + id: 'some-id', + page: 1, + }); + expect(fakePaginatedQuery).toBeDefined(); + }); +}); diff --git a/src/libs/tests/unit/ddd/value-object.base.spec.ts b/src/libs/tests/unit/ddd/value-object.base.spec.ts new file mode 100644 index 0000000..2b99005 --- /dev/null +++ b/src/libs/tests/unit/ddd/value-object.base.spec.ts @@ -0,0 +1,42 @@ +import { ValueObject } from '@libs/ddd'; + +interface FakeProps { + name: string; +} + +class FakeValueObject extends ValueObject { + get name(): string { + return this.props.name; + } + + // eslint-disable-next-line @typescript-eslint/no-unused-vars + protected validate(props: FakeProps): void { + return; + } +} + +describe('Value Object Base', () => { + it('should create a base value object', () => { + const fakeValueObject = new FakeValueObject({ name: 'fakeName' }); + expect(fakeValueObject).toBeDefined(); + expect(ValueObject.isValueObject(fakeValueObject)).toBeTruthy(); + }); + + it('should compare value objects', () => { + const fakeValueObject = new FakeValueObject({ name: 'fakeName' }); + const fakeValueObjectClone = new FakeValueObject({ name: 'fakeName' }); + const undefinedFakeValueObject: FakeValueObject = undefined; + const nullFakeValueObject: FakeValueObject = null; + expect(fakeValueObject.equals(undefinedFakeValueObject)).toBeFalsy(); + expect(fakeValueObject.equals(nullFakeValueObject)).toBeFalsy(); + expect(fakeValueObject.equals(fakeValueObject)).toBeTruthy(); + expect(fakeValueObject.equals(fakeValueObjectClone)).toBeTruthy(); + }); + + it('should unpack value object props', () => { + const fakeValueObject = new FakeValueObject({ name: 'fakeName' }); + expect(fakeValueObject.unpack()).toEqual({ + name: 'fakeName', + }); + }); +}); From 6fb473fa6035f9523f5f70a12e4eb08e67b3b7d4 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:19:43 +0200 Subject: [PATCH 23/29] add event handlers --- src/app.module.ts | 1 - src/libs/db/prisma-repository.base.ts | 1 + src/modules/ad/ad.module.ts | 4 + ...when-ad-is-created.domain-event-handler.ts | 21 +++++ ...when-ad-is-created.domain-event-handler.ts | 18 ++++ ...ad-is-created.domain-event-handler.spec.ts | 94 +++++++++++++++++++ ...ad-is-created.domain-event-handler.spec.ts | 94 +++++++++++++++++++ 7 files changed, 232 insertions(+), 1 deletion(-) create mode 100644 src/modules/ad/core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler.ts create mode 100644 src/modules/ad/core/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts create mode 100644 src/modules/ad/tests/unit/core/publish-log-message-when-ad-is-created.domain-event-handler.spec.ts create mode 100644 src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts diff --git a/src/app.module.ts b/src/app.module.ts index ebb535c..a457cad 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,6 +1,5 @@ import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; -// import { HealthModule } from './modules/health/health.module'; import { AdModule } from './modules/ad/ad.module'; import { MessageBrokerModule, diff --git a/src/libs/db/prisma-repository.base.ts b/src/libs/db/prisma-repository.base.ts index 897834a..cfb1ed7 100644 --- a/src/libs/db/prisma-repository.base.ts +++ b/src/libs/db/prisma-repository.base.ts @@ -41,6 +41,7 @@ export abstract class PrismaRepositoryBase< await this.prisma.create({ data: this.mapper.toPersistence(entity), }); + entity.publishEvents(this.logger, this.eventEmitter); } catch (e) { if (e instanceof Prisma.PrismaClientKnownRequestError) { if (e.message.includes('Already exists')) { diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 40dd238..15bc8fb 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -22,6 +22,8 @@ import { PrismaService } from '@libs/db/prisma.service'; import { TimeConverter } from './infrastructure/time-converter'; import { FindAdByIdGrpcController } from './interface/grpc-controllers/find-ad-by-id.grpc.controller'; import { FindAdByIdQueryHandler } from './core/queries/find-ad-by-id/find-ad-by-id.query-handler'; +import { PublishMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-message-when-ad-is-created.domain-event-handler'; +import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler'; @Module({ imports: [CqrsModule], @@ -29,6 +31,8 @@ import { FindAdByIdQueryHandler } from './core/queries/find-ad-by-id/find-ad-by- providers: [ CreateAdService, FindAdByIdQueryHandler, + PublishMessageWhenAdIsCreatedDomainEventHandler, + PublishLogMessageWhenAdIsCreatedDomainEventHandler, PrismaService, AdMapper, { diff --git a/src/modules/ad/core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler.ts b/src/modules/ad/core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler.ts new file mode 100644 index 0000000..7913c7b --- /dev/null +++ b/src/modules/ad/core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler.ts @@ -0,0 +1,21 @@ +import { MessagePublisherPort } from '@libs/ports/message-publisher.port'; +import { Inject, Injectable } from '@nestjs/common'; +import { OnEvent } from '@nestjs/event-emitter'; +import { MESSAGE_PUBLISHER } from '@src/app.constants'; +import { AdCreatedDomainEvent } from '../events/ad-created.domain-events'; + +@Injectable() +export class PublishLogMessageWhenAdIsCreatedDomainEventHandler { + constructor( + @Inject(MESSAGE_PUBLISHER) + private readonly messagePublisher: MessagePublisherPort, + ) {} + + @OnEvent(AdCreatedDomainEvent.name, { async: true, promisify: true }) + async handle(event: AdCreatedDomainEvent): Promise { + this.messagePublisher.publish( + 'logging.ad.created.info', + JSON.stringify(event), + ); + } +} diff --git a/src/modules/ad/core/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts b/src/modules/ad/core/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts new file mode 100644 index 0000000..69e4d38 --- /dev/null +++ b/src/modules/ad/core/event-handlers/publish-message-when-ad-is-created.domain-event-handler.ts @@ -0,0 +1,18 @@ +import { MessagePublisherPort } from '@libs/ports/message-publisher.port'; +import { Inject, Injectable } from '@nestjs/common'; +import { OnEvent } from '@nestjs/event-emitter'; +import { MESSAGE_PUBLISHER } from '@src/app.constants'; +import { AdCreatedDomainEvent } from '../events/ad-created.domain-events'; + +@Injectable() +export class PublishMessageWhenAdIsCreatedDomainEventHandler { + constructor( + @Inject(MESSAGE_PUBLISHER) + private readonly messagePublisher: MessagePublisherPort, + ) {} + + @OnEvent(AdCreatedDomainEvent.name, { async: true, promisify: true }) + async handle(event: AdCreatedDomainEvent): Promise { + this.messagePublisher.publish('ad.created', JSON.stringify(event)); + } +} diff --git a/src/modules/ad/tests/unit/core/publish-log-message-when-ad-is-created.domain-event-handler.spec.ts b/src/modules/ad/tests/unit/core/publish-log-message-when-ad-is-created.domain-event-handler.spec.ts new file mode 100644 index 0000000..2dc72fd --- /dev/null +++ b/src/modules/ad/tests/unit/core/publish-log-message-when-ad-is-created.domain-event-handler.spec.ts @@ -0,0 +1,94 @@ +import { Frequency } from '@modules/ad/core/ad.types'; +import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from '@modules/ad/core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler'; +import { AdCreatedDomainEvent } from '@modules/ad/core/events/ad-created.domain-events'; +import { Test, TestingModule } from '@nestjs/testing'; +import { MESSAGE_PUBLISHER } from '@src/app.constants'; + +const mockMessagePublisher = { + publish: jest.fn().mockImplementation(), +}; + +describe('Publish log message when ad is created domain event handler', () => { + let publishLogMessageWhenAdIsCreatedDomainEventHandler: PublishLogMessageWhenAdIsCreatedDomainEventHandler; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: MESSAGE_PUBLISHER, + useValue: mockMessagePublisher, + }, + PublishLogMessageWhenAdIsCreatedDomainEventHandler, + ], + }).compile(); + + publishLogMessageWhenAdIsCreatedDomainEventHandler = + module.get( + PublishLogMessageWhenAdIsCreatedDomainEventHandler, + ); + }); + + it('should publish a log message', () => { + jest.spyOn(mockMessagePublisher, 'publish'); + const adCreatedDomainEvent: AdCreatedDomainEvent = { + id: 'some-domain-event-id', + aggregateId: 'some-aggregate-id', + userId: 'some-user-id', + driver: false, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: '2023-06-28', + toDate: '2023-06-28', + monTime: undefined, + tueTime: undefined, + wedTime: '07:15', + thuTime: undefined, + friTime: undefined, + satTime: undefined, + sunTime: undefined, + monMarginDuration: 900, + tueMarginDuration: 900, + wedMarginDuration: 900, + thuMarginDuration: 900, + friMarginDuration: 900, + satMarginDuration: 900, + sunMarginDuration: 900, + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [ + { + position: 0, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + lat: 48.689445, + lon: 6.1765102, + }, + { + position: 1, + locality: 'Paris', + postalCode: '75000', + country: 'France', + lat: 48.8566, + lon: 2.3522, + }, + ], + metadata: { + timestamp: new Date('2023-06-28T05:00:00Z').getTime(), + correlationId: 'some-correlation-id', + }, + }; + publishLogMessageWhenAdIsCreatedDomainEventHandler.handle( + adCreatedDomainEvent, + ); + expect(publishLogMessageWhenAdIsCreatedDomainEventHandler).toBeDefined(); + expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1); + expect(mockMessagePublisher.publish).toHaveBeenCalledWith( + 'logging.ad.created.info', + '{"id":"some-domain-event-id","aggregateId":"some-aggregate-id","userId":"some-user-id","driver":false,"passenger":true,"frequency":"PUNCTUAL","fromDate":"2023-06-28","toDate":"2023-06-28","wedTime":"07:15","monMarginDuration":900,"tueMarginDuration":900,"wedMarginDuration":900,"thuMarginDuration":900,"friMarginDuration":900,"satMarginDuration":900,"sunMarginDuration":900,"seatsProposed":3,"seatsRequested":1,"strict":false,"waypoints":[{"position":0,"houseNumber":"5","street":"Avenue Foch","locality":"Nancy","postalCode":"54000","country":"France","lat":48.689445,"lon":6.1765102},{"position":1,"locality":"Paris","postalCode":"75000","country":"France","lat":48.8566,"lon":2.3522}],"metadata":{"timestamp":1687928400000,"correlationId":"some-correlation-id"}}', + ); + }); +}); diff --git a/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts b/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts new file mode 100644 index 0000000..d07fc97 --- /dev/null +++ b/src/modules/ad/tests/unit/core/publish-message-when-ad-is-created.domain-event-handler.spec.ts @@ -0,0 +1,94 @@ +import { Frequency } from '@modules/ad/core/ad.types'; +import { PublishMessageWhenAdIsCreatedDomainEventHandler } from '@modules/ad/core/event-handlers/publish-message-when-ad-is-created.domain-event-handler'; +import { AdCreatedDomainEvent } from '@modules/ad/core/events/ad-created.domain-events'; +import { Test, TestingModule } from '@nestjs/testing'; +import { MESSAGE_PUBLISHER } from '@src/app.constants'; + +const mockMessagePublisher = { + publish: jest.fn().mockImplementation(), +}; + +describe('Publish message when ad is created domain event handler', () => { + let publishMessageWhenAdIsCreatedDomainEventHandler: PublishMessageWhenAdIsCreatedDomainEventHandler; + + beforeAll(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + { + provide: MESSAGE_PUBLISHER, + useValue: mockMessagePublisher, + }, + PublishMessageWhenAdIsCreatedDomainEventHandler, + ], + }).compile(); + + publishMessageWhenAdIsCreatedDomainEventHandler = + module.get( + PublishMessageWhenAdIsCreatedDomainEventHandler, + ); + }); + + it('should publish a message', () => { + jest.spyOn(mockMessagePublisher, 'publish'); + const adCreatedDomainEvent: AdCreatedDomainEvent = { + id: 'some-domain-event-id', + aggregateId: 'some-aggregate-id', + userId: 'some-user-id', + driver: false, + passenger: true, + frequency: Frequency.PUNCTUAL, + fromDate: '2023-06-28', + toDate: '2023-06-28', + monTime: undefined, + tueTime: undefined, + wedTime: '07:15', + thuTime: undefined, + friTime: undefined, + satTime: undefined, + sunTime: undefined, + monMarginDuration: 900, + tueMarginDuration: 900, + wedMarginDuration: 900, + thuMarginDuration: 900, + friMarginDuration: 900, + satMarginDuration: 900, + sunMarginDuration: 900, + seatsProposed: 3, + seatsRequested: 1, + strict: false, + waypoints: [ + { + position: 0, + houseNumber: '5', + street: 'Avenue Foch', + locality: 'Nancy', + postalCode: '54000', + country: 'France', + lat: 48.689445, + lon: 6.1765102, + }, + { + position: 1, + locality: 'Paris', + postalCode: '75000', + country: 'France', + lat: 48.8566, + lon: 2.3522, + }, + ], + metadata: { + timestamp: new Date('2023-06-28T05:00:00Z').getTime(), + correlationId: 'some-correlation-id', + }, + }; + publishMessageWhenAdIsCreatedDomainEventHandler.handle( + adCreatedDomainEvent, + ); + expect(publishMessageWhenAdIsCreatedDomainEventHandler).toBeDefined(); + expect(mockMessagePublisher.publish).toHaveBeenCalledTimes(1); + expect(mockMessagePublisher.publish).toHaveBeenCalledWith( + 'ad.created', + '{"id":"some-domain-event-id","aggregateId":"some-aggregate-id","userId":"some-user-id","driver":false,"passenger":true,"frequency":"PUNCTUAL","fromDate":"2023-06-28","toDate":"2023-06-28","wedTime":"07:15","monMarginDuration":900,"tueMarginDuration":900,"wedMarginDuration":900,"thuMarginDuration":900,"friMarginDuration":900,"satMarginDuration":900,"sunMarginDuration":900,"seatsProposed":3,"seatsRequested":1,"strict":false,"waypoints":[{"position":0,"houseNumber":"5","street":"Avenue Foch","locality":"Nancy","postalCode":"54000","country":"France","lat":48.689445,"lon":6.1765102},{"position":1,"locality":"Paris","postalCode":"75000","country":"France","lat":48.8566,"lon":2.3522}],"metadata":{"timestamp":1687928400000,"correlationId":"some-correlation-id"}}', + ); + }); +}); From d9fc46513d384d6f10fdfddb3934d5b0bdf2eeb2 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:21:10 +0200 Subject: [PATCH 24/29] remove automap --- package-lock.json | 64 +++++++++++------------------------------------ package.json | 3 --- 2 files changed, 14 insertions(+), 53 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7d0b76b..e6301ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,6 @@ "version": "0.0.1", "license": "AGPL", "dependencies": { - "@automapper/classes": "^8.7.7", - "@automapper/core": "^8.7.7", - "@automapper/nestjs": "^8.7.7", "@grpc/grpc-js": "^1.8.14", "@grpc/proto-loader": "^0.7.6", "@liaoliaots/nestjs-redis": "^9.0.5", @@ -62,6 +59,15 @@ "typescript": "^4.7.4" } }, + "node_modules/@aashutoshrathi/word-wrap": { + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz", + "integrity": "sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/@acuminous/bitsyntax": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/@acuminous/bitsyntax/-/bitsyntax-0.1.2.tgz", @@ -180,39 +186,6 @@ "node": ">=12.0.0" } }, - "node_modules/@automapper/classes": { - "version": "8.7.7", - "resolved": "https://registry.npmjs.org/@automapper/classes/-/classes-8.7.7.tgz", - "integrity": "sha512-FSbvt6QE8XnhKKQZA3kpKLuLrr9x1iW+lNYTrawVLjxQ05zsCGccLxe7moMNrg1wFAVAouQKupFgCGQ7XRjmJw==", - "engines": { - "node": ">=14.0.0" - }, - "peerDependencies": { - "@automapper/core": "8.7.7", - "reflect-metadata": "~0.1.13" - } - }, - "node_modules/@automapper/core": { - "version": "8.7.7", - "resolved": "https://registry.npmjs.org/@automapper/core/-/core-8.7.7.tgz", - "integrity": "sha512-YfpDJ/xqwUuC0S+BLNk81ZJfeL7CmjirUX/Gk9eQyx146DKvneBZgeZ9v5rDB51Ti14jTxVHis+5JuT7W/q0TA==", - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/@automapper/nestjs": { - "version": "8.7.7", - "resolved": "https://registry.npmjs.org/@automapper/nestjs/-/nestjs-8.7.7.tgz", - "integrity": "sha512-9/uYY2cmN7SJjr2QxnfyXsteHrn/RHD+Dg0VMBflzK/e8Bh/KWyOve7+kaFixlUoyHe44aXs2LVaCslqt8wnhQ==", - "engines": { - "node": ">=14.0.0" - }, - "peerDependencies": { - "@automapper/core": "8.7.7", - "@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0", - "@nestjs/core": "^7.0.0 || ^8.0.0 || ^9.0.0" - } - }, "node_modules/@babel/code-frame": { "version": "7.22.5", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.5.tgz", @@ -6853,17 +6826,17 @@ } }, "node_modules/optionator": { - "version": "0.9.1", - "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.1.tgz", - "integrity": "sha512-74RlY5FCnhq4jRxVUPKDaRwrVNXMqsGsiW6AJw4XK8hmtm10wC0ypZBLw5IIp85NZMr91+qd1RvvENwg7jjRFw==", + "version": "0.9.3", + "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", + "integrity": "sha512-JjCoypp+jKn1ttEFExxhetCKeJt9zhAgAve5FXHixTvFDW/5aEktX9bufBKLRRMdU7bNtpLfcGu94B3cdEJgjg==", "dev": true, "dependencies": { + "@aashutoshrathi/word-wrap": "^1.2.3", "deep-is": "^0.1.3", "fast-levenshtein": "^2.0.6", "levn": "^0.4.1", "prelude-ls": "^1.2.1", - "type-check": "^0.4.0", - "word-wrap": "^1.2.3" + "type-check": "^0.4.0" }, "engines": { "node": ">= 0.8.0" @@ -8983,15 +8956,6 @@ "node": ">=8.12.0" } }, - "node_modules/word-wrap": { - "version": "1.2.3", - "resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz", - "integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==", - "dev": true, - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/wrap-ansi": { "version": "7.0.0", "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz", diff --git a/package.json b/package.json index ad046cd..baf3060 100644 --- a/package.json +++ b/package.json @@ -34,9 +34,6 @@ "migrate:deploy": "npx prisma migrate deploy" }, "dependencies": { - "@automapper/classes": "^8.7.7", - "@automapper/core": "^8.7.7", - "@automapper/nestjs": "^8.7.7", "@grpc/grpc-js": "^1.8.14", "@grpc/proto-loader": "^0.7.6", "@liaoliaots/nestjs-redis": "^9.0.5", From 9728ed69ab44d00c2ef0171bf7628e70f855a9dc Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:24:42 +0200 Subject: [PATCH 25/29] refactor private property in health module --- .../core/usecases/repositories.health-indicator.usecase.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts b/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts index 36e95c1..255263b 100644 --- a/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts +++ b/src/modules/health/core/usecases/repositories.health-indicator.usecase.ts @@ -14,7 +14,7 @@ import { MessagePublisherPort } from '@libs/ports/message-publisher.port'; @Injectable() export class RepositoriesHealthIndicatorUseCase extends HealthIndicator { - private checkRepositories: CheckRepositoryPort[]; + private _checkRepositories: CheckRepositoryPort[]; constructor( @Inject(AD_REPOSITORY) private readonly adRepository: AdRepositoryPort, @@ -22,12 +22,12 @@ export class RepositoriesHealthIndicatorUseCase extends HealthIndicator { private readonly messagePublisher: MessagePublisherPort, ) { super(); - this.checkRepositories = [adRepository]; + this._checkRepositories = [adRepository]; } isHealthy = async (key: string): Promise => { try { await Promise.all( - this.checkRepositories.map( + this._checkRepositories.map( async (checkRepository: CheckRepositoryPort) => { await checkRepository.healthCheck(); }, From 2d612a77d8800528cae0269637e765933bf1ea1c Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:26:48 +0200 Subject: [PATCH 26/29] pretty --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 546b2f7..ed12947 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,7 +20,7 @@ "paths": { "@libs/*": ["src/libs/*"], "@modules/*": ["src/modules/*"], - "@src/*": ["src/*"], + "@src/*": ["src/*"] } } } From aa06b8c54c1822a82123b991a49577773451f423 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:29:33 +0200 Subject: [PATCH 27/29] refactor main.ts --- src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 505ada1..e12915e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,7 +19,7 @@ async function bootstrap() { 'modules/health/interface/grpc-controllers/health.proto', ), ], - url: process.env.SERVICE_URL + ':' + process.env.SERVICE_PORT, + url: `${process.env.SERVICE_URL}:${process.env.SERVICE_PORT}`, loader: { keepCase: true }, }, }); From 74a855c1b9dbe029db9b990f3a4588f932accf8f Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:47:47 +0200 Subject: [PATCH 28/29] refactor modules --- src/app.module.ts | 2 - src/modules/ad/ad.module.ts | 90 +++++++++++++++++++---------- src/modules/health/health.module.ts | 44 ++++++++------ 3 files changed, 85 insertions(+), 51 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index a457cad..a2b6bc2 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -53,7 +53,5 @@ import { HealthModule } from '@modules/health/health.module'; HealthModule, AdModule, ], - controllers: [], - providers: [], }) export class AppModule {} diff --git a/src/modules/ad/ad.module.ts b/src/modules/ad/ad.module.ts index 15bc8fb..5a5e3ce 100644 --- a/src/modules/ad/ad.module.ts +++ b/src/modules/ad/ad.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Module, Provider } from '@nestjs/common'; import { CreateAdGrpcController } from './interface/grpc-controllers/create-ad.grpc.controller'; import { CqrsModule } from '@nestjs/cqrs'; import { @@ -25,40 +25,66 @@ import { FindAdByIdQueryHandler } from './core/queries/find-ad-by-id/find-ad-by- import { PublishMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-message-when-ad-is-created.domain-event-handler'; import { PublishLogMessageWhenAdIsCreatedDomainEventHandler } from './core/event-handlers/publish-log-message-when-ad-is-created.domain-event-handler'; +const grpcControllers = [CreateAdGrpcController, FindAdByIdGrpcController]; + +const eventHandlers: Provider[] = [ + PublishMessageWhenAdIsCreatedDomainEventHandler, + PublishLogMessageWhenAdIsCreatedDomainEventHandler, +]; + +const commandHandlers: Provider[] = [CreateAdService]; + +const queryHandlers: Provider[] = [FindAdByIdQueryHandler]; + +const mappers: Provider[] = [AdMapper]; + +const repositories: Provider[] = [ + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, +]; + +const messageBrokers: Provider[] = [ + { + provide: MESSAGE_BROKER_PUBLISHER, + useClass: MessageBrokerPublisher, + }, + { + provide: MESSAGE_PUBLISHER, + useClass: MessagePublisher, + }, +]; + +const orms: Provider[] = [PrismaService]; + +const utilities: Provider[] = [ + { + provide: PARAMS_PROVIDER, + useClass: DefaultParamsProvider, + }, + { + provide: TIMEZONE_FINDER, + useClass: TimezoneFinder, + }, + { + provide: TIME_CONVERTER, + useClass: TimeConverter, + }, +]; + @Module({ imports: [CqrsModule], - controllers: [CreateAdGrpcController, FindAdByIdGrpcController], + controllers: [...grpcControllers], providers: [ - CreateAdService, - FindAdByIdQueryHandler, - PublishMessageWhenAdIsCreatedDomainEventHandler, - PublishLogMessageWhenAdIsCreatedDomainEventHandler, - PrismaService, - AdMapper, - { - provide: AD_REPOSITORY, - useClass: AdRepository, - }, - { - provide: PARAMS_PROVIDER, - useClass: DefaultParamsProvider, - }, - { - provide: MESSAGE_BROKER_PUBLISHER, - useClass: MessageBrokerPublisher, - }, - { - provide: MESSAGE_PUBLISHER, - useClass: MessagePublisher, - }, - { - provide: TIMEZONE_FINDER, - useClass: TimezoneFinder, - }, - { - provide: TIME_CONVERTER, - useClass: TimeConverter, - }, + ...eventHandlers, + ...commandHandlers, + ...queryHandlers, + ...mappers, + ...repositories, + ...messageBrokers, + ...orms, + ...utilities, ], exports: [ PrismaService, diff --git a/src/modules/health/health.module.ts b/src/modules/health/health.module.ts index 6b04c26..988cd14 100644 --- a/src/modules/health/health.module.ts +++ b/src/modules/health/health.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { Module, Provider } from '@nestjs/common'; import { HealthHttpController } from './interface/http-controllers/health.http.controller'; import { TerminusModule } from '@nestjs/terminus'; import { MESSAGE_BROKER_PUBLISHER, MESSAGE_PUBLISHER } from 'src/app.constants'; @@ -10,23 +10,33 @@ import { AD_REPOSITORY } from './health.di-tokens'; import { HealthGrpcController } from './interface/grpc-controllers/health.grpc.controller'; import { AdModule } from '@modules/ad/ad.module'; +const grpcControllers = [HealthGrpcController]; + +const httpControllers = [HealthHttpController]; + +const useCases: Provider[] = [RepositoriesHealthIndicatorUseCase]; + +const repositories: Provider[] = [ + { + provide: AD_REPOSITORY, + useClass: AdRepository, + }, +]; + +const messageBrokers: Provider[] = [ + { + provide: MESSAGE_BROKER_PUBLISHER, + useClass: MessageBrokerPublisher, + }, + { + provide: MESSAGE_PUBLISHER, + useClass: MessagePublisher, + }, +]; + @Module({ imports: [TerminusModule, AdModule], - controllers: [HealthGrpcController, HealthHttpController], - providers: [ - RepositoriesHealthIndicatorUseCase, - { - provide: AD_REPOSITORY, - useClass: AdRepository, - }, - { - provide: MESSAGE_BROKER_PUBLISHER, - useClass: MessageBrokerPublisher, - }, - { - provide: MESSAGE_PUBLISHER, - useClass: MessagePublisher, - }, - ], + controllers: [...grpcControllers, ...httpControllers], + providers: [...useCases, ...repositories, ...messageBrokers], }) export class HealthModule {} From d00fb431046008c90f0148779ca85c4ea8352e73 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 10:50:09 +0200 Subject: [PATCH 29/29] 1.0.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index e6301ea..6eca592 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@mobicoop/ad", - "version": "0.0.1", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@mobicoop/ad", - "version": "0.0.1", + "version": "1.0.0", "license": "AGPL", "dependencies": { "@grpc/grpc-js": "^1.8.14", diff --git a/package.json b/package.json index baf3060..c8bc9a0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mobicoop/ad", - "version": "0.0.1", + "version": "1.0.0", "description": "Mobicoop V3 Ad", "author": "sbriat", "private": true,