All files / src/common/interceptors idempotency.interceptor.ts

61.29% Statements 19/31
40.74% Branches 11/27
66.66% Functions 2/3
58.62% Lines 17/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 6211x 11x 11x 11x 11x     11x   11x 11x         296x       296x 296x   296x 296x 47x       249x 249x 249x                                                              
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, Inject } from '@nestjs/common';
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Cache } from 'cache-manager';
import { ResilienceMetricsService } from '@common/services/resilience-metrics.service';
 
@Injectable()
export class IdempotencyInterceptor implements NestInterceptor {
  constructor(
    @Inject('CACHE_MANAGER') private cache: Cache,
    private readonly metrics: ResilienceMetricsService,
  ) {}
 
  async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
    // Only apply to HTTP requests
    Iif (context.getType() !== 'http') {
      return next.handle();
    }
 
    const http = context.switchToHttp();
    const req = http.getRequest();
 
    const method: string = (req.method || 'GET').toUpperCase();
    if (!['POST', 'PUT', 'PATCH', 'DELETE'].includes(method)) {
      return next.handle();
    }
 
    // Support common header names
    const keyHeader: string | undefined = req.headers['idempotency-key'] || req.headers['x-idempotency-key'];
    if (!keyHeader || typeof keyHeader !== 'string' || keyHeader.trim().length === 0) {
      return next.handle();
    }
 
    const userId = (req.user?.id ?? 'anon') as string | number;
    const cacheKey = `idem:${method}:${req.originalUrl}:${userId}:${keyHeader}`;
 
    try {
      const cached = await this.cache.get<any>(cacheKey);
      Iif (cached !== undefined) {
        this.metrics.incrementIdempotencyHits('hit', req.originalUrl || req.url || 'unknown');
        return of(cached);
      }
    } catch {
      // Cache failure should not block request execution
    }
 
    return next.handle().pipe(
      tap(async (response) => {
        try {
          // Store for 24 hours by default
          await this.cache.set(cacheKey, response, 24 * 60 * 60 * 1000);
          this.metrics.incrementIdempotencyHits('store', req.originalUrl || req.url || 'unknown');
        } catch {
          // Ignore cache store errors
        }
      }),
    );
  }
}