creat order bug
This commit is contained in:
@@ -18,7 +18,7 @@ export class OrdersController {
|
||||
constructor(private readonly ordersService: OrdersService) { }
|
||||
|
||||
@Post('public/checkout')
|
||||
// @UseGuards(AuthGuard)
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'Checkout : create order ' })
|
||||
@ApiBody({ type: CreateOrderDto })
|
||||
checkout(@UserId() userId: string, @Body() body: CreateOrderDto) {
|
||||
|
||||
@@ -1,20 +1,12 @@
|
||||
import { IsString, IsOptional, IsBoolean, IsInt, Min, IsArray, IsNumber } from 'class-validator';
|
||||
import {
|
||||
IsString, IsOptional, IsBoolean,
|
||||
IsInt, Min, IsArray, IsNumber,
|
||||
IsNotEmpty,
|
||||
ArrayMinSize
|
||||
} from 'class-validator';
|
||||
import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
|
||||
export class CreateOrderDto {
|
||||
@IsArray()
|
||||
@ApiProperty({ isArray: true })
|
||||
items: CreateOrderItemDto[];
|
||||
|
||||
@IsString()
|
||||
content: string
|
||||
|
||||
@IsArray()
|
||||
@IsString({ each: true })
|
||||
attachments: string[]
|
||||
}
|
||||
|
||||
export class CreateOrderItemDto {
|
||||
|
||||
@IsInt()
|
||||
@@ -22,8 +14,37 @@ export class CreateOrderItemDto {
|
||||
productId: bigint;
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
quantity: number
|
||||
|
||||
@ApiProperty()
|
||||
@IsArray()
|
||||
attributesValues: string[]
|
||||
}
|
||||
}
|
||||
|
||||
export class CreateOrderDto {
|
||||
@IsArray()
|
||||
@ApiProperty({
|
||||
isArray: true, type: [CreateOrderItemDto], example: [
|
||||
{
|
||||
productId: 1,
|
||||
quantity: 100,
|
||||
attributesValues: []
|
||||
}
|
||||
]
|
||||
})
|
||||
@IsNotEmpty()
|
||||
@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[]
|
||||
}
|
||||
|
||||
|
||||
@@ -4,11 +4,8 @@ import { OrdersService } from './providers/orders.service';
|
||||
import { OrdersController } from './controllers/orders.controller';
|
||||
import { Order } from './entities/order.entity';
|
||||
import { OrderItem } from './entities/order-item.entity';
|
||||
import { User } from '../user/entities/user.entity';
|
||||
import { Product } from '../product/entities/product.entity';
|
||||
import { UserAddress } from '../user/entities/user-address.entity';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
import { PaymentsModule } from '../payment/payments.module';
|
||||
import { PaymentModule } from '../payment/payments.module';
|
||||
import { JwtModule } from '@nestjs/jwt';
|
||||
import { OrderRepository } from './repositories/order.repository';
|
||||
import { OrderListeners } from './listeners/order.listeners';
|
||||
@@ -25,19 +22,18 @@ import { OrderItemRepository } from './repositories/order-item.repository';
|
||||
imports: [
|
||||
MikroOrmModule.forFeature([Order, OrderItem]),
|
||||
AuthModule,
|
||||
forwardRef(() => PaymentsModule),
|
||||
forwardRef(() => PaymentModule),
|
||||
JwtModule,
|
||||
AdminModule,
|
||||
NotificationsModule,
|
||||
UtilsModule,
|
||||
forwardRef(() => UserModule),
|
||||
// PaymentsModule,
|
||||
productModule,
|
||||
TicketModule
|
||||
],
|
||||
controllers: [OrdersController],
|
||||
providers: [OrdersService, OrderRepository, OrderListeners,
|
||||
OrdersCrone,OrderItemRepository],
|
||||
providers: [OrdersService, OrderRepository, OrderListeners,
|
||||
OrdersCrone, OrderItemRepository],
|
||||
exports: [OrderRepository, OrdersService],
|
||||
})
|
||||
export class OrdersModule { }
|
||||
export class OrderModule { }
|
||||
|
||||
@@ -48,7 +48,8 @@ export class OrdersService {
|
||||
|
||||
async createOrder(userId: string, dto: CreateOrderDto) {
|
||||
const { attachments, content, items } = dto
|
||||
this.em.transactional(async (em) => {
|
||||
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
const user = await this.userService.findById(userId)
|
||||
if (!user) {
|
||||
throw new BadRequestException("user not found")
|
||||
@@ -95,14 +96,17 @@ export class OrdersService {
|
||||
content,
|
||||
user,
|
||||
attachments,
|
||||
status: TicketStatus.PENDING
|
||||
status: TicketStatus.PENDING,
|
||||
admin: null
|
||||
})
|
||||
em.persist(ticket)
|
||||
|
||||
em.flush()
|
||||
await em.flush()
|
||||
|
||||
return order
|
||||
})
|
||||
|
||||
return order
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user