diff --git a/src/config/configuration.interface.ts b/src/config/configuration.interface.ts index ddcc84d12..6611653d3 100644 --- a/src/config/configuration.interface.ts +++ b/src/config/configuration.interface.ts @@ -79,7 +79,7 @@ export interface Configuration { }; tracking: { - mode: 'all' | 'api-key'; + mode: 'all' | 'api-key' | 'authenticated'; index: string; deleteOldLogs: boolean; deleteOldLogsDays: number; diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 2e46af516..39ca1acb4 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -89,7 +89,9 @@ const configuration: Configuration = { paymentMethodTypes: ['card'], }, tracking: { - mode: process.env.TRACKING_MODE === 'all' ? 'all' : 'api-key', + mode: + (process.env.TRACKING_MODE as Configuration['tracking']['mode']) ?? + 'api-key', index: process.env.TRACKING_INDEX ?? 'staart-logs', deleteOldLogs: process.env.TRACKING_DELETE_OLD_LOGS !== undefined diff --git a/src/middleware/api-logger.middleware.ts b/src/middleware/api-logger.middleware.ts index e66647fa4..4cb87a363 100644 --- a/src/middleware/api-logger.middleware.ts +++ b/src/middleware/api-logger.middleware.ts @@ -1,6 +1,7 @@ import { Injectable, NestMiddleware } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { NextFunction, Request, Response } from 'express'; +import { validate } from 'uuid'; import { Configuration } from '../config/configuration.interface'; import { ElasticSearchService } from '../providers/elasticsearch/elasticsearch.service'; @@ -28,7 +29,12 @@ export class ApiLoggerMiddleware implements NestMiddleware { }; if (config.mode === 'all') this.elasticSearchService.index(config.index, obj); - else if (config.mode === 'api-key' && req.headers.authorization) + else if (config.mode === 'authenticated' && req.headers.authorization) + this.elasticSearchService.index(config.index, obj); + else if ( + config.mode === 'api-key' && + validate(req.headers.authorization?.replace('Bearer ', '')) + ) this.elasticSearchService.index(config.index, obj); }); next();