chore: discount module

This commit is contained in:
Matin
2025-02-16 16:30:17 +03:30
parent 6785b3e9d0
commit 20d6c5b294
31 changed files with 1752 additions and 141 deletions
@@ -1,9 +1,11 @@
import { randomBytes } from "node:crypto";
import { BadRequestException, Injectable } from "@nestjs/common";
import { BadRequestException, Injectable, NotFoundException } from "@nestjs/common";
import { In } from "typeorm";
import { CommonMessage, DiscountMessage } from "../../../common/enums/message.enum";
// import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
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 { CreateDiscountDto } from "../DTO/create-discount.dto";
import { DiscountRepository } from "../repositories/discount.repository";
@@ -11,13 +13,14 @@ import { DiscountRepository } from "../repositories/discount.repository";
export class DiscountService {
constructor(
private readonly discountRepository: DiscountRepository,
// private readonly danakServicesService: DanakServicesService,
private readonly subscriptionPlanRepository: SubscriptionsPlanRepository,
private readonly userRepository: UserRepository,
) {}
/******************************************** */
async create(createDiscountDto: CreateDiscountDto) {
const { services, ...discountData } = createDiscountDto;
const { subscriptionPlanIds, ...discountData } = createDiscountDto;
const code = await this.generateUniqueCouponCode();
@@ -26,10 +29,28 @@ export class DiscountService {
code,
});
if (services && services.length) {
// const foundServices = await this.danakServicesService.findServicesByIds(services);
// discount.services = foundServices;
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);
}
discount.subscriptionPlans = subscriptionPlans;
}
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);
}
discount.users = users;
}
await this.discountRepository.save(discount);
return {
@@ -40,9 +61,39 @@ export class DiscountService {
/******************************************** */
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(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 },
},
});
return { discounts };
}
/******************************************** */
async findAll() {
const services = await this.discountRepository.find({ relations: ["services"] });
return { services };
const discounts = await this.discountRepository.find({ relations: ["subscriptionPlans"] });
return { discounts };
}
/******************************************** */
@@ -50,7 +101,7 @@ export class DiscountService {
async findOne(id: string) {
const discount = await this.discountRepository.findOne({
where: { id },
relations: ["services"],
relations: ["subscriptionPlans"],
});
if (!discount) throw new BadRequestException(DiscountMessage.NOT_FOUND);
return { discount };