Browse Source

Fix unused custom asset profiles created by activities import

pull/7673/head
Thomas Kaul 1 week ago
parent
commit
6959369899
  1. 45
      apps/api/src/app/import/import.service.ts
  2. 10
      apps/api/src/services/data-provider/data-provider.service.ts

45
apps/api/src/app/import/import.service.ts

@ -637,12 +637,33 @@ export class ImportService {
}); });
if (assetProfileToCreate) { if (assetProfileToCreate) {
// Create the new asset profile and its market data later, once it const assetProfileToCreateIdentifier =
// is known which activities are imported getAssetProfileIdentifier(assetProfileToCreate);
assetProfilesToCreate.push({
marketDataObjects, const duplicateAssetProfileToCreate = assetProfilesToCreate.find(
assetProfile: assetProfileToCreate ({ assetProfile }) => {
}); return (
getAssetProfileIdentifier(assetProfile) ===
assetProfileToCreateIdentifier
);
}
);
if (duplicateAssetProfileToCreate) {
// The import contains the same asset profile more than once,
// which would fail with a unique constraint violation. Keep the
// first asset profile and merge the market data into it.
duplicateAssetProfileToCreate.marketDataObjects.push(
...marketDataObjects
);
} else {
// Create the new asset profile and its market data later, once it
// is known which activities are imported
assetProfilesToCreate.push({
marketDataObjects,
assetProfile: assetProfileToCreate
});
}
} else { } else {
// Insert or update market data // Insert or update market data
await this.marketDataService.updateMany({ await this.marketDataService.updateMany({
@ -732,10 +753,10 @@ export class ImportService {
}) ?? { id: TAG_ID_DRAFT, name: 'DRAFT' }; }) ?? { id: TAG_ID_DRAFT, name: 'DRAFT' };
// Create the new asset profiles of the activities to import only, so that // Create the new asset profiles of the activities to import only, so that
// no unused asset profile remains, for example if an activity is a // no unused asset profile remains, for example if no activity refers to
// duplicate. An asset profile which is created before the validation of // the asset profile. An asset profile which is created before the
// the activities would stay behind, because the import is not rolled back // validation of the activities would stay behind, because the import is
// on an error. // not rolled back on an error.
if (!isDryRun) { if (!isDryRun) {
for (const { for (const {
assetProfile, assetProfile,
@ -744,9 +765,9 @@ export class ImportService {
activities: activitiesExtendedWithErrors, activities: activitiesExtendedWithErrors,
assetProfiles: assetProfilesToCreate assetProfiles: assetProfilesToCreate
})) { })) {
await this.symbolProfileService.add(assetProfile);
await this.marketDataService.updateMany({ data: marketDataObjects }); await this.marketDataService.updateMany({ data: marketDataObjects });
await this.symbolProfileService.add(assetProfile);
} }
} }

10
apps/api/src/services/data-provider/data-provider.service.ts

@ -284,19 +284,19 @@ export class DataProviderService implements OnModuleInit {
// A custom asset profile of the import is created after the // A custom asset profile of the import is created after the
// validation, thus the data provider cannot resolve it yet // validation, thus the data provider cannot resolve it yet
if ( if (
(dataSource === DataSource.MANUAL && (dataSource === DataSource.MANUAL && type === 'BUY') ||
(type === 'BUY' || Boolean(assetProfileInImport))) || Boolean(assetProfileInImport) ||
NON_INVESTMENT_ACTIVITY_TYPES.includes(type) NON_INVESTMENT_ACTIVITY_TYPES.includes(type)
) { ) {
assetProfiles[assetProfileIdentifier] = { assetProfiles[assetProfileIdentifier] = {
currency,
dataSource,
symbol,
...omit(assetProfileInImport ?? {}, [ ...omit(assetProfileInImport ?? {}, [
'dataSource', 'dataSource',
'marketData', 'marketData',
'symbol' 'symbol'
]), ]),
currency,
dataSource,
symbol,
name: assetProfileInImport?.name ?? symbol name: assetProfileInImport?.name ?? symbol
}; };

Loading…
Cancel
Save