order item
This commit is contained in:
@@ -6,7 +6,7 @@ import { FindOrdersDto } from '../dto/find-orders.dto';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { CreateOrderDto, CreateOrderItemAsUserDto, CreateOrderItemDtoAsAdmin } from '../dto/create-order.dto';
|
||||
import { UserService } from 'src/modules/user/providers/user.service';
|
||||
import { AddOrderItem, OrderStatusEnum } from '../interface/order.interface';
|
||||
import { AddOrderItem, OrderStatusEnum, UpdateOrderItem } from '../interface/order.interface';
|
||||
import { OrderItemRepository } from '../repositories/order-item.repository';
|
||||
import { ProductService } from 'src/modules/product/providers/product.service';
|
||||
import { ProductRepository } from 'src/modules/product/repositories/product.repository';
|
||||
@@ -16,7 +16,7 @@ import { AdminRepository } from 'src/modules/admin/repositories/admin.repository
|
||||
import { CreateOrderAsAdminDto } from '../dto/create-order-as-admin.dto';
|
||||
import { Order } from '../entities/order.entity';
|
||||
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
|
||||
import { UpdateOrderItemDto } from '../dto/update-order-item.dto';
|
||||
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||
import { UpdateOrderDto } from '../dto/update-order.dto';
|
||||
|
||||
|
||||
@@ -97,7 +97,7 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async addOrderItemAsUser(orderId: string, dto: CreateOrderItemAsUserDto) {
|
||||
async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
@@ -105,6 +105,10 @@ export class OrderService {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
if (order.user.id !== userId) {
|
||||
throw new BadRequestException(`This order doest belongs to you!`)
|
||||
}
|
||||
|
||||
const orderItem = this.persistOrderItem(order, dto)
|
||||
|
||||
await this.em.flush()
|
||||
@@ -157,19 +161,35 @@ export class OrderService {
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItemAsUser(orderId: string, itemId: string, dto: UpdateOrderItemDto) {
|
||||
const { attributesValues, description, productId, quantity, attachments } = dto
|
||||
async updateOrderItemAsUser(userId: string, orderId: string, itemId: string, dto: UpdateOrderItemDtoAsUser) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
if (order.user.id !== userId) {
|
||||
throw new BadRequestException(`This order doesnt belongs to you`)
|
||||
}
|
||||
|
||||
if (order.status !== OrderStatusEnum.CREATED) {
|
||||
throw new BadRequestException(`You can not update when status is ${order.status}`)
|
||||
}
|
||||
|
||||
const orderItem = await this.updateOrderItem(itemId, dto)
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItemAsAdmin(orderId: string, itemId: string, dto: UpdateOrderItemDtoAsAdmin) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
const orderItem = await this.updateOrderItem(itemId, dto)
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async updateOrderItem(itemId: string, dto: UpdateOrderItem) {
|
||||
const { attributesValues, description, productId, quantity, attachments, discount, unitPrice } = dto
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
id: itemId
|
||||
})
|
||||
@@ -202,23 +222,47 @@ export class OrderService {
|
||||
if (attachments) {
|
||||
orderItem.attachments = attachments
|
||||
}
|
||||
if (discount) {
|
||||
orderItem.discount = discount
|
||||
}
|
||||
|
||||
if (unitPrice) {
|
||||
orderItem.unitPrice = unitPrice
|
||||
}
|
||||
|
||||
await this.em.flush()
|
||||
|
||||
return orderItem
|
||||
}
|
||||
|
||||
async removeOrderItem(orderId: string, itemId: string) {
|
||||
async removeOrderItemAsAdmin(orderId: string, itemId: string) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (!order) {
|
||||
throw new BadRequestException(`Order not found`)
|
||||
await this.removeOrderItem(itemId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async removeOrderItemAsUser(userId: string, orderId: string, itemId: string) {
|
||||
|
||||
const order = await this.findOneOrFail(orderId)
|
||||
|
||||
if (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}`)
|
||||
}
|
||||
|
||||
await this.removeOrderItem(itemId)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
async removeOrderItem(itemId: string) {
|
||||
|
||||
const orderItem = await this.orderItemRepository.findOne({
|
||||
id: itemId
|
||||
})
|
||||
@@ -229,8 +273,7 @@ export class OrderService {
|
||||
|
||||
await this.em.removeAndFlush(orderItem)
|
||||
|
||||
|
||||
return orderItem
|
||||
return true
|
||||
}
|
||||
|
||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
|
||||
Reference in New Issue
Block a user