Browse Source

fix(client): resolve type errors in component

pull/6722/head
KenTandrian 1 month ago
parent
commit
ef0ff3dbd7
  1. 139
      apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts

139
apps/client/src/app/components/admin-market-data/asset-profile-dialog/asset-profile-dialog.component.ts

@ -87,6 +87,7 @@ import {
readerOutline, readerOutline,
serverOutline serverOutline
} from 'ionicons/icons'; } from 'ionicons/icons';
import { isBoolean } from 'lodash';
import ms from 'ms'; import ms from 'ms';
import { EMPTY } from 'rxjs'; import { EMPTY } from 'rxjs';
import { catchError } from 'rxjs/operators'; import { catchError } from 'rxjs/operators';
@ -146,8 +147,8 @@ export class GfAssetProfileDialogComponent implements OnInit {
public assetProfile: AdminMarketDataDetails['assetProfile']; public assetProfile: AdminMarketDataDetails['assetProfile'];
public assetProfileForm = this.formBuilder.group({ public assetProfileForm = this.formBuilder.group({
assetClass: new FormControl<AssetClass>(undefined), assetClass: new FormControl<AssetClass | null>(null),
assetSubClass: new FormControl<AssetSubClass>(undefined), assetSubClass: new FormControl<AssetSubClass | null>(null),
comment: '', comment: '',
countries: ['', jsonValidator()], countries: ['', jsonValidator()],
currency: '', currency: '',
@ -156,11 +157,15 @@ export class GfAssetProfileDialogComponent implements OnInit {
}), }),
isActive: [true], isActive: [true],
name: ['', Validators.required], name: ['', Validators.required],
scraperConfiguration: this.formBuilder.group({ scraperConfiguration: this.formBuilder.group<
defaultMarketPrice: null, Omit<ScraperConfiguration, 'headers'> & {
headers: [JSON.stringify({}), jsonValidator()], headers: FormControl<string | null>;
}
>({
defaultMarketPrice: undefined,
headers: new FormControl(JSON.stringify({}), jsonValidator()),
locale: '', locale: '',
mode: '', mode: 'lazy',
selector: '', selector: '',
url: '' url: ''
}), }),
@ -171,10 +176,9 @@ export class GfAssetProfileDialogComponent implements OnInit {
public assetProfileIdentifierForm = this.formBuilder.group( public assetProfileIdentifierForm = this.formBuilder.group(
{ {
assetProfileIdentifier: new FormControl<AssetProfileIdentifier>( assetProfileIdentifier: new FormControl<
{ symbol: null, dataSource: null }, AssetProfileIdentifier | { symbol: null; dataSource: null }
[Validators.required] >({ symbol: null, dataSource: null }, [Validators.required])
)
}, },
{ {
validators: (control) => { validators: (control) => {
@ -192,7 +196,7 @@ export class GfAssetProfileDialogComponent implements OnInit {
public currencies: string[] = []; public currencies: string[] = [];
public dateRangeOptions = [ public readonly dateRangeOptions = [
{ {
label: $localize`Current week` + ' (' + $localize`WTD` + ')', label: $localize`Current week` + ' (' + $localize`WTD` + ')',
value: 'wtd' value: 'wtd'
@ -222,7 +226,7 @@ export class GfAssetProfileDialogComponent implements OnInit {
public isBenchmark = false; public isBenchmark = false;
public isDataGatheringEnabled: boolean; public isDataGatheringEnabled: boolean;
public isEditAssetProfileIdentifierMode = false; public isEditAssetProfileIdentifierMode = false;
public isUUID = isUUID; public readonly isUUID = isUUID;
public marketDataItems: MarketData[] = []; public marketDataItems: MarketData[] = [];
public modeValues = [ public modeValues = [
@ -278,7 +282,7 @@ export class GfAssetProfileDialogComponent implements OnInit {
} }
public initialize() { public initialize() {
this.historicalDataItems = undefined; this.historicalDataItems = [];
this.adminService this.adminService
.fetchAdminData() .fetchAdminData()
@ -300,8 +304,12 @@ export class GfAssetProfileDialogComponent implements OnInit {
this.assetProfileForm this.assetProfileForm
.get('assetClass') .get('assetClass')
.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)) ?.valueChanges.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((assetClass) => { .subscribe((assetClass) => {
if (!assetClass) {
return;
}
const assetSubClasses = ASSET_CLASS_MAPPING.get(assetClass) ?? []; const assetSubClasses = ASSET_CLASS_MAPPING.get(assetClass) ?? [];
this.assetSubClassOptions = assetSubClasses this.assetSubClassOptions = assetSubClasses
@ -313,7 +321,7 @@ export class GfAssetProfileDialogComponent implements OnInit {
}) })
.sort((a, b) => a.label.localeCompare(b.label)); .sort((a, b) => a.label.localeCompare(b.label));
this.assetProfileForm.get('assetSubClass').setValue(null); this.assetProfileForm.get('assetSubClass')?.setValue(null);
this.changeDetectorRef.markForCheck(); this.changeDetectorRef.markForCheck();
}); });
@ -327,8 +335,10 @@ export class GfAssetProfileDialogComponent implements OnInit {
.subscribe(({ assetProfile, marketData }) => { .subscribe(({ assetProfile, marketData }) => {
this.assetProfile = assetProfile; this.assetProfile = assetProfile;
this.assetClassLabel = translate(this.assetProfile?.assetClass); this.assetClassLabel = translate(this.assetProfile?.assetClass ?? '');
this.assetSubClassLabel = translate(this.assetProfile?.assetSubClass); this.assetSubClassLabel = translate(
this.assetProfile?.assetSubClass ?? ''
);
this.canEditAssetProfile = !isCurrency( this.canEditAssetProfile = !isCurrency(
getCurrencyFromSymbol(this.data.symbol) getCurrencyFromSymbol(this.data.symbol)
@ -350,7 +360,10 @@ export class GfAssetProfileDialogComponent implements OnInit {
this.marketDataItems = marketData; this.marketDataItems = marketData;
this.sectors = {}; this.sectors = {};
if (this.assetProfile?.countries?.length > 0) { if (
this.assetProfile?.countries &&
this.assetProfile.countries.length > 0
) {
for (const { code, name, weight } of this.assetProfile.countries) { for (const { code, name, weight } of this.assetProfile.countries) {
this.countries[code] = { this.countries[code] = {
name, name,
@ -359,7 +372,10 @@ export class GfAssetProfileDialogComponent implements OnInit {
} }
} }
if (this.assetProfile?.sectors?.length > 0) { if (
this.assetProfile?.sectors &&
this.assetProfile.sectors.length > 0
) {
for (const { name, weight } of this.assetProfile.sectors) { for (const { name, weight } of this.assetProfile.sectors) {
this.sectors[name] = { this.sectors[name] = {
name, name,
@ -377,12 +393,14 @@ export class GfAssetProfileDialogComponent implements OnInit {
return { code, weight }; return { code, weight };
}) ?? [] }) ?? []
), ),
currency: this.assetProfile?.currency, currency: this.assetProfile?.currency ?? null,
historicalData: { historicalData: {
csvString: GfAssetProfileDialogComponent.HISTORICAL_DATA_TEMPLATE csvString: GfAssetProfileDialogComponent.HISTORICAL_DATA_TEMPLATE
}, },
isActive: this.assetProfile?.isActive, isActive: isBoolean(this.assetProfile?.isActive)
name: this.assetProfile.name ?? this.assetProfile.symbol, ? this.assetProfile.isActive
: null,
name: this.assetProfile.name ?? this.assetProfile.symbol ?? null,
scraperConfiguration: { scraperConfiguration: {
defaultMarketPrice: defaultMarketPrice:
this.assetProfile?.scraperConfiguration?.defaultMarketPrice ?? this.assetProfile?.scraperConfiguration?.defaultMarketPrice ??
@ -480,7 +498,7 @@ export class GfAssetProfileDialogComponent implements OnInit {
public async onSubmitAssetProfileForm() { public async onSubmitAssetProfileForm() {
let countries = []; let countries = [];
let scraperConfiguration: ScraperConfiguration = { let scraperConfiguration: Prisma.InputJsonObject | undefined = {
selector: '', selector: '',
url: '' url: ''
}; };
@ -488,34 +506,37 @@ export class GfAssetProfileDialogComponent implements OnInit {
let symbolMapping = {}; let symbolMapping = {};
try { try {
countries = JSON.parse(this.assetProfileForm.get('countries').value); countries = JSON.parse(
this.assetProfileForm.get('countries')?.value ?? '[]'
);
} catch {} } catch {}
try { try {
scraperConfiguration = { scraperConfiguration = {
defaultMarketPrice: defaultMarketPrice:
(this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'defaultMarketPrice' 'defaultMarketPrice'
].value as number) || undefined, ]?.value ?? undefined,
headers: JSON.parse( headers: JSON.parse(
this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'headers' 'headers'
].value ].value ?? '{}'
), ),
locale: locale:
this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'locale' 'locale'
].value || undefined, ]?.value ?? undefined,
mode: this.assetProfileForm.controls['scraperConfiguration'].controls[ mode:
'mode' this.assetProfileForm.controls['scraperConfiguration'].controls[
].value as ScraperConfiguration['mode'], 'mode'
]?.value ?? undefined,
selector: selector:
this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'selector' 'selector'
].value, ].value ?? '',
url: this.assetProfileForm.controls['scraperConfiguration'].controls[ url:
'url' this.assetProfileForm.controls['scraperConfiguration'].controls['url']
].value .value ?? ''
}; };
if (!scraperConfiguration.selector || !scraperConfiguration.url) { if (!scraperConfiguration.selector || !scraperConfiguration.url) {
@ -536,28 +557,29 @@ export class GfAssetProfileDialogComponent implements OnInit {
} }
try { try {
sectors = JSON.parse(this.assetProfileForm.get('sectors').value); sectors = JSON.parse(this.assetProfileForm.get('sectors')?.value ?? '[]');
} catch {} } catch {}
try { try {
symbolMapping = JSON.parse( symbolMapping = JSON.parse(
this.assetProfileForm.get('symbolMapping').value this.assetProfileForm.get('symbolMapping')?.value ?? '{}'
); );
} catch {} } catch {}
const isActive = this.assetProfileForm.get('isActive')?.value;
const assetProfile: UpdateAssetProfileDto = { const assetProfile: UpdateAssetProfileDto = {
countries, countries,
scraperConfiguration,
sectors, sectors,
symbolMapping, symbolMapping,
assetClass: this.assetProfileForm.get('assetClass').value, assetClass: this.assetProfileForm.get('assetClass')?.value ?? undefined,
assetSubClass: this.assetProfileForm.get('assetSubClass').value, assetSubClass:
comment: this.assetProfileForm.get('comment').value || null, this.assetProfileForm.get('assetSubClass')?.value ?? undefined,
currency: this.assetProfileForm.get('currency').value, comment: this.assetProfileForm.get('comment')?.value ?? undefined,
isActive: this.assetProfileForm.get('isActive').value, currency: this.assetProfileForm.get('currency')?.value ?? undefined,
name: this.assetProfileForm.get('name').value, isActive: isBoolean(isActive) ? isActive : undefined,
scraperConfiguration: name: this.assetProfileForm.get('name')?.value ?? undefined,
scraperConfiguration as unknown as Prisma.InputJsonObject, url: this.assetProfileForm.get('url')?.value ?? undefined
url: this.assetProfileForm.get('url').value || null
}; };
try { try {
@ -617,9 +639,9 @@ export class GfAssetProfileDialogComponent implements OnInit {
public async onSubmitAssetProfileIdentifierForm() { public async onSubmitAssetProfileIdentifierForm() {
const assetProfileIdentifier: UpdateAssetProfileDto = { const assetProfileIdentifier: UpdateAssetProfileDto = {
dataSource: this.assetProfileIdentifierForm.get('assetProfileIdentifier') dataSource: this.assetProfileIdentifierForm.get('assetProfileIdentifier')
.value.dataSource, ?.value.dataSource,
symbol: this.assetProfileIdentifierForm.get('assetProfileIdentifier') symbol: this.assetProfileIdentifierForm.get('assetProfileIdentifier')
.value.symbol ?.value.symbol
}; };
try { try {
@ -681,21 +703,22 @@ export class GfAssetProfileDialogComponent implements OnInit {
.testMarketData({ .testMarketData({
dataSource: this.data.dataSource, dataSource: this.data.dataSource,
scraperConfiguration: { scraperConfiguration: {
defaultMarketPrice: this.assetProfileForm.controls[ defaultMarketPrice:
'scraperConfiguration' this.assetProfileForm.controls['scraperConfiguration'].controls[
].controls['defaultMarketPrice'].value as number, 'defaultMarketPrice'
]?.value,
headers: JSON.parse( headers: JSON.parse(
this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'headers' 'headers'
].value ]?.value ?? '{}'
), ),
locale: locale:
this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'locale' 'locale'
].value || undefined, ]?.value || undefined,
mode: this.assetProfileForm.controls['scraperConfiguration'].controls[ mode: this.assetProfileForm.controls['scraperConfiguration'].controls[
'mode' 'mode'
].value, ]?.value,
selector: selector:
this.assetProfileForm.controls['scraperConfiguration'].controls[ this.assetProfileForm.controls['scraperConfiguration'].controls[
'selector' 'selector'
@ -723,7 +746,7 @@ export class GfAssetProfileDialogComponent implements OnInit {
' ' + ' ' +
price + price +
' ' + ' ' +
this.assetProfileForm.get('currency').value this.assetProfileForm.get('currency')?.value
}); });
}); });
} }
@ -761,9 +784,11 @@ export class GfAssetProfileDialogComponent implements OnInit {
} }
} }
private isNewSymbolValid(control: AbstractControl): ValidationErrors { private isNewSymbolValid(
control: AbstractControl
): ValidationErrors | undefined {
const currentAssetProfileIdentifier: AssetProfileIdentifier | undefined = const currentAssetProfileIdentifier: AssetProfileIdentifier | undefined =
control.get('assetProfileIdentifier').value; control.get('assetProfileIdentifier')?.value;
if ( if (
currentAssetProfileIdentifier?.dataSource === this.data?.dataSource && currentAssetProfileIdentifier?.dataSource === this.data?.dataSource &&

Loading…
Cancel
Save