fix: bug in thre login process

This commit is contained in:
mahyargdz
2025-03-02 16:28:07 +03:30
parent 4d0344c376
commit 28486d64ee
21 changed files with 606 additions and 588 deletions
@@ -1,182 +1,182 @@
import { randomBytes } from "node:crypto";
// import { randomBytes } from "node:crypto";
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { In } from "typeorm";
// import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
// 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";
// 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()
export class DiscountService {
constructor(
private readonly discountRepository: DiscountRepository,
private readonly subscriptionPlanRepository: SubscriptionsPlanRepository,
private readonly userRepository: UserRepository,
) {}
// @Injectable()
// export class DiscountService {
// constructor(
// private readonly discountRepository: DiscountRepository,
// private readonly subscriptionPlanRepository: SubscriptionsPlanRepository,
// private readonly userRepository: UserRepository,
// ) {}
/******************************************** */
// /******************************************** */
async create(createDiscountDto: CreateDiscountDto) {
const { subscriptionPlanIds, ...discountData } = createDiscountDto;
// async create(createDiscountDto: CreateDiscountDto) {
// const { subscriptionPlanIds, ...discountData } = createDiscountDto;
const code = await this.generateUniqueCouponCode();
// const code = await this.generateUniqueCouponCode();
const discount = this.discountRepository.create({
...discountData,
code,
});
// const discount = this.discountRepository.create({
// ...discountData,
// code,
// });
if (createDiscountDto.subscriptionPlanIds && createDiscountDto.subscriptionPlanIds.length) {
const subscriptionPlans = await this.subscriptionPlanRepository.find({
where: { id: In(createDiscountDto.subscriptionPlanIds) },
});
// if (createDiscountDto.subscriptionPlanIds && createDiscountDto.subscriptionPlanIds.length) {
// const subscriptionPlans = await this.subscriptionPlanRepository.find({
// where: { id: In(createDiscountDto.subscriptionPlanIds) },
// });
if (subscriptionPlans.length !== createDiscountDto.subscriptionPlanIds.length) {
throw new NotFoundException(SubscriptionMessage.NOT_FOUND);
}
// if (subscriptionPlans.length !== createDiscountDto.subscriptionPlanIds.length) {
// throw new NotFoundException(SubscriptionMessage.NOT_FOUND);
// }
discount.subscriptionPlans = subscriptionPlans;
}
// discount.subscriptionPlans = subscriptionPlans;
// }
if (createDiscountDto.userIds) {
const users = await this.userRepository.find({ where: { id: In(createDiscountDto.userIds) } });
// if (createDiscountDto.userIds) {
// const users = await this.userRepository.find({ where: { id: In(createDiscountDto.userIds) } });
if (users.length !== createDiscountDto.userIds.length) {
throw new NotFoundException(UserMessage.USER_NOT_FOUND);
}
// if (users.length !== createDiscountDto.userIds.length) {
// throw new NotFoundException(UserMessage.USER_NOT_FOUND);
// }
discount.users = users;
}
// discount.users = users;
// }
await this.discountRepository.save(discount);
// await this.discountRepository.save(discount);
return {
message: CommonMessage.CREATED,
discount,
};
}
// return {
// message: CommonMessage.CREATED,
// discount,
// };
// }
/******************************************** */
// /******************************************** */
async findByCode(code: string) {
const discount = await this.discountRepository.findOne({
where: { code, isActive: true },
relations: ["subscriptionPlans"],
});
if (!discount) {
throw new NotFoundException(DiscountMessage.NOT_FOUND);
}
return { discount };
}
// async findByCode(code: string) {
// const discount = await this.discountRepository.findOne({
// where: { code, isActive: true },
// relations: ["subscriptionPlans"],
// });
// if (!discount) {
// throw new NotFoundException(DiscountMessage.NOT_FOUND);
// }
// return { discount };
// }
/******************************************** */
// /******************************************** */
async findDiscountsByUser(queryDto: SearchDiscountsDto, userId: string) {
const user = await this.userRepository.findOneBy({
id: userId,
});
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
// async findDiscountsByUser(queryDto: SearchDiscountsDto, userId: string) {
// const user = await this.userRepository.findOneBy({
// id: userId,
// });
// if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
const { limit, skip } = PaginationUtils(queryDto);
// const { limit, skip } = PaginationUtils(queryDto);
const queryBuilder = this.discountRepository.createQueryBuilder("discount");
// 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"]);
// 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}%` });
}
// if (queryDto.q) {
// queryBuilder.andWhere("discount.code ILIKE :q", { q: `%${queryDto.q}%` });
// }
queryBuilder.orderBy("discount.createdAt", "DESC").skip(skip).take(limit);
// queryBuilder.orderBy("discount.createdAt", "DESC").skip(skip).take(limit);
const [discounts, count] = await queryBuilder.getManyAndCount();
// const [discounts, count] = await queryBuilder.getManyAndCount();
return {
discounts,
count,
paginate: true,
};
}
// return {
// discounts,
// count,
// paginate: true,
// };
// }
/******************************************** */
// /******************************************** */
async findAll(queryDto: SearchDiscountsDto) {
const { limit, skip } = PaginationUtils(queryDto);
// async findAll(queryDto: SearchDiscountsDto) {
// const { limit, skip } = PaginationUtils(queryDto);
const queryBuilder = this.discountRepository.createQueryBuilder("discount");
// 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");
// 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}%` });
}
// if (queryDto.q) {
// queryBuilder.andWhere("discount.code ILIKE :q", { q: `%${queryDto.q}%` });
// }
queryBuilder.orderBy("discount.createdAt", "DESC").skip(skip).take(limit);
// queryBuilder.orderBy("discount.createdAt", "DESC").skip(skip).take(limit);
const [discounts, count] = await queryBuilder.getManyAndCount();
// const [discounts, count] = await queryBuilder.getManyAndCount();
return {
discounts,
count,
paginate: true,
};
}
// return {
// discounts,
// count,
// paginate: true,
// };
// }
/******************************************** */
// /******************************************** */
async findOne(id: string) {
const discount = await this.discountRepository.findOne({
where: { id },
relations: ["subscriptionPlans"],
});
if (!discount) throw new BadRequestException(DiscountMessage.NOT_FOUND);
return { discount };
}
// async findOne(id: string) {
// const discount = await this.discountRepository.findOne({
// where: { id },
// relations: ["subscriptionPlans"],
// });
// if (!discount) throw new BadRequestException(DiscountMessage.NOT_FOUND);
// return { discount };
// }
/******************************************** */
// /******************************************** */
async toggleActive(id: string) {
const discount = await this.discountRepository.findOne({ where: { id } });
if (!discount) {
throw new BadRequestException(DiscountMessage.NOT_FOUND);
}
// async toggleActive(id: string) {
// const discount = await this.discountRepository.findOne({ where: { id } });
// if (!discount) {
// throw new BadRequestException(DiscountMessage.NOT_FOUND);
// }
discount.isActive = !discount.isActive;
// discount.isActive = !discount.isActive;
const updatedDiscount = await this.discountRepository.save(discount);
// const updatedDiscount = await this.discountRepository.save(discount);
return {
message: CommonMessage.UPDATE_SUCCESS,
updatedDiscount,
};
}
// return {
// message: CommonMessage.UPDATE_SUCCESS,
// updatedDiscount,
// };
// }
/******************************************** */
// /******************************************** */
async generateUniqueCouponCode(): Promise<string> {
const code = this.generateCouponCode();
const exists = await this.discountRepository.findOne({ where: { code } });
if (exists) return this.generateUniqueCouponCode();
return code;
}
// async generateUniqueCouponCode(): Promise<string> {
// const code = this.generateCouponCode();
// const exists = await this.discountRepository.findOne({ where: { code } });
// if (exists) return this.generateUniqueCouponCode();
// return code;
// }
/******************************************** */
// /******************************************** */
private generateCouponCode(length = 8) {
return randomBytes(length).toString("hex").slice(0, length).toUpperCase();
}
}
// private generateCouponCode(length = 8) {
// return randomBytes(length).toString("hex").slice(0, length).toUpperCase();
// }
// }