This commit is contained in:
2025-12-03 10:49:27 +03:30
parent 46d1f96115
commit ae9aa72c44
2 changed files with 17 additions and 8 deletions
@@ -7,6 +7,7 @@ import { Restaurant } from '../../restaurants/entities/restaurant.entity';
import { UserAddress } from '../../users/entities/user-address.entity';
import { PaymentMethod } from '../../payments/entities/payment-method.entity';
import { OrderItem } from './order-item.entity';
import { Delivery } from '../../delivery/entities/delivery.entity';
@Entity({ tableName: 'orders' })
export class Order extends BaseEntity {
@@ -16,6 +17,9 @@ export class Order extends BaseEntity {
@ManyToOne(() => Restaurant)
restaurant!: Restaurant;
@ManyToOne(() => Delivery)
deliveryMethod!: Delivery;
@OneToMany(() => OrderItem, item => item.order, {
cascade: [Cascade.ALL],
orphanRemoval: true,
+13 -8
View File
@@ -30,13 +30,14 @@ export class OrdersService {
const { order } = await this.createOrder(userId, restaurantId, cart);
await this.cartService.clearCart(userId, restaurantId);
if (!order.paymentMethod) {
throw new BadRequestException('Payment method is required for checkout');
}
const { paymentUrl } = await this.paymentsService.initializePayment(order.paymentMethod.id, order.total, order.id);
await this.cartService.clearCart(userId, restaurantId);
return { order, paymentUrl };
}
@@ -48,6 +49,7 @@ export class OrdersService {
const order = em.create(Order, {
user: validationResult.user,
restaurant: validationResult.restaurant,
deliveryMethod: validationResult.delivery,
address: validationResult.address,
paymentMethod: validationResult.paymentMethod,
couponDiscount: cart.couponDiscount || 0,
@@ -107,6 +109,7 @@ export class OrdersService {
): Promise<{
user: User;
restaurant: Restaurant;
delivery: Delivery;
address: UserAddress | null;
paymentMethod: PaymentMethod;
orderItemsData: Array<{ food: Food; quantity: number; unitPrice: number; discount: number }>;
@@ -139,7 +142,8 @@ export class OrdersService {
throw new NotFoundException('Restaurant not found');
}
const delivery = await this.em.findOne(Delivery, { id: cart.deliveryMethodId });
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const delivery = await this.em.findOne(Delivery, { id: cart.deliveryMethodId! });
if (!delivery) {
throw new NotFoundException('Delivery not found');
}
@@ -157,10 +161,6 @@ export class OrdersService {
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(
@@ -217,6 +217,7 @@ export class OrdersService {
return {
user,
restaurant,
delivery,
address,
paymentMethod,
orderItemsData,
@@ -224,7 +225,11 @@ export class OrdersService {
}
async findAll() {
const orders = await this.em.find(Order, {}, { populate: ['user', 'restaurant', 'address', 'paymentMethod'] });
const orders = await this.em.find(
Order,
{},
{ populate: ['user', 'restaurant', 'deliveryMethod', 'address', 'paymentMethod'] },
);
if (!orders) {
throw new NotFoundException('No orders found');
}