Browse Source

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

Improve type safety
pull/1946/merge
Kenrick Tandrian 19 hours ago
committed by GitHub
parent
commit
89c953da03
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 11
      apps/api/src/app/endpoints/auth/api-key.strategy.ts
  2. 2
      apps/api/src/app/endpoints/info/info.service.ts
  3. 6
      apps/api/src/services/api-key/api-key.service.ts
  4. 4
      apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts
  5. 2
      apps/api/src/services/impersonation/impersonation.service.ts
  6. 4
      apps/api/src/services/market-data/market-data.service.ts
  7. 2
      apps/api/src/services/platform/platform.service.ts
  8. 2
      apps/api/src/services/symbol/symbol.service.ts

11
apps/api/src/app/endpoints/auth/api-key.strategy.ts

@ -53,11 +53,14 @@ export class ApiKeyStrategy extends PassportStrategy(
private async validateApiKey(apiKey: string) { private async validateApiKey(apiKey: string) {
if (apiKey) { if (apiKey) {
try { try {
const { id } = await this.apiKeyService.getUserByApiKey(apiKey); const userOfApiKey = await this.apiKeyService.getUserByApiKey(apiKey);
const user = await this.userService.user({ id });
if (user) { if (userOfApiKey) {
return user; const user = await this.userService.user({ id: userOfApiKey.id });
if (user) {
return user;
}
} }
} catch {} } catch {}
} }

2
apps/api/src/app/endpoints/info/info.service.ts

@ -49,7 +49,7 @@ export class InfoService {
const info: Partial<InfoItem> = {}; const info: Partial<InfoItem> = {};
let isReadOnlyMode: boolean | undefined; let isReadOnlyMode: boolean | undefined;
let latestFearAndGreedStocksMarketDataPromise: let latestFearAndGreedStocksMarketDataPromise:
Promise<MarketData> | undefined; Promise<MarketData | null> | undefined;
const globalPermissions: string[] = []; const globalPermissions: string[] = [];

6
apps/api/src/services/api-key/api-key.service.ts

@ -32,12 +32,12 @@ export class ApiKeyService {
public async getUserByApiKey(apiKey: string) { public async getUserByApiKey(apiKey: string) {
const hashedKey = this.hashApiKey(apiKey); const hashedKey = this.hashApiKey(apiKey);
const { user } = await this.prismaService.apiKey.findUnique({ const apiKeyFromDatabase = await this.prismaService.apiKey.findUnique({
include: { user: true }, include: { user: true },
where: { hashedKey } where: { hashedKey }
}); });
return user; return apiKeyFromDatabase?.user;
} }
public hashApiKey(apiKey: string): string { public hashApiKey(apiKey: string): string {
@ -53,7 +53,7 @@ export class ApiKeyService {
private generateApiKey(): string { private generateApiKey(): string {
return getRandomString(32) return getRandomString(32)
.split('') .split('')
.reduce((acc, char, index) => { .reduce<string[]>((acc, char, index) => {
const chunkIndex = Math.floor(index / 4); const chunkIndex = Math.floor(index / 4);
acc[chunkIndex] = (acc[chunkIndex] || '') + char; acc[chunkIndex] = (acc[chunkIndex] || '') + char;
return acc; return acc;

4
apps/api/src/services/exchange-rate-data/exchange-rate-data.service.ts

@ -337,7 +337,7 @@ export class ExchangeRateDataService {
date: aDate, date: aDate,
symbol: `${DEFAULT_CURRENCY}${aFromCurrency}` symbol: `${DEFAULT_CURRENCY}${aFromCurrency}`
}) })
)?.marketPrice; )?.marketPrice as number;
} }
} catch {} } catch {}
@ -351,7 +351,7 @@ export class ExchangeRateDataService {
date: aDate, date: aDate,
symbol: `${DEFAULT_CURRENCY}${aToCurrency}` symbol: `${DEFAULT_CURRENCY}${aToCurrency}`
}) })
)?.marketPrice; )?.marketPrice as number;
} }
} catch {} } catch {}

2
apps/api/src/services/impersonation/impersonation.service.ts

@ -50,7 +50,7 @@ export class ImpersonationService {
authenticatedUserSubscription: user?.subscription, authenticatedUserSubscription: user?.subscription,
isActive: false, isActive: false,
scopes: getScopesOfOwnAccess(), scopes: getScopesOfOwnAccess(),
userId: user?.id, userId: user?.id as string,
userSettings: user?.settings?.settings ?? {}, userSettings: user?.settings?.settings ?? {},
userSubscription: user?.subscription userSubscription: user?.subscription
}; };

4
apps/api/src/services/market-data/market-data.service.ts

@ -30,7 +30,7 @@ export class MarketDataService {
dataSource, dataSource,
date = new Date(), date = new Date(),
symbol symbol
}: DataGatheringItem): Promise<MarketData> { }: DataGatheringItem): Promise<MarketData | null> {
return await this.prismaService.marketData.findFirst({ return await this.prismaService.marketData.findFirst({
where: { where: {
dataSource, dataSource,
@ -43,7 +43,7 @@ export class MarketDataService {
public async getLatest({ public async getLatest({
dataSource, dataSource,
symbol symbol
}: AssetProfileIdentifier): Promise<MarketData> { }: AssetProfileIdentifier): Promise<MarketData | null> {
return this.prismaService.marketData.findFirst({ return this.prismaService.marketData.findFirst({
orderBy: [{ date: 'desc' }], orderBy: [{ date: 'desc' }],
where: { where: {

2
apps/api/src/services/platform/platform.service.ts

@ -21,7 +21,7 @@ export class PlatformService {
public async getPlatform( public async getPlatform(
platformWhereUniqueInput: Prisma.PlatformWhereUniqueInput platformWhereUniqueInput: Prisma.PlatformWhereUniqueInput
): Promise<Platform> { ): Promise<Platform | null> {
return this.prismaService.platform.findUnique({ return this.prismaService.platform.findUnique({
where: platformWhereUniqueInput where: platformWhereUniqueInput
}); });

2
apps/api/src/services/symbol/symbol.service.ts

@ -50,7 +50,7 @@ export class SymbolService {
symbol: dataGatheringItem.symbol symbol: dataGatheringItem.symbol
}); });
marketPrice = latestMarketData?.marketPrice; marketPrice = latestMarketData?.marketPrice as number;
} else { } else {
const quotes = await this.dataProviderService.getQuotes({ const quotes = await this.dataProviderService.getQuotes({
items: [dataGatheringItem] items: [dataGatheringItem]

Loading…
Cancel
Save