Browse Source
Task/improve type safety of key in PropertyService (#7219)
Improve type safety
pull/7239/head
Akash Negi
1 week ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with
21 additions and
5 deletions
-
apps/api/src/app/admin/admin.service.ts
-
apps/api/src/services/property/property.service.ts
-
libs/common/src/lib/types/index.ts
-
libs/common/src/lib/types/property-key.type.ts
|
|
|
@ -21,6 +21,7 @@ import { |
|
|
|
AdminUsersResponse, |
|
|
|
AssetProfileIdentifier |
|
|
|
} from '@ghostfolio/common/interfaces'; |
|
|
|
import { PropertyKey } from '@ghostfolio/common/types'; |
|
|
|
|
|
|
|
import { |
|
|
|
BadRequestException, |
|
|
|
@ -347,9 +348,14 @@ export class AdminService { |
|
|
|
let response: Property; |
|
|
|
|
|
|
|
if (value) { |
|
|
|
response = await this.propertyService.put({ key, value }); |
|
|
|
response = await this.propertyService.put({ |
|
|
|
value, |
|
|
|
key: key as PropertyKey |
|
|
|
}); |
|
|
|
} else { |
|
|
|
response = await this.propertyService.delete({ key }); |
|
|
|
response = await this.propertyService.delete({ |
|
|
|
key: key as PropertyKey |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (key === PROPERTY_IS_READ_ONLY_MODE && value === 'true') { |
|
|
|
|
|
|
|
@ -3,6 +3,7 @@ import { |
|
|
|
PROPERTY_CURRENCIES, |
|
|
|
PROPERTY_IS_USER_SIGNUP_ENABLED |
|
|
|
} from '@ghostfolio/common/config'; |
|
|
|
import { PropertyKey } from '@ghostfolio/common/types'; |
|
|
|
|
|
|
|
import { Injectable } from '@nestjs/common'; |
|
|
|
|
|
|
|
@ -12,7 +13,7 @@ import { PropertyValue } from './interfaces/interfaces'; |
|
|
|
export class PropertyService { |
|
|
|
public constructor(private readonly prismaService: PrismaService) {} |
|
|
|
|
|
|
|
public async delete({ key }: { key: string }) { |
|
|
|
public async delete({ key }: { key: PropertyKey }) { |
|
|
|
return this.prismaService.property.delete({ |
|
|
|
where: { key } |
|
|
|
}); |
|
|
|
@ -40,7 +41,7 @@ export class PropertyService { |
|
|
|
return response; |
|
|
|
} |
|
|
|
|
|
|
|
public async getByKey<TValue extends PropertyValue>(aKey: string) { |
|
|
|
public async getByKey<TValue extends PropertyValue>(aKey: PropertyKey) { |
|
|
|
const properties = await this.get(); |
|
|
|
return properties[aKey] as TValue; |
|
|
|
} |
|
|
|
@ -51,7 +52,7 @@ export class PropertyService { |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
public async put({ key, value }: { key: string; value: string }) { |
|
|
|
public async put({ key, value }: { key: PropertyKey; value: string }) { |
|
|
|
return this.prismaService.property.upsert({ |
|
|
|
create: { key, value }, |
|
|
|
update: { value }, |
|
|
|
|
|
|
|
@ -18,6 +18,7 @@ import type { Market } from './market.type'; |
|
|
|
import type { OrderWithAccount } from './order-with-account.type'; |
|
|
|
import type { ProductCategory } from './product-category.type'; |
|
|
|
import type { ProductPlatform } from './product-platform.type'; |
|
|
|
import type { PropertyKey } from './property-key.type'; |
|
|
|
import type { RequestWithUser } from './request-with-user.type'; |
|
|
|
import type { SectorName } from './sector-name.type'; |
|
|
|
import type { SubscriptionOfferKey } from './subscription-offer-key.type'; |
|
|
|
@ -45,6 +46,7 @@ export type { |
|
|
|
OrderWithAccount, |
|
|
|
ProductCategory, |
|
|
|
ProductPlatform, |
|
|
|
PropertyKey, |
|
|
|
RequestWithUser, |
|
|
|
SectorName, |
|
|
|
SubscriptionOfferKey, |
|
|
|
|
|
|
|
@ -0,0 +1,7 @@ |
|
|
|
import type * as config from '../config'; |
|
|
|
|
|
|
|
export type PropertyKey = { |
|
|
|
[Key in keyof typeof config]: Key extends `PROPERTY_${string}` |
|
|
|
? (typeof config)[Key] |
|
|
|
: never; |
|
|
|
}[keyof typeof config]; |