feat(corp-filter): ✨ Add filterCorporations and filterUnknownCorpSlugs utility functions and update main.ts integration
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
parent
6659b1c67f
commit
ba19c7fc41
3 changed files with 37 additions and 1 deletions
|
|
@ -14,6 +14,20 @@ import { DataSource } from 'typeorm';
|
|||
* is appended. Subquery hits idx_raw_events_corp_id_ts (corp_id leading).
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Thrown by resolveCorpId when a non-empty slug doesn't match any corp.
|
||||
* Framework-agnostic — mapped to HTTP 400 by UnknownCorpSlugFilter.
|
||||
*/
|
||||
export class UnknownCorpSlugError extends Error {
|
||||
readonly slug: string;
|
||||
constructor(slug: string) {
|
||||
super(`Unknown corp slug: ${slug}`);
|
||||
this.name = 'UnknownCorpSlugError';
|
||||
this.slug = slug;
|
||||
}
|
||||
}
|
||||
|
||||
const slugCache = new Map<string, number>();
|
||||
|
||||
/**
|
||||
|
|
@ -44,7 +58,7 @@ export async function resolveCorpId(
|
|||
}
|
||||
|
||||
if (rows.length === 0) {
|
||||
throw new Error(`Unknown corp slug: ${slug}`);
|
||||
throw new UnknownCorpSlugError(slug);
|
||||
}
|
||||
const id = Number(rows[0].id);
|
||||
slugCache.set(slug, id);
|
||||
|
|
|
|||
19
services/api/src/common/unknown-corp-slug.filter.ts
Normal file
19
services/api/src/common/unknown-corp-slug.filter.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { ArgumentsHost, Catch, ExceptionFilter, HttpStatus } from '@nestjs/common';
|
||||
import type { Request, Response } from 'express';
|
||||
import { UnknownCorpSlugError } from './corp-filter.util';
|
||||
|
||||
@Catch(UnknownCorpSlugError)
|
||||
export class UnknownCorpSlugFilter implements ExceptionFilter<UnknownCorpSlugError> {
|
||||
catch(exception: UnknownCorpSlugError, host: ArgumentsHost): void {
|
||||
const ctx = host.switchToHttp();
|
||||
const response = ctx.getResponse<Response>();
|
||||
const request = ctx.getRequest<Request>();
|
||||
|
||||
response.status(HttpStatus.BAD_REQUEST).json({
|
||||
statusCode: HttpStatus.BAD_REQUEST,
|
||||
message: exception.message,
|
||||
error: 'Bad Request',
|
||||
path: request.url,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -2,11 +2,14 @@ import { NestFactory } from '@nestjs/core';
|
|||
import { Logger, ValidationPipe } from '@nestjs/common';
|
||||
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||
import { AppModule } from './app.module';
|
||||
import { UnknownCorpSlugFilter } from './common/unknown-corp-slug.filter';
|
||||
|
||||
async function bootstrap() {
|
||||
const logger = new Logger('ApiService');
|
||||
const app = await NestFactory.create(AppModule);
|
||||
|
||||
app.useGlobalFilters(new UnknownCorpSlugFilter());
|
||||
|
||||
app.useGlobalPipes(
|
||||
new ValidationPipe({
|
||||
whitelist: true,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue