diff --git a/src/Application/Customer/Query/GetCustomersQuery.ts b/src/Application/Customer/Query/GetCustomersQuery.ts index d3888a7a..f92fc29e 100644 --- a/src/Application/Customer/Query/GetCustomersQuery.ts +++ b/src/Application/Customer/Query/GetCustomersQuery.ts @@ -1,5 +1,5 @@ import { IQuery } from 'src/Application/IQuery'; export class GetCustomersQuery implements IQuery { - constructor(public readonly page: number) {} + constructor(public readonly page: number | null) {} } diff --git a/src/Application/Customer/Query/GetCustomersQueryHandler.spec.ts b/src/Application/Customer/Query/GetCustomersQueryHandler.spec.ts index 3fbaf609..46270e61 100644 --- a/src/Application/Customer/Query/GetCustomersQueryHandler.spec.ts +++ b/src/Application/Customer/Query/GetCustomersQueryHandler.spec.ts @@ -44,4 +44,38 @@ describe('GetCustomersQueryHandler', () => { verify(customerRepository.findCustomers(1)).once(); }); + + it('testGetAllCustomers', async () => { + const customerRepository = mock(CustomerRepository); + + const customer1 = mock(Customer); + when(customer1.getId()).thenReturn('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2'); + when(customer1.getName()).thenReturn('Customer 1'); + + const customer2 = mock(Customer); + when(customer2.getId()).thenReturn('d54f15d6-1a1d-47e8-8672-9f46018f9960'); + when(customer2.getName()).thenReturn('Customer 2'); + + when(customerRepository.findCustomers(null)).thenResolve([ + [instance(customer2), instance(customer1)], + 2 + ]); + + const queryHandler = new GetCustomersQueryHandler( + instance(customerRepository) + ); + + const expectedResult = new Pagination( + [ + new CustomerView('d54f15d6-1a1d-47e8-8672-9f46018f9960', 'Customer 2'), + new CustomerView('eb9e1d9b-dce2-48a9-b64f-f0872f3157d2', 'Customer 1') + ], + 2 + ); + + expect( + await queryHandler.execute(new GetCustomersQuery(null)) + ).toMatchObject(expectedResult); + verify(customerRepository.findCustomers(null)).once(); + }); }); diff --git a/src/Domain/Customer/Repository/ICustomerRepository.ts b/src/Domain/Customer/Repository/ICustomerRepository.ts index 049b5839..60b569f2 100644 --- a/src/Domain/Customer/Repository/ICustomerRepository.ts +++ b/src/Domain/Customer/Repository/ICustomerRepository.ts @@ -4,5 +4,5 @@ export interface ICustomerRepository { save(customer: Customer): Promise; findOneByName(name: string): Promise; findOneById(id: string): Promise; - findCustomers(page: number): Promise<[Customer[], number]>; + findCustomers(page: number | null): Promise<[Customer[], number]>; } diff --git a/src/Infrastructure/Customer/Repository/CustomerRepository.ts b/src/Infrastructure/Customer/Repository/CustomerRepository.ts index 430d0a05..de24e1e4 100644 --- a/src/Infrastructure/Customer/Repository/CustomerRepository.ts +++ b/src/Infrastructure/Customer/Repository/CustomerRepository.ts @@ -31,13 +31,18 @@ export class CustomerRepository implements ICustomerRepository { .getOne(); } - public findCustomers(page: number): Promise<[Customer[], number]> { - return this.repository + public findCustomers(page: number | null): Promise<[Customer[], number]> { + let query = this.repository .createQueryBuilder('customer') .select(['customer.id', 'customer.name']) - .orderBy('customer.name', 'ASC') - .limit(MAX_ITEMS_PER_PAGE) - .offset((page - 1) * MAX_ITEMS_PER_PAGE) - .getManyAndCount(); + .orderBy('customer.name', 'ASC'); + + if (typeof page === 'number') { + query = query + .limit(MAX_ITEMS_PER_PAGE) + .offset((page - 1) * MAX_ITEMS_PER_PAGE); + } + + return query.getManyAndCount(); } } diff --git a/src/Infrastructure/Project/Controller/AddProjectController.ts b/src/Infrastructure/Project/Controller/AddProjectController.ts index e907a754..849723c4 100644 --- a/src/Infrastructure/Project/Controller/AddProjectController.ts +++ b/src/Infrastructure/Project/Controller/AddProjectController.ts @@ -38,7 +38,7 @@ export class AddProjectController { @Render('pages/projects/add.njk') public async get() { const customers: Pagination = await this.queryBus.execute( - new GetCustomersQuery(1) + new GetCustomersQuery(null) ); return { @@ -48,7 +48,7 @@ export class AddProjectController { } @Post() - public async poqr(@Body() projectDto: ProjectDTO, @Res() res: Response) { + public async post(@Body() projectDto: ProjectDTO, @Res() res: Response) { const { name, customerId, active } = projectDto; try { diff --git a/src/Infrastructure/Project/Controller/EditProjectController.ts b/src/Infrastructure/Project/Controller/EditProjectController.ts index 362ee6f7..aa0f8e1c 100644 --- a/src/Infrastructure/Project/Controller/EditProjectController.ts +++ b/src/Infrastructure/Project/Controller/EditProjectController.ts @@ -45,7 +45,7 @@ export class EditProjectController { ); const customers: Pagination = await this.queryBus.execute( - new GetCustomersQuery(1) + new GetCustomersQuery(null) ); return {