order kitem entity update
This commit is contained in:
@@ -23,14 +23,14 @@ export class OrderController {
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'create order as user' })
|
||||
createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) {
|
||||
return this.orderService.createOrder(userId, body);
|
||||
return this.orderService.createNewOrder(userId, body);
|
||||
}
|
||||
|
||||
@Get('public/orders')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'Get all orders with pagination and filters' })
|
||||
async findAll(@Query() dto: FindOrdersDto, @UserId() userId: string) {
|
||||
const orders = await this.orderService.findAllForUser(userId, dto);
|
||||
const orders = await this.orderService.findUserOrders(userId, dto);
|
||||
return orders
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
IsString, IsOptional, IsBoolean,
|
||||
IsInt, Min, IsArray, IsNumber,
|
||||
IsString,
|
||||
IsInt, IsArray, IsNumber,
|
||||
IsNotEmpty,
|
||||
ArrayMinSize,
|
||||
IsMobilePhone
|
||||
@@ -25,6 +25,15 @@ export class CreateOrderItemDto {
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
unitPrice: number
|
||||
|
||||
@ApiPropertyOptional({ example: 'توضیحات' })
|
||||
@IsString()
|
||||
content: string
|
||||
|
||||
@ApiPropertyOptional({ example: [] })
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
attachments: { url: string, type: string }[]
|
||||
}
|
||||
|
||||
export class CreateOrderAsAdminDto {
|
||||
@@ -49,18 +58,5 @@ export class CreateOrderAsAdminDto {
|
||||
@Type(() => CreateOrderItemDto)
|
||||
items: CreateOrderItemDto[];
|
||||
|
||||
@ApiPropertyOptional({ example: 'توضیحات' })
|
||||
@IsString()
|
||||
content: string
|
||||
|
||||
@ApiPropertyOptional({ example: [] })
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
attachments: string[]
|
||||
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
discount: number
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,14 @@ export class CreateOrderItemDto {
|
||||
@ApiProperty()
|
||||
@IsArray()
|
||||
attributesValues: string[]
|
||||
|
||||
@ApiPropertyOptional({ example: 'توضیحات' })
|
||||
@IsString()
|
||||
description: string
|
||||
|
||||
@ApiPropertyOptional({ example: [] })
|
||||
@IsArray()
|
||||
attachments: { url: string, type: string }[]
|
||||
}
|
||||
|
||||
export class CreateOrderDto {
|
||||
@@ -37,14 +45,5 @@ export class CreateOrderDto {
|
||||
@ArrayMinSize(1, { message: 'At least one product is required' })
|
||||
@Type(() => CreateOrderItemDto)
|
||||
items: CreateOrderItemDto[];
|
||||
|
||||
@ApiPropertyOptional({ example: 'توضیحات' })
|
||||
@IsString()
|
||||
content: string
|
||||
|
||||
@ApiPropertyOptional({ example: [] })
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
attachments: string[]
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,12 @@ export class OrderItem extends BaseEntity {
|
||||
@Property({ nullable: true })
|
||||
description?: string;
|
||||
|
||||
@Property({ nullable: true })
|
||||
adminDescription?: string;
|
||||
|
||||
@Property({ type: 'json', nullable: true })
|
||||
attachments?: { type: string, url: string }[] // user attachments like voice and photos
|
||||
|
||||
// @Enum(() => OrderItemStatus)
|
||||
// status!: OrderItemStatus;
|
||||
@Property({ nullable: true, columnType: 'timestamptz' })
|
||||
|
||||
@@ -35,8 +35,8 @@ export class OrderService {
|
||||
private readonly adminRepository: AdminRepository,
|
||||
) { }
|
||||
|
||||
async createOrder(userId: string, dto: CreateOrderDto) {
|
||||
const { attachments, content, items } = dto
|
||||
async createNewOrder(userId: string, dto: CreateOrderDto) {
|
||||
const { items } = dto
|
||||
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
const user = await this.userService.findById(userId)
|
||||
@@ -48,7 +48,6 @@ export class OrderService {
|
||||
user,
|
||||
discount: 0,
|
||||
subTotal: 0,
|
||||
orderNumber: 1,
|
||||
total: 0,
|
||||
paidAmount: 0,
|
||||
balance: 0,
|
||||
@@ -79,19 +78,13 @@ export class OrderService {
|
||||
unitPrice: 0,
|
||||
total: 0,
|
||||
subTotal: 0,
|
||||
discount: 0
|
||||
discount: 0,
|
||||
attachments: item.attachments,
|
||||
description: item.description
|
||||
})
|
||||
em.persist(orderItem)
|
||||
});
|
||||
|
||||
const ticket = this.ticketRepository.create({
|
||||
content,
|
||||
user,
|
||||
attachments,
|
||||
admin: null,
|
||||
order
|
||||
})
|
||||
em.persist(ticket)
|
||||
|
||||
await em.flush()
|
||||
|
||||
@@ -102,7 +95,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async createOrdeAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||
const { attachments, content, items, discount, userPhone } = dto
|
||||
const { items, userPhone } = dto
|
||||
|
||||
const user = await this.userService.findByPhone(userPhone)
|
||||
if (!user) {
|
||||
@@ -116,18 +109,14 @@ export class OrderService {
|
||||
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
|
||||
let subTotal = 0
|
||||
items.forEach(item => { subTotal += item.unitPrice * item.quantity })
|
||||
const total = subTotal - discount
|
||||
|
||||
const order = this.orderRepository.create({
|
||||
creator: admin,
|
||||
user,
|
||||
discount,
|
||||
subTotal,
|
||||
total,
|
||||
discount: 0,
|
||||
subTotal: 0,
|
||||
total: 0,
|
||||
paidAmount: 0,
|
||||
balance: total,
|
||||
balance: 0,
|
||||
status: OrderStatusEnum.INVOICED,
|
||||
taxAmount: 0
|
||||
})
|
||||
@@ -150,7 +139,6 @@ export class OrderService {
|
||||
throw new BadRequestException(`product ${product} not found`)
|
||||
}
|
||||
|
||||
|
||||
const orderItem = this.orderItemRepository.create({
|
||||
order,
|
||||
attributesValues: item.attributesValues,
|
||||
@@ -165,16 +153,7 @@ export class OrderService {
|
||||
em.persist(orderItem)
|
||||
});
|
||||
|
||||
|
||||
const ticket = this.ticketRepository.create({
|
||||
content,
|
||||
user,
|
||||
attachments,
|
||||
admin: null,
|
||||
order
|
||||
})
|
||||
|
||||
em.persist(ticket)
|
||||
// TODO : calculation must be done after create order
|
||||
|
||||
await em.flush()
|
||||
|
||||
@@ -185,7 +164,7 @@ export class OrderService {
|
||||
|
||||
}
|
||||
|
||||
async findAllForUser(userId: string, dto: FindOrdersDto) {
|
||||
async findUserOrders(userId: string, dto: FindOrdersDto) {
|
||||
const orders = await this.orderRepository.findAllPaginated({ userId, ...dto })
|
||||
return orders
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user