orders pagination
This commit is contained in:
@@ -16,6 +16,9 @@ import { PaymentsService } from '../payments/services/payments.service';
|
||||
import { DeliveryMethodEnum } from '../delivery/interface/delivery';
|
||||
import { Delivery } from '../delivery/entities/delivery.entity';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
import { OrderRepository } from './repositories/order.repository';
|
||||
import { FindOrdersDto } from './dto/find-orders.dto';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
|
||||
@Injectable()
|
||||
export class OrdersService {
|
||||
@@ -25,6 +28,7 @@ export class OrdersService {
|
||||
private readonly em: EntityManager,
|
||||
private readonly cartService: CartService,
|
||||
private readonly paymentsService: PaymentsService,
|
||||
private readonly orderRepository: OrderRepository,
|
||||
// private readonly paymentGatewayService: PaymentGatewayService,
|
||||
) {}
|
||||
|
||||
@@ -234,16 +238,18 @@ export class OrdersService {
|
||||
};
|
||||
}
|
||||
|
||||
async findAll(restId: string) {
|
||||
const orders = await this.em.find(
|
||||
Order,
|
||||
{ restaurant: { id: restId } },
|
||||
{ populate: ['user', 'restaurant', 'deliveryMethod', 'address', 'paymentMethod'] },
|
||||
);
|
||||
if (!orders) {
|
||||
throw new NotFoundException('No orders found');
|
||||
}
|
||||
return orders;
|
||||
async findAll(restId: string, dto: FindOrdersDto): Promise<PaginatedResult<Order>> {
|
||||
return this.orderRepository.findAllPaginated(restId, {
|
||||
page: dto.page,
|
||||
limit: dto.limit,
|
||||
status: dto.status,
|
||||
paymentStatus: dto.paymentStatus,
|
||||
search: dto.search,
|
||||
startDate: dto.startDate,
|
||||
endDate: dto.endDate,
|
||||
orderBy: dto.orderBy,
|
||||
order: dto.order,
|
||||
});
|
||||
}
|
||||
|
||||
findOne(id: number) {
|
||||
@@ -299,8 +305,17 @@ export class OrdersService {
|
||||
return order;
|
||||
}
|
||||
|
||||
remove(id: number) {
|
||||
return `This action removes a #${id} order`;
|
||||
async cancelOrder(orderId: string) {
|
||||
const order = await this.em.findOne(Order, { id: orderId });
|
||||
if (!order) {
|
||||
throw new NotFoundException('Order not found');
|
||||
}
|
||||
if (order.status !== OrderStatus.Pending) {
|
||||
throw new BadRequestException('Order is not pending');
|
||||
}
|
||||
order.status = OrderStatus.CancelledByUser;
|
||||
await this.em.persistAndFlush(order);
|
||||
return order;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user