order
This commit is contained in:
@@ -11,7 +11,7 @@ import {
|
||||
import { AdminId } from 'src/common/decorators/admin-id.decorator';
|
||||
import { AssignDesignerDto } from '../dto/assign-designer.dto';
|
||||
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||
import { UpdateOrderDtoAsAdmin } from '../dto/update-order.dto';
|
||||
import { UpdateOrderAsAdminDto } from '../dto/update-order.dto';
|
||||
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
|
||||
|
||||
@ApiTags('orders')
|
||||
@@ -107,14 +107,14 @@ export class OrderController {
|
||||
@Patch('admin/orders/:orderId')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiOperation({ summary: 'Update Order ' })
|
||||
updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) {
|
||||
updateOrder(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) {
|
||||
return this.orderService.updateOrderAsAdmin(orderId, dto);
|
||||
}
|
||||
|
||||
@Patch('admin/orders/:orderId/print-form')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiOperation({ summary: 'Update Order ' })
|
||||
updateOrderForPrint(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) {
|
||||
updateOrderForPrint(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) {
|
||||
return this.orderService.updateOrderAsAdmin(orderId, dto);
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ export class OrderController {
|
||||
@Post('admin/orders/:orderId/invoice')
|
||||
@UseGuards(AdminAuthGuard)
|
||||
@ApiOperation({ summary: 'Create Order invoice ' })
|
||||
createInvoice(@Param('orderId') orderId: string, @Body() dto: UpdateOrderDtoAsAdmin) {
|
||||
createInvoice(@Param('orderId') orderId: string, @Body() dto: UpdateOrderAsAdminDto) {
|
||||
return this.orderService.createInvoice(orderId);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { OmitType, PartialType } from '@nestjs/swagger';
|
||||
import { CreateOrderAsAdminDto } from './create-order.dto';
|
||||
|
||||
export class UpdateOrderDtoAsAdmin extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) { }
|
||||
export class UpdateOrderAsAdminDto extends PartialType(OmitType(CreateOrderAsAdminDto, 'items' as any)) { }
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user