Browse Source

fix(api): improve type safety in api key, impersonation, platform, and market data services

pull/8356/head
KenTandrian 4 weeks ago
parent
commit
a75da38596
  1. 6
      apps/api/src/services/api-key/api-key.service.ts
  2. 2
      apps/api/src/services/impersonation/impersonation.service.ts
  3. 4
      apps/api/src/services/market-data/market-data.service.ts
  4. 2
      apps/api/src/services/platform/platform.service.ts

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 apiKeyEntity = await this.prismaService.apiKey.findUnique({
include: { user: true }, include: { user: true },
where: { hashedKey } where: { hashedKey }
}); });
return user; return apiKeyEntity?.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;

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
}); });

Loading…
Cancel
Save