mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 22:18:12 +00:00
Fare and route-coach, production checklist updates
This commit is contained in:
@@ -3,6 +3,17 @@ import { Reflector } from '@nestjs/core';
|
||||
import { ThrottlerGuard, ThrottlerStorage, getOptionsToken, getStorageToken } from '@nestjs/throttler';
|
||||
import { SystemConfigService, CONFIG_KEYS } from '../modules/system-config/system-config.service';
|
||||
|
||||
// Route-prefix → throttler tier mapping.
|
||||
// Evaluated in order; first match wins.
|
||||
const ROUTE_TIERS: Array<{ prefix: string; tier: 'auth' | 'strict' | 'default' }> = [
|
||||
{ prefix: '/auth', tier: 'auth' },
|
||||
{ prefix: '/fayda/verification',tier: 'auth' },
|
||||
{ prefix: '/bookings', tier: 'strict' },
|
||||
{ prefix: '/passengers', tier: 'strict' },
|
||||
{ prefix: '/payments', tier: 'strict' },
|
||||
{ prefix: '/wallet', tier: 'strict' },
|
||||
];
|
||||
|
||||
@Injectable()
|
||||
export class DynamicThrottlerGuard extends ThrottlerGuard {
|
||||
constructor(
|
||||
@@ -29,9 +40,17 @@ export class DynamicThrottlerGuard extends ThrottlerGuard {
|
||||
this.systemConfig.getNumber(CONFIG_KEYS.THROTTLE_DEFAULT_TTL_MS),
|
||||
]);
|
||||
|
||||
this.throttlers = [
|
||||
{ name: 'default', ttl: defaultTtl, limit: defaultLimit },
|
||||
];
|
||||
const url: string = context.switchToHttp().getRequest<{ url: string }>().url ?? '';
|
||||
const matched = ROUTE_TIERS.find(({ prefix }) => url.startsWith(prefix));
|
||||
const tier = matched?.tier ?? 'default';
|
||||
|
||||
if (tier === 'auth') {
|
||||
this.throttlers = [{ name: 'auth', ttl: authTtl, limit: authLimit }];
|
||||
} else if (tier === 'strict') {
|
||||
this.throttlers = [{ name: 'strict', ttl: strictTtl, limit: strictLimit }];
|
||||
} else {
|
||||
this.throttlers = [{ name: 'default', ttl: defaultTtl, limit: defaultLimit }];
|
||||
}
|
||||
|
||||
return super.canActivate(context);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||
import { Injectable, Logger, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(PrismaService.name);
|
||||
|
||||
constructor() {
|
||||
super({
|
||||
// In production set connection_limit and pool_timeout in DATABASE_URL:
|
||||
// ?connection_limit=10&pool_timeout=20&sslmode=require
|
||||
log:
|
||||
process.env.NODE_ENV === 'development'
|
||||
? [{ emit: 'event', level: 'query' }, { emit: 'stdout', level: 'warn' }, { emit: 'stdout', level: 'error' }]
|
||||
: [{ emit: 'stdout', level: 'warn' }, { emit: 'stdout', level: 'error' }],
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
(this as any).$on('query', (e: { query: string; duration: number }) => {
|
||||
if (e.duration > 500) {
|
||||
this.logger.warn(`Slow query (${e.duration}ms): ${e.query}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleInit() { await this.$connect(); }
|
||||
async onModuleDestroy() { await this.$disconnect(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user