cart
This commit is contained in:
@@ -6,12 +6,11 @@ import { User } from '../users/entities/user.entity';
|
||||
import { Restaurant } from '../restaurants/entities/restaurant.entity';
|
||||
import { Food } from '../foods/entities/food.entity';
|
||||
import { UserAddress } from '../users/entities/user-address.entity';
|
||||
import { RestaurantPaymentMethod } from '../payments/entities/restaurant-payment-method.entity';
|
||||
import { CartService } from '../cart/providers/cart.service';
|
||||
import { OrderStatus } from './interface/order-status';
|
||||
import { PaymentStatusEnum } from '../payments/interface/payment';
|
||||
import { Cart } from '../cart/interfaces/cart.interface';
|
||||
import { RestaurantPaymentMethodRepository } from '../payments/repositories/restaurant-payment-method.repository';
|
||||
import { PaymentMethod } from '../payments/entities/payment-method.entity';
|
||||
// import { PaymentGatewayService } from '../payments/services/payment-gateway.service.tss';
|
||||
import { PaymentsService } from '../payments/services/payments.service';
|
||||
|
||||
@@ -21,14 +20,13 @@ export class OrdersService {
|
||||
private readonly em: EntityManager,
|
||||
private readonly cartService: CartService,
|
||||
private readonly paymentsService: PaymentsService,
|
||||
private readonly RestaurantPaymentMethodRepository: RestaurantPaymentMethodRepository,
|
||||
// private readonly paymentGatewayService: PaymentGatewayService,
|
||||
) {}
|
||||
|
||||
async checkout(userId: string, restaurantId: string) {
|
||||
const cart = await this.cartService.findOne(userId, restaurantId);
|
||||
|
||||
const { order } = await this.create(userId, restaurantId, cart);
|
||||
const { order } = await this.createOrder(userId, restaurantId, cart);
|
||||
|
||||
await this.cartService.clearCart(userId, restaurantId);
|
||||
|
||||
@@ -40,7 +38,7 @@ export class OrdersService {
|
||||
return { order, paymentUrl };
|
||||
}
|
||||
|
||||
async create(userId: string, restaurantId: string, cart: Cart) {
|
||||
async createOrder(userId: string, restaurantId: string, cart: Cart) {
|
||||
const validationResult = await this.validateCartForOrder(userId, restaurantId, cart);
|
||||
// Create order within a transaction
|
||||
return this.em.transactional(async em => {
|
||||
@@ -108,7 +106,7 @@ export class OrdersService {
|
||||
user: User;
|
||||
restaurant: Restaurant;
|
||||
address: UserAddress;
|
||||
paymentMethod: RestaurantPaymentMethod;
|
||||
paymentMethod: PaymentMethod;
|
||||
orderItemsData: Array<{ food: Food; quantity: number; unitPrice: number; discount: number }>;
|
||||
}> {
|
||||
// Validate cart has items
|
||||
@@ -150,12 +148,12 @@ export class OrdersService {
|
||||
}
|
||||
|
||||
const paymentMethod = await this.em.findOne(
|
||||
RestaurantPaymentMethod,
|
||||
PaymentMethod,
|
||||
{
|
||||
id: cart.paymentMethodId,
|
||||
restaurant: { id: restaurantId },
|
||||
paymentMethod: { id: cart.paymentMethodId },
|
||||
},
|
||||
{ populate: ['restaurant', 'paymentMethod'] },
|
||||
{ populate: ['restaurant'] },
|
||||
);
|
||||
|
||||
if (!paymentMethod) {
|
||||
@@ -164,8 +162,8 @@ export class OrdersService {
|
||||
);
|
||||
}
|
||||
|
||||
if (!paymentMethod.isActive) {
|
||||
throw new BadRequestException('Payment method is not active for this restaurant');
|
||||
if (!paymentMethod.enabled) {
|
||||
throw new BadRequestException('Payment method is not enabled for this restaurant');
|
||||
}
|
||||
|
||||
// Validate stock and prepare order items data
|
||||
|
||||
Reference in New Issue
Block a user