order
This commit is contained in:
@@ -25,6 +25,7 @@ import { User } from 'src/modules/user/entities/user.entity';
|
||||
import { OrderType } from '../entities/order-type.entity';
|
||||
import { Product } from 'src/modules/product/entities/product.entity';
|
||||
import { InvoiceItem } from 'src/modules/invoice/entities/invoice-item.entity';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
|
||||
@Injectable()
|
||||
export class OrderService {
|
||||
@@ -50,12 +51,12 @@ export class OrderService {
|
||||
let user: User | null = null
|
||||
|
||||
if (dto.invoiceItemId) {
|
||||
invoiceItem = await this.em.findOne(InvoiceItem, { id: dto.invoiceItemId },{populate:['invoice','invoice.user']});
|
||||
invoiceItem = await this.em.findOne(InvoiceItem, { id: dto.invoiceItemId }, { populate: ['invoice', 'invoice.user'] });
|
||||
if (!invoiceItem) {
|
||||
throw new NotFoundException(`Invoice item with ID ${dto.invoiceItemId} not found.`);
|
||||
}
|
||||
|
||||
user=invoiceItem.invoice.user
|
||||
user = invoiceItem.invoice.user
|
||||
}
|
||||
if (!invoiceItem) {
|
||||
|
||||
@@ -70,10 +71,10 @@ export class OrderService {
|
||||
}
|
||||
}
|
||||
|
||||
if(!user){
|
||||
if (!user) {
|
||||
throw new NotFoundException(`User not found`);
|
||||
}
|
||||
|
||||
|
||||
const creator = await this.adminService.findOrFail(adminId);
|
||||
|
||||
const type = await this.em.findOne(OrderType, { id: dto.typeId });
|
||||
@@ -104,6 +105,25 @@ export class OrderService {
|
||||
return order;
|
||||
}
|
||||
|
||||
async findOrdersAsUser(userId: string, dto: FindOrdersDto): Promise<PaginatedResult<Order>> {
|
||||
return this.orderRepository.findAllPaginated({
|
||||
userId,
|
||||
...dto
|
||||
})
|
||||
}
|
||||
|
||||
async findOrdersAsAdmin(dto: FindOrdersDto): Promise<PaginatedResult<Order>> {
|
||||
return this.orderRepository.findAllPaginated(dto)
|
||||
}
|
||||
|
||||
async getOrderAsUser(userId: string, orderId: string): Promise<Order> {
|
||||
const order = await this.findOrderOrFail(orderId)
|
||||
if (order.user.id !== userId) {
|
||||
throw new BadRequestException("this order doesnt belongs to you")
|
||||
}
|
||||
return order
|
||||
}
|
||||
|
||||
async findOrderOrFail(id: string): Promise<Order> {
|
||||
const order = await this.em.findOne(Order, { id }, { populate: ['user', 'type', 'product', 'creator'] });
|
||||
if (!order) {
|
||||
@@ -111,7 +131,7 @@ export class OrderService {
|
||||
}
|
||||
return order;
|
||||
}
|
||||
// user should not be change able
|
||||
|
||||
async updateOrderAsAdmin(id: string, dto: UpdateOrderAsAdminDto): Promise<Order> {
|
||||
const order = await this.findOrderOrFail(id);
|
||||
|
||||
@@ -150,4 +170,19 @@ export class OrderService {
|
||||
await this.em.flush();
|
||||
return order;
|
||||
}
|
||||
|
||||
async softDeleteOrder(orderId: string): Promise<void> {
|
||||
const order = await this.findOrderOrFail(orderId);
|
||||
order.deletedAt = new Date();
|
||||
await this.em.flush();
|
||||
}
|
||||
|
||||
async assignDesigner(orderId: string, designerId: string) {
|
||||
const order = await this.findOrderOrFail(orderId)
|
||||
const designer = await this.adminService.findOrFail(designerId)
|
||||
|
||||
order.designer = designer
|
||||
await this.em.flush()
|
||||
return order
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user