cart
This commit is contained in:
@@ -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],
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user