Browse Source
Task/improve type safety in api key, impersonation, platform, and market data services (#8356)
Improve type safety
pull/1946/merge
Kenrick Tandrian
20 hours ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with
18 additions and
15 deletions
-
apps/api/src/app/endpoints/auth/api-key.strategy.ts
-
apps/api/src/app/endpoints/info/info.service.ts
-
apps/api/src/services/api-key/api-key.service.ts
-
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
-
apps/api/src/services/impersonation/impersonation.service.ts
-
apps/api/src/services/market-data/market-data.service.ts
-
apps/api/src/services/platform/platform.service.ts
-
apps/api/src/services/symbol/symbol.service.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 {} |
|
|
|
} |
|
|
|
|
|
|
|
@ -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[] = []; |
|
|
|
|
|
|
|
|
|
|
|
@ -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; |
|
|
|
|
|
|
|
@ -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 {} |
|
|
|
|
|
|
|
|
|
|
|
@ -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 |
|
|
|
}; |
|
|
|
|
|
|
|
@ -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: { |
|
|
|
|
|
|
|
@ -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 |
|
|
|
}); |
|
|
|
|
|
|
|
@ -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] |
|
|
|
|