Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

Task/improve type safety of key in PropertyService (#7219)

Akash Negi5ad9e06d172f
diff --git a/apps/api/src/app/admin/admin.service.ts b/apps/api/src/app/admin/admin.service.ts
index e50c0c77fc..e6f5fd7009 100644
--- a/apps/api/src/app/admin/admin.service.ts
+++ b/apps/api/src/app/admin/admin.service.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') {
diff --git a/apps/api/src/services/property/property.service.ts b/apps/api/src/services/property/property.service.ts
index 212635f498..80643482fa 100644
--- a/apps/api/src/services/property/property.service.ts
+++ b/apps/api/src/services/property/property.service.ts
@@ -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 },
diff --git a/libs/common/src/lib/types/index.ts b/libs/common/src/lib/types/index.ts
index aefaf3e111..aa6893bc66 100644
--- a/libs/common/src/lib/types/index.ts
+++ b/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 { 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,
diff --git a/libs/common/src/lib/types/property-key.type.ts b/libs/common/src/lib/types/property-key.type.ts
new file mode 100644
index 0000000000..6c89a5054c
--- /dev/null
+++ b/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];