Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

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

Kenrick Tandrianb2832fc11d14
diff --git a/apps/api/src/events/portfolio-changed.listener.ts b/apps/api/src/events/portfolio-changed.listener.ts
index 4c0dd7589d..d10daf1f60 100644
--- a/apps/api/src/events/portfolio-changed.listener.ts
+++ b/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
}
diff --git a/apps/api/src/guards/access.guard.ts b/apps/api/src/guards/access.guard.ts
index f57c5a5b85..593f56afa7 100644
--- a/apps/api/src/guards/access.guard.ts
+++ b/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
diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts
index 91c4fd9b6b..8139feea45 100644
--- a/apps/api/src/main.ts
+++ b/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;
diff --git a/apps/api/src/middlewares/mcp-authorization.middleware.ts b/apps/api/src/middlewares/mcp-authorization.middleware.ts
index d22ffbd3ab..42c999a794 100644
--- a/apps/api/src/middlewares/mcp-authorization.middleware.ts
+++ b/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}"`];