update: commit for the new file
This commit is contained in:
Regular → Executable
+42
-5
@@ -1,22 +1,28 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
import { DataSource, QueryRunner } from "typeorm";
|
||||
|
||||
import { DiscountMessage } from "../../../common/enums/message.enum";
|
||||
import { SubscriptionsService } from "../../subscriptions/providers/subscriptions.service";
|
||||
import { CreateDiscountDto } from "../DTO/create-discount.dto";
|
||||
import { DiscountType } from "../enums/discount-type.enum";
|
||||
import { DirectDiscountRepository } from "../repositories/direct-discounts.repository";
|
||||
import { DiscountRepository } from "../repositories/discounts.repository";
|
||||
|
||||
@Injectable()
|
||||
export class DiscountsService {
|
||||
private MAX_ATTEMPTS = 10;
|
||||
|
||||
constructor(
|
||||
private readonly discountRepository: DiscountRepository,
|
||||
private readonly directDiscountRepository: DirectDiscountRepository,
|
||||
private readonly subscriptionService: SubscriptionsService,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
//***************************************** */
|
||||
|
||||
async create(createDiscountDto: CreateDiscountDto) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
@@ -24,6 +30,8 @@ export class DiscountsService {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
await this.validateDates(createDiscountDto.startDate, createDiscountDto.endDate);
|
||||
|
||||
if (!createDiscountDto.direct) {
|
||||
await this.createCodeBasedDiscount(createDiscountDto, queryRunner);
|
||||
} else {
|
||||
@@ -42,7 +50,7 @@ export class DiscountsService {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************** */
|
||||
private async createCodeBasedDiscount(createDiscountDto: CreateDiscountDto, queryRunner: QueryRunner) {
|
||||
const discount = queryRunner.manager.create(this.discountRepository.target, createDiscountDto);
|
||||
|
||||
@@ -63,12 +71,21 @@ export class DiscountsService {
|
||||
await queryRunner.manager.save(discount);
|
||||
return discount;
|
||||
}
|
||||
|
||||
//**************************** */
|
||||
//***************************************** */
|
||||
private async createDirectBasedDiscount(createDiscountDto: CreateDiscountDto, queryRunner: QueryRunner) {
|
||||
const discount = queryRunner.manager.create(this.discountRepository.target, createDiscountDto);
|
||||
// Create the discount entity
|
||||
const discount = queryRunner.manager.create(this.directDiscountRepository.target, createDiscountDto);
|
||||
discount.value = this.calculateDiscountValue(createDiscountDto);
|
||||
|
||||
await queryRunner.manager.save(discount);
|
||||
|
||||
const subscriptionPlans = await this.subscriptionService.getAllServiceSubscriptions(createDiscountDto.targetServices ?? []);
|
||||
|
||||
for (const subscriptionPlan of subscriptionPlans) {
|
||||
subscriptionPlan.directDiscount = discount;
|
||||
await queryRunner.manager.save(subscriptionPlan);
|
||||
}
|
||||
|
||||
return discount;
|
||||
}
|
||||
//**************************** */
|
||||
@@ -91,4 +108,24 @@ export class DiscountsService {
|
||||
|
||||
throw new BadRequestException(DiscountMessage.INVALID_DISCOUNT_TYPE);
|
||||
}
|
||||
|
||||
//**************************** */
|
||||
|
||||
private async validateDates(startDate: string, endDate: string) {
|
||||
if (dayjs(startDate).isAfter(dayjs(endDate))) {
|
||||
throw new BadRequestException(DiscountMessage.START_DATE_MUST_BE_BEFORE_END_DATE);
|
||||
}
|
||||
|
||||
if (dayjs(startDate).isBefore(dayjs().subtract(1, "day"))) {
|
||||
throw new BadRequestException(DiscountMessage.START_DATE_MUST_BE_AFTER_NOW);
|
||||
}
|
||||
|
||||
if (dayjs(startDate).isSame(dayjs(endDate))) {
|
||||
throw new BadRequestException(DiscountMessage.START_DATE_MUST_BE_BEFORE_END_DATE);
|
||||
}
|
||||
|
||||
if (dayjs(endDate).isBefore(dayjs())) {
|
||||
throw new BadRequestException(DiscountMessage.END_DATE_MUST_BE_AFTER_NOW);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user