update: add new field for subscription of user when purchased
This commit is contained in:
@@ -225,11 +225,13 @@ export class SubscriptionsService {
|
||||
|
||||
if (!plan) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
||||
|
||||
const startDate = new Date();
|
||||
const endDate = new Date();
|
||||
endDate.setDate(startDate.getDate() + plan.duration);
|
||||
const startDate = dayjs().toDate();
|
||||
const endDate = dayjs().add(plan.duration, "day").toDate();
|
||||
// const startDate = new Date();
|
||||
// const endDate = new Date();
|
||||
// endDate.setDate(startDate.getDate() + plan.duration);
|
||||
//
|
||||
const userSubscription = queryRunner.manager.create(UserSubscription, { user, plan, startDate, endDate });
|
||||
const userSubscription = queryRunner.manager.create(UserSubscription, { ...subscribeDto, user, plan, startDate, endDate });
|
||||
|
||||
await queryRunner.manager.save(UserSubscription, userSubscription);
|
||||
|
||||
@@ -237,18 +239,16 @@ export class SubscriptionsService {
|
||||
|
||||
let finalPrice = new Decimal(plan.price);
|
||||
|
||||
if (plan.discounts && plan.discounts.length > 0) {
|
||||
plan.discounts.forEach((discount) => {
|
||||
if (discount.isActive && new Date() >= discount.startDate && new Date() <= discount.endDate) {
|
||||
if (discount.calculationType === DiscountCalculationType.PERCENTAGE) {
|
||||
const discountAmount = finalPrice.mul(discount.amount).div(100);
|
||||
finalPrice = finalPrice.sub(discountAmount);
|
||||
} else if (discount.calculationType === DiscountCalculationType.FIXED) {
|
||||
finalPrice = finalPrice.sub(discount.amount);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
const activeDiscounts =
|
||||
plan.discounts?.filter((discount) => discount.isActive && new Date() >= discount.startDate && new Date() <= discount.endDate) || [];
|
||||
|
||||
activeDiscounts.forEach((discount) => {
|
||||
if (discount.calculationType === DiscountCalculationType.PERCENTAGE) {
|
||||
finalPrice = finalPrice.sub(finalPrice.mul(discount.amount).div(100));
|
||||
} else if (discount.calculationType === DiscountCalculationType.FIXED) {
|
||||
finalPrice = finalPrice.sub(discount.amount);
|
||||
}
|
||||
});
|
||||
|
||||
plan.price = finalPrice;
|
||||
|
||||
@@ -264,8 +264,7 @@ export class SubscriptionsService {
|
||||
|
||||
await this.walletsService.createSubscriptionTransaction(finalPrice, userWallet.id, queryRunner);
|
||||
//TODO: need queue handling for notification
|
||||
const invoiceDueDate = new Date(userSubscription.endDate);
|
||||
invoiceDueDate.setDate(userSubscription.endDate.getDate() - 3);
|
||||
const invoiceDueDate = dayjs(userSubscription.endDate).subtract(3, "day").toDate();
|
||||
|
||||
const invoice = await this.invoicesService.createInvoiceForSubscription(userId, plan, invoiceDueDate, queryRunner);
|
||||
userSubscription.status = SubscriptionStatus.ACTIVE;
|
||||
@@ -419,19 +418,4 @@ export class SubscriptionsService {
|
||||
const growthRate = ((currentYearCount - lastYearCount) / lastYearCount) * 100;
|
||||
return Math.round(growthRate * 100) / 100;
|
||||
}
|
||||
|
||||
/********************************* */
|
||||
|
||||
async activeUserSubscription(userId: string, serviceId: string) {
|
||||
const activeSubscription = await this.userSubscriptionsRepository.findOne({
|
||||
where: {
|
||||
user: { id: userId },
|
||||
plan: { service: { id: serviceId } },
|
||||
status: SubscriptionStatus.ACTIVE,
|
||||
},
|
||||
relations: ["plan", "plan.service"],
|
||||
});
|
||||
console.log(activeSubscription);
|
||||
return activeSubscription;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user