create order as admn
This commit is contained in:
@@ -11,6 +11,8 @@ import { Permissions } from 'src/common/decorators/permissions.decorator';
|
|||||||
import { Permission } from 'src/common/enums/permission.enum';
|
import { Permission } from 'src/common/enums/permission.enum';
|
||||||
import { CreateOrderDto } from '../dto/create-order.dto';
|
import { CreateOrderDto } from '../dto/create-order.dto';
|
||||||
import { CreateInvoiceDto } from '../dto/create-invoice.dto';
|
import { CreateInvoiceDto } from '../dto/create-invoice.dto';
|
||||||
|
import { AdminId } from 'src/common/decorators/admin-id.decorator';
|
||||||
|
import { CreateOrderAsAdminDto } from '../dto/create-order-as-admin.dto';
|
||||||
|
|
||||||
@ApiTags('orders')
|
@ApiTags('orders')
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@@ -18,11 +20,10 @@ import { CreateInvoiceDto } from '../dto/create-invoice.dto';
|
|||||||
export class OrderController {
|
export class OrderController {
|
||||||
constructor(private readonly orderService: OrderService) { }
|
constructor(private readonly orderService: OrderService) { }
|
||||||
|
|
||||||
@Post('public/checkout')
|
@Post('public/order')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: 'Checkout : create order ' })
|
@ApiOperation({ summary: 'Checkout : create order ' })
|
||||||
@ApiBody({ type: CreateOrderDto })
|
createOrder(@UserId() userId: string, @Body() body: CreateOrderDto) {
|
||||||
checkout(@UserId() userId: string, @Body() body: CreateOrderDto) {
|
|
||||||
return this.orderService.createOrder(userId, body);
|
return this.orderService.createOrder(userId, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,9 +48,15 @@ export class OrderController {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*========================== Admin Routes =====================*/
|
/*========================== Admin Routes =====================*/
|
||||||
|
|
||||||
|
@Post('admin/order')
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@ApiOperation({ summary: 'create order ' })
|
||||||
|
createOrderAsAdmin(@AdminId() adminId: string, @Body() body: CreateOrderAsAdminDto) {
|
||||||
|
return this.orderService.createOrdeAsAdmin(adminId, body);
|
||||||
|
}
|
||||||
|
|
||||||
@Post('admin/orders/:orderId/create-invoice')
|
@Post('admin/orders/:orderId/create-invoice')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: 'Create invoice for new order' })
|
@ApiOperation({ summary: 'Create invoice for new order' })
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import {
|
||||||
|
IsString, IsOptional, IsBoolean,
|
||||||
|
IsInt, Min, IsArray, IsNumber,
|
||||||
|
IsNotEmpty,
|
||||||
|
ArrayMinSize,
|
||||||
|
IsMobilePhone
|
||||||
|
} from 'class-validator';
|
||||||
|
import { ApiPropertyOptional, ApiProperty } from '@nestjs/swagger';
|
||||||
|
import { Type } from 'class-transformer';
|
||||||
|
|
||||||
|
export class CreateOrderItemDto {
|
||||||
|
|
||||||
|
@IsInt()
|
||||||
|
@ApiProperty()
|
||||||
|
productId: bigint;
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsNumber()
|
||||||
|
quantity: number
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsArray()
|
||||||
|
attributesValues: string[]
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsNumber()
|
||||||
|
unitPrice: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CreateOrderAsAdminDto {
|
||||||
|
@ApiPropertyOptional({ example: 'توضیحات' })
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsMobilePhone('fa-IR')
|
||||||
|
userPhone: string
|
||||||
|
|
||||||
|
@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[]
|
||||||
|
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsNumber()
|
||||||
|
discount: number
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,6 +8,8 @@ import {
|
|||||||
Cascade,
|
Cascade,
|
||||||
Enum,
|
Enum,
|
||||||
PrimaryKey,
|
PrimaryKey,
|
||||||
|
BeforeCreate,
|
||||||
|
type EventArgs,
|
||||||
} from '@mikro-orm/core';
|
} from '@mikro-orm/core';
|
||||||
import { BaseEntity } from '../../../common/entities/base.entity';
|
import { BaseEntity } from '../../../common/entities/base.entity';
|
||||||
import { OrderStatusEnum } from '../interface/order.interface';
|
import { OrderStatusEnum } from '../interface/order.interface';
|
||||||
@@ -15,6 +17,7 @@ import { User } from '../../user/entities/user.entity';
|
|||||||
import { OrderItem } from './order-item.entity';
|
import { OrderItem } from './order-item.entity';
|
||||||
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
import { Payment } from 'src/modules/payment/entities/payment.entity';
|
||||||
import { ulid } from 'ulid';
|
import { ulid } from 'ulid';
|
||||||
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
|
|
||||||
@Entity({ tableName: 'orders' })
|
@Entity({ tableName: 'orders' })
|
||||||
@Index({ properties: ['user', 'status'] })
|
@Index({ properties: ['user', 'status'] })
|
||||||
@@ -38,11 +41,14 @@ export class Order extends BaseEntity {
|
|||||||
})
|
})
|
||||||
items = new Collection<OrderItem>(this);
|
items = new Collection<OrderItem>(this);
|
||||||
|
|
||||||
|
@ManyToOne(() => Admin, { nullable: true })
|
||||||
|
creator?: Admin
|
||||||
|
|
||||||
// @Property({ nullable: true })
|
// @Property({ nullable: true })
|
||||||
// userAddress?: string | null;
|
// userAddress?: string | null;
|
||||||
|
|
||||||
@Property({ type: 'bigint', autoincrement: true })
|
@Property({ type: 'int', nullable: true })
|
||||||
orderNumber: number;
|
orderNumber?: number;
|
||||||
|
|
||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||||
@@ -79,4 +85,23 @@ export class Order extends BaseEntity {
|
|||||||
@Enum(() => OrderStatusEnum)
|
@Enum(() => OrderStatusEnum)
|
||||||
status!: OrderStatusEnum;
|
status!: OrderStatusEnum;
|
||||||
|
|
||||||
|
@BeforeCreate()
|
||||||
|
async generateOrderNumber(args: EventArgs<Order>) {
|
||||||
|
const em = args.em;
|
||||||
|
const order = args.entity;
|
||||||
|
|
||||||
|
|
||||||
|
const maxOrder = await em.findOne(
|
||||||
|
Order,
|
||||||
|
{},
|
||||||
|
{
|
||||||
|
orderBy: { orderNumber: 'DESC' },
|
||||||
|
fields: ['orderNumber'],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set the next order number (1 if no orders exist, otherwise max + 1)
|
||||||
|
order.orderNumber = maxOrder?.orderNumber ? maxOrder.orderNumber + 1 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ import { TicketService } from 'src/modules/ticket/providers/tickets.service';
|
|||||||
import { TicketRepository } from 'src/modules/ticket/repositories/tickets.repository';
|
import { TicketRepository } from 'src/modules/ticket/repositories/tickets.repository';
|
||||||
import { TicketStatus } from 'src/modules/ticket/enums/ticket-status.enum';
|
import { TicketStatus } from 'src/modules/ticket/enums/ticket-status.enum';
|
||||||
import { CreateInvoiceDto } from '../dto/create-invoice.dto';
|
import { CreateInvoiceDto } from '../dto/create-invoice.dto';
|
||||||
|
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
|
||||||
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
|
import { CreateOrderAsAdminDto } from '../dto/create-order-as-admin.dto';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -36,6 +39,7 @@ export class OrderService {
|
|||||||
private readonly ticketRepository: TicketRepository,
|
private readonly ticketRepository: TicketRepository,
|
||||||
private readonly ticketService: TicketService,
|
private readonly ticketService: TicketService,
|
||||||
private readonly eventEmitter: EventEmitter2,
|
private readonly eventEmitter: EventEmitter2,
|
||||||
|
private readonly adminRepository: AdminRepository,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async createOrder(userId: string, dto: CreateOrderDto) {
|
async createOrder(userId: string, dto: CreateOrderDto) {
|
||||||
@@ -46,6 +50,7 @@ export class OrderService {
|
|||||||
if (!user) {
|
if (!user) {
|
||||||
throw new BadRequestException("user not found")
|
throw new BadRequestException("user not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
const order = this.orderRepository.create({
|
const order = this.orderRepository.create({
|
||||||
user,
|
user,
|
||||||
discount: 0,
|
discount: 0,
|
||||||
@@ -100,6 +105,88 @@ export class OrderService {
|
|||||||
return order
|
return order
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async createOrdeAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||||
|
const { attachments, content, items, discount, userPhone } = dto
|
||||||
|
|
||||||
|
const user = await this.userService.findByPhone(userPhone)
|
||||||
|
if (!user) {
|
||||||
|
throw new BadRequestException("User with this phone not found!")
|
||||||
|
}
|
||||||
|
|
||||||
|
const admin = await this.adminRepository.findOne({ id: adminId })
|
||||||
|
if (!admin) {
|
||||||
|
throw new BadRequestException("Admin not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
paidAmount: 0,
|
||||||
|
balance:total,
|
||||||
|
status: OrderStatusEnum.NEW
|
||||||
|
})
|
||||||
|
|
||||||
|
em.persist(order)
|
||||||
|
|
||||||
|
const productIds = items.map(item => item.productId)
|
||||||
|
const products = await this.productRepository.find({
|
||||||
|
id: { $in: productIds }
|
||||||
|
})
|
||||||
|
if (productIds.length !== products.length) {
|
||||||
|
throw new BadRequestException("some products not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
items.forEach(item => {
|
||||||
|
const product = products.find(p => p.id == item.productId)
|
||||||
|
|
||||||
|
if (!product) {
|
||||||
|
throw new BadRequestException(`product ${product} not found`)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const orderItem = this.orderItemRepository.create({
|
||||||
|
order,
|
||||||
|
attributesValues: item.attributesValues,
|
||||||
|
product,
|
||||||
|
quantity: item.quantity,
|
||||||
|
status: OrderItemStatus.CONFIRMED,
|
||||||
|
unitPrice: item.unitPrice,
|
||||||
|
totalPrice: item.unitPrice * item.quantity,
|
||||||
|
})
|
||||||
|
|
||||||
|
em.persist(orderItem)
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
const ticket = this.ticketRepository.create({
|
||||||
|
content,
|
||||||
|
user,
|
||||||
|
attachments,
|
||||||
|
status: TicketStatus.PENDING,
|
||||||
|
admin: null
|
||||||
|
})
|
||||||
|
|
||||||
|
em.persist(ticket)
|
||||||
|
|
||||||
|
await em.flush()
|
||||||
|
|
||||||
|
return order
|
||||||
|
})
|
||||||
|
|
||||||
|
return order
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
async findAllForUser(userId: string, dto: FindOrdersDto) {
|
async findAllForUser(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