extend subscription
This commit is contained in:
@@ -479,6 +479,7 @@ export const enum NotificationMessage {
|
|||||||
|
|
||||||
export const enum SubscriptionMessage {
|
export const enum SubscriptionMessage {
|
||||||
DURATION_SHOULD_BE_INT = "مدت زمان باید یک عدد صحیح باشد",
|
DURATION_SHOULD_BE_INT = "مدت زمان باید یک عدد صحیح باشد",
|
||||||
|
INVOICE_CREATED_SUCCESSFULLY = "صورتحساب شما با موفقیت صادر شد",
|
||||||
DURATION_REQUIRED = "مدت زمان مورد نیاز است",
|
DURATION_REQUIRED = "مدت زمان مورد نیاز است",
|
||||||
PRICE_REQUIRED = "قیمت مورد نیاز است",
|
PRICE_REQUIRED = "قیمت مورد نیاز است",
|
||||||
PRICE_SHOULD_BE_INT = "قیمت باید یک عدد صحیح باشد",
|
PRICE_SHOULD_BE_INT = "قیمت باید یک عدد صحیح باشد",
|
||||||
@@ -496,6 +497,7 @@ export const enum SubscriptionMessage {
|
|||||||
NOT_FOUND = "اشتراک یافت نشد",
|
NOT_FOUND = "اشتراک یافت نشد",
|
||||||
UPDATED = "اشتراک با موفقیت به روز رسانی شد",
|
UPDATED = "اشتراک با موفقیت به روز رسانی شد",
|
||||||
PLAN_ID_REQUIRED = "شناسه اشتراک مورد نیاز است",
|
PLAN_ID_REQUIRED = "شناسه اشتراک مورد نیاز است",
|
||||||
|
PLAN_NOT_FOUND = " پلن یافت نشد",
|
||||||
PLAN_ID_SHOULD_BE_UUID = "شناسه اشتراک باید یک UUID معتبر باشد",
|
PLAN_ID_SHOULD_BE_UUID = "شناسه اشتراک باید یک UUID معتبر باشد",
|
||||||
SUBSCRIBED = "اشتراک شما با موفقیت ثبت شد",
|
SUBSCRIBED = "اشتراک شما با موفقیت ثبت شد",
|
||||||
SEARCH_QUERY_STRING = "رشته جستجو باید یک رشته باشد",
|
SEARCH_QUERY_STRING = "رشته جستجو باید یک رشته باشد",
|
||||||
|
|||||||
@@ -310,7 +310,6 @@ export class SubscriptionsService {
|
|||||||
async extendSubscribeToPlan(userSubscriptionId: string, dto: ExtendUSerSubscribeServiceDto, userId: string) {
|
async extendSubscribeToPlan(userSubscriptionId: string, dto: ExtendUSerSubscribeServiceDto, userId: string) {
|
||||||
const queryRunner = this.dataSource.createQueryRunner();
|
const queryRunner = this.dataSource.createQueryRunner();
|
||||||
|
|
||||||
const isFree = false;
|
|
||||||
try {
|
try {
|
||||||
await queryRunner.connect();
|
await queryRunner.connect();
|
||||||
await queryRunner.startTransaction();
|
await queryRunner.startTransaction();
|
||||||
@@ -322,6 +321,7 @@ export class SubscriptionsService {
|
|||||||
plan: true,
|
plan: true,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!userSubscription) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
if (!userSubscription) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
||||||
|
|
||||||
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
const user = await this.usersService.findOneByIdWithQueryRunner(userId, queryRunner);
|
||||||
@@ -330,35 +330,26 @@ export class SubscriptionsService {
|
|||||||
where: { id: dto.planId, service: { id: dto.serviceId } },
|
where: { id: dto.planId, service: { id: dto.serviceId } },
|
||||||
relations: { service: true, directDiscount: true },
|
relations: { service: true, directDiscount: true },
|
||||||
});
|
});
|
||||||
if (!plan) throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
|
||||||
|
if (!plan) throw new BadRequestException(SubscriptionMessage.PLAN_NOT_FOUND);
|
||||||
|
|
||||||
if (plan.isFree) {
|
if (plan.isFree) {
|
||||||
throw new BadRequestException(SubscriptionMessage.FREE_PLAN_NOT_ALLOWED);
|
throw new BadRequestException(SubscriptionMessage.FREE_PLAN_NOT_ALLOWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
const startDate =
|
|
||||||
userSubscription && dayjs(userSubscription.endDate).isAfter(dayjs()) ? dayjs(userSubscription.endDate).toDate() : dayjs().toDate();
|
|
||||||
|
|
||||||
const endDate = dayjs(startDate).add(plan.duration, "day").toDate();
|
|
||||||
|
|
||||||
// await queryRunner.manager.save(UserSubscription, userSubscription);
|
|
||||||
const invoiceDueDate = dayjs().add(INVOICE.DUEDATE, "day").toDate();
|
const invoiceDueDate = dayjs().add(INVOICE.DUEDATE, "day").toDate();
|
||||||
|
|
||||||
const invoice = await this.invoicesService.createInvoiceForSubscription(user, plan, userSubscription, invoiceDueDate, queryRunner);
|
const invoice = await this.invoicesService.createInvoiceForSubscription(user, plan, userSubscription, invoiceDueDate, queryRunner);
|
||||||
|
|
||||||
userSubscription.plan = plan;
|
userSubscription.plan = plan;
|
||||||
userSubscription.endDate = endDate;
|
|
||||||
await queryRunner.manager.save(userSubscription);
|
await queryRunner.manager.save(userSubscription);
|
||||||
|
|
||||||
await this.addProvisioningJob(userSubscription, plan, user);
|
await this.addProvisioningJob(userSubscription, plan, user);
|
||||||
|
|
||||||
if (isFree) {
|
|
||||||
await this.invoicesService.payInvoice(invoice.id, user.id, undefined, queryRunner);
|
|
||||||
}
|
|
||||||
|
|
||||||
await queryRunner.commitTransaction();
|
await queryRunner.commitTransaction();
|
||||||
return {
|
return {
|
||||||
message: SubscriptionMessage.SUBSCRIBED,
|
message: SubscriptionMessage.INVOICE_CREATED_SUCCESSFULLY,
|
||||||
userSubscription,
|
userSubscription,
|
||||||
invoice,
|
invoice,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user