chore: update user profile

This commit is contained in:
Matin
2025-02-20 09:50:01 +03:30
parent c306dca107
commit 99fa47356a
5 changed files with 59 additions and 9 deletions
@@ -310,34 +310,39 @@ export class InvoicesService {
}
async cancelDiscount(invoiceId: string, userId: string) {
// Find the invoice by ID and ensure it is in a PENDING status
const invoice = await this.invoiceRepository.findOneBy({ id: invoiceId, status: InvoiceStatus.PENDING });
if (!invoice) throw new BadRequestException(InvoiceMessage.NOT_FOUND_BY_ID);
// Find the usage discount associated with the user and invoice
const usage = await this.usageDiscountRepository.findOne({
where: {
users: {
id: userId,
},
users: { id: userId },
discount: { invoice: { id: invoice.id } },
},
relations: {
discount: true,
users: true,
},
});
console.log(usage);
if (!usage) throw new BadRequestException(DiscountMessage.NOT_FOUND);
// Find the discount associated with the usage
const discount = await this.discountRepository.findOneBy({ id: usage.discount.id });
if (!discount) throw new BadRequestException(DiscountMessage.NOT_FOUND);
// Calculate the discount amount to be removed from the invoice
const discountAmount =
discount.calculationType === "PERCENTAGE"
? new Decimal(invoice.totalPrice).mul(discount.amount).div(100)
: new Decimal(discount.amount);
// Update the invoice's total price by adding the discount amount (effectively removing the discount)
invoice.totalPrice = new Decimal(invoice.totalPrice).add(discountAmount);
await this.invoiceRepository.save(invoice);
// Delete the usage discount record
await this.usageDiscountRepository.delete(usage.id);
return {