Skip to content

Commit

Permalink
feat: [Contracts] Add date range filtering to get many contracts for web
Browse files Browse the repository at this point in the history
  • Loading branch information
radulescuandrew committed Sep 25, 2024
1 parent dadd24e commit f86d0ab
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
6 changes: 6 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@aws-sdk/s3-request-presigner": "^3.614.0",
"@bull-board/api": "^5.21.0",
"@bull-board/express": "^5.21.0",
"@date-fns/utc": "2.1.0",
"@nestjs-modules/mailer": "^2.0.2",
"@nestjs/axios": "^3.0.2",
"@nestjs/bull": "^10.1.1",
Expand Down
11 changes: 11 additions & 0 deletions backend/src/api/documents/dto/create-document-contract.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UTCDate } from '@date-fns/utc';
import { Transform } from 'class-transformer';
import { IsDate, IsString, MaxLength, MinDate } from 'class-validator';
import { IsDateGreaterThanOrEqualTo } from 'src/common/validators/is-date-gte.validator';

Expand All @@ -17,20 +19,29 @@ export class CreateDocumentContractDto {
'Document date must be greater than or equal to the current date',
},
)
@Transform(({ value }) => {
return new UTCDate(value);
})
documentDate: Date;

@IsDate()
@IsDateGreaterThanOrEqualTo('documentDate', {
message:
'Document start date must be greater than or equal to the document date',
})
@Transform(({ value }) => {
return new UTCDate(value);
})
documentStartDate: Date;

@IsDate()
@IsDateGreaterThanOrEqualTo('documentStartDate', {
message:
'Document end date must be greater than or equal to the document start date',
})
@Transform(({ value }) => {
return new UTCDate(value);
})
documentEndDate: Date;

@IsString()
Expand Down
12 changes: 10 additions & 2 deletions backend/src/api/documents/dto/get-many-document-contracts.dto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UTCDate } from '@date-fns/utc';
import { Transform } from 'class-transformer';
import { IsDate, IsEnum, IsOptional, IsString } from 'class-validator';
import { BasePaginationFilterDto } from 'src/infrastructure/base/base-pagination-filter.dto';
import { DocumentContractStatus } from 'src/modules/documents/enums/contract-status.enum';
Expand All @@ -13,9 +15,15 @@ export class GetManyDocumentContractsDto extends BasePaginationFilterDto {

@IsDate()
@IsOptional()
startDate?: Date;
@Transform(({ value }) => {
return new UTCDate(value);
})
documentStartDate?: Date;

@IsDate()
@IsOptional()
endDate?: Date;
@Transform(({ value }) => {
return new UTCDate(value);
})
documentEndDate?: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export type FindManyDocumentContractListViewOptions =
organizationId: string;
volunteerId?: string;
status?: DocumentContractStatus;
documentStartDate?: Date;
documentEndDate?: Date;
};

export class DocumentContractListViewTransformer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export class DocumentContractListViewRepository extends RepositoryWithPagination
limit,
page,

documentStartDate,
documentEndDate,

organizationId,
volunteerId,
status,
Expand Down Expand Up @@ -61,6 +64,27 @@ export class DocumentContractListViewRepository extends RepositoryWithPagination
query.andWhere('documentContractListView.status = :status', { status });
}

if (documentStartDate && documentEndDate) {
query.andWhere(
'(documentContractListView.documentStartDate >= :documentStartDate::DATE AND documentContractListView.documentEndDate <= :documentEndDate::DATE)',
{ documentStartDate, documentEndDate },
);
} else {
if (documentStartDate) {
query.andWhere(
'documentContractListView.documentStartDate >= :documentStartDate::DATE',
{ documentStartDate },
);
}

if (documentEndDate) {
query.andWhere(
'documentContractListView.documentEndDate <= :documentEndDate::DATE',
{ documentEndDate },
);
}
}

if (search) {
query.andWhere(
this.buildBracketSearchQuery(
Expand Down

0 comments on commit f86d0ab

Please sign in to comment.