invoice
This commit is contained in:
@@ -20,14 +20,7 @@ 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.createNewOrder(userId, body);
|
return this.orderService.createOrderAsUser(userId, body);
|
||||||
}
|
|
||||||
|
|
||||||
@Patch('public/order/:id/item/:itemId')
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@ApiOperation({ summary: 'Update order item as user' })
|
|
||||||
updateOrder(@Param('id') orderId: string, @UserId() userId: string, @Param(':itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) {
|
|
||||||
return this.orderService.updateOrderItemAsUser(userId, orderId, itemId, body);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete('public/order/:id')
|
@Delete('public/order/:id')
|
||||||
@@ -37,14 +30,6 @@ export class OrderController {
|
|||||||
return this.orderService.removeOrderAsUser(orderId);
|
return this.orderService.removeOrderAsUser(orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('public/order/:id/item')
|
|
||||||
@UseGuards(AuthGuard)
|
|
||||||
@ApiOperation({ summary: 'add item to order as user' })
|
|
||||||
addOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Body() body: CreateOrderItemAsUserDto) {
|
|
||||||
return this.orderService.addOrderItemAsUser(userId, orderId, 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' })
|
||||||
@@ -53,7 +38,25 @@ export class OrderController {
|
|||||||
return orders
|
return orders
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post('public/orders/:orderId/items/:orderItemId')
|
|
||||||
|
@Patch('public/order/:id/item')
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@ApiOperation({ summary: 'add item to order as user' })
|
||||||
|
addOrderItem(@Param('id') orderId: string, @UserId() userId: string, @Body() body: CreateOrderItemAsUserDto) {
|
||||||
|
return this.orderService.addOrderItemAsUser(userId, orderId, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Patch('public/order/:id/item/:itemId')
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@ApiOperation({ summary: 'Update order item as user' })
|
||||||
|
updateOrder(@Param('id') orderId: string, @UserId() userId: string, @Param(':itemId') itemId: string, @Body() body: UpdateOrderItemDtoAsUser) {
|
||||||
|
return this.orderService.updateOrderItemAsUser(userId, orderId, itemId, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Post('public/orders/:orderId/items/:orderItemId/confirm')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: 'Confirm Invoice Item By User' })
|
@ApiOperation({ summary: 'Confirm Invoice Item By User' })
|
||||||
confirmOrderItem(
|
confirmOrderItem(
|
||||||
@@ -99,11 +102,11 @@ export class OrderController {
|
|||||||
return this.orderService.assignDesigner(orderId, body.designerId);
|
return this.orderService.assignDesigner(orderId, body.designerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch('admin/order/:id/item')
|
@Post('admin/orders/:orderId/invoice')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: 'add item to order By Admin' })
|
@ApiOperation({ summary: 'Create Order invoice ' })
|
||||||
addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) {
|
createInvoice(@Param('orderId') orderId: string) {
|
||||||
return this.orderService.addOrderItemAsAdmin(orderId, body);
|
return this.orderService.createInvoice(orderId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -117,6 +120,13 @@ export class OrderController {
|
|||||||
return this.orderService.removeOrderItemAsAdmin(orderId, orderItemId);
|
return this.orderService.removeOrderItemAsAdmin(orderId, orderItemId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Patch('admin/order/:id/item')
|
||||||
|
@UseGuards(AuthGuard)
|
||||||
|
@ApiOperation({ summary: 'add item to order By Admin' })
|
||||||
|
addOrderItemAsAdmin(@Param('id') orderId: string, @Body() body: CreateOrderItemDtoAsAdmin) {
|
||||||
|
return this.orderService.addOrderItemAsAdmin(orderId, body);
|
||||||
|
}
|
||||||
|
|
||||||
@Patch('admin/order/:id/item/:itemId')
|
@Patch('admin/order/:id/item/:itemId')
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
@ApiOperation({ summary: 'Update order item' })
|
@ApiOperation({ summary: 'Update order item' })
|
||||||
|
|||||||
@@ -1,91 +0,0 @@
|
|||||||
import {
|
|
||||||
IsArray, IsNumber,
|
|
||||||
IsNotEmpty,
|
|
||||||
ArrayMinSize,
|
|
||||||
IsBoolean,
|
|
||||||
IsString
|
|
||||||
} from 'class-validator';
|
|
||||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
|
||||||
import { Type } from 'class-transformer';
|
|
||||||
|
|
||||||
export class InvoiceOrderItemDto {
|
|
||||||
@IsNumber()
|
|
||||||
@ApiProperty()
|
|
||||||
orderItemId: number;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
unitPrice: number
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
discount: number
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
quantity: number
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export class InvoiceOrderNewItemDto {
|
|
||||||
@IsNumber()
|
|
||||||
@ApiProperty()
|
|
||||||
productId: number;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsArray()
|
|
||||||
attributesValues: string[]
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
quantity: number
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
unitPrice: number
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
discount: number
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export class UpdateInvoicedOrderDto {
|
|
||||||
@IsArray()
|
|
||||||
@ApiProperty({
|
|
||||||
isArray: true, type: [InvoiceOrderItemDto], example: [
|
|
||||||
{
|
|
||||||
orderItemId: 1,
|
|
||||||
unitPrice: 100,
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
|
||||||
@ArrayMinSize(1, { message: 'At least one product is required' })
|
|
||||||
@Type(() => InvoiceOrderItemDto)
|
|
||||||
items: InvoiceOrderItemDto[];
|
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@ApiProperty({
|
|
||||||
isArray: true, type: [InvoiceOrderNewItemDto], example: [
|
|
||||||
{
|
|
||||||
orderItemId: 1,
|
|
||||||
unitPrice: 100,
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
@Type(() => InvoiceOrderNewItemDto)
|
|
||||||
newItems: InvoiceOrderNewItemDto[];
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsBoolean()
|
|
||||||
enableTax: boolean
|
|
||||||
|
|
||||||
|
|
||||||
@ApiPropertyOptional({ example: [] })
|
|
||||||
@IsArray()
|
|
||||||
@IsString({ each: true })
|
|
||||||
attachments: string[]
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
+1
-33
@@ -27,30 +27,9 @@ export class ItemDto {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class NewItemDto {
|
|
||||||
@IsNumber()
|
|
||||||
@ApiProperty()
|
|
||||||
productId: number;
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsArray()
|
|
||||||
attributesValues: string[]
|
|
||||||
|
|
||||||
@ApiProperty()
|
export class UpdateOrderDtoAsUser {
|
||||||
@IsNumber()
|
|
||||||
quantity: number
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
unitPrice: number
|
|
||||||
|
|
||||||
@ApiProperty()
|
|
||||||
@IsNumber()
|
|
||||||
discount: number
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export class UpdateOrderDto {
|
|
||||||
@IsArray()
|
@IsArray()
|
||||||
@ApiProperty({
|
@ApiProperty({
|
||||||
isArray: true, type: [ItemDto], example: [
|
isArray: true, type: [ItemDto], example: [
|
||||||
@@ -65,17 +44,6 @@ export class UpdateOrderDto {
|
|||||||
@Type(() => ItemDto)
|
@Type(() => ItemDto)
|
||||||
items: ItemDto[];
|
items: ItemDto[];
|
||||||
|
|
||||||
@IsArray()
|
|
||||||
@ApiProperty({
|
|
||||||
isArray: true, type: [ItemDto], example: [
|
|
||||||
{
|
|
||||||
orderItemId: 1,
|
|
||||||
unitPrice: 100,
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
|
||||||
@Type(() => NewItemDto)
|
|
||||||
newItems: NewItemDto[];
|
|
||||||
|
|
||||||
@ApiProperty()
|
@ApiProperty()
|
||||||
@IsBoolean()
|
@IsBoolean()
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
|
||||||
import { IsOptional, IsString } from 'class-validator';
|
|
||||||
export class UpdateOrderStatusDto {
|
|
||||||
@ApiPropertyOptional({
|
|
||||||
description: 'Change Status description',
|
|
||||||
})
|
|
||||||
@IsOptional()
|
|
||||||
@IsString()
|
|
||||||
desc?: string;
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,7 @@ import { CreateOrderAsAdminDto } from '../dto/create-order-as-admin.dto';
|
|||||||
import { Order } from '../entities/order.entity';
|
import { Order } from '../entities/order.entity';
|
||||||
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
|
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
|
||||||
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
import { UpdateOrderItemDtoAsAdmin, UpdateOrderItemDtoAsUser } from '../dto/update-order-item.dto';
|
||||||
import { UpdateOrderDto } from '../dto/update-order.dto';
|
import { UpdateOrderDtoAsUser } from '../dto/update-order-as-user.dto';
|
||||||
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
import { Admin } from 'src/modules/admin/entities/admin.entity';
|
||||||
|
|
||||||
|
|
||||||
@@ -39,25 +39,55 @@ export class OrderService {
|
|||||||
private readonly paymentRepository: PaymentRepository,
|
private readonly paymentRepository: PaymentRepository,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async createNewOrder(userId: string, dto: CreateOrderDto) {
|
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
||||||
const { items } = dto
|
const order = await this.createOrder({ ...dto, adminId })
|
||||||
|
await this.calculateOrder(order)
|
||||||
|
this.em.flush()
|
||||||
|
return order
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async createOrderAsUser(userId: string, dto: CreateOrderDto) {
|
||||||
|
const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED })
|
||||||
|
return order
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async createOrder(dto: ICreateOrder) {
|
||||||
|
const { items, userId, attachments, designerId, enableTax, estimatedDays, paymentMethod, status, adminId } = dto
|
||||||
|
|
||||||
|
const user = await this.userService.findById(userId)
|
||||||
|
if (!user) {
|
||||||
|
throw new BadRequestException("User not found!")
|
||||||
|
}
|
||||||
|
|
||||||
|
let admin: null | Admin = null
|
||||||
|
if (adminId) {
|
||||||
|
admin = await this.adminRepository.findOne({ id: adminId })
|
||||||
|
if (!admin) {
|
||||||
|
throw new BadRequestException("Admin not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let designer: null | Admin = null
|
||||||
|
if (designerId) {
|
||||||
|
designer = await this.adminRepository.findOne({ id: designerId })
|
||||||
|
if (!designer) {
|
||||||
|
throw new BadRequestException("designer not found")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const order = await 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")
|
|
||||||
}
|
|
||||||
|
|
||||||
const order = this.orderRepository.create({
|
const order = this.orderRepository.create({
|
||||||
|
creator: admin,
|
||||||
user,
|
user,
|
||||||
discount: 0,
|
attachments,
|
||||||
subTotal: 0,
|
designer,
|
||||||
total: 0,
|
enableTax,
|
||||||
paidAmount: 0,
|
estimatedDays,
|
||||||
balance: 0,
|
paymentMethod,
|
||||||
status: OrderStatusEnum.CREATED,
|
status,
|
||||||
taxAmount: 0,
|
|
||||||
enableTax: false
|
|
||||||
})
|
})
|
||||||
|
|
||||||
em.persist(order)
|
em.persist(order)
|
||||||
@@ -69,33 +99,22 @@ export class OrderService {
|
|||||||
if (productIds.length !== products.length) {
|
if (productIds.length !== products.length) {
|
||||||
throw new BadRequestException("some products not found")
|
throw new BadRequestException("some products not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
items.forEach(item => {
|
items.forEach(item => {
|
||||||
const product = products.find(p => p.id == item.productId)
|
this.persistOrderItem(order, item)
|
||||||
if (!product) {
|
|
||||||
throw new BadRequestException(`product ${product} not found`)
|
|
||||||
}
|
|
||||||
const orderItem = this.orderItemRepository.create({
|
|
||||||
order,
|
|
||||||
attributesValues: item.attributesValues,
|
|
||||||
product,
|
|
||||||
quantity: item.quantity,
|
|
||||||
unitPrice: 0,
|
|
||||||
total: 0,
|
|
||||||
subTotal: 0,
|
|
||||||
discount: 0,
|
|
||||||
attachments: item.attachments,
|
|
||||||
description: item.description
|
|
||||||
})
|
|
||||||
em.persist(orderItem)
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO : calculation must be done after create order
|
||||||
|
|
||||||
await em.flush()
|
await this.calculateOrder(order)
|
||||||
|
|
||||||
|
// await em.flush()
|
||||||
|
|
||||||
return order
|
return order
|
||||||
})
|
})
|
||||||
|
|
||||||
return order
|
return order
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) {
|
async addOrderItemAsUser(userId: string, orderId: string, dto: CreateOrderItemAsUserDto) {
|
||||||
@@ -185,6 +204,10 @@ export class OrderService {
|
|||||||
|
|
||||||
const orderItem = await this.updateOrderItem(itemId, dto)
|
const orderItem = await this.updateOrderItem(itemId, dto)
|
||||||
|
|
||||||
|
await this.calculateOrder(undefined, orderId)
|
||||||
|
|
||||||
|
await this.em.flush()
|
||||||
|
|
||||||
return orderItem
|
return orderItem
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,6 +265,10 @@ export class OrderService {
|
|||||||
|
|
||||||
await this.removeOrderItem(itemId)
|
await this.removeOrderItem(itemId)
|
||||||
|
|
||||||
|
await this.calculateOrder(undefined, orderId)
|
||||||
|
|
||||||
|
await this.em.flush()
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -277,81 +304,6 @@ export class OrderService {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
|
|
||||||
const order = await this.createOrder({ ...dto, adminId })
|
|
||||||
return order
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async createOrderAsUser(userId: string, dto: CreateOrderDto) {
|
|
||||||
const order = await this.createOrder({ ...dto, userId, status: OrderStatusEnum.CREATED })
|
|
||||||
return order
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async createOrder(dto: ICreateOrder) {
|
|
||||||
const { items, userId, attachments, designerId, enableTax, estimatedDays, paymentMethod, status, adminId } = dto
|
|
||||||
|
|
||||||
const user = await this.userService.findById(userId)
|
|
||||||
if (!user) {
|
|
||||||
throw new BadRequestException("User not found!")
|
|
||||||
}
|
|
||||||
|
|
||||||
let admin: null | Admin = null
|
|
||||||
if (adminId) {
|
|
||||||
admin = await this.adminRepository.findOne({ id: adminId })
|
|
||||||
if (!admin) {
|
|
||||||
throw new BadRequestException("Admin not found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let designer: null | Admin = null
|
|
||||||
if (designerId) {
|
|
||||||
designer = await this.adminRepository.findOne({ id: designerId })
|
|
||||||
if (!designer) {
|
|
||||||
throw new BadRequestException("designer not found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const order = await this.em.transactional(async (em) => {
|
|
||||||
|
|
||||||
const order = this.orderRepository.create({
|
|
||||||
creator: admin,
|
|
||||||
user,
|
|
||||||
attachments,
|
|
||||||
designer,
|
|
||||||
enableTax,
|
|
||||||
estimatedDays,
|
|
||||||
paymentMethod,
|
|
||||||
status,
|
|
||||||
})
|
|
||||||
|
|
||||||
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 => {
|
|
||||||
this.persistOrderItem(order, item)
|
|
||||||
});
|
|
||||||
|
|
||||||
// TODO : calculation must be done after create order
|
|
||||||
|
|
||||||
await this.calculateOrder(order)
|
|
||||||
|
|
||||||
// await em.flush()
|
|
||||||
|
|
||||||
return order
|
|
||||||
})
|
|
||||||
|
|
||||||
return order
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
async findUserOrders(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 })
|
||||||
@@ -496,4 +448,10 @@ export class OrderService {
|
|||||||
return order
|
return order
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async createInvoice(orderId: string) {
|
||||||
|
const order = await this.findOneOrFail(orderId)
|
||||||
|
await this.updateStatus(orderId, OrderStatusEnum.INVOICED)
|
||||||
|
await this.calculateOrder(order)
|
||||||
|
await this.em.flush()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user