chore: delete permission for get ticket categories
This commit is contained in:
@@ -1,13 +1,18 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { QueryRunner } from "typeorm";
|
||||
import { DataSource, QueryRunner } from "typeorm";
|
||||
|
||||
import { DiscountMessage, InvoiceMessage } from "../../../common/enums/message.enum";
|
||||
import { DiscountMessage, InvoiceMessage, WalletMessage } from "../../../common/enums/message.enum";
|
||||
import { DiscountRepository } from "../../discounts/repositories/discount.repository";
|
||||
import { UsageDiscountRepository } from "../../discounts/repositories/usage-discount.repository";
|
||||
import { SubscriptionPlan } from "../../subscriptions/entities/subscription.entity";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { Wallet } from "../../wallets/entities/wallet.entity";
|
||||
import { WalletsService } from "../../wallets/providers/wallets.service";
|
||||
import { ApplyDiscountDto } from "../DTO/apply-discount-invoice.dto";
|
||||
import { CreateInvoiceDto } from "../DTO/create-invoice.dto";
|
||||
import { InvoicesSearchQueryDto, UserInvoicesSearchQueryDto } from "../DTO/invoices-search-query.dto";
|
||||
@@ -21,6 +26,10 @@ export class InvoicesService {
|
||||
constructor(
|
||||
private readonly invoiceRepository: InvoicesRepository,
|
||||
private readonly discountRepository: DiscountRepository,
|
||||
private readonly usageDiscountRepository: UsageDiscountRepository,
|
||||
private readonly usersService: UsersService,
|
||||
private readonly walletsService: WalletsService,
|
||||
private readonly dataSource: DataSource,
|
||||
// private readonly invoiceItemsRepository: InvoiceItemsRepository,
|
||||
) {}
|
||||
|
||||
@@ -198,8 +207,56 @@ export class InvoicesService {
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async applyDiscount(invoiceId: string, applyDiscountDto: ApplyDiscountDto) {
|
||||
const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId });
|
||||
async payInvoice(invoiceId: string, userId: string) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
try {
|
||||
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
||||
|
||||
const invoice = await this.getPendingInvoiceByIdWithQueryRunner(invoiceId, user.id, queryRunner);
|
||||
|
||||
const userWallet = await this.walletsService.getWalletByUserId(user.id, queryRunner);
|
||||
|
||||
if (new Decimal(userWallet.balance).lessThan(invoice.totalPrice)) throw new BadRequestException(WalletMessage.INSUFFICIENT_BALANCE);
|
||||
|
||||
userWallet.balance = new Decimal(userWallet.balance).sub(invoice.totalPrice);
|
||||
|
||||
await queryRunner.manager.save(Wallet, userWallet);
|
||||
|
||||
await this.walletsService.createInvoiceTransaction(invoice.totalPrice, userWallet.id, queryRunner);
|
||||
|
||||
invoice.status = InvoiceStatus.PAID;
|
||||
invoice.paidAt = new Date();
|
||||
|
||||
await queryRunner.manager.save(Invoice, invoice);
|
||||
console.log(invoice);
|
||||
await queryRunner.commitTransaction();
|
||||
return {
|
||||
message: InvoiceMessage.INVOICE_PAID,
|
||||
invoice,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async getPendingInvoiceByIdWithQueryRunner(invoiceId: string, userId: string, queryRunner: QueryRunner) {
|
||||
const invoice = await queryRunner.manager.findOneBy(Invoice, { user: { id: userId }, status: InvoiceStatus.PENDING, id: invoiceId });
|
||||
console.log(invoice, invoiceId, userId);
|
||||
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
|
||||
return invoice;
|
||||
}
|
||||
|
||||
//*********************************** */
|
||||
|
||||
async applyDiscount(invoiceId: string, applyDiscountDto: ApplyDiscountDto, userId: string) {
|
||||
const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId, status: InvoiceStatus.PENDING });
|
||||
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
|
||||
|
||||
const discount = await this.discountRepository.findOne({
|
||||
@@ -210,6 +267,9 @@ export class InvoicesService {
|
||||
throw new BadRequestException(DiscountMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
const hasUsedDiscount = await this.hasUserUsedDiscount(userId, discount.id);
|
||||
if (hasUsedDiscount) throw new BadRequestException(DiscountMessage.ALREADY_USED);
|
||||
|
||||
if (discount.subscriptionPlans && discount.subscriptionPlans.length > 0) {
|
||||
const isApplicable = invoice.items.some((item) => {
|
||||
const subscriptionPlan = item.subscriptionPlan;
|
||||
@@ -222,15 +282,82 @@ export class InvoicesService {
|
||||
}
|
||||
|
||||
if (discount.users && discount.users.length > 0) {
|
||||
if (!invoice.user || !discount.users.some((user) => user.id === invoice.user.id)) {
|
||||
throw new BadRequestException("This discount is not applicable for this user.");
|
||||
if (!discount.users.some((user) => user.id === userId)) {
|
||||
throw new BadRequestException(DiscountMessage.CAN_NOT_APPLICABLE);
|
||||
}
|
||||
}
|
||||
|
||||
invoice.discount = discount;
|
||||
const discountAmount =
|
||||
discount.calculationType === "PERCENTAGE"
|
||||
? new Decimal(invoice.totalPrice).mul(discount.amount).div(100)
|
||||
: new Decimal(discount.amount);
|
||||
|
||||
invoice.totalPrice = new Decimal(invoice.totalPrice).minus(discountAmount);
|
||||
await this.invoiceRepository.save(invoice);
|
||||
|
||||
return { invoice };
|
||||
const usageDiscount = this.usageDiscountRepository.create({
|
||||
discount,
|
||||
users: [{ id: userId }],
|
||||
usageDate: new Date(),
|
||||
});
|
||||
|
||||
await this.usageDiscountRepository.save(usageDiscount);
|
||||
|
||||
return {
|
||||
message: InvoiceMessage.DISCOUNT_APPLIED,
|
||||
invoice,
|
||||
};
|
||||
}
|
||||
|
||||
async cancelDiscount(invoiceId: string, userId: string) {
|
||||
const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId, status: InvoiceStatus.PENDING });
|
||||
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
|
||||
|
||||
const usage = await this.usageDiscountRepository.findOne({
|
||||
where: {
|
||||
users: {
|
||||
id: userId,
|
||||
},
|
||||
},
|
||||
relations: {
|
||||
discount: true,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(usage);
|
||||
if (!usage) throw new BadRequestException(DiscountMessage.NOT_FOUND);
|
||||
|
||||
const discount = await this.discountRepository.findOneBy({ id: usage.discount.id });
|
||||
if (!discount) throw new BadRequestException(DiscountMessage.NOT_FOUND);
|
||||
|
||||
const discountAmount =
|
||||
discount.calculationType === "PERCENTAGE"
|
||||
? new Decimal(invoice.totalPrice).mul(discount.amount).div(100)
|
||||
: new Decimal(discount.amount);
|
||||
|
||||
invoice.totalPrice = new Decimal(invoice.totalPrice).add(discountAmount);
|
||||
await this.invoiceRepository.save(invoice);
|
||||
|
||||
await this.usageDiscountRepository.delete(usage.id);
|
||||
|
||||
return {
|
||||
message: InvoiceMessage.DISCOUNT_CANCELLED,
|
||||
invoice,
|
||||
};
|
||||
}
|
||||
|
||||
private async hasUserUsedDiscount(userId: string, discountId: string): Promise<boolean> {
|
||||
const usage = await this.usageDiscountRepository.findOne({
|
||||
where: {
|
||||
users: {
|
||||
id: userId,
|
||||
},
|
||||
discount: {
|
||||
id: discountId,
|
||||
},
|
||||
},
|
||||
});
|
||||
return !!usage;
|
||||
}
|
||||
|
||||
// async calculateFinalPrice(invoiceId: string): Promise<Decimal> {
|
||||
|
||||
Reference in New Issue
Block a user