mm-share-lib 0.0.14 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +25 -25
- package/.prettierrc +3 -3
- package/README.md +73 -73
- package/index.ts +1 -1
- package/nest-cli.json +8 -8
- package/package.json +91 -91
- package/src/constant/entity-state.constant.ts +4 -4
- package/src/constant/index.ts +1 -1
- package/src/dto/index.ts +2 -2
- package/src/exception/bad-request-error.exception.spec.ts +24 -24
- package/src/exception/bad-request-error.exception.ts +8 -8
- package/src/exception/base.exception.ts +36 -36
- package/src/exception/conflict-error.exception.spec.ts +23 -23
- package/src/exception/conflict-error.exception.ts +8 -8
- package/src/exception/forbidden-error.exception.spec.ts +23 -23
- package/src/exception/forbidden-error.exception.ts +8 -8
- package/src/exception/index.ts +7 -7
- package/src/exception/internal-server-error.exception.spec.ts +23 -23
- package/src/exception/internal-server-error.exception.ts +12 -12
- package/src/exception/not-found-error.exception.spec.ts +23 -23
- package/src/exception/not-found-error.exception.ts +8 -8
- package/src/exception/unauthorized-error.exception.spec.ts +23 -23
- package/src/exception/unauthorized-error.exception.ts +12 -12
- package/src/filter/http-exception.filter.ts +23 -23
- package/src/filter/index.ts +1 -1
- package/src/filter/rpc-exception.filter.ts +15 -15
- package/src/generic/entity/entity.generic.ts +34 -34
- package/src/generic/entity/index.ts +1 -1
- package/src/generic/index.ts +2 -2
- package/src/generic/service/index.ts +1 -1
- package/src/generic/service/service.generic.ts +112 -112
- package/src/lib/datasource-mapper/datasource-mapper.ts +39 -0
- package/src/lib/datasource-mapper/index.ts +1 -0
- package/src/lib/index.ts +1 -0
- package/src/lib/search-engine/interface/index.ts +1 -1
- package/src/lib/search-engine/interface/search-document.interface.ts +13 -13
- package/src/lib/search-engine/interface/transform-service.interface.ts +10 -10
- package/src/lib/search-engine/typesense/service/client.service.ts +37 -24
- package/src/setup/index.ts +1 -1
- package/src/util/app.utils.ts +18 -18
- package/src/util/date.util.spec.ts +49 -49
- package/src/util/date.util.ts +10 -10
- package/src/util/generator.util.spec.ts +79 -79
- package/src/util/generator.util.ts +34 -34
- package/src/util/index.ts +4 -4
- package/test/app.e2e-spec.ts +24 -24
- package/test/jest-e2e.json +9 -9
- package/tsconfig.build.json +4 -4
- package/tsconfig.json +22 -21
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -18
- package/dist/index.js.map +0 -1
- package/dist/src/auth/apple/apple.module.d.ts +0 -2
- package/dist/src/auth/apple/apple.module.js +0 -22
- package/dist/src/auth/apple/apple.module.js.map +0 -1
- package/dist/src/auth/apple/apple.service.d.ts +0 -9
- package/dist/src/auth/apple/apple.service.js +0 -38
- package/dist/src/auth/apple/apple.service.js.map +0 -1
- package/dist/src/auth/apple/dto/apple-login.dto.d.ts +0 -5
- package/dist/src/auth/apple/dto/apple-login.dto.js +0 -33
- package/dist/src/auth/apple/dto/apple-login.dto.js.map +0 -1
- package/dist/src/auth/apple/dto/index.d.ts +0 -1
- package/dist/src/auth/apple/dto/index.js +0 -18
- package/dist/src/auth/apple/dto/index.js.map +0 -1
- package/dist/src/auth/apple/index.d.ts +0 -3
- package/dist/src/auth/apple/index.js +0 -20
- package/dist/src/auth/apple/index.js.map +0 -1
- package/dist/src/auth/config/apple.config.d.ts +0 -3
- package/dist/src/auth/config/apple.config.js +0 -29
- package/dist/src/auth/config/apple.config.js.map +0 -1
- package/dist/src/auth/config/auth.config.d.ts +0 -3
- package/dist/src/auth/config/auth.config.js +0 -42
- package/dist/src/auth/config/auth.config.js.map +0 -1
- package/dist/src/auth/config/facebook.config.d.ts +0 -3
- package/dist/src/auth/config/facebook.config.js +0 -34
- package/dist/src/auth/config/facebook.config.js.map +0 -1
- package/dist/src/auth/config/file.config.d.ts +0 -3
- package/dist/src/auth/config/file.config.js +0 -66
- package/dist/src/auth/config/file.config.js.map +0 -1
- package/dist/src/auth/config/google.config.d.ts +0 -3
- package/dist/src/auth/config/google.config.js +0 -34
- package/dist/src/auth/config/google.config.js.map +0 -1
- package/dist/src/auth/config/index.d.ts +0 -6
- package/dist/src/auth/config/index.js +0 -23
- package/dist/src/auth/config/index.js.map +0 -1
- package/dist/src/auth/config/twitter.config.d.ts +0 -8
- package/dist/src/auth/config/twitter.config.js +0 -34
- package/dist/src/auth/config/twitter.config.js.map +0 -1
- package/dist/src/auth/facebook/dto/facebook-login.dto.d.ts +0 -3
- package/dist/src/auth/facebook/dto/facebook-login.dto.js +0 -23
- package/dist/src/auth/facebook/dto/facebook-login.dto.js.map +0 -1
- package/dist/src/auth/facebook/dto/index.d.ts +0 -1
- package/dist/src/auth/facebook/dto/index.js +0 -18
- package/dist/src/auth/facebook/dto/index.js.map +0 -1
- package/dist/src/auth/facebook/facebook.module.d.ts +0 -2
- package/dist/src/auth/facebook/facebook.module.js +0 -23
- package/dist/src/auth/facebook/facebook.module.js.map +0 -1
- package/dist/src/auth/facebook/facebook.service.d.ts +0 -10
- package/dist/src/auth/facebook/facebook.service.js +0 -50
- package/dist/src/auth/facebook/facebook.service.js.map +0 -1
- package/dist/src/auth/facebook/index.d.ts +0 -4
- package/dist/src/auth/facebook/index.js +0 -21
- package/dist/src/auth/facebook/index.js.map +0 -1
- package/dist/src/auth/facebook/interface/facebook.interface.d.ts +0 -6
- package/dist/src/auth/facebook/interface/facebook.interface.js +0 -3
- package/dist/src/auth/facebook/interface/facebook.interface.js.map +0 -1
- package/dist/src/auth/facebook/interface/index.d.ts +0 -1
- package/dist/src/auth/facebook/interface/index.js +0 -18
- package/dist/src/auth/facebook/interface/index.js.map +0 -1
- package/dist/src/auth/google/dto/google-login.dto.d.ts +0 -3
- package/dist/src/auth/google/dto/google-login.dto.js +0 -23
- package/dist/src/auth/google/dto/google-login.dto.js.map +0 -1
- package/dist/src/auth/google/dto/index.d.ts +0 -1
- package/dist/src/auth/google/dto/index.js +0 -18
- package/dist/src/auth/google/dto/index.js.map +0 -1
- package/dist/src/auth/google/google.module.d.ts +0 -2
- package/dist/src/auth/google/google.module.js +0 -22
- package/dist/src/auth/google/google.module.js.map +0 -1
- package/dist/src/auth/google/google.service.d.ts +0 -10
- package/dist/src/auth/google/google.service.js +0 -49
- package/dist/src/auth/google/google.service.js.map +0 -1
- package/dist/src/auth/google/index.d.ts +0 -3
- package/dist/src/auth/google/index.js +0 -20
- package/dist/src/auth/google/index.js.map +0 -1
- package/dist/src/auth/index.d.ts +0 -5
- package/dist/src/auth/index.js +0 -22
- package/dist/src/auth/index.js.map +0 -1
- package/dist/src/auth/twitter/dto/index.d.ts +0 -1
- package/dist/src/auth/twitter/dto/index.js +0 -18
- package/dist/src/auth/twitter/dto/index.js.map +0 -1
- package/dist/src/auth/twitter/dto/twitter-login.dto.d.ts +0 -4
- package/dist/src/auth/twitter/dto/twitter-login.dto.js +0 -28
- package/dist/src/auth/twitter/dto/twitter-login.dto.js.map +0 -1
- package/dist/src/auth/twitter/index.d.ts +0 -3
- package/dist/src/auth/twitter/index.js +0 -20
- package/dist/src/auth/twitter/index.js.map +0 -1
- package/dist/src/auth/twitter/twitter.module.d.ts +0 -2
- package/dist/src/auth/twitter/twitter.module.js +0 -23
- package/dist/src/auth/twitter/twitter.module.js.map +0 -1
- package/dist/src/auth/twitter/twitter.service.d.ts +0 -9
- package/dist/src/auth/twitter/twitter.service.js +0 -49
- package/dist/src/auth/twitter/twitter.service.js.map +0 -1
- package/dist/src/config/index.d.ts +0 -1
- package/dist/src/config/index.js +0 -18
- package/dist/src/config/index.js.map +0 -1
- package/dist/src/config/type.config.d.ts +0 -79
- package/dist/src/config/type.config.js +0 -3
- package/dist/src/config/type.config.js.map +0 -1
- package/dist/src/constant/entity-state.constant.d.ts +0 -4
- package/dist/src/constant/entity-state.constant.js +0 -9
- package/dist/src/constant/entity-state.constant.js.map +0 -1
- package/dist/src/constant/index.d.ts +0 -1
- package/dist/src/constant/index.js +0 -18
- package/dist/src/constant/index.js.map +0 -1
- package/dist/src/dto/base-filter.dto.d.ts +0 -4
- package/dist/src/dto/base-filter.dto.js +0 -11
- package/dist/src/dto/base-filter.dto.js.map +0 -1
- package/dist/src/dto/index.d.ts +0 -2
- package/dist/src/dto/index.js +0 -19
- package/dist/src/dto/index.js.map +0 -1
- package/dist/src/dto/pagination.dto.d.ts +0 -5
- package/dist/src/dto/pagination.dto.js +0 -37
- package/dist/src/dto/pagination.dto.js.map +0 -1
- package/dist/src/exception/bad-request-error.exception.d.ts +0 -4
- package/dist/src/exception/bad-request-error.exception.js +0 -12
- package/dist/src/exception/bad-request-error.exception.js.map +0 -1
- package/dist/src/exception/base.exception.d.ts +0 -21
- package/dist/src/exception/base.exception.js +0 -16
- package/dist/src/exception/base.exception.js.map +0 -1
- package/dist/src/exception/conflict-error.exception.d.ts +0 -4
- package/dist/src/exception/conflict-error.exception.js +0 -12
- package/dist/src/exception/conflict-error.exception.js.map +0 -1
- package/dist/src/exception/forbidden-error.exception.d.ts +0 -4
- package/dist/src/exception/forbidden-error.exception.js +0 -12
- package/dist/src/exception/forbidden-error.exception.js.map +0 -1
- package/dist/src/exception/index.d.ts +0 -7
- package/dist/src/exception/index.js +0 -24
- package/dist/src/exception/index.js.map +0 -1
- package/dist/src/exception/internal-server-error.exception.d.ts +0 -4
- package/dist/src/exception/internal-server-error.exception.js +0 -12
- package/dist/src/exception/internal-server-error.exception.js.map +0 -1
- package/dist/src/exception/not-found-error.exception.d.ts +0 -4
- package/dist/src/exception/not-found-error.exception.js +0 -12
- package/dist/src/exception/not-found-error.exception.js.map +0 -1
- package/dist/src/exception/unauthorized-error.exception.d.ts +0 -4
- package/dist/src/exception/unauthorized-error.exception.js +0 -12
- package/dist/src/exception/unauthorized-error.exception.js.map +0 -1
- package/dist/src/filter/http-exception.filter.d.ts +0 -4
- package/dist/src/filter/http-exception.filter.js +0 -27
- package/dist/src/filter/http-exception.filter.js.map +0 -1
- package/dist/src/filter/index.d.ts +0 -1
- package/dist/src/filter/index.js +0 -18
- package/dist/src/filter/index.js.map +0 -1
- package/dist/src/filter/rpc-exception.filter.d.ts +0 -6
- package/dist/src/filter/rpc-exception.filter.js +0 -21
- package/dist/src/filter/rpc-exception.filter.js.map +0 -1
- package/dist/src/generic/entity/entity.generic.d.ts +0 -10
- package/dist/src/generic/entity/entity.generic.js +0 -48
- package/dist/src/generic/entity/entity.generic.js.map +0 -1
- package/dist/src/generic/entity/index.d.ts +0 -1
- package/dist/src/generic/entity/index.js +0 -18
- package/dist/src/generic/entity/index.js.map +0 -1
- package/dist/src/generic/index.d.ts +0 -2
- package/dist/src/generic/index.js +0 -19
- package/dist/src/generic/index.js.map +0 -1
- package/dist/src/generic/service/index.d.ts +0 -1
- package/dist/src/generic/service/index.js +0 -18
- package/dist/src/generic/service/index.js.map +0 -1
- package/dist/src/generic/service/service.generic.d.ts +0 -18
- package/dist/src/generic/service/service.generic.js +0 -85
- package/dist/src/generic/service/service.generic.js.map +0 -1
- package/dist/src/index.d.ts +0 -11
- package/dist/src/index.js +0 -28
- package/dist/src/index.js.map +0 -1
- package/dist/src/interface/index.d.ts +0 -1
- package/dist/src/interface/index.js +0 -18
- package/dist/src/interface/index.js.map +0 -1
- package/dist/src/interface/social.interface.d.ts +0 -6
- package/dist/src/interface/social.interface.js +0 -3
- package/dist/src/interface/social.interface.js.map +0 -1
- package/dist/src/lib/index.d.ts +0 -4
- package/dist/src/lib/index.js +0 -21
- package/dist/src/lib/index.js.map +0 -1
- package/dist/src/lib/mailer/config/index.d.ts +0 -1
- package/dist/src/lib/mailer/config/index.js +0 -18
- package/dist/src/lib/mailer/config/index.js.map +0 -1
- package/dist/src/lib/mailer/config/mailer.config.d.ts +0 -3
- package/dist/src/lib/mailer/config/mailer.config.js +0 -72
- package/dist/src/lib/mailer/config/mailer.config.js.map +0 -1
- package/dist/src/lib/mailer/index.d.ts +0 -2
- package/dist/src/lib/mailer/index.js +0 -19
- package/dist/src/lib/mailer/index.js.map +0 -1
- package/dist/src/lib/mailer/mailer.module.d.ts +0 -2
- package/dist/src/lib/mailer/mailer.module.js +0 -20
- package/dist/src/lib/mailer/mailer.module.js.map +0 -1
- package/dist/src/lib/mailer/mailer.service.d.ts +0 -12
- package/dist/src/lib/mailer/mailer.service.js +0 -66
- package/dist/src/lib/mailer/mailer.service.js.map +0 -1
- package/dist/src/lib/redis/index.d.ts +0 -2
- package/dist/src/lib/redis/index.js +0 -7
- package/dist/src/lib/redis/index.js.map +0 -1
- package/dist/src/lib/search-engine/document/base.document.d.ts +0 -3
- package/dist/src/lib/search-engine/document/base.document.js +0 -7
- package/dist/src/lib/search-engine/document/base.document.js.map +0 -1
- package/dist/src/lib/search-engine/document/index.d.ts +0 -1
- package/dist/src/lib/search-engine/document/index.js +0 -18
- package/dist/src/lib/search-engine/document/index.js.map +0 -1
- package/dist/src/lib/search-engine/index.d.ts +0 -3
- package/dist/src/lib/search-engine/index.js +0 -20
- package/dist/src/lib/search-engine/index.js.map +0 -1
- package/dist/src/lib/search-engine/interface/index.d.ts +0 -2
- package/dist/src/lib/search-engine/interface/index.js +0 -19
- package/dist/src/lib/search-engine/interface/index.js.map +0 -1
- package/dist/src/lib/search-engine/interface/search-document.interface.d.ts +0 -4
- package/dist/src/lib/search-engine/interface/search-document.interface.js +0 -3
- package/dist/src/lib/search-engine/interface/search-document.interface.js.map +0 -1
- package/dist/src/lib/search-engine/interface/transform-service.interface.d.ts +0 -6
- package/dist/src/lib/search-engine/interface/transform-service.interface.js +0 -3
- package/dist/src/lib/search-engine/interface/transform-service.interface.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/collection/index.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/collection/index.js +0 -18
- package/dist/src/lib/search-engine/typesense/collection/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/document/index.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/document/index.js +0 -18
- package/dist/src/lib/search-engine/typesense/document/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/index.d.ts +0 -4
- package/dist/src/lib/search-engine/typesense/index.js +0 -21
- package/dist/src/lib/search-engine/typesense/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/metadata/index.d.ts +0 -2
- package/dist/src/lib/search-engine/typesense/metadata/index.js +0 -19
- package/dist/src/lib/search-engine/typesense/metadata/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/metadata/schema.metadata.d.ts +0 -13
- package/dist/src/lib/search-engine/typesense/metadata/schema.metadata.js +0 -3
- package/dist/src/lib/search-engine/typesense/metadata/schema.metadata.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.d.ts +0 -10
- package/dist/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.js +0 -33
- package/dist/src/lib/search-engine/typesense/metadata/typesense.metadata-registry.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/service/client.service.d.ts +0 -34
- package/dist/src/lib/search-engine/typesense/service/client.service.js +0 -200
- package/dist/src/lib/search-engine/typesense/service/client.service.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/service/index.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/service/index.js +0 -18
- package/dist/src/lib/search-engine/typesense/service/index.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense-module.interface.d.ts +0 -27
- package/dist/src/lib/search-engine/typesense/typesense-module.interface.js +0 -3
- package/dist/src/lib/search-engine/typesense/typesense-module.interface.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.constants.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.constants.js +0 -5
- package/dist/src/lib/search-engine/typesense/typesense.constants.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.module.d.ts +0 -8
- package/dist/src/lib/search-engine/typesense/typesense.module.js +0 -68
- package/dist/src/lib/search-engine/typesense/typesense.module.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.module.test.d.ts +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.module.test.js +0 -89
- package/dist/src/lib/search-engine/typesense/typesense.module.test.js.map +0 -1
- package/dist/src/lib/search-engine/typesense/typesense.providers.d.ts +0 -4
- package/dist/src/lib/search-engine/typesense/typesense.providers.js +0 -40
- package/dist/src/lib/search-engine/typesense/typesense.providers.js.map +0 -1
- package/dist/src/lib/storage/index.d.ts +0 -2
- package/dist/src/lib/storage/index.js +0 -7
- package/dist/src/lib/storage/index.js.map +0 -1
- package/dist/src/response/base-entity.response.d.ts +0 -3
- package/dist/src/response/base-entity.response.js +0 -23
- package/dist/src/response/base-entity.response.js.map +0 -1
- package/dist/src/response/index.d.ts +0 -2
- package/dist/src/response/index.js +0 -19
- package/dist/src/response/index.js.map +0 -1
- package/dist/src/response/login.response.d.ts +0 -0
- package/dist/src/response/login.response.js +0 -1
- package/dist/src/response/login.response.js.map +0 -1
- package/dist/src/response/pagination.response.d.ts +0 -17
- package/dist/src/response/pagination.response.js +0 -68
- package/dist/src/response/pagination.response.js.map +0 -1
- package/dist/src/setup/index.d.ts +0 -1
- package/dist/src/setup/index.js +0 -18
- package/dist/src/setup/index.js.map +0 -1
- package/dist/src/setup/microservice.setup.d.ts +0 -2
- package/dist/src/setup/microservice.setup.js +0 -25
- package/dist/src/setup/microservice.setup.js.map +0 -1
- package/dist/src/setup/swagger.setup.d.ts +0 -0
- package/dist/src/setup/swagger.setup.js +0 -1
- package/dist/src/setup/swagger.setup.js.map +0 -1
- package/dist/src/type/index.d.ts +0 -1
- package/dist/src/type/index.js +0 -18
- package/dist/src/type/index.js.map +0 -1
- package/dist/src/type/unknown.type.d.ts +0 -1
- package/dist/src/type/unknown.type.js +0 -3
- package/dist/src/type/unknown.type.js.map +0 -1
- package/dist/src/util/app.utils.d.ts +0 -4
- package/dist/src/util/app.utils.js +0 -21
- package/dist/src/util/app.utils.js.map +0 -1
- package/dist/src/util/date.util.d.ts +0 -2
- package/dist/src/util/date.util.js +0 -14
- package/dist/src/util/date.util.js.map +0 -1
- package/dist/src/util/generator.util.d.ts +0 -4
- package/dist/src/util/generator.util.js +0 -29
- package/dist/src/util/generator.util.js.map +0 -1
- package/dist/src/util/index.d.ts +0 -4
- package/dist/src/util/index.js +0 -21
- package/dist/src/util/index.js.map +0 -1
- package/dist/src/util/validate-config.d.ts +0 -2
- package/dist/src/util/validate-config.js +0 -19
- package/dist/src/util/validate-config.js.map +0 -1
- package/dist/tsconfig.build.tsbuildinfo +0 -1
@@ -1,112 +1,112 @@
|
|
1
|
-
import {
|
2
|
-
Repository,
|
3
|
-
Connection,
|
4
|
-
EntityManager,
|
5
|
-
SelectQueryBuilder,
|
6
|
-
} from 'typeorm';
|
7
|
-
import { EntityGeneric } from '../entity';
|
8
|
-
import { PaginationDto, BaseFilterDto } from '../../dto';
|
9
|
-
import { EntityStateConstant } from '../../constant';
|
10
|
-
import { PaginationResponse } from '../../response';
|
11
|
-
|
12
|
-
export abstract class ServiceGeneric<
|
13
|
-
Entity extends EntityGeneric,
|
14
|
-
CustomRepository extends Repository<Entity>,
|
15
|
-
> {
|
16
|
-
protected readonly entityName: string;
|
17
|
-
protected readonly loggable: boolean = false;
|
18
|
-
protected repository: CustomRepository;
|
19
|
-
protected connection: Connection;
|
20
|
-
constructor(
|
21
|
-
protected readonly connectionOrManager: Connection | EntityManager,
|
22
|
-
repositoryType: { new (connection: Connection): CustomRepository },
|
23
|
-
) {
|
24
|
-
if (connectionOrManager instanceof EntityManager) {
|
25
|
-
this.connection = connectionOrManager.connection;
|
26
|
-
} else {
|
27
|
-
this.connection = connectionOrManager;
|
28
|
-
}
|
29
|
-
this.repository = this.connection.getCustomRepository(repositoryType);
|
30
|
-
}
|
31
|
-
|
32
|
-
async create(entity: Entity): Promise<Entity> {
|
33
|
-
return this.repository.save(entity);
|
34
|
-
}
|
35
|
-
|
36
|
-
async update(entity: Entity): Promise<Entity> {
|
37
|
-
return this.repository.save(entity);
|
38
|
-
}
|
39
|
-
|
40
|
-
async getListWithPagination(
|
41
|
-
paginationDto: PaginationDto,
|
42
|
-
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
43
|
-
): Promise<PaginationResponse<Entity>> {
|
44
|
-
const { limit = 25, offset = 0 } = paginationDto;
|
45
|
-
const query = this.repository.createQueryBuilder(this.entityName);
|
46
|
-
query.limit(limit);
|
47
|
-
query.offset(offset);
|
48
|
-
query.where(`${this.entityName}.state != :state`, {
|
49
|
-
state: EntityStateConstant.Archived,
|
50
|
-
});
|
51
|
-
const defaultSelectable = ['createdAt', 'updatedAt'];
|
52
|
-
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
53
|
-
const selection = defaultSelectable.map(
|
54
|
-
(column: string) => `${this.entityName}.${column}`,
|
55
|
-
);
|
56
|
-
query.addSelect(selection);
|
57
|
-
if (callback != null) {
|
58
|
-
callback(query);
|
59
|
-
}
|
60
|
-
const entities = await query.getMany();
|
61
|
-
const total = await query.getCount();
|
62
|
-
const response = new PaginationResponse(entities, total, limit, offset);
|
63
|
-
return response;
|
64
|
-
}
|
65
|
-
|
66
|
-
async getAutocompleteWithPagination(
|
67
|
-
paginationDto: PaginationDto,
|
68
|
-
filter: BaseFilterDto,
|
69
|
-
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
70
|
-
): Promise<PaginationResponse<Entity>> {
|
71
|
-
const { limit = 25, offset = 0 } = paginationDto;
|
72
|
-
const query = this.repository.createQueryBuilder(this.entityName);
|
73
|
-
query.limit(limit);
|
74
|
-
query.offset(offset);
|
75
|
-
query.where(`${this.entityName}.state != :state`, {
|
76
|
-
state: EntityStateConstant.Archived,
|
77
|
-
});
|
78
|
-
const defaultSelectable = ['createdAt', 'updatedAt'];
|
79
|
-
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
80
|
-
const selection = defaultSelectable.map(
|
81
|
-
(column: string) => `${this.entityName}.${column}`,
|
82
|
-
);
|
83
|
-
query.addSelect(selection);
|
84
|
-
if (callback != null) {
|
85
|
-
callback(query);
|
86
|
-
}
|
87
|
-
const { excludeIds = [], includeIds = [] } = { ...filter };
|
88
|
-
// Exclude some ids from the list.
|
89
|
-
if (excludeIds?.length > 0) {
|
90
|
-
query.andWhere(`${this.entityName}.id NOT IN (:ids)`, {
|
91
|
-
ids: excludeIds,
|
92
|
-
});
|
93
|
-
}
|
94
|
-
let entities = await query.getMany();
|
95
|
-
const total = await query.getCount();
|
96
|
-
// Include some ids to the list.
|
97
|
-
const allIds: number[] = [];
|
98
|
-
for (const id of includeIds) {
|
99
|
-
const entity = entities.filter((entity: Entity) => entity.id === id);
|
100
|
-
if (entity.length == 0) {
|
101
|
-
allIds.push(id);
|
102
|
-
}
|
103
|
-
}
|
104
|
-
if (allIds.length > 0) {
|
105
|
-
query.where(`${this.entityName}.id IN (:ids)`, { ids: allIds });
|
106
|
-
const data = await query.getMany();
|
107
|
-
entities = data.concat(entities);
|
108
|
-
}
|
109
|
-
const response = new PaginationResponse(entities, total, limit, offset);
|
110
|
-
return response;
|
111
|
-
}
|
112
|
-
}
|
1
|
+
import {
|
2
|
+
Repository,
|
3
|
+
Connection,
|
4
|
+
EntityManager,
|
5
|
+
SelectQueryBuilder,
|
6
|
+
} from 'typeorm';
|
7
|
+
import { EntityGeneric } from '../entity';
|
8
|
+
import { PaginationDto, BaseFilterDto } from '../../dto';
|
9
|
+
import { EntityStateConstant } from '../../constant';
|
10
|
+
import { PaginationResponse } from '../../response';
|
11
|
+
|
12
|
+
export abstract class ServiceGeneric<
|
13
|
+
Entity extends EntityGeneric,
|
14
|
+
CustomRepository extends Repository<Entity>,
|
15
|
+
> {
|
16
|
+
protected readonly entityName: string;
|
17
|
+
protected readonly loggable: boolean = false;
|
18
|
+
protected repository: CustomRepository;
|
19
|
+
protected connection: Connection;
|
20
|
+
constructor(
|
21
|
+
protected readonly connectionOrManager: Connection | EntityManager,
|
22
|
+
repositoryType: { new (connection: Connection): CustomRepository },
|
23
|
+
) {
|
24
|
+
if (connectionOrManager instanceof EntityManager) {
|
25
|
+
this.connection = connectionOrManager.connection;
|
26
|
+
} else {
|
27
|
+
this.connection = connectionOrManager;
|
28
|
+
}
|
29
|
+
this.repository = this.connection.getCustomRepository(repositoryType);
|
30
|
+
}
|
31
|
+
|
32
|
+
async create(entity: Entity): Promise<Entity> {
|
33
|
+
return this.repository.save(entity);
|
34
|
+
}
|
35
|
+
|
36
|
+
async update(entity: Entity): Promise<Entity> {
|
37
|
+
return this.repository.save(entity);
|
38
|
+
}
|
39
|
+
|
40
|
+
async getListWithPagination(
|
41
|
+
paginationDto: PaginationDto,
|
42
|
+
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
43
|
+
): Promise<PaginationResponse<Entity>> {
|
44
|
+
const { limit = 25, offset = 0 } = paginationDto;
|
45
|
+
const query = this.repository.createQueryBuilder(this.entityName);
|
46
|
+
query.limit(limit);
|
47
|
+
query.offset(offset);
|
48
|
+
query.where(`${this.entityName}.state != :state`, {
|
49
|
+
state: EntityStateConstant.Archived,
|
50
|
+
});
|
51
|
+
const defaultSelectable = ['createdAt', 'updatedAt'];
|
52
|
+
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
53
|
+
const selection = defaultSelectable.map(
|
54
|
+
(column: string) => `${this.entityName}.${column}`,
|
55
|
+
);
|
56
|
+
query.addSelect(selection);
|
57
|
+
if (callback != null) {
|
58
|
+
callback(query);
|
59
|
+
}
|
60
|
+
const entities = await query.getMany();
|
61
|
+
const total = await query.getCount();
|
62
|
+
const response = new PaginationResponse(entities, total, limit, offset);
|
63
|
+
return response;
|
64
|
+
}
|
65
|
+
|
66
|
+
async getAutocompleteWithPagination(
|
67
|
+
paginationDto: PaginationDto,
|
68
|
+
filter: BaseFilterDto,
|
69
|
+
callback?: (query: SelectQueryBuilder<Entity>) => void,
|
70
|
+
): Promise<PaginationResponse<Entity>> {
|
71
|
+
const { limit = 25, offset = 0 } = paginationDto;
|
72
|
+
const query = this.repository.createQueryBuilder(this.entityName);
|
73
|
+
query.limit(limit);
|
74
|
+
query.offset(offset);
|
75
|
+
query.where(`${this.entityName}.state != :state`, {
|
76
|
+
state: EntityStateConstant.Archived,
|
77
|
+
});
|
78
|
+
const defaultSelectable = ['createdAt', 'updatedAt'];
|
79
|
+
query.orderBy(`${this.entityName}.updatedAt`, 'DESC');
|
80
|
+
const selection = defaultSelectable.map(
|
81
|
+
(column: string) => `${this.entityName}.${column}`,
|
82
|
+
);
|
83
|
+
query.addSelect(selection);
|
84
|
+
if (callback != null) {
|
85
|
+
callback(query);
|
86
|
+
}
|
87
|
+
const { excludeIds = [], includeIds = [] } = { ...filter };
|
88
|
+
// Exclude some ids from the list.
|
89
|
+
if (excludeIds?.length > 0) {
|
90
|
+
query.andWhere(`${this.entityName}.id NOT IN (:ids)`, {
|
91
|
+
ids: excludeIds,
|
92
|
+
});
|
93
|
+
}
|
94
|
+
let entities = await query.getMany();
|
95
|
+
const total = await query.getCount();
|
96
|
+
// Include some ids to the list.
|
97
|
+
const allIds: number[] = [];
|
98
|
+
for (const id of includeIds) {
|
99
|
+
const entity = entities.filter((entity: Entity) => entity.id === id);
|
100
|
+
if (entity.length == 0) {
|
101
|
+
allIds.push(id);
|
102
|
+
}
|
103
|
+
}
|
104
|
+
if (allIds.length > 0) {
|
105
|
+
query.where(`${this.entityName}.id IN (:ids)`, { ids: allIds });
|
106
|
+
const data = await query.getMany();
|
107
|
+
entities = data.concat(entities);
|
108
|
+
}
|
109
|
+
const response = new PaginationResponse(entities, total, limit, offset);
|
110
|
+
return response;
|
111
|
+
}
|
112
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
import { DataSource } from 'typeorm';
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
3
|
+
import { DatabaseConfig } from '../../config';
|
4
|
+
|
5
|
+
export class DataSourceManager {
|
6
|
+
private static instance: DataSourceManager;
|
7
|
+
|
8
|
+
private dataSources: { [key: string]: DataSource };
|
9
|
+
|
10
|
+
private constructor(private configService: ConfigService<DatabaseConfig>) {
|
11
|
+
this.dataSources = {};
|
12
|
+
}
|
13
|
+
|
14
|
+
public static getInstance(): DataSourceManager {
|
15
|
+
if (!DataSourceManager.instance) {
|
16
|
+
// TODO: Fix DataSourceManager object instantiation
|
17
|
+
// DataSourceManager.instance = new DataSourceManager();
|
18
|
+
}
|
19
|
+
|
20
|
+
return DataSourceManager.instance;
|
21
|
+
}
|
22
|
+
|
23
|
+
async getDBDataSource(dataSourceName: string): Promise<DataSource> {
|
24
|
+
if (this.dataSources[dataSourceName]) {
|
25
|
+
const dataSource = this.dataSources[dataSourceName];
|
26
|
+
return dataSource.isInitialized
|
27
|
+
? dataSource
|
28
|
+
: await dataSource.initialize();
|
29
|
+
}
|
30
|
+
|
31
|
+
const newDataSource = new DataSource({
|
32
|
+
/** connection info */
|
33
|
+
} as any);
|
34
|
+
|
35
|
+
this.dataSources[dataSourceName] = newDataSource;
|
36
|
+
|
37
|
+
return await newDataSource.initialize();
|
38
|
+
}
|
39
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './datasource-mapper';
|
package/src/lib/index.ts
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
export * from './search-document.interface';
|
1
|
+
export * from './search-document.interface';
|
2
2
|
export * from './transform-service.interface';
|
@@ -1,13 +1,13 @@
|
|
1
|
-
import {
|
2
|
-
SearchOptions,
|
3
|
-
SearchParams,
|
4
|
-
SearchResponse,
|
5
|
-
DocumentSchema,
|
6
|
-
} from 'typesense/lib/Typesense/Documents';
|
7
|
-
|
8
|
-
export interface SearchDocumentService<Document extends DocumentSchema> {
|
9
|
-
searchDocument(
|
10
|
-
searchParameters: SearchParams,
|
11
|
-
options: SearchOptions,
|
12
|
-
): Promise<SearchResponse<Document>>;
|
13
|
-
}
|
1
|
+
import {
|
2
|
+
SearchOptions,
|
3
|
+
SearchParams,
|
4
|
+
SearchResponse,
|
5
|
+
DocumentSchema,
|
6
|
+
} from 'typesense/lib/Typesense/Documents';
|
7
|
+
|
8
|
+
export interface SearchDocumentService<Document extends DocumentSchema> {
|
9
|
+
searchDocument(
|
10
|
+
searchParameters: SearchParams,
|
11
|
+
options: SearchOptions,
|
12
|
+
): Promise<SearchResponse<Document>>;
|
13
|
+
}
|
@@ -1,10 +1,10 @@
|
|
1
|
-
import { EntityGeneric } from '../../../generic';
|
2
|
-
import { BaseDocument } from '../document';
|
3
|
-
|
4
|
-
export interface TransformerService<
|
5
|
-
T extends EntityGeneric,
|
6
|
-
Document extends BaseDocument,
|
7
|
-
> {
|
8
|
-
transform(data: T): Document;
|
9
|
-
transforms(datas: T[]): Document[];
|
10
|
-
}
|
1
|
+
import { EntityGeneric } from '../../../generic';
|
2
|
+
import { BaseDocument } from '../document';
|
3
|
+
|
4
|
+
export interface TransformerService<
|
5
|
+
T extends EntityGeneric,
|
6
|
+
Document extends BaseDocument,
|
7
|
+
> {
|
8
|
+
transform(data: T): Document;
|
9
|
+
transforms(datas: T[]): Document[];
|
10
|
+
}
|
@@ -3,16 +3,17 @@ import { Logger, Type } from '@nestjs/common';
|
|
3
3
|
import { plainToClass } from 'class-transformer';
|
4
4
|
import { CollectionCreateSchema } from '../collection';
|
5
5
|
import {
|
6
|
-
SearchParams,
|
7
|
-
SearchOptions,
|
8
|
-
SearchResponse,
|
9
6
|
DeleteResponse,
|
10
7
|
ImportResponse,
|
8
|
+
SearchOptions,
|
9
|
+
SearchParams,
|
10
|
+
SearchResponse,
|
11
11
|
} from '../document';
|
12
12
|
import { BaseDocument } from '../../document';
|
13
13
|
import { SearchDocumentService } from '../../interface';
|
14
14
|
import { EntityGeneric } from '../../../../generic';
|
15
15
|
import { PaginationResponse } from '../../../../response';
|
16
|
+
import { BaseFilterDto } from '../../../../dto';
|
16
17
|
|
17
18
|
export abstract class ClientService<
|
18
19
|
Document extends BaseDocument,
|
@@ -23,6 +24,7 @@ export abstract class ClientService<
|
|
23
24
|
protected readonly entity: Type<Entity>;
|
24
25
|
protected abstract cache_s: number;
|
25
26
|
protected readonly skipCheckSchema: boolean = false;
|
27
|
+
|
26
28
|
constructor(
|
27
29
|
protected readonly client: Client,
|
28
30
|
protected readonly schema: CollectionCreateSchema,
|
@@ -33,31 +35,42 @@ export abstract class ClientService<
|
|
33
35
|
}
|
34
36
|
}
|
35
37
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
// filter_by,
|
40
|
-
// archived = false,
|
41
|
-
// } = { ...searchParameters };
|
42
|
-
// searchParameters.per_page = per_page;
|
43
|
-
// return this.client.collections(this?.schema?.name || this.prefix).documents().search(searchParameters, options);
|
44
|
-
// }
|
38
|
+
getCollectionName(): string {
|
39
|
+
return this?.schema?.name || this.prefix;
|
40
|
+
}
|
45
41
|
|
46
42
|
async searchDocument(
|
47
43
|
searchParameters: SearchParams,
|
48
44
|
options: SearchOptions,
|
45
|
+
filter?: BaseFilterDto,
|
46
|
+
): Promise<SearchResponse<any>> {
|
47
|
+
return this.searchDocumentByCollection(
|
48
|
+
this.getCollectionName(),
|
49
|
+
searchParameters,
|
50
|
+
options,
|
51
|
+
filter,
|
52
|
+
);
|
53
|
+
}
|
54
|
+
|
55
|
+
async searchDocumentByCollection(
|
56
|
+
collection: string,
|
57
|
+
searchParameters: SearchParams,
|
58
|
+
options: SearchOptions,
|
59
|
+
filter?: BaseFilterDto,
|
49
60
|
): Promise<SearchResponse<any>> {
|
50
61
|
try {
|
51
|
-
const {
|
62
|
+
const { per_page = 25 } = searchParameters;
|
52
63
|
// TODO: should be support with include/exclude ids.
|
64
|
+
const { includeIds = [] } = filter;
|
53
65
|
const includeDocuments = [];
|
66
|
+
|
54
67
|
if (includeIds.length > 0) {
|
55
68
|
const includeOpts = {
|
56
69
|
...searchParameters,
|
57
70
|
filter_by: `id:=[${includeIds.join(', ')}]`,
|
58
71
|
};
|
59
72
|
const includeDocs = await this.client
|
60
|
-
.collections(
|
73
|
+
.collections(collection)
|
61
74
|
.documents()
|
62
75
|
.search(includeOpts, options);
|
63
76
|
if (includeDocs?.hits?.length > 0) {
|
@@ -73,7 +86,7 @@ export abstract class ClientService<
|
|
73
86
|
includeDocuments.push(...includeDocs?.hits);
|
74
87
|
}
|
75
88
|
const documents = await this.client
|
76
|
-
.collections(
|
89
|
+
.collections(collection)
|
77
90
|
.documents()
|
78
91
|
.search(searchParameters, options);
|
79
92
|
if (includeDocuments.length > 0) {
|
@@ -108,7 +121,7 @@ export abstract class ClientService<
|
|
108
121
|
if (data.length > 0) {
|
109
122
|
try {
|
110
123
|
return this.client
|
111
|
-
.collections(this
|
124
|
+
.collections(this.getCollectionName())
|
112
125
|
.documents()
|
113
126
|
.import(data, { action: 'upsert' });
|
114
127
|
} catch (error) {
|
@@ -123,7 +136,7 @@ export abstract class ClientService<
|
|
123
136
|
filter_by: `${key}:!=[${data.join(', ')}]`,
|
124
137
|
};
|
125
138
|
return await this.client
|
126
|
-
.collections(this
|
139
|
+
.collections(this.getCollectionName())
|
127
140
|
.documents()
|
128
141
|
.delete(deleteParameters);
|
129
142
|
}
|
@@ -131,14 +144,14 @@ export abstract class ClientService<
|
|
131
144
|
|
132
145
|
async insertIndex(data: Document): Promise<any> {
|
133
146
|
return this.client
|
134
|
-
.collections(this
|
147
|
+
.collections(this.getCollectionName())
|
135
148
|
.documents()
|
136
149
|
.create(data, { action: 'upsert' });
|
137
150
|
}
|
138
151
|
|
139
152
|
async updateIndex(data: Document): Promise<any> {
|
140
153
|
return this.client
|
141
|
-
.collections(this
|
154
|
+
.collections(this.getCollectionName())
|
142
155
|
.documents()
|
143
156
|
.upsert(data, { action: 'upsert' });
|
144
157
|
}
|
@@ -146,12 +159,12 @@ export abstract class ClientService<
|
|
146
159
|
updateDocumentById = async (data: Document) => {
|
147
160
|
if (data?.id) {
|
148
161
|
const exist = await this.client
|
149
|
-
.collections(this
|
162
|
+
.collections(this.getCollectionName())
|
150
163
|
.documents(data.id)
|
151
164
|
.retrieve();
|
152
165
|
if (exist) {
|
153
166
|
return this.client
|
154
|
-
.collections(this
|
167
|
+
.collections(this.getCollectionName())
|
155
168
|
.documents(data.id)
|
156
169
|
.update(data);
|
157
170
|
}
|
@@ -164,14 +177,14 @@ export abstract class ClientService<
|
|
164
177
|
|
165
178
|
async deleteIndex(data: Document): Promise<DeleteResponse> {
|
166
179
|
return this.client
|
167
|
-
.collections(this
|
180
|
+
.collections(this.getCollectionName())
|
168
181
|
.documents()
|
169
182
|
.delete({ filter_by: `id: ${data.id}` });
|
170
183
|
}
|
171
184
|
|
172
185
|
async deleteBatchIndex(ids: string[]): Promise<DeleteResponse> {
|
173
186
|
return this.client
|
174
|
-
.collections(this
|
187
|
+
.collections(this.getCollectionName())
|
175
188
|
.documents()
|
176
189
|
.delete({ filter_by: `id: [${ids.join(',')}]` });
|
177
190
|
}
|
@@ -181,7 +194,7 @@ export abstract class ClientService<
|
|
181
194
|
value: number,
|
182
195
|
): Promise<DeleteResponse> {
|
183
196
|
return this.client
|
184
|
-
.collections(this
|
197
|
+
.collections(this.getCollectionName())
|
185
198
|
.documents()
|
186
199
|
.delete({ filter_by: `${key}:=${value}` });
|
187
200
|
}
|
package/src/setup/index.ts
CHANGED
@@ -1 +1 @@
|
|
1
|
-
export * from './microservice.setup';
|
1
|
+
export * from './microservice.setup';
|
package/src/util/app.utils.ts
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
import type { INestApplication } from '@nestjs/common';
|
2
|
-
|
3
|
-
export class AppUtils {
|
4
|
-
public static killAppWithGrace = (app: INestApplication) => {
|
5
|
-
process.on('SIGINT', async () => {
|
6
|
-
setTimeout(() => process.exit(1), 5000);
|
7
|
-
await app.close();
|
8
|
-
process.exit(0);
|
9
|
-
});
|
10
|
-
|
11
|
-
// kill -15
|
12
|
-
process.on('SIGTERM', async () => {
|
13
|
-
setTimeout(() => process.exit(1), 5000);
|
14
|
-
await app.close();
|
15
|
-
process.exit(0);
|
16
|
-
});
|
17
|
-
};
|
18
|
-
}
|
1
|
+
import type { INestApplication } from '@nestjs/common';
|
2
|
+
|
3
|
+
export class AppUtils {
|
4
|
+
public static killAppWithGrace = (app: INestApplication) => {
|
5
|
+
process.on('SIGINT', async () => {
|
6
|
+
setTimeout(() => process.exit(1), 5000);
|
7
|
+
await app.close();
|
8
|
+
process.exit(0);
|
9
|
+
});
|
10
|
+
|
11
|
+
// kill -15
|
12
|
+
process.on('SIGTERM', async () => {
|
13
|
+
setTimeout(() => process.exit(1), 5000);
|
14
|
+
await app.close();
|
15
|
+
process.exit(0);
|
16
|
+
});
|
17
|
+
};
|
18
|
+
}
|
@@ -1,49 +1,49 @@
|
|
1
|
-
import dayjs from 'dayjs';
|
2
|
-
import { addSecond, isExpired } from './date.util';
|
3
|
-
|
4
|
-
describe('Utility Functions', () => {
|
5
|
-
test('addSecond should return a Date object with the specified duration added', () => {
|
6
|
-
// Arrange
|
7
|
-
const duration = 10;
|
8
|
-
|
9
|
-
// Act
|
10
|
-
const result = addSecond(duration);
|
11
|
-
|
12
|
-
// Assert
|
13
|
-
const expectedDate = dayjs().add(duration, 'seconds').toDate();
|
14
|
-
expect(result).toEqual(expectedDate);
|
15
|
-
});
|
16
|
-
|
17
|
-
test('isExpired should return true for a past date', () => {
|
18
|
-
// Arrange
|
19
|
-
const pastDate = dayjs().subtract(1, 'day').toDate();
|
20
|
-
|
21
|
-
// Act
|
22
|
-
const result = isExpired(pastDate);
|
23
|
-
|
24
|
-
// Assert
|
25
|
-
expect(result).toBe(true);
|
26
|
-
});
|
27
|
-
|
28
|
-
test('isExpired should return false for a future date', () => {
|
29
|
-
// Arrange
|
30
|
-
const futureDate = dayjs().add(1, 'day').toDate();
|
31
|
-
|
32
|
-
// Act
|
33
|
-
const result = isExpired(futureDate);
|
34
|
-
|
35
|
-
// Assert
|
36
|
-
expect(result).toBe(false);
|
37
|
-
});
|
38
|
-
|
39
|
-
test('isExpired should return false for the current date', () => {
|
40
|
-
// Arrange
|
41
|
-
const currentDate = dayjs().toDate();
|
42
|
-
|
43
|
-
// Act
|
44
|
-
const result = isExpired(currentDate);
|
45
|
-
|
46
|
-
// Assert
|
47
|
-
expect(result).toBe(false);
|
48
|
-
});
|
49
|
-
});
|
1
|
+
import dayjs from 'dayjs';
|
2
|
+
import { addSecond, isExpired } from './date.util';
|
3
|
+
|
4
|
+
describe('Utility Functions', () => {
|
5
|
+
test('addSecond should return a Date object with the specified duration added', () => {
|
6
|
+
// Arrange
|
7
|
+
const duration = 10;
|
8
|
+
|
9
|
+
// Act
|
10
|
+
const result = addSecond(duration);
|
11
|
+
|
12
|
+
// Assert
|
13
|
+
const expectedDate = dayjs().add(duration, 'seconds').toDate();
|
14
|
+
expect(result).toEqual(expectedDate);
|
15
|
+
});
|
16
|
+
|
17
|
+
test('isExpired should return true for a past date', () => {
|
18
|
+
// Arrange
|
19
|
+
const pastDate = dayjs().subtract(1, 'day').toDate();
|
20
|
+
|
21
|
+
// Act
|
22
|
+
const result = isExpired(pastDate);
|
23
|
+
|
24
|
+
// Assert
|
25
|
+
expect(result).toBe(true);
|
26
|
+
});
|
27
|
+
|
28
|
+
test('isExpired should return false for a future date', () => {
|
29
|
+
// Arrange
|
30
|
+
const futureDate = dayjs().add(1, 'day').toDate();
|
31
|
+
|
32
|
+
// Act
|
33
|
+
const result = isExpired(futureDate);
|
34
|
+
|
35
|
+
// Assert
|
36
|
+
expect(result).toBe(false);
|
37
|
+
});
|
38
|
+
|
39
|
+
test('isExpired should return false for the current date', () => {
|
40
|
+
// Arrange
|
41
|
+
const currentDate = dayjs().toDate();
|
42
|
+
|
43
|
+
// Act
|
44
|
+
const result = isExpired(currentDate);
|
45
|
+
|
46
|
+
// Assert
|
47
|
+
expect(result).toBe(false);
|
48
|
+
});
|
49
|
+
});
|
package/src/util/date.util.ts
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
import dayjs from 'dayjs';
|
2
|
-
|
3
|
-
export const addSecond = (duration: number): Date => {
|
4
|
-
return dayjs().add(duration, 'seconds').toDate();
|
5
|
-
};
|
6
|
-
|
7
|
-
export const isExpired = (date: Date): boolean => {
|
8
|
-
const currentDate = dayjs();
|
9
|
-
return dayjs(date).isBefore(currentDate);
|
10
|
-
};
|
1
|
+
import dayjs from 'dayjs';
|
2
|
+
|
3
|
+
export const addSecond = (duration: number): Date => {
|
4
|
+
return dayjs().add(duration, 'seconds').toDate();
|
5
|
+
};
|
6
|
+
|
7
|
+
export const isExpired = (date: Date): boolean => {
|
8
|
+
const currentDate = dayjs();
|
9
|
+
return dayjs(date).isBefore(currentDate);
|
10
|
+
};
|