Browse Source

Task/improve type safety in bootstrap, guards, middlewares, and events (#8351)

Improve type safety
pull/8352/head
Kenrick Tandrian 4 days ago
committed by GitHub
parent
commit
b2832fc11d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 14
      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

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

@ -3,10 +3,12 @@ import { PortfolioSnapshotService } from '@ghostfolio/api/services/queues/portfo
import { RedisCacheService } from '@ghostfolio/api/services/redis-cache/redis-cache.service';
import { UserService } from '@ghostfolio/api/services/user/user.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';
@ -67,21 +69,25 @@ export class PortfolioChangedListener {
userSettings
});
const calculationType =
userSettings.performanceCalculationType ??
PerformanceCalculationType.ROAI;
// Recompute in the background to avoid a cold start on the next request
await this.portfolioSnapshotService.addJobToQueue({
data: {
calculationType,
filters,
userId,
calculationType: userSettings.performanceCalculationType,
userCurrency: userSettings.baseCurrency
userCurrency: userSettings.baseCurrency ?? DEFAULT_CURRENCY
},
name: PORTFOLIO_SNAPSHOT_PROCESS_JOB_NAME,
opts: {
...PORTFOLIO_SNAPSHOT_PROCESS_JOB_OPTIONS,
jobId: this.redisCacheService.getPortfolioSnapshotKey({
calculationType,
filters,
userId,
calculationType: userSettings.performanceCalculationType
userId
}),
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