order list for user

This commit is contained in:
2026-01-17 18:37:04 +03:30
parent e07075a7c5
commit 02ab8fbc6a
9 changed files with 84 additions and 91 deletions
@@ -3,22 +3,13 @@ import { EntityManager, EntityRepository } from '@mikro-orm/postgresql';
import { FilterQuery } from '@mikro-orm/core';
import { Order } from '../entities/order.entity';
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
import { OrderStatus } from '../interface/order.interface';
import { OrderStatusEnum } from '../interface/order.interface';
import { PaymentMethodEnum, PaymentStatusEnum } from '../../payment/interface/payment';
import { FindOrdersDto } from '../dto/find-orders.dto';
type FindOrdersOpts = {
page?: number;
limit?: number;
statuses?: OrderStatus[];
paymentStatus?: PaymentStatusEnum;
search?: string;
startDate?: string;
endDate?: string;
orderBy?: string;
order?: 'asc' | 'desc';
class FindOrdersOpts extends FindOrdersDto {
userId?: string;
excludeOnlinePendingPayment?: boolean;
};
@Injectable()
@@ -31,30 +22,29 @@ export class OrderRepository extends EntityRepository<Order> {
* Find orders with pagination and optional filters.
* Supports: statuses, paymentStatus, search (orderNumber), date range, ordering.
*/
async findAllPaginated( opts: FindOrdersOpts = {}): Promise<PaginatedResult<Order>> {
async findAllPaginated(opts: FindOrdersOpts): Promise<PaginatedResult<Order>> {
const {
page = 1,
limit = 10,
statuses,
search,
startDate,
endDate,
orderBy = 'createdAt',
order = 'desc',
userId,
excludeOnlinePendingPayment = false,
from,
to
} = opts;
const offset = (page - 1) * limit;
const where: FilterQuery<Order> = { };
const where: FilterQuery<Order> = {};
// Filter by statuses
if (statuses) {
// Ensure statuses is always an array and normalize it
const normalizedStatuses = Array.isArray(statuses) ? statuses : [statuses];
// Filter out any empty values
const validStatuses = normalizedStatuses.filter((s): s is OrderStatus => !!s);
const validStatuses = normalizedStatuses.filter((s): s is OrderStatusEnum => !!s);
if (validStatuses.length > 0) {
// Use direct equality for single value, $in for multiple values to avoid SQL generation issues
@@ -71,13 +61,13 @@ export class OrderRepository extends EntityRepository<Order> {
}
// Filter by date range
if (startDate || endDate) {
if (from || to) {
where.createdAt = {};
if (startDate) {
where.createdAt.$gte = new Date(startDate);
if (from) {
where.createdAt.$gte = new Date(from);
}
if (endDate) {
where.createdAt.$lte = new Date(endDate);
if (to) {
where.createdAt.$lte = new Date(to);
}
}
@@ -99,40 +89,38 @@ export class OrderRepository extends EntityRepository<Order> {
where.$or = searchConditions;
}
// First, fetch orders without reviews
const [data, total] = await this.findAndCount(where, {
limit,
offset,
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
populate: ['user', 'restaurant', 'deliveryMethod', 'paymentMethod', 'items', 'items.product'] as never,
populate: ['items','items.product'],
});
// Collect all (orderId, productId) pairs for efficient review lookup
const orderproductPairs: Array<{ orderId: string; productId: bigint }> = [];
for (const order of data) {
for (const item of order.items.getItems()) {
if (item.product?.id) {
orderproductPairs.push({ orderId: order.id, productId: item.product.id });
}
}
}
// const orderproductPairs: Array<{ orderId: string; productId: bigint }> = [];
// for (const order of data) {
// for (const item of order.items.getItems()) {
// if (item.product?.id) {
// orderproductPairs.push({ orderId: order.id, productId: item.product.id });
// }
// }
// }
// Map reviewIds to products
for (const order of data) {
for (const item of order.items.getItems()) {
if (item.product) {
const key = `${order.id}-${item.product.id}`;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const product = item.product as any;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
}
}
}
// for (const order of data) {
// for (const item of order.items.getItems()) {
// if (item.product) {
// const key = `${order.id}-${item.product.id}`;
// // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
// const product = item.product as any;
// // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
// }
// }
// }
const totalPages = Math.ceil(total / limit);