chore: get discount data with all relation
This commit is contained in:
@@ -6,7 +6,9 @@ import { In } from "typeorm";
|
||||
import { CommonMessage, DiscountMessage, SubscriptionMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { SubscriptionsPlanRepository } from "../../subscriptions/repositories/subscriptions.repository";
|
||||
import { UserRepository } from "../../users/repositories/users.repository";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { CreateDiscountDto } from "../DTO/create-discount.dto";
|
||||
import { SearchDiscountsDto } from "../DTO/discount-search-query.dto";
|
||||
import { DiscountRepository } from "../repositories/discount.repository";
|
||||
|
||||
@Injectable()
|
||||
@@ -74,26 +76,64 @@ export class DiscountService {
|
||||
|
||||
/******************************************** */
|
||||
|
||||
async findDiscountsByUser(userId: string) {
|
||||
async findDiscountsByUser(queryDto: SearchDiscountsDto, userId: string) {
|
||||
const user = await this.userRepository.findOneBy({
|
||||
id: userId,
|
||||
});
|
||||
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
|
||||
|
||||
const discounts = await this.discountRepository.find({
|
||||
where: {
|
||||
users: { id: user.id },
|
||||
},
|
||||
});
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
return { discounts };
|
||||
const queryBuilder = this.discountRepository.createQueryBuilder("discount");
|
||||
|
||||
queryBuilder
|
||||
.leftJoin("discount.users", "user")
|
||||
.where("user.id = :userId", { userId: user.id })
|
||||
.leftJoinAndSelect("discount.subscriptionPlans", "subscriptionPlans")
|
||||
.leftJoin("subscriptionPlans.service", "service")
|
||||
.addSelect(["service.id", "service.name", "service.title", "service.link", "service.icon", "service.description"]);
|
||||
|
||||
if (queryDto.q) {
|
||||
queryBuilder.andWhere("discount.code ILIKE :q", { q: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
queryBuilder.orderBy("discount.createdAt", "DESC").skip(skip).take(limit);
|
||||
|
||||
const [discounts, count] = await queryBuilder.getManyAndCount();
|
||||
|
||||
return {
|
||||
discounts,
|
||||
count,
|
||||
paginate: true,
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
|
||||
async findAll() {
|
||||
const discounts = await this.discountRepository.find({ relations: ["subscriptionPlans"] });
|
||||
return { discounts };
|
||||
async findAll(queryDto: SearchDiscountsDto) {
|
||||
const { limit, skip } = PaginationUtils(queryDto);
|
||||
|
||||
const queryBuilder = this.discountRepository.createQueryBuilder("discount");
|
||||
|
||||
queryBuilder
|
||||
.leftJoinAndSelect("discount.subscriptionPlans", "subscriptionPlans")
|
||||
.leftJoin("subscriptionPlans.service", "service")
|
||||
.addSelect(["service.id", "service.name", "service.title", "service.link", "service.icon", "service.description"])
|
||||
.leftJoinAndSelect("discount.users", "users");
|
||||
|
||||
if (queryDto.q) {
|
||||
queryBuilder.andWhere("discount.code ILIKE :q", { q: `%${queryDto.q}%` });
|
||||
}
|
||||
|
||||
queryBuilder.orderBy("discount.createdAt", "DESC").skip(skip).take(limit);
|
||||
|
||||
const [discounts, count] = await queryBuilder.getManyAndCount();
|
||||
|
||||
return {
|
||||
discounts,
|
||||
count,
|
||||
paginate: true,
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
|
||||
Reference in New Issue
Block a user