Skip to content

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

Task/improve type safety in api key, impersonation, platform, and market data services (#8356)

Kenrick Tandrian89c953da03bb
diff --git a/apps/api/src/app/endpoints/auth/api-key.strategy.ts b/apps/api/src/app/endpoints/auth/api-key.strategy.ts
index c84d3ccd2d..fa979822a2 100644
--- a/apps/api/src/app/endpoints/auth/api-key.strategy.ts
+++ b/apps/api/src/app/endpoints/auth/api-key.strategy.ts
@@ -53,11 +53,14 @@ export class ApiKeyStrategy extends PassportStrategy(
private async validateApiKey(apiKey: string) {
if (apiKey) {
try {
- const { id } = await this.apiKeyService.getUserByApiKey(apiKey);
- const user = await this.userService.user({ id });
+ const userOfApiKey = await this.apiKeyService.getUserByApiKey(apiKey);
- if (user) {
- return user;
+ if (userOfApiKey) {
+ const user = await this.userService.user({ id: userOfApiKey.id });
+
+ if (user) {
+ return user;
+ }
}
} catch {}
}
diff --git a/apps/api/src/app/endpoints/info/info.service.ts b/apps/api/src/app/endpoints/info/info.service.ts
index ecff1c16b5..80f2224d6a 100644
--- a/apps/api/src/app/endpoints/info/info.service.ts
+++ b/apps/api/src/app/endpoints/info/info.service.ts
@@ -49,7 +49,7 @@ export class InfoService {
const info: Partial<InfoItem> = {};
let isReadOnlyMode: boolean | undefined;
let latestFearAndGreedStocksMarketDataPromise:
- Promise<MarketData> | undefined;
+ Promise<MarketData | null> | undefined;
const globalPermissions: string[] = [];
diff --git a/apps/api/src/services/api-key/api-key.service.ts b/apps/api/src/services/api-key/api-key.service.ts
index b911191dc2..e42d924d79 100644
--- a/apps/api/src/services/api-key/api-key.service.ts
+++ b/apps/api/src/services/api-key/api-key.service.ts
@@ -32,12 +32,12 @@ export class ApiKeyService {
public async getUserByApiKey(apiKey: string) {
const hashedKey = this.hashApiKey(apiKey);
- const { user } = await this.prismaService.apiKey.findUnique({
+ const apiKeyFromDatabase = await this.prismaService.apiKey.findUnique({
include: { user: true },
where: { hashedKey }
});
- return user;
+ return apiKeyFromDatabase?.user;
}
public hashApiKey(apiKey: string): string {
@@ -53,7 +53,7 @@ export class ApiKeyService {
private generateApiKey(): string {
return getRandomString(32)
.split('')
- .reduce((acc, char, index) => {
+ .reduce<string[]>((acc, char, index) => {
const chunkIndex = Math.floor(index / 4);
acc[chunkIndex] = (acc[chunkIndex] || '') + char;
return acc;
diff --git a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
index b68ed2fb15..f67748c007 100644
--- a/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
+++ b/apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
@@ -337,7 +337,7 @@ export class ExchangeRateDataService {
date: aDate,
symbol: `${DEFAULT_CURRENCY}${aFromCurrency}`
})
- )?.marketPrice;
+ )?.marketPrice as number;
}
} catch {}
@@ -351,7 +351,7 @@ export class ExchangeRateDataService {
date: aDate,
symbol: `${DEFAULT_CURRENCY}${aToCurrency}`
})
- )?.marketPrice;
+ )?.marketPrice as number;
}
} catch {}
diff --git a/apps/api/src/services/impersonation/impersonation.service.ts b/apps/api/src/services/impersonation/impersonation.service.ts
index 97c3295f02..e8448d7f2c 100644
--- a/apps/api/src/services/impersonation/impersonation.service.ts
+++ b/apps/api/src/services/impersonation/impersonation.service.ts
@@ -50,7 +50,7 @@ export class ImpersonationService {
authenticatedUserSubscription: user?.subscription,
isActive: false,
scopes: getScopesOfOwnAccess(),
- userId: user?.id,
+ userId: user?.id as string,
userSettings: user?.settings?.settings ?? {},
userSubscription: user?.subscription
};
diff --git a/apps/api/src/services/market-data/market-data.service.ts b/apps/api/src/services/market-data/market-data.service.ts
index c1a31ce815..0e67079a05 100644
--- a/apps/api/src/services/market-data/market-data.service.ts
+++ b/apps/api/src/services/market-data/market-data.service.ts
@@ -30,7 +30,7 @@ export class MarketDataService {
dataSource,
date = new Date(),
symbol
- }: DataGatheringItem): Promise<MarketData> {
+ }: DataGatheringItem): Promise<MarketData | null> {
return await this.prismaService.marketData.findFirst({
where: {
dataSource,
@@ -43,7 +43,7 @@ export class MarketDataService {
public async getLatest({
dataSource,
symbol
- }: AssetProfileIdentifier): Promise<MarketData> {
+ }: AssetProfileIdentifier): Promise<MarketData | null> {
return this.prismaService.marketData.findFirst({
orderBy: [{ date: 'desc' }],
where: {
diff --git a/apps/api/src/services/platform/platform.service.ts b/apps/api/src/services/platform/platform.service.ts
index 56da19b2a5..f13be12c7a 100644
--- a/apps/api/src/services/platform/platform.service.ts
+++ b/apps/api/src/services/platform/platform.service.ts
@@ -21,7 +21,7 @@ export class PlatformService {
public async getPlatform(
platformWhereUniqueInput: Prisma.PlatformWhereUniqueInput
- ): Promise<Platform> {
+ ): Promise<Platform | null> {
return this.prismaService.platform.findUnique({
where: platformWhereUniqueInput
});
diff --git a/apps/api/src/services/symbol/symbol.service.ts b/apps/api/src/services/symbol/symbol.service.ts
index 69d9d54f32..856ed4bf01 100644
--- a/apps/api/src/services/symbol/symbol.service.ts
+++ b/apps/api/src/services/symbol/symbol.service.ts
@@ -50,7 +50,7 @@ export class SymbolService {
symbol: dataGatheringItem.symbol
});
- marketPrice = latestMarketData?.marketPrice;
+ marketPrice = latestMarketData?.marketPrice as number;
} else {
const quotes = await this.dataProviderService.getQuotes({
items: [dataGatheringItem]