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
parent
commit
5ad9e06d17
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      apps/api/src/app/admin/admin.service.ts
  2. 7
      apps/api/src/services/property/property.service.ts
  3. 2
      libs/common/src/lib/types/index.ts
  4. 7
      libs/common/src/lib/types/property-key.type.ts

10
apps/api/src/app/admin/admin.service.ts

@ -21,6 +21,7 @@ import {
AdminUsersResponse, AdminUsersResponse,
AssetProfileIdentifier AssetProfileIdentifier
} from '@ghostfolio/common/interfaces'; } from '@ghostfolio/common/interfaces';
import { PropertyKey } from '@ghostfolio/common/types';
import { import {
BadRequestException, BadRequestException,
@ -347,9 +348,14 @@ export class AdminService {
let response: Property; let response: Property;
if (value) { if (value) {
response = await this.propertyService.put({ key, value }); response = await this.propertyService.put({
value,
key: key as PropertyKey
});
} else { } 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') { if (key === PROPERTY_IS_READ_ONLY_MODE && value === 'true') {

7
apps/api/src/services/property/property.service.ts

@ -3,6 +3,7 @@ import {
PROPERTY_CURRENCIES, PROPERTY_CURRENCIES,
PROPERTY_IS_USER_SIGNUP_ENABLED PROPERTY_IS_USER_SIGNUP_ENABLED
} from '@ghostfolio/common/config'; } from '@ghostfolio/common/config';
import { PropertyKey } from '@ghostfolio/common/types';
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
@ -12,7 +13,7 @@ import { PropertyValue } from './interfaces/interfaces';
export class PropertyService { export class PropertyService {
public constructor(private readonly prismaService: PrismaService) {} public constructor(private readonly prismaService: PrismaService) {}
public async delete({ key }: { key: string }) { public async delete({ key }: { key: PropertyKey }) {
return this.prismaService.property.delete({ return this.prismaService.property.delete({
where: { key } where: { key }
}); });
@ -40,7 +41,7 @@ export class PropertyService {
return response; return response;
} }
public async getByKey<TValue extends PropertyValue>(aKey: string) { public async getByKey<TValue extends PropertyValue>(aKey: PropertyKey) {
const properties = await this.get(); const properties = await this.get();
return properties[aKey] as TValue; 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({ return this.prismaService.property.upsert({
create: { key, value }, create: { key, value },
update: { value }, update: { value },

2
libs/common/src/lib/types/index.ts

@ -18,6 +18,7 @@ import type { Market } from './market.type';
import type { OrderWithAccount } from './order-with-account.type'; import type { OrderWithAccount } from './order-with-account.type';
import type { ProductCategory } from './product-category.type'; import type { ProductCategory } from './product-category.type';
import type { ProductPlatform } from './product-platform.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 { RequestWithUser } from './request-with-user.type';
import type { SectorName } from './sector-name.type'; import type { SectorName } from './sector-name.type';
import type { SubscriptionOfferKey } from './subscription-offer-key.type'; import type { SubscriptionOfferKey } from './subscription-offer-key.type';
@ -45,6 +46,7 @@ export type {
OrderWithAccount, OrderWithAccount,
ProductCategory, ProductCategory,
ProductPlatform, ProductPlatform,
PropertyKey,
RequestWithUser, RequestWithUser,
SectorName, SectorName,
SubscriptionOfferKey, SubscriptionOfferKey,

7
libs/common/src/lib/types/property-key.type.ts

@ -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];
Loading…
Cancel
Save