Skip to content
Merged
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
29 changes: 23 additions & 6 deletions src/discount/controllers/coupon.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {
HttpStatus,
HttpCode,
UseInterceptors,
Query,
} from '@nestjs/common';
import { CouponService } from '../services/coupon.service';
import {
CouponDTO,
UpdateCouponDTO,
ResponseCouponDTO,
CouponQueryDTO,
} from '../dto/coupon.dto';
import { AuthGuard } from 'src/auth/auth.guard';
import { RolesGuard } from 'src/auth/roles.guard';
Expand All @@ -31,9 +33,6 @@ import {
import { UserRole } from 'src/user/entities/user.entity';
import { PaginationDTO } from 'src/utils/dto/pagination.dto';
import { PaginationInterceptor } from 'src/utils/pagination.interceptor';
import { PaginationQueryDTO } from 'src/utils/dto/pagination.dto';
import { Pagination } from 'src/utils/pagination.decorator';

@Controller('coupon')
@ApiExtraModels(PaginationDTO, ResponseCouponDTO)
@ApiBearerAuth()
Expand Down Expand Up @@ -72,6 +71,19 @@ export class CouponController {
type: Number,
example: 10,
})
@ApiQuery({
name: 'q',
required: false,
description: 'Search text to filter by code',
type: String,
})
@ApiQuery({
name: 'expirationBetween',
required: false,
description: 'Filter by expiration date range',
type: String,
example: '2023-01-01,2023-12-31',
})
@ApiResponse({
status: HttpStatus.OK,
description: 'Successful retrieval of coupons',
Expand All @@ -90,10 +102,15 @@ export class CouponController {
},
})
async findAll(
@Pagination() pagination: PaginationQueryDTO,
@Query() pagination: CouponQueryDTO,
): Promise<{ data: ResponseCouponDTO[]; total: number }> {
const { page, limit } = pagination;
const data = await this.couponService.findAll(page, limit);
const { page, limit, q, expirationBetween } = pagination;
const data = await this.couponService.findAll(
page,
limit,
q,
expirationBetween,
);
const total = await this.couponService.countCoupon();
return { data, total };
}
Expand Down
34 changes: 28 additions & 6 deletions src/discount/controllers/promo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
HttpCode,
ParseUUIDPipe,
UseInterceptors,
Query,
} from '@nestjs/common';
import { AuthGuard } from 'src/auth/auth.guard';
import { RolesGuard } from 'src/auth/roles.guard';
Expand All @@ -23,13 +24,16 @@ import {
ApiResponse,
getSchemaPath,
} from '@nestjs/swagger';
import { PromoDTO, UpdatePromoDTO, ResponsePromoDTO } from '../dto/promo.dto';
import {
PromoDTO,
UpdatePromoDTO,
ResponsePromoDTO,
PromoQueryDTO,
} from '../dto/promo.dto';
import { PromoService } from '../services/promo.service';
import { UserRole } from 'src/user/entities/user.entity';
import { PaginationDTO } from 'src/utils/dto/pagination.dto';
import { PaginationInterceptor } from 'src/utils/pagination.interceptor';
import { PaginationQueryDTO } from 'src/utils/dto/pagination.dto';
import { Pagination } from 'src/utils/pagination.decorator';

@Controller('promo')
@ApiExtraModels(PaginationDTO, ResponsePromoDTO)
Expand Down Expand Up @@ -67,6 +71,19 @@ export class PromoController {
type: Number,
example: 10,
})
@ApiQuery({
name: 'q',
required: false,
description: 'Search text to filter by name',
type: String,
})
@ApiQuery({
name: 'expirationBetween',
required: false,
description: 'Filter by expiration date range',
type: String,
example: '2023-01-01,2023-12-31',
})
@ApiResponse({
description: 'Successful retrieval of promos',
status: HttpStatus.OK,
Expand All @@ -85,10 +102,15 @@ export class PromoController {
},
})
async findAll(
@Pagination() pagination: PaginationQueryDTO,
@Query() pagination: PromoQueryDTO,
): Promise<{ data: ResponsePromoDTO[]; total: number }> {
const { page, limit } = pagination;
const data = await this.promoService.findAll(page, limit);
const { page, limit, q, expirationBetween } = pagination;
const data = await this.promoService.findAll(
page,
limit,
q,
expirationBetween,
);
const total = await this.promoService.countPromos();
return { data, total };
}
Expand Down
22 changes: 22 additions & 0 deletions src/discount/dto/coupon.dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { ApiProperty, PartialType, IntersectionType } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
IsNotEmpty,
IsString,
Length,
IsInt,
Min,
IsDateString,
IsOptional,
} from 'class-validator';
import { BaseDTO } from 'src/utils/dto/base.dto';
import { PaginationQueryDTO } from 'src/utils/dto/pagination.dto';

export class CouponDTO {
@ApiProperty({
Expand Down Expand Up @@ -51,3 +54,22 @@ export class CouponDTO {
export class UpdateCouponDTO extends PartialType(CouponDTO) {}

export class ResponseCouponDTO extends IntersectionType(CouponDTO, BaseDTO) {}

export class CouponQueryDTO extends PaginationQueryDTO {
@IsOptional()
@Transform(({ value }: { value: string }) =>
value ? value.split(',').map((value) => new Date(value)) : [],
)
expirationBetween: Date[];

constructor(
page: number,
limit: number,
q?: string,
expirationBetween?: Date[],
) {
super(page, limit);
this.q = q ? q : '';
this.expirationBetween = expirationBetween ? expirationBetween : [];
}
}
30 changes: 28 additions & 2 deletions src/discount/dto/promo.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger';
import { Expose } from 'class-transformer';
import { IsNotEmpty, IsNumber, IsString, IsDateString } from 'class-validator';
import { Expose, Transform } from 'class-transformer';
import {
IsNotEmpty,
IsNumber,
IsString,
IsDateString,
IsOptional,
} from 'class-validator';
import { BaseDTO } from 'src/utils/dto/base.dto';
import { PaginationQueryDTO } from 'src/utils/dto/pagination.dto';

export class PromoDTO {
@Expose()
Expand Down Expand Up @@ -32,3 +39,22 @@ export class PromoDTO {
export class UpdatePromoDTO extends PartialType(PromoDTO) {}

export class ResponsePromoDTO extends IntersectionType(BaseDTO, PromoDTO) {}

export class PromoQueryDTO extends PaginationQueryDTO {
@IsOptional()
@Transform(({ value }: { value: string }) =>
value ? value.split(',').map((value) => new Date(value)) : [],
)
expirationBetween: Date[];

constructor(
page: number,
limit: number,
q?: string,
expirationBetween?: Date[],
) {
super(page, limit);
this.q = q ? q : '';
this.expirationBetween = expirationBetween ? expirationBetween : [];
}
}
25 changes: 20 additions & 5 deletions src/discount/services/coupon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,26 @@ export class CouponService {
async countCoupon(): Promise<number> {
return await this.couponRepository.count();
}
async findAll(page: number, pageSize: number): Promise<Coupon[]> {
return await this.couponRepository.find({
skip: (page - 1) * pageSize,
take: pageSize,
});
async findAll(
page: number,
pageSize: number,
q?: string,
expirationBetween?: Date[],
): Promise<Coupon[]> {
const coupons = this.couponRepository
.createQueryBuilder('coupon')
.skip((page - 1) * pageSize)
.take(pageSize);
if (q) {
coupons.where('coupon.code ILIKE :code', { code: `%${q}%` });
}
if (expirationBetween && expirationBetween.length === 2) {
coupons.andWhere('coupon.expiration_date BETWEEN :start AND :end', {
start: expirationBetween[0],
end: expirationBetween[1],
});
}
return await coupons.getMany();
}

async findOne(code: string): Promise<Coupon> {
Expand Down
26 changes: 20 additions & 6 deletions src/discount/services/promo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,26 @@ export class PromoService {
});
}

async findAll(page: number, pageSize: number): Promise<Promo[]> {
return await this.promoRepository.find({
where: { deletedAt: IsNull() },
skip: (page - 1) * pageSize,
take: pageSize,
});
async findAll(
page: number,
pageSize: number,
q?: string,
expiredBetween?: Date[],
): Promise<Promo[]> {
const promos = this.promoRepository
.createQueryBuilder('promo')
.skip((page - 1) * pageSize)
.take(pageSize);
if (q) {
promos.where('promo.name ILIKE :name', { name: `%${q}%` });
}
if (expiredBetween && expiredBetween.length === 2) {
promos.andWhere('promo.expired_at BETWEEN :start AND :end', {
start: expiredBetween[0],
end: expiredBetween[1],
});
}
return await promos.getMany();
}

async findOne(id: string): Promise<Promo> {
Expand Down
7 changes: 7 additions & 0 deletions src/products/controllers/presentation.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export class PresentationController {
type: Number,
example: 10,
})
@ApiQuery({
name: 'q',
required: false,
description: 'Filter presentations by name',
type: String,
example: 'presentation name',
})
@ApiResponse({
description: 'Successful retrieval of presentations',
status: HttpStatus.OK,
Expand Down
14 changes: 12 additions & 2 deletions src/products/services/presentation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,19 @@ export class PresentationService {
});
}

async findAll(page: number, pageSize: number): Promise<Presentation[]> {
async findAll(
page: number,
pageSize: number,
q?: string,
): Promise<Presentation[]> {
let where;
if (q) {
where = {
name: q,
};
}
return await this.presentationRepository.find({
where: { deletedAt: IsNull() },
where,
skip: (page - 1) * pageSize,
take: pageSize,
});
Expand Down
Loading