This commit is contained in:
2025-12-02 23:15:17 +03:30
parent 8298e2fb3e
commit c759a120e7
5 changed files with 36 additions and 47 deletions
+8 -1
View File
@@ -4,12 +4,19 @@ import { CartService } from './providers/cart.service';
import { CartController } from './controllers/cart.controller';
import { Food } from '../foods/entities/food.entity';
import { Restaurant } from '../restaurants/entities/restaurant.entity';
import { PaymentMethod } from '../payments/entities/payment-method.entity';
import { UserAddress } from '../users/entities/user-address.entity';
import { AuthModule } from '../auth/auth.module';
import { JwtModule } from '@nestjs/jwt';
import { UtilsModule } from '../utils/utils.module';
@Module({
imports: [MikroOrmModule.forFeature([Food, Restaurant]), AuthModule, JwtModule, UtilsModule],
imports: [
MikroOrmModule.forFeature([Food, Restaurant, PaymentMethod, UserAddress]),
AuthModule,
JwtModule,
UtilsModule,
],
controllers: [CartController],
providers: [CartService],
exports: [CartService],
+14 -30
View File
@@ -10,7 +10,7 @@ import { ApplyCouponDto } from '../dto/apply-coupon.dto';
import { SetAddressDto } from '../dto/set-address.dto';
import { SetPaymentMethodDto } from '../dto/set-payment-method.dto';
import { UserAddress } from '../../users/entities/user-address.entity';
import { RestaurantPaymentMethod } from '../../payments/entities/restaurant-payment-method.entity';
import { PaymentMethod } from '../../payments/entities/payment-method.entity';
import { Cart, CartItem, CartCoupon } from '../interfaces/cart.interface';
@Injectable()
@@ -423,35 +423,29 @@ export class CartService {
const cart = await this.findOne(userId, restaurantId);
// Find and validate payment method belongs to restaurant
const restaurantPaymentMethod = await this.em.findOne(
RestaurantPaymentMethod,
const paymentMethod = await this.em.findOne(
PaymentMethod,
{
id: setPaymentMethodDto.paymentMethodId,
restaurant: { id: restaurantId },
paymentMethod: { id: setPaymentMethodDto.paymentMethodId },
},
{ populate: ['restaurant', 'paymentMethod'] },
{ populate: ['restaurant'] },
);
if (!restaurantPaymentMethod) {
if (!paymentMethod) {
throw new NotFoundException(
`Payment method with ID ${setPaymentMethodDto.paymentMethodId} not found for restaurant ${restaurantId}`,
);
}
// Verify payment method is active
if (!restaurantPaymentMethod.isActive) {
throw new BadRequestException('Payment method is not active for this restaurant');
}
// Verify the payment method itself is active
if (!restaurantPaymentMethod.paymentMethod.isActive) {
throw new BadRequestException('Payment method is not active');
// Verify payment method is enabled
if (!paymentMethod.enabled) {
throw new BadRequestException('Payment method is not enabled for this restaurant');
}
cart.paymentMethodId = setPaymentMethodDto.paymentMethodId;
// cart.shipmentFee = Number(restaurantPaymentMethod.price) || 0;
// Recalculate cart totals to include delivery fee
// Recalculate cart totals
await this.recalculateCartTotals(cart);
await this.saveCart(cart);
@@ -505,22 +499,12 @@ export class CartService {
cart.tax = tax;
// Get shipment fee if payment method is set
let shipmentFee = 0;
// if (cart.paymentMethodId) {
// const restaurantPaymentMethod = await this.em.findOne(RestaurantPaymentMethod, {
// restaurant: { id: cart.restaurantId },
// paymentMethod: { id: cart.paymentMethodId },
// });
// if (restaurantPaymentMethod) {
// shipmentFee = Number(restaurantPaymentMethod.price) || 0;
// shipmentFee = 1;
// }
// }
cart.shipmentFee = shipmentFee;
// Shipment fee is calculated separately via delivery method
// For now, it's set to 0. It should be set when a delivery method is selected
cart.shipmentFee = 0;
// Calculate total: total = subtotal totalDiscount + tax + shipmentFee
cart.total = subTotal - cart.totalDiscount + tax + shipmentFee;
cart.total = subTotal - cart.totalDiscount + tax + cart.shipmentFee;
cart.totalItems = totalItems;
cart.updatedAt = new Date().toISOString();
}