chore: complete create discount
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
|
||||
import { CommonMessage, DiscountMessage } from "../../../common/enums/message.enum";
|
||||
import { DanakServicesService } from "../../danak-services/providers/danak-services.service";
|
||||
import { CreateDiscountDto } from "../DTO/create-discount.dto";
|
||||
import { DiscountRepository } from "../repositories/discount.repository";
|
||||
|
||||
@Injectable()
|
||||
export class DiscountService {
|
||||
constructor(
|
||||
private readonly discountRepository: DiscountRepository,
|
||||
private readonly danakServicesService: DanakServicesService,
|
||||
) {}
|
||||
|
||||
/******************************************** */
|
||||
|
||||
async create(createDiscountDto: CreateDiscountDto) {
|
||||
const { services, ...discountData } = createDiscountDto;
|
||||
|
||||
const code = await this.generateUniqueCouponCode();
|
||||
|
||||
const discount = this.discountRepository.create({
|
||||
...discountData,
|
||||
code,
|
||||
});
|
||||
|
||||
if (services && services.length) {
|
||||
const foundServices = await this.danakServicesService.findServicesByIds(services);
|
||||
discount.services = foundServices;
|
||||
}
|
||||
await this.discountRepository.save(discount);
|
||||
|
||||
return {
|
||||
message: CommonMessage.CREATED,
|
||||
discount,
|
||||
};
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
|
||||
async findAll() {
|
||||
const services = await this.discountRepository.find({ relations: ["services"] });
|
||||
return { services };
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
|
||||
async findOne(id: string) {
|
||||
const discount = await this.discountRepository.findOne({
|
||||
where: { id },
|
||||
relations: ["services"],
|
||||
});
|
||||
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);
|
||||
}
|
||||
|
||||
discount.isActive = !discount.isActive;
|
||||
|
||||
const updatedDiscount = await this.discountRepository.save(discount);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/******************************************** */
|
||||
|
||||
private generateCouponCode(length = 8) {
|
||||
return randomBytes(length).toString("hex").slice(0, length).toUpperCase();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user