fix controller bugs
This commit is contained in:
@@ -116,7 +116,7 @@ export class OrderService {
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItemAsUser(userId: string, orderId: string, itemId: string, dto: UpdateOrderItemDtoAsUser) {
|
||||
async updateOrderItemAsUser(userId: string, orderId: string, itemId: number, dto: UpdateOrderItemDtoAsUser) {
|
||||
|
||||
const order = await this.findOrderOrFail(orderId)
|
||||
|
||||
@@ -133,9 +133,9 @@ export class OrderService {
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItemAsAdmin(orderId: string, itemId: string, dto: UpdateOrderItemDtoAsAdmin) {
|
||||
async updateOrderItemAsAdmin(orderId: string, itemId: number, dto: UpdateOrderItemDtoAsAdmin) {
|
||||
|
||||
await this.findOrderOrFail(orderId)
|
||||
await this.findOrderItemOrFail(orderId, itemId)
|
||||
|
||||
const orderItem = await this.updateOrderItem(itemId, dto)
|
||||
|
||||
@@ -333,7 +333,9 @@ export class OrderService {
|
||||
const admin = adminId ? await this.adminService.findOrFail(adminId) : undefined
|
||||
const designer = designerId ? await this.adminService.findOrFail(designerId) : undefined
|
||||
const products = await this.productService.findProductsByIds(items.map(item => item.productId))
|
||||
|
||||
const productMap = new Map(
|
||||
products.map(p => [Number(p.id), p])
|
||||
);
|
||||
|
||||
return this.em.transactional(async (em) => {
|
||||
|
||||
@@ -351,7 +353,7 @@ export class OrderService {
|
||||
em.persist(order)
|
||||
|
||||
for (const item of items) {
|
||||
const product = products.find(p => Number(p.id) === Number(item.productId))
|
||||
const product = productMap.get(Number(item.productId))
|
||||
|
||||
if (!product) {
|
||||
throw new BadRequestException("Product not found!")
|
||||
@@ -421,7 +423,7 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async updateOrderItem(itemId: string, dto: UpdateOrderItem) {
|
||||
async updateOrderItem(itemId: number, dto: UpdateOrderItem) {
|
||||
const { attributes, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
@@ -505,12 +507,20 @@ export class OrderService {
|
||||
|
||||
async findOrderOrFail(orderId: string) {
|
||||
const order = await this.orderRepository.findOne({ id: orderId },
|
||||
{ populate: ['items', 'items.product', 'payments'] })
|
||||
{ populate: ['items', 'items.product', 'payments', 'user'] })
|
||||
if (!order) {
|
||||
throw new BadRequestException('Order not found')
|
||||
}
|
||||
return order
|
||||
}
|
||||
|
||||
async findOrderItemOrFail(orderId: string, itemId: number) {
|
||||
const orderItem = await this.orderItemRepository.findOne({ id: itemId, order: { id: orderId } },
|
||||
{ populate: [] })
|
||||
if (!orderItem) {
|
||||
throw new BadRequestException('Order item not found')
|
||||
}
|
||||
return orderItem
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user