Browse Source

fix(api): improve type safety in bootstrap, guards, middlewares, and events

pull/8351/head
KenTandrian 4 weeks ago
parent
commit
26efdf1f5f
  1. 12
      apps/api/src/events/portfolio-changed.listener.ts
  2. 2
      apps/api/src/guards/access.guard.ts
  3. 4
      apps/api/src/main.ts
  4. 2
      apps/api/src/middlewares/mcp-authorization.middleware.ts

12
apps/api/src/events/portfolio-changed.listener.ts

@ -3,10 +3,12 @@ import { ApiService } from '@ghostfolio/api/services/api/api.service';
import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service'; import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfolio-snapshot/portfolio-snapshot.service';
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service'; import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service';
import { import {
DEFAULT_CURRENCY,
PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW, PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW,
PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME, PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type';
import { Injectable, Logger } from '@nestjs/common'; import { Injectable, Logger } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter'; import { OnEvent } from '@nestjs/event-emitter';
@ -72,8 +74,10 @@ export class PortfolioChangedListener {
data: { data: {
filters, filters,
userId, userId,
calculationType: userSettings.performanceCalculationType, calculationType:
userCurrency: userSettings.baseCurrency userSettings.performanceCalculationType ??
PerformanceCalculationType.ROAI,
userCurrency: userSettings.baseCurrency ?? DEFAULT_CURRENCY
}, },
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME, name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: { opts: {
@ -81,7 +85,9 @@ export class PortfolioChangedListener {
jobId: this.redisCacheService.getPortfolioSnapshotKey({ jobId: this.redisCacheService.getPortfolioSnapshotKey({
filters, filters,
userId, userId,
calculationType: userSettings.performanceCalculationType calculationType:
userSettings.performanceCalculationType ??
PerformanceCalculationType.ROAI
}), }),
priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW priority: PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW
} }

2
apps/api/src/guards/access.guard.ts

@ -32,7 +32,7 @@ export class AccessGuard implements CanActivate {
const impersonationOfBearerToken = const impersonationOfBearerToken =
getActiveImpersonationOfBearerToken(request); getActiveImpersonationOfBearerToken(request);
if (!impersonationOfBearerToken) { if (!request || !impersonationOfBearerToken) {
throw new HttpException( throw new HttpException(
getReasonPhrase(StatusCodes.FORBIDDEN), getReasonPhrase(StatusCodes.FORBIDDEN),
StatusCodes.FORBIDDEN StatusCodes.FORBIDDEN

4
apps/api/src/main.ts

@ -53,7 +53,7 @@ async function bootstrap() {
try { try {
customLogLevels = JSON.parse( customLogLevels = JSON.parse(
configService.get<string>('LOG_LEVELS') configService.get<string>('LOG_LEVELS') ?? ''
) as LogLevel[]; ) as LogLevel[];
} catch {} } catch {}
@ -191,7 +191,7 @@ async function bootstrap() {
let address = app.getHttpServer().address(); let address = app.getHttpServer().address();
if (typeof address === 'object') { if (address && typeof address === 'object') {
const addressObject = address; const addressObject = address;
let host = addressObject.address; let host = addressObject.address;

2
apps/api/src/middlewares/mcp-authorization.middleware.ts

@ -92,7 +92,7 @@ function sendUnauthorizedResponse({
accessId, accessId,
response response
}: { }: {
accessId: string; accessId?: string;
response: Response; response: Response;
}) { }) {
const parameters = [`realm="${MCP_REALM}"`]; const parameters = [`realm="${MCP_REALM}"`];

Loading…
Cancel
Save