payment
This commit is contained in:
@@ -5,7 +5,7 @@ import { Order } from '../entities/order.entity';
|
||||
import { PaginatedResult } from 'src/common/interfaces/pagination.interface';
|
||||
import { OrderStatus } from '../interface/order.interface';
|
||||
import { PaymentMethodEnum, PaymentStatusEnum } from '../../payment/interface/payment';
|
||||
import { Review } from '../../review/entities/review.entity';
|
||||
|
||||
|
||||
type FindOrdersOpts = {
|
||||
page?: number;
|
||||
@@ -31,7 +31,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
* Find orders with pagination and optional filters.
|
||||
* Supports: statuses, paymentStatus, search (orderNumber), date range, ordering.
|
||||
*/
|
||||
async findAllPaginated(: string, opts: FindOrdersOpts = {}): Promise<PaginatedResult<Order>> {
|
||||
async findAllPaginated( opts: FindOrdersOpts = {}): Promise<PaginatedResult<Order>> {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 10,
|
||||
@@ -47,7 +47,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
const where: FilterQuery<Order> = { restaurant: { id: } };
|
||||
const where: FilterQuery<Order> = { };
|
||||
|
||||
// Filter by statuses
|
||||
if (statuses) {
|
||||
@@ -99,19 +99,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
where.$or = searchConditions;
|
||||
}
|
||||
|
||||
// Filter: Exclude orders with payment method Online and status pending_payment
|
||||
if (excludeOnlinePendingPayment) {
|
||||
const existingConditions = where.$and || [];
|
||||
where.$and = [
|
||||
...existingConditions,
|
||||
{
|
||||
$or: [
|
||||
{ paymentMethod: { method: { $ne: PaymentMethodEnum.Online } } },
|
||||
{ status: { $ne: OrderStatus.PENDING_PAYMENT } },
|
||||
],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
// First, fetch orders without reviews
|
||||
const [data, total] = await this.findAndCount(where, {
|
||||
@@ -122,7 +110,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
});
|
||||
|
||||
// Collect all (orderId, productId) pairs for efficient review lookup
|
||||
const orderproductPairs: Array<{ orderId: string; productId: string }> = [];
|
||||
const orderproductPairs: Array<{ orderId: string; productId: bigint }> = [];
|
||||
for (const order of data) {
|
||||
for (const item of order.items.getItems()) {
|
||||
if (item.product?.id) {
|
||||
@@ -131,30 +119,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch all relevant reviews in a single query
|
||||
const reviewsMap: Map<string, string> = new Map();
|
||||
if (orderproductPairs.length > 0) {
|
||||
const orderIds = [...new Set(orderproductPairs.map(p => p.orderId))];
|
||||
const productIds = [...new Set(orderproductPairs.map(p => p.productId))];
|
||||
|
||||
const reviews = await this.em.find(
|
||||
Review,
|
||||
{
|
||||
order: { id: { $in: orderIds } },
|
||||
product: { id: { $in: productIds } },
|
||||
},
|
||||
{
|
||||
fields: ['id', 'order', 'product'],
|
||||
populate: ['order', 'product'],
|
||||
},
|
||||
);
|
||||
|
||||
// Create a map: key = `${orderId}-${productId}`, value = reviewId
|
||||
for (const review of reviews) {
|
||||
const key = `${review.order.id}-${review.product.id}`;
|
||||
reviewsMap.set(key, review.id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Map reviewIds to products
|
||||
for (const order of data) {
|
||||
@@ -164,7 +129,7 @@ export class OrderRepository extends EntityRepository<Order> {
|
||||
// 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
|
||||
product.reviewId = reviewsMap.get(key) || null;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user