chore: apply discount on plan
This commit is contained in:
@@ -59,9 +59,4 @@ export class InvoicesController {
|
||||
async applyDiscount(@Param() paramDto: ParamDto, @Body() applyDiscountDto: ApplyDiscountDto) {
|
||||
return this.invoiceService.applyDiscount(paramDto.id, applyDiscountDto);
|
||||
}
|
||||
|
||||
@Get(":id/final-price")
|
||||
async getFinalPrice(@Param() paramDto: ParamDto) {
|
||||
return this.invoiceService.calculateFinalPrice(paramDto.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import dayjs from "dayjs";
|
||||
import Decimal from "decimal.js";
|
||||
import { QueryRunner } from "typeorm";
|
||||
|
||||
import { InvoiceMessage } from "../../../common/enums/message.enum";
|
||||
import { DiscountMessage, InvoiceMessage } from "../../../common/enums/message.enum";
|
||||
import { DiscountRepository } from "../../discounts/repositories/discount.repository";
|
||||
import { SubscriptionPlan } from "../../subscriptions/entities/subscription.entity";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
@@ -207,7 +207,7 @@ export class InvoicesService {
|
||||
relations: ["subscriptionPlans", "users"],
|
||||
});
|
||||
if (!discount) {
|
||||
throw new BadRequestException(`Discount with code ${applyDiscountDto.discountCode} not found or inactive`);
|
||||
throw new BadRequestException(DiscountMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
if (discount.subscriptionPlans && discount.subscriptionPlans.length > 0) {
|
||||
@@ -217,7 +217,7 @@ export class InvoicesService {
|
||||
return discount.subscriptionPlans.some((plan) => plan.id === subscriptionPlan.id);
|
||||
});
|
||||
if (!isApplicable) {
|
||||
throw new BadRequestException("This discount is not applicable to any subscription plan in the invoice.");
|
||||
throw new BadRequestException(DiscountMessage.CAN_NOT_APPLICABLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,18 +233,18 @@ export class InvoicesService {
|
||||
return { invoice };
|
||||
}
|
||||
|
||||
async calculateFinalPrice(invoiceId: string): Promise<Decimal> {
|
||||
const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId });
|
||||
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
|
||||
// async calculateFinalPrice(invoiceId: string): Promise<Decimal> {
|
||||
// const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId });
|
||||
// if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
|
||||
|
||||
let discountValue = new Decimal(0);
|
||||
if (invoice.discount) {
|
||||
if (invoice.discount.calculationType === "PERCENTAGE") {
|
||||
discountValue = invoice.totalPrice.mul(invoice.discount.amount).div(100);
|
||||
} else if (invoice.discount.calculationType === "FIXED") {
|
||||
discountValue = new Decimal(invoice.discount.amount);
|
||||
}
|
||||
}
|
||||
return invoice.totalPrice.minus(discountValue);
|
||||
}
|
||||
// let discountValue = new Decimal(0);
|
||||
// if (invoice.discount) {
|
||||
// if (invoice.discount.calculationType === "PERCENTAGE") {
|
||||
// discountValue = invoice.totalPrice.mul(invoice.discount.amount).div(100);
|
||||
// } else if (invoice.discount.calculationType === "FIXED") {
|
||||
// discountValue = new Decimal(invoice.discount.amount);
|
||||
// }
|
||||
// }
|
||||
// return invoice.totalPrice.minus(discountValue);
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user