Files
canina/backend/src/common/interceptors/decimal.interceptor.ts
T
parsa 809c69f923
Deploy Canina / deploy (push) Failing after 28s
feat(phase2): implement database optimization and RBAC enforcement
- TASK-FIN-001: Eliminate N+1 query loop in OrdersService.create with batched findMany query.
- Perform monetary calculations strictly using Prisma.Decimal methods (.add(), .mul(), .sub()).
- Add validation for duplicate product IDs and invalid quantities in order items.
- Implement global DecimalInterceptor registered in main.ts to serialize Prisma.Decimal values into exact string representations in API responses.
- TASK-SEC-003: Create roles.decorator.ts and roles.guard.ts in common directory with backward-compatible re-exports in auth directory.
- Enforce RBAC on SettingsController with @UseGuards(JwtAuthGuard, RolesGuard) and @Roles('Admin').
- Add unit specs for DecimalInterceptor, RolesGuard, SettingsController, and OrdersService Decimal batch operations.
2026-08-06 21:29:20 +03:30

41 lines
1011 B
TypeScript

import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Prisma } from '@prisma/client';
@Injectable()
export class DecimalInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
return next.handle().pipe(map((data) => this.transform(data)));
}
private transform(data: any): any {
if (data === null || data === undefined) {
return data;
}
if (Prisma.Decimal.isDecimal(data)) {
return data.toString();
}
if (Array.isArray(data)) {
return data.map((item) => this.transform(item));
}
if (typeof data === 'object' && !(data instanceof Date)) {
const transformedObj: Record<string, any> = {};
for (const key of Object.keys(data)) {
transformedObj[key] = this.transform(data[key]);
}
return transformedObj;
}
return data;
}
}