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