This commit is contained in:
2026-01-31 09:18:04 +03:30
parent 5e5eeec5be
commit f3ab8163f2
4 changed files with 44 additions and 38 deletions
+21 -21
View File
@@ -31,7 +31,7 @@ import { Admin } from 'src/modules/admin/entities/admin.entity';
import { Product } from 'src/modules/product/entities/product.entity';
import { AdminService } from 'src/modules/admin/providers/admin.service';
import { OrderItem } from '../entities/order-item.entity';
import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto';
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
@Injectable()
@@ -70,7 +70,7 @@ export class OrderService {
}
async updateOrderAsAdmin(orderId: string, dto: UpdateOrderDtoAsAdmin) {
async updateOrderAsAdmin(orderId: string, dto: UpdateOrderAsAdminDto) {
const order = await this.findOrderOrFail(orderId)
const updateOrder = await this.updateOrder(order, dto)
@@ -129,7 +129,7 @@ export class OrderService {
async updateOrderItemAsUser(userId: string, orderId: string, itemId: number, dto: UpdateOrderItemDtoAsUser) {
const orderItem = await this.findOrderItemOrFail(orderId,itemId)
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
if (orderItem.order.user.id !== userId) {
throw new BadRequestException(`This order doesnt belongs to you`)
@@ -139,6 +139,10 @@ export class OrderService {
throw new BadRequestException(`You can not update when status is ${orderItem.order.status}`)
}
if (orderItem.confirmedAt) {
throw new BadRequestException(`You can not update when item is confirmed`)
}
const updated = await this.updateOrderItem(orderItem, dto)
return updated
@@ -159,32 +163,36 @@ export class OrderService {
async removeOrderItemAsAdmin(orderId: string, itemId: number) {
const order = await this.findOrderOrFail(orderId)
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
await this.removeOrderItem(itemId)
await this.removeOrderItem(orderItem)
// await this.calculateOrder(undefined, orderId)
// await this.em.flush()
return true
return { message: 'success' }
}
async removeOrderItemAsUser(userId: string, orderId: string, itemId: number) {
const order = await this.findOrderOrFail(orderId)
const orderItem = await this.findOrderItemOrFail(orderId, itemId)
if (order.user.id !== userId) {
if (orderItem.order.user.id !== userId) {
throw new BadRequestException(`this order is not belongs to you`)
}
if (order.status !== OrderStatusEnum.CREATED) {
throw new BadRequestException(`You can not update when status is ${order.status}`)
if (orderItem.order.status !== OrderStatusEnum.CREATED) {
throw new BadRequestException(`You can not delete when order status is ${orderItem.order.status}`)
}
await this.removeOrderItem(itemId)
if (orderItem.confirmedAt) {
throw new BadRequestException(`You can not delete when item is confirmed`)
}
return true
await this.removeOrderItem(orderItem)
return { message: 'success' }
}
@@ -480,15 +488,7 @@ export class OrderService {
return orderItem
}
async removeOrderItem(itemId: number) {
const orderItem = await this.orderItemRepository.findOne({
id: itemId
})
if (!orderItem) {
throw new BadRequestException(`orderItem not found`)
}
async removeOrderItem(orderItem: OrderItem) {
await this.em.removeAndFlush(orderItem)