Add date and createdBy filters to sales payments functionality

- Introduced new filters `date` and `createdBy` in the `ListSalesPaymentsFilters` and `ListSalesPaymentsQuery` types to enhance querying capabilities.
- Updated the `SalesPaymentsRepository` to handle filtering based on the new fields.
- Enhanced the `SalesPaymentsService` to process the new filters and ensure proper date handling.
- Added unit tests to validate the integration of the new filters in the service layer.
- Updated DTOs to include validation for the new fields, ensuring correct data formats in API requests.
This commit is contained in:
shancheas
2026-09-01 15:29:07 +07:00
parent 51db4f4a4d
commit 6b3ddfcff9
5 changed files with 45 additions and 0 deletions
@@ -12,6 +12,8 @@ import {
MaxLength,
ValidateNested,
} from 'class-validator';
const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;
import {
PaginationQueryDto,
UserRelationDto,
@@ -163,6 +165,17 @@ export class ListSalesPaymentsQueryDto extends PaginationQueryDto {
@IsOptional()
@IsString()
search?: string;
@ApiPropertyOptional({ example: '2026-09-01' })
@IsOptional()
@IsString()
@Matches(DATE_PATTERN)
date?: string;
@ApiPropertyOptional({ format: 'uuid' })
@IsOptional()
@IsUUID('4')
createdBy?: string;
}
export class SalesPaymentDto {
@@ -65,6 +65,8 @@ export type ListSalesPaymentsFilters = {
readonly code?: string;
readonly status?: string;
readonly search?: string;
readonly date?: number;
readonly createdBy?: string;
readonly orderBy?: string;
readonly orderType?: string;
readonly limit: number;
@@ -316,6 +316,12 @@ export class SalesPaymentsRepository {
parts.push(search);
}
}
if (filters.date !== undefined) {
parts.push(eq(salesPayments.date, filters.date));
}
if (filters.createdBy) {
parts.push(eq(salesPayments.createdBy, filters.createdBy));
}
if (parts.length === 0) {
return undefined;
}
@@ -141,6 +141,24 @@ describe('SalesPaymentsService', () => {
);
});
it('list forwards date and createdBy filters to the repository', async () => {
repository.list.mockResolvedValue({ data: [], total: 0 });
await service.list({
date: '2026-09-01',
createdBy: 'user-1',
page: 1,
limit: 10,
});
expect(repository.list).toHaveBeenCalledWith(
expect.objectContaining({
date: DateTime.create('2026-09-01').startOfDay().value,
createdBy: 'user-1',
limit: 10,
offset: 0,
}),
);
});
it('approving a payment recomputes referenced invoices', async () => {
repository.findById.mockResolvedValue({
...sample,
@@ -49,6 +49,8 @@ export type ListSalesPaymentsQuery = {
readonly code?: string;
readonly status?: string;
readonly search?: string;
readonly date?: string;
readonly createdBy?: string;
readonly orderBy?: string;
readonly orderType?: string;
readonly page?: number;
@@ -75,6 +77,10 @@ export class SalesPaymentsService {
code: query.code,
status: query.status,
search: query.search,
date: query.date
? this.assertDate(query.date).startOfDay().value
: undefined,
createdBy: query.createdBy,
orderBy: query.orderBy,
orderType: query.orderType,
limit: page.limit,