feat: enhance consumer and partner middleware with Redis caching

- Added Redis caching to ConsumerMiddleware and PartnerMiddleware to improve performance by reducing database calls for consumer and partner data.
- Implemented logic to check for cached data before querying the database.
- Updated the InvoicesService to include settlement_type in the selected fields.
- Removed unnecessary console logs from CustomersService.
- Modified SalesInvoicesService to optimize query logic and improve performance.
- Updated StatisticsService to streamline data retrieval and processing.
- Created a new migration to add last_attempt_no and last_tsp_status columns to sales_invoices for better tracking of TSP attempts.
This commit is contained in:
2026-06-09 13:31:58 +03:30
parent f61100bf25
commit 47a27fb54f
19 changed files with 482 additions and 134 deletions
+24 -1
View File
@@ -1,5 +1,7 @@
import { RedisKeyMaker } from '@/common/utils'
import { checkAndDecodeJwtToken } from '@/common/utils/jwt-user.util'
import { PrismaService } from '@/prisma/prisma.service'
import { RedisService } from '@/redis/redis.service'
import {
ForbiddenException,
Injectable,
@@ -14,6 +16,7 @@ export class ConsumerMiddleware implements NestMiddleware {
constructor(
private prisma: PrismaService,
private jwtService: JwtService,
private redisService: RedisService,
) {}
async use(req: Request, _res: Response, next: NextFunction) {
@@ -21,7 +24,15 @@ export class ConsumerMiddleware implements NestMiddleware {
throw new ForbiddenException('شما دسترسی لازم را ندارید')
}
try {
const token = req.cookies?.accessToken
const cacheKey = RedisKeyMaker.consumerMiddleware(token)
let cachedConsumerData: unknown
try {
cachedConsumerData = await this.redisService.getJson(cacheKey)
} catch (_) {}
const tokenAccount = checkAndDecodeJwtToken(req, this.jwtService)
req.decodedToken = tokenAccount!
if (
tokenAccount?.type !== 'CONSUMER' ||
@@ -30,6 +41,16 @@ export class ConsumerMiddleware implements NestMiddleware {
throw new UnauthorizedException()
}
if (
cachedConsumerData &&
typeof cachedConsumerData === 'object' &&
'account_id' in cachedConsumerData &&
'id' in cachedConsumerData
) {
req.consumerData = cachedConsumerData as any
return next()
}
const consumerAccount = await this.prisma.consumerAccount.findUnique({
where: {
id: tokenAccount.account_id,
@@ -49,7 +70,9 @@ export class ConsumerMiddleware implements NestMiddleware {
id: consumerAccount.consumer_id,
}
req.decodedToken = tokenAccount
try {
await this.redisService.setJson(cacheKey, req.consumerData, 60 * 60)
} catch (_) {}
return next()
} catch (error) {