fix : return orginal error
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
import Decimal from "decimal.js";
|
||||
|
||||
import { SubscriptionMessage } from "../../../common/enums/message.enum";
|
||||
import { InvoicesService } from "../../invoices/providers/invoices.service";
|
||||
import { UserSubscription } from "../../subscriptions/entities/user-subscription.entity";
|
||||
import { UserSubscriptionsRepository } from "../../subscriptions/repositories/user-subscriptions.repository";
|
||||
import { BuyPremiumPlanDto } from "../DTO/buy-premium-plan.dto";
|
||||
|
||||
@Injectable()
|
||||
export class PremiumService {
|
||||
constructor(
|
||||
private readonly userSubscriptionsRepository: UserSubscriptionsRepository,
|
||||
private readonly invoicesService: InvoicesService,
|
||||
) {}
|
||||
|
||||
async buyPremiumPlan(dto: BuyPremiumPlanDto, userId: string) {
|
||||
// Find the user's current subscription
|
||||
const userSubscription = await this.userSubscriptionsRepository.findOne({
|
||||
where: { id: dto.userSubscriptionId, user: { id: userId } },
|
||||
relations: {
|
||||
user: true,
|
||||
plan: { service: true },
|
||||
},
|
||||
});
|
||||
|
||||
if (!userSubscription) {
|
||||
throw new BadRequestException(SubscriptionMessage.NOT_FOUND);
|
||||
}
|
||||
|
||||
// Calculate remaining days
|
||||
const remainingDays = this.calculateRemainingDays(userSubscription);
|
||||
|
||||
// Extract duration from current plan and convert to months
|
||||
const planDurationInMonths = Math.round(userSubscription.plan.duration / 30);
|
||||
|
||||
// Get premium pricing based on extracted duration
|
||||
const premiumPrice = this.getPremiumPrice(planDurationInMonths);
|
||||
|
||||
// Create invoice items for premium purchase
|
||||
const invoiceItems = [{
|
||||
name: `Premium Plan - ${planDurationInMonths} Months`,
|
||||
count: 1,
|
||||
unitPrice: premiumPrice.toNumber(),
|
||||
discount: 0,
|
||||
}];
|
||||
|
||||
// Create invoice directly
|
||||
const createInvoiceDto = {
|
||||
userId: userSubscription.user.id,
|
||||
items: invoiceItems,
|
||||
isRecurring: false,
|
||||
};
|
||||
|
||||
const invoice = await this.invoicesService.createInvoiceAdmin(createInvoiceDto, {
|
||||
ip: '',
|
||||
headers: {},
|
||||
userId: userSubscription.user.id
|
||||
});
|
||||
|
||||
return {
|
||||
message: "Premium plan purchased successfully",
|
||||
userSubscription,
|
||||
invoice,
|
||||
remainingDays,
|
||||
premiumPrice: premiumPrice.toNumber(),
|
||||
};
|
||||
}
|
||||
|
||||
//************************************ */
|
||||
private calculateRemainingDays(userSubscription: UserSubscription): number {
|
||||
const now = dayjs();
|
||||
const endDate = dayjs(userSubscription.endDate);
|
||||
|
||||
if (endDate.isBefore(now)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return endDate.diff(now, 'day');
|
||||
}
|
||||
|
||||
//************************************ */
|
||||
private getPremiumPrice(durationInMonths: number): Decimal {
|
||||
switch (durationInMonths) {
|
||||
case 6:
|
||||
return new Decimal(5000000);
|
||||
case 12:
|
||||
return new Decimal(10000000);
|
||||
default:
|
||||
throw new BadRequestException("Invalid premium plan duration. Only 6 and 12 month plans are supported.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,12 @@ export class RestaurantService {
|
||||
);
|
||||
return data;
|
||||
} catch (error: unknown) {
|
||||
// Return validation errors or other error types from external API
|
||||
if (error instanceof AxiosError && error.response?.data) {
|
||||
this.logger.error(`External API error response:`, error.response.data);
|
||||
return error.response.data;
|
||||
}
|
||||
|
||||
this.logger.error(`Error fetching restaurants: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
throw error;
|
||||
}
|
||||
@@ -131,6 +137,12 @@ export class RestaurantService {
|
||||
);
|
||||
return data;
|
||||
} catch (error: unknown) {
|
||||
// Return validation errors or other error types from external API
|
||||
if (error instanceof AxiosError && error.response?.data) {
|
||||
this.logger.error(`External API error response:`, error.response.data);
|
||||
return error.response.data;
|
||||
}
|
||||
|
||||
this.logger.error(`Error getting restaurant subscription: ${error instanceof Error ? error.message : "Unknown error"}`);
|
||||
throw error;
|
||||
}
|
||||
@@ -146,13 +158,19 @@ export class RestaurantService {
|
||||
.pipe(
|
||||
catchError((err: AxiosError) => {
|
||||
this.logger.error("error in getting SMS count by restaurant", err);
|
||||
throw new BadRequestException("Failed to fetch SMS count data");
|
||||
return throwError(() => err);
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
} catch (error: unknown) {
|
||||
// Return validation errors or other error types from external API
|
||||
if (error instanceof AxiosError && error.response?.data) {
|
||||
this.logger.error(`External API error response:`, error.response.data);
|
||||
return error.response.data;
|
||||
}
|
||||
|
||||
this.logger.error("error in getSmsCountByRestaurant", error);
|
||||
throw error;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user