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

Ot.past donations #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 0 additions & 1 deletion apps/backend/src/donations/donations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class DonationsController {

@Get('orders')
filter(@Body() filterDonationsDto: FilterDonationsDto) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to replace the @Body() with @query() because we usually do @query() with GET requests and @Body with POST or PUT
https://dev.to/shameel/nestjs-request-param-body-query-headers-ip-5e6k

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean instead of using a FilterDonationsDto to put all of the filter conditions in the url to use @query?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

console.log('HERE');
return this.donationsService.filter(filterDonationsDto);
}

Expand Down
33 changes: 23 additions & 10 deletions apps/backend/src/donations/donations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,29 @@ export class DonationsService {
return `This action removes a #${id} donation`;
}

filter(filterDonationsDto: FilterDonationsDto) {
return this.repo.find({
where: {
pantry_id: In(filterDonationsDto.pantry_ids),
async filter(filterDonationsDto: FilterDonationsDto) {
let query = this.repo.createQueryBuilder('donation');
if (filterDonationsDto.pantry_ids != null) {
query = query.where('donation.pantry_id IN (:...ids)', {
ids: filterDonationsDto.pantry_ids,
});
}
if (filterDonationsDto.status != null) {
query = query.andWhere('donation.status = :status', {
status: filterDonationsDto.status,
due_date: Between(
filterDonationsDto.due_date_start,
filterDonationsDto.due_date_end,
),
},
});
});
}
if (filterDonationsDto.due_date_start != null) {
query = query.andWhere('donation.due_date >= :start', {
start: filterDonationsDto.due_date_start,
});
}
if (filterDonationsDto.due_date_end != null) {
query = query.andWhere('donation.due_date <= :end', {
end: filterDonationsDto.due_date_end,
});
}
const donations = await query.getMany();
return donations;
}
}
12 changes: 11 additions & 1 deletion apps/backend/src/donations/dto/filter-donations.dto.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import { IsDate, IsDateString, IsEnum, IsInt } from 'class-validator';
import {
IsDate,
IsDateString,
IsEnum,
IsInt,
IsOptional,
} from 'class-validator';
import { DonationStatus } from '../types';
import { Timestamp } from 'typeorm';

export class FilterDonationsDto {
@IsDateString()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do @isdate() instead to match with the Date types in DTO

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and it broke. Is there anything wrong with isDateString?

@IsOptional()
due_date_start: Date;

@IsDateString()
@IsOptional()
due_date_end: Date;

@IsInt({ each: true })
@IsOptional()
pantry_ids: number[];

@IsEnum(DonationStatus)
@IsOptional()
status: string;
}