create order as admn
This commit is contained in:
@@ -19,6 +19,9 @@ import { TicketService } from 'src/modules/ticket/providers/tickets.service';
|
||||
import { TicketRepository } from 'src/modules/ticket/repositories/tickets.repository';
|
||||
import { TicketStatus } from 'src/modules/ticket/enums/ticket-status.enum';
|
||||
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()
|
||||
@@ -36,6 +39,7 @@ export class OrderService {
|
||||
private readonly ticketRepository: TicketRepository,
|
||||
private readonly ticketService: TicketService,
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
private readonly adminRepository: AdminRepository,
|
||||
) { }
|
||||
|
||||
async createOrder(userId: string, dto: CreateOrderDto) {
|
||||
@@ -46,6 +50,7 @@ export class OrderService {
|
||||
if (!user) {
|
||||
throw new BadRequestException("user not found")
|
||||
}
|
||||
|
||||
const order = this.orderRepository.create({
|
||||
user,
|
||||
discount: 0,
|
||||
@@ -100,6 +105,88 @@ export class OrderService {
|
||||
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) {
|
||||
const orders = await this.orderRepository.findAllPaginated({ userId, ...dto })
|
||||
return orders
|
||||
|
||||
Reference in New Issue
Block a user