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 { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service';
import {
DEFAULT_CURRENCY,
PORTFOLIO_SNAPSHOT_COMPUTATION_QUEUE_PRIORITY_LOW,
PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS
} from '@ghostfolio/common/config';
import { PerformanceCalculationType } from '@ghostfolio/common/types/performance-calculation-type.type';
import { Injectable, Logger } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
@ -72,8 +74,10 @@ export class PortfolioChangedListener {
data: {
filters,
userId,
calculationType: userSettings.performanceCalculationType,
userCurrency: userSettings.baseCurrency
calculationType:
userSettings.performanceCalculationType ??
PerformanceCalculationType.ROAI,
userCurrency: userSettings.baseCurrency ?? DEFAULT_CURRENCY
},
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: {
@ -81,7 +85,9 @@ export class PortfolioChangedListener {
jobId: this.redisCacheService.getPortfolioSnapshotKey({
filters,
userId,
calculationType: userSettings.performanceCalculationType
calculationType:
userSettings.performanceCalculationType ??
PerformanceCalculationType.ROAI
}),
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 =
getActiveImpersonationOfBearerToken(request);
if (!impersonationOfBearerToken) {
if (!request || !impersonationOfBearerToken) {
throw new HttpException(
getReasonPhrase(StatusCodes.FORBIDDEN),
StatusCodes.FORBIDDEN

4
apps/api/src/main.ts

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

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

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

Loading…
Cancel
Save