update: the danak services module and add new route to fetch the danak service
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
|
||||
import { Discount } from "../entities/discount.entity";
|
||||
|
||||
@Injectable()
|
||||
export class DiscountRepository extends Repository<Discount> {
|
||||
constructor(@InjectRepository(Discount) repository: Repository<Discount>) {
|
||||
super(repository.target, repository.manager, repository.queryRunner);
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Discount | null> {
|
||||
return this.findOne({
|
||||
where: { id },
|
||||
relations: ["productRelations"],
|
||||
});
|
||||
}
|
||||
|
||||
async findByCode(code: string): Promise<Discount | null> {
|
||||
return this.findOne({
|
||||
where: { code },
|
||||
relations: ["productRelations"],
|
||||
});
|
||||
}
|
||||
|
||||
async findActiveByCode(code: string): Promise<Discount | null> {
|
||||
return this.findOne({
|
||||
where: {
|
||||
code,
|
||||
isActive: true,
|
||||
},
|
||||
relations: ["productRelations"],
|
||||
});
|
||||
}
|
||||
|
||||
async findAll(options?: any): Promise<[Discount[], number]> {
|
||||
const query = this.createQueryBuilder("discount").leftJoinAndSelect("discount.productRelations", "productRelations");
|
||||
|
||||
if (options?.status) {
|
||||
query.andWhere("discount.status = :status", { status: options.status });
|
||||
}
|
||||
|
||||
if (options?.isActive !== undefined) {
|
||||
query.andWhere("discount.isActive = :isActive", { isActive: options.isActive });
|
||||
}
|
||||
|
||||
if (options?.search) {
|
||||
query.andWhere("(discount.title ILIKE :search OR discount.code ILIKE :search)", {
|
||||
search: `%${options.search}%`,
|
||||
});
|
||||
}
|
||||
|
||||
if (options?.productId) {
|
||||
query.andWhere("EXISTS (SELECT 1 FROM discount_product dp WHERE dp.discountId = discount.id AND dp.productId = :productId)", {
|
||||
productId: options.productId,
|
||||
});
|
||||
}
|
||||
|
||||
if (options?.sort) {
|
||||
const [field, order] = options.sort.split(",");
|
||||
query.orderBy(`discount.${field}`, order.toUpperCase());
|
||||
} else {
|
||||
query.orderBy("discount.createdAt", "DESC");
|
||||
}
|
||||
|
||||
return query
|
||||
.skip(options?.skip || 0)
|
||||
.take(options?.take || 10)
|
||||
.getManyAndCount();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user