Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Affiche tous les Clients dans le formulaire Projet #455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Application/Customer/Query/GetCustomersQuery.ts
Original file line number Diff line number Diff line change
@@ -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) {}
}
34 changes: 34 additions & 0 deletions src/Application/Customer/Query/GetCustomersQueryHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CustomerView>(
[
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();
});
});
2 changes: 1 addition & 1 deletion src/Domain/Customer/Repository/ICustomerRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export interface ICustomerRepository {
save(customer: Customer): Promise<Customer>;
findOneByName(name: string): Promise<Customer | undefined>;
findOneById(id: string): Promise<Customer | undefined>;
findCustomers(page: number): Promise<[Customer[], number]>;
findCustomers(page: number | null): Promise<[Customer[], number]>;
}
17 changes: 11 additions & 6 deletions src/Infrastructure/Customer/Repository/CustomerRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@
.getOne();
}

public findCustomers(page: number): Promise<[Customer[], number]> {
return this.repository
public findCustomers(page: number | null): Promise<[Customer[], number]> {
let query = this.repository

Check warning on line 35 in src/Infrastructure/Customer/Repository/CustomerRepository.ts

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure/Customer/Repository/CustomerRepository.ts#L34-L35

Added lines #L34 - L35 were not covered by tests
.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

Check warning on line 41 in src/Infrastructure/Customer/Repository/CustomerRepository.ts

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure/Customer/Repository/CustomerRepository.ts#L41

Added line #L41 was not covered by tests
.limit(MAX_ITEMS_PER_PAGE)
.offset((page - 1) * MAX_ITEMS_PER_PAGE);
}

return query.getManyAndCount();

Check warning on line 46 in src/Infrastructure/Customer/Repository/CustomerRepository.ts

View check run for this annotation

Codecov / codecov/patch

src/Infrastructure/Customer/Repository/CustomerRepository.ts#L46

Added line #L46 was not covered by tests
}
}
4 changes: 2 additions & 2 deletions src/Infrastructure/Project/Controller/AddProjectController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class AddProjectController {
@Render('pages/projects/add.njk')
public async get() {
const customers: Pagination<CustomerView> = await this.queryBus.execute(
new GetCustomersQuery(1)
new GetCustomersQuery(null)
);

return {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class EditProjectController {
);

const customers: Pagination<CustomerView> = await this.queryBus.execute(
new GetCustomersQuery(1)
new GetCustomersQuery(null)
);

return {
Expand Down
Loading