This commit is contained in:
2025-12-03 10:28:21 +03:30
parent afbe480db6
commit 46d1f96115
2 changed files with 43 additions and 29 deletions
+25 -9
View File
@@ -13,6 +13,8 @@ import { Cart } from '../cart/interfaces/cart.interface';
import { PaymentMethod } from '../payments/entities/payment-method.entity';
// import { PaymentGatewayService } from '../payments/services/payment-gateway.service.tss';
import { PaymentsService } from '../payments/services/payments.service';
import { DeliveryMethodEnum } from '../delivery/interface/delivery';
import { Delivery } from '../delivery/entities/delivery.entity';
@Injectable()
export class OrdersService {
@@ -105,7 +107,7 @@ export class OrdersService {
): Promise<{
user: User;
restaurant: Restaurant;
address: UserAddress;
address: UserAddress | null;
paymentMethod: PaymentMethod;
orderItemsData: Array<{ food: Food; quantity: number; unitPrice: number; discount: number }>;
}> {
@@ -115,8 +117,8 @@ export class OrdersService {
}
// Validate address is set
if (!cart.addressId) {
throw new BadRequestException('Address is required. Please set a delivery address before creating an order.');
if (!cart.deliveryMethodId) {
throw new NotFoundException('Delivery method not found');
}
// Validate payment method is set
@@ -137,14 +139,28 @@ export class OrdersService {
throw new NotFoundException('Restaurant not found');
}
const address = await this.em.findOne(UserAddress, { id: cart.addressId }, { populate: ['user'] });
if (!address) {
throw new NotFoundException('Address not found');
const delivery = await this.em.findOne(Delivery, { id: cart.deliveryMethodId });
if (!delivery) {
throw new NotFoundException('Delivery not found');
}
// Verify address belongs to the user
if (address.user.id !== userId) {
throw new BadRequestException('Address does not belong to the current user');
if (delivery.method === DeliveryMethodEnum.DeliveryCourier && !cart.addressId) {
throw new BadRequestException('Address is required. Please set a delivery address before creating an order.');
}
let address: UserAddress | null = null;
if (cart.addressId) {
address = await this.em.findOne(UserAddress, { id: cart.addressId }, { populate: ['user'] });
if (!address) {
throw new NotFoundException('Address not found');
}
// Verify address belongs to the user
if (address.user.id !== userId) {
throw new BadRequestException('Address does not belong to the current user');
}
// Verify address belongs to the user
if (address.user.id !== userId) {
throw new BadRequestException('Address does not belong to the current user');
}
}
const paymentMethod = await this.em.findOne(