From 745bf41dddb38c41a61779f80e00a65c962882cb Mon Sep 17 00:00:00 2001 From: MajedAlaitwniCap Date: Fri, 6 Oct 2023 11:04:55 +0200 Subject: [PATCH] temporar implementation --- .gitignore | 3 +- src/modules/person/persistence/person.repo.ts | 40 +++++++++---------- src/shared/interface/person-search-query.ts | 7 ++++ 3 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 src/shared/interface/person-search-query.ts diff --git a/.gitignore b/.gitignore index 3052ac9c0..5daf2d735 100644 --- a/.gitignore +++ b/.gitignore @@ -132,4 +132,5 @@ dist .pnp.* # VSCode user settings -.vscode/settings.json \ No newline at end of file +.vscode/settings.json +.vscode/settings.default.json diff --git a/src/modules/person/persistence/person.repo.ts b/src/modules/person/persistence/person.repo.ts index 06d5dde7c..413734da7 100644 --- a/src/modules/person/persistence/person.repo.ts +++ b/src/modules/person/persistence/person.repo.ts @@ -6,16 +6,21 @@ import { EntityManager } from '@mikro-orm/postgresql'; import { Inject, Injectable } from '@nestjs/common'; import { PersonDo } from '../domain/person.do.js'; import { PersonEntity } from './person.entity.js'; -import { Loaded, QueryOrderMap } from '@mikro-orm/core'; +import { EntityName, Loaded, QueryOrderMap } from '@mikro-orm/core'; import { IFindOptions, IPagination, SortOrder } from '../../../shared/interface/find-options.js'; import { Page } from '../../../shared/interface/page.js'; import { Scope } from '../../../shared/repo/scope.js'; import { PersonScope } from './person.scope.js'; import { PersonSortingMapper } from './person-sorting.mapper.js'; +import { PersonSearchQuery } from '../../../shared/interface/person-search-query.js'; @Injectable() export class PersonRepo { public constructor(private readonly em: EntityManager, @Inject(getMapperToken()) private readonly mapper: Mapper) {} + public get entityName(): EntityName { + return PersonEntity; + } + public async findById(id: string): Promise>> { const person: Option = await this.em.findOne(PersonEntity, { id }); if (person) { @@ -66,21 +71,26 @@ export class PersonRepo { } public async findAll( - personDo: PersonDo, - options?: IFindOptions[]>, - ): Promise[]> { + query: PersonSearchQuery, + options?: IFindOptions>, + ): Promise>> { const pagination: IPagination = options?.pagination || {}; const order: QueryOrderMap = PersonSortingMapper.mapDOSortOrderToQueryOrder(options?.order || {}); const scope: Scope = new PersonScope() - .byFirstName(personDo.firstName) - .byLastName(personDo.lastName) - .byBirthDate(personDo.birthDate) + .byFirstName(query.firstName) + .byLastName(query.lastName) + .byBirthDate(query.birthDate) .allowEmptyQuery(true); - if (order.id == null) { + if (order.firstName == null) { order.firstName = SortOrder.asc; } + const [entities, total]: [PersonEntity[], number] = await this.em.findAndCount(PersonEntity, scope.query, { + ...pagination, + orderBy: order, + }); + // if (personDo.firstName) { // query['firstName'] = { $ilike: personDo.firstName }; // } @@ -93,22 +103,12 @@ export class PersonRepo { // query['referrer'] = personDo.referrer; // } - const [entities, total]: [PersonEntity[], number] = await this.em.findAndCount(PersonEntity, scope.personDo, { - offset: pagination?.skip, - limit: pagination?.limit, - }); - - const entityDos: PersonDo[] = entities.map((person: PersonEntity) => + const entityDos: PersonDo[] = entities.map((person: PersonEntity) => this.mapper.map(person, PersonEntity, PersonDo), ); // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - const page: Page[]> = new Page>(entityDos, total); + const page: Page> = new Page>(entityDos, total); // return result.map((person: PersonEntity) => this.mapper.map(person, PersonEntity, PersonDo)); return page; } - // mapEntityToDO(entity: PersonEntity): PersonDo { - // const domainObject = PersonRepo.mapEntityToDO(entity); - - // return domainObject; - // } } diff --git a/src/shared/interface/person-search-query.ts b/src/shared/interface/person-search-query.ts new file mode 100644 index 000000000..85072048d --- /dev/null +++ b/src/shared/interface/person-search-query.ts @@ -0,0 +1,7 @@ +export interface PersonSearchQuery { + firstName?: string; + + lastName?: string; + + birthDate?: Date; +}