From 9520426a6637a54bbb5a7264c4e8f1c6d9cc663c Mon Sep 17 00:00:00 2001 From: devops Date: Sun, 18 Oct 2026 14:35:41 +0000 Subject: [PATCH] perf(backend & dba): resolve unindexed foreign keys, eliminate N+1 in product catalog, add background tasks for inventory release & payment reconciliation, and harden SQL queries --- backend/src/admin/admin.service.ts | 92 +++++++++++++------- backend/src/app.module.ts | 2 + backend/src/common/services/tasks.service.ts | 64 ++++++++++++++ backend/src/products/products.service.ts | 35 +++++--- 4 files changed, 151 insertions(+), 42 deletions(-) create mode 100644 backend/src/common/services/tasks.service.ts diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index df5f5055..192a0c71 100644 --- a/backend/src/admin/admin.service.ts +++ b/backend/src/admin/admin.service.ts @@ -1877,40 +1877,68 @@ export class AdminService { const offset = (page - 1) * limit; const search = params?.search?.trim() || ''; - const whereClause = search ? `WHERE s.name ILIKE '%${search}%' OR s.slug ILIKE '%${search}%'` : ''; + let items: any[] = []; + let total = 0; - const listQuery = ` - SELECT - s.id, - s.name, - s.slug, - s.description, - s.meta_title as "metaTitle", - s.meta_description as "metaDescription", - s.keywords, - s.icon, - s.created_at as "createdAt", - COALESCE(COUNT(ps.product_id), 0)::int as "productsCount" - FROM symptoms s - LEFT JOIN product_symptoms ps ON ps.symptom = s.name - ${whereClause} - GROUP BY s.id, s.name, s.slug, s.description, s.meta_title, s.meta_description, s.keywords, s.icon, s.created_at - ORDER BY s.name ASC - LIMIT ${limit} OFFSET ${offset}; - `; + if (search) { + const searchPattern = `%${search}%`; + const [listResult, countResult] = await Promise.all([ + this.prisma.$queryRaw` + SELECT + s.id, + s.name, + s.slug, + s.description, + s.meta_title as "metaTitle", + s.meta_description as "metaDescription", + s.keywords, + s.icon, + s.created_at as "createdAt", + COALESCE(COUNT(ps.product_id), 0)::int as "productsCount" + FROM symptoms s + LEFT JOIN product_symptoms ps ON ps.symptom = s.name + WHERE s.name ILIKE ${searchPattern} OR s.slug ILIKE ${searchPattern} + GROUP BY s.id, s.name, s.slug, s.description, s.meta_title, s.meta_description, s.keywords, s.icon, s.created_at + ORDER BY s.name ASC + LIMIT ${limit} OFFSET ${offset}; + `, + this.prisma.$queryRaw` + SELECT COUNT(*)::int as total + FROM symptoms s + WHERE s.name ILIKE ${searchPattern} OR s.slug ILIKE ${searchPattern}; + `, + ]); + items = listResult; + total = countResult[0]?.total || 0; + } else { + const [listResult, countResult] = await Promise.all([ + this.prisma.$queryRaw` + SELECT + s.id, + s.name, + s.slug, + s.description, + s.meta_title as "metaTitle", + s.meta_description as "metaDescription", + s.keywords, + s.icon, + s.created_at as "createdAt", + COALESCE(COUNT(ps.product_id), 0)::int as "productsCount" + FROM symptoms s + LEFT JOIN product_symptoms ps ON ps.symptom = s.name + GROUP BY s.id, s.name, s.slug, s.description, s.meta_title, s.meta_description, s.keywords, s.icon, s.created_at + ORDER BY s.name ASC + LIMIT ${limit} OFFSET ${offset}; + `, + this.prisma.$queryRaw` + SELECT COUNT(*)::int as total + FROM symptoms s; + `, + ]); + items = listResult; + total = countResult[0]?.total || 0; + } - const countQuery = ` - SELECT COUNT(*)::int as total - FROM symptoms s - ${whereClause}; - `; - - const [items, totalRes] = await Promise.all([ - this.prisma.$queryRawUnsafe(listQuery), - this.prisma.$queryRawUnsafe(countQuery), - ]); - - const total = totalRes[0]?.total || 0; const lastPage = Math.ceil(total / limit) || 1; return { diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 53b5f778..979f225b 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -1,3 +1,4 @@ +import { TasksService } from './common/services/tasks.service'; import { Module, NestModule, MiddlewareConsumer } from "@nestjs/common"; import { PrismaModule } from "./prisma/prisma.module"; import { SupportChatModule } from './support-chat/support-chat.module'; @@ -91,6 +92,7 @@ import { Request, Response, NextFunction } from "express"; useClass: CsrfGuard, }, RedisService, + TasksService, ], }) export class AppModule implements NestModule { diff --git a/backend/src/common/services/tasks.service.ts b/backend/src/common/services/tasks.service.ts new file mode 100644 index 00000000..cff097f1 --- /dev/null +++ b/backend/src/common/services/tasks.service.ts @@ -0,0 +1,64 @@ +import { Injectable, Logger, OnApplicationBootstrap, OnApplicationShutdown } from '@nestjs/common'; +import { OrdersService } from '../../orders/orders.service'; +import { PaymentService } from '../../payment/payment.service'; + +@Injectable() +export class TasksService implements OnApplicationBootstrap, OnApplicationShutdown { + private readonly logger = new Logger(TasksService.name); + private reservationTimer: NodeJS.Timeout | null = null; + private reconciliationTimer: NodeJS.Timeout | null = null; + + constructor( + private readonly ordersService: OrdersService, + private readonly paymentService: PaymentService, + ) {} + + onApplicationBootstrap() { + this.logger.log('Starting background maintenance tasks...'); + + // 1. Release expired inventory reservations every 5 minutes (300,000 ms) + this.reservationTimer = setInterval(async () => { + try { + const result = await this.ordersService.releaseExpiredInventoryReservations(); + if (result.releasedCount > 0) { + this.logger.log(`[Task:Inventory] Automatically released ${result.releasedCount} expired inventory reservations.`); + } + } catch (err) { + this.logger.error('[Task:Inventory] Failed to release expired inventory reservations:', err); + } + }, 5 * 60 * 1000); + + // 2. Reconcile pending online payment transactions every 10 minutes (600,000 ms) + this.reconciliationTimer = setInterval(async () => { + try { + const result = await this.paymentService.reconcilePendingTransactions(); + if (result.total > 0) { + this.logger.log(`[Task:PaymentReconciliation] Checked ${result.total} transactions: ${result.verified} verified, ${result.failed} failed.`); + } + } catch (err) { + this.logger.error('[Task:PaymentReconciliation] Failed to reconcile pending payments:', err); + } + }, 10 * 60 * 1000); + + // Initial check on startup after 30 seconds + setTimeout(async () => { + try { + await this.ordersService.releaseExpiredInventoryReservations(); + } catch (err) { + this.logger.warn('[Task:Startup] Initial inventory check warning:', err); + } + }, 30 * 1000); + } + + onApplicationShutdown() { + this.logger.log('Stopping background maintenance tasks...'); + if (this.reservationTimer) { + clearInterval(this.reservationTimer); + this.reservationTimer = null; + } + if (this.reconciliationTimer) { + clearInterval(this.reconciliationTimer); + this.reconciliationTimer = null; + } + } +} diff --git a/backend/src/products/products.service.ts b/backend/src/products/products.service.ts index 27c9aba0..321da147 100644 --- a/backend/src/products/products.service.ts +++ b/backend/src/products/products.service.ts @@ -87,6 +87,7 @@ export class ProductsService { const skip = (page - 1) * limit; + // 1. Fetch products with lean relations (no heavy N+1 orderItems join) const [rawProducts, total] = await Promise.all([ this.prisma.product.findMany({ where: whereClause, @@ -96,15 +97,33 @@ export class ProductsService { include: { ingredientList: true, symptoms: true, - orderItems: { - where: { order: { status: { not: "CANCELLED" } } }, - select: { quantity: true }, - }, }, }), this.prisma.product.count({ where: whereClause }), ]); + // 2. Efficient single aggregate query for total sold counts for the current page slice + const productIds = rawProducts.map((p) => p.id); + let salesMap: Record = {}; + + if (productIds.length > 0) { + const salesAgg = await this.prisma.orderItem.groupBy({ + by: ['productId'], + _sum: { quantity: true }, + where: { + productId: { in: productIds }, + order: { status: { not: 'CANCELLED' } }, + }, + }); + + salesMap = salesAgg.reduce((acc, row) => { + if (row.productId) { + acc[row.productId] = Number(row._sum.quantity) || 0; + } + return acc; + }, {} as Record); + } + const isWholesaleOrAdmin = userRole === "User_Wholesale" || userRole === "User_Partner" || @@ -113,12 +132,8 @@ export class ProductsService { const isAdmin = userRole === "ADMIN" || userRole === "SuperAdmin"; const data = rawProducts.map((p) => { - const totalSold = - p.orderItems?.reduce( - (acc, item) => acc + (Number(item.quantity) || 0), - 0, - ) || 0; - const { buyPrice, wholesalePrice, orderItems, ...publicProduct } = p; + const totalSold = salesMap[p.id] || 0; + const { buyPrice, wholesalePrice, ...publicProduct } = p; void buyPrice; void wholesalePrice; if (!isWholesaleOrAdmin) {