Browse Source

fix(core): Ensure deleted entities in events include ids

Michael Bromley 3 years ago
parent
commit
265bb15ead

+ 2 - 1
packages/core/src/service/services/channel.service.ts

@@ -263,12 +263,13 @@ export class ChannelService {
 
 
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         const channel = await this.connection.getEntityOrThrow(ctx, Channel, id);
         const channel = await this.connection.getEntityOrThrow(ctx, Channel, id);
+        const deletedChannel = new Channel(channel);
         await this.connection.getRepository(ctx, Session).delete({ activeChannelId: id });
         await this.connection.getRepository(ctx, Session).delete({ activeChannelId: id });
         await this.connection.getRepository(ctx, Channel).delete(id);
         await this.connection.getRepository(ctx, Channel).delete(id);
         await this.connection.getRepository(ctx, ProductVariantPrice).delete({
         await this.connection.getRepository(ctx, ProductVariantPrice).delete({
             channelId: id,
             channelId: id,
         });
         });
-        this.eventBus.publish(new ChannelEvent(ctx, channel, 'deleted', id));
+        this.eventBus.publish(new ChannelEvent(ctx, deletedChannel, 'deleted', id));
 
 
         return {
         return {
             result: DeletionResult.DELETED,
             result: DeletionResult.DELETED,

+ 4 - 2
packages/core/src/service/services/collection.service.ts

@@ -494,13 +494,15 @@ export class CollectionService implements OnModuleInit {
         const collection = await this.connection.getEntityOrThrow(ctx, Collection, id, {
         const collection = await this.connection.getEntityOrThrow(ctx, Collection, id, {
             channelId: ctx.channelId,
             channelId: ctx.channelId,
         });
         });
+        const deletedCollection = new Collection(collection);
         const descendants = await this.getDescendants(ctx, collection.id);
         const descendants = await this.getDescendants(ctx, collection.id);
         for (const coll of [...descendants.reverse(), collection]) {
         for (const coll of [...descendants.reverse(), collection]) {
             const affectedVariantIds = await this.getCollectionProductVariantIds(coll);
             const affectedVariantIds = await this.getCollectionProductVariantIds(coll);
+            const deletedColl = new Collection(coll);
             await this.connection.getRepository(ctx, Collection).remove(coll);
             await this.connection.getRepository(ctx, Collection).remove(coll);
-            this.eventBus.publish(new CollectionModificationEvent(ctx, coll, affectedVariantIds));
+            this.eventBus.publish(new CollectionModificationEvent(ctx, deletedColl, affectedVariantIds));
         }
         }
-        this.eventBus.publish(new CollectionEvent(ctx, collection, 'deleted', id));
+        this.eventBus.publish(new CollectionEvent(ctx, deletedCollection, 'deleted', id));
         return {
         return {
             result: DeletionResult.DELETED,
             result: DeletionResult.DELETED,
         };
         };

+ 2 - 1
packages/core/src/service/services/country.service.ts

@@ -130,8 +130,9 @@ export class CountryService {
                 message: ctx.translate('message.country-used-in-addresses', { count: addressesUsingCountry }),
                 message: ctx.translate('message.country-used-in-addresses', { count: addressesUsingCountry }),
             };
             };
         } else {
         } else {
+            const deletedCountry = new Country(country);
             await this.connection.getRepository(ctx, Country).remove(country);
             await this.connection.getRepository(ctx, Country).remove(country);
-            this.eventBus.publish(new CountryEvent(ctx, country, 'deleted', id));
+            this.eventBus.publish(new CountryEvent(ctx, deletedCountry, 'deleted', id));
             return {
             return {
                 result: DeletionResult.DELETED,
                 result: DeletionResult.DELETED,
                 message: '',
                 message: '',

+ 2 - 1
packages/core/src/service/services/customer-group.service.ts

@@ -125,8 +125,9 @@ export class CustomerGroupService {
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         const group = await this.connection.getEntityOrThrow(ctx, CustomerGroup, id);
         const group = await this.connection.getEntityOrThrow(ctx, CustomerGroup, id);
         try {
         try {
+            const deletedGroup = new CustomerGroup(group);
             await this.connection.getRepository(ctx, CustomerGroup).remove(group);
             await this.connection.getRepository(ctx, CustomerGroup).remove(group);
-            this.eventBus.publish(new CustomerGroupEntityEvent(ctx, group, 'deleted', id));
+            this.eventBus.publish(new CustomerGroupEntityEvent(ctx, deletedGroup, 'deleted', id));
             return {
             return {
                 result: DeletionResult.DELETED,
                 result: DeletionResult.DELETED,
             };
             };

+ 2 - 1
packages/core/src/service/services/customer.service.ts

@@ -771,9 +771,10 @@ export class CustomerService {
                 address: addressToLine(address),
                 address: addressToLine(address),
             },
             },
         });
         });
+        const deletedAddress = new Address(address);
         await this.connection.getRepository(ctx, Address).remove(address);
         await this.connection.getRepository(ctx, Address).remove(address);
         address.customer = customer;
         address.customer = customer;
-        this.eventBus.publish(new CustomerAddressEvent(ctx, address, 'deleted', id));
+        this.eventBus.publish(new CustomerAddressEvent(ctx, deletedAddress, 'deleted', id));
         return true;
         return true;
     }
     }
 
 

+ 8 - 5
packages/core/src/service/services/facet-value.service.ts

@@ -14,7 +14,7 @@ import { Translated } from '../../common/types/locale-types';
 import { assertFound } from '../../common/utils';
 import { assertFound } from '../../common/utils';
 import { ConfigService } from '../../config/config.service';
 import { ConfigService } from '../../config/config.service';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { TransactionalConnection } from '../../connection/transactional-connection';
-import { Product, ProductVariant } from '../../entity';
+import { Asset, Product, ProductVariant } from '../../entity';
 import { FacetValueTranslation } from '../../entity/facet-value/facet-value-translation.entity';
 import { FacetValueTranslation } from '../../entity/facet-value/facet-value-translation.entity';
 import { FacetValue } from '../../entity/facet-value/facet-value.entity';
 import { FacetValue } from '../../entity/facet-value/facet-value.entity';
 import { Facet } from '../../entity/facet/facet.entity';
 import { Facet } from '../../entity/facet/facet.entity';
@@ -148,15 +148,18 @@ export class FacetValueService {
         let message = '';
         let message = '';
         let result: DeletionResult;
         let result: DeletionResult;
 
 
+        const facetValue = await this.connection.getEntityOrThrow(ctx, FacetValue, id);
+        // Create a new facetValue so that the id is still available
+        // after deletion (the .remove() method sets it to undefined)
+        const deletedFacetValue = new FacetValue(facetValue);
+
         if (!isInUse) {
         if (!isInUse) {
-            const facetValue = await this.connection.getEntityOrThrow(ctx, FacetValue, id);
             await this.connection.getRepository(ctx, FacetValue).remove(facetValue);
             await this.connection.getRepository(ctx, FacetValue).remove(facetValue);
-            this.eventBus.publish(new FacetValueEvent(ctx, facetValue, 'deleted', id));
+            this.eventBus.publish(new FacetValueEvent(ctx, deletedFacetValue, 'deleted', id));
             result = DeletionResult.DELETED;
             result = DeletionResult.DELETED;
         } else if (force) {
         } else if (force) {
-            const facetValue = await this.connection.getEntityOrThrow(ctx, FacetValue, id);
             await this.connection.getRepository(ctx, FacetValue).remove(facetValue);
             await this.connection.getRepository(ctx, FacetValue).remove(facetValue);
-            this.eventBus.publish(new FacetValueEvent(ctx, facetValue, 'deleted', id));
+            this.eventBus.publish(new FacetValueEvent(ctx, deletedFacetValue, 'deleted', id));
             message = ctx.translate('message.facet-value-force-deleted', i18nVars);
             message = ctx.translate('message.facet-value-force-deleted', i18nVars);
             result = DeletionResult.DELETED;
             result = DeletionResult.DELETED;
         } else {
         } else {

+ 3 - 1
packages/core/src/service/services/facet.service.ts

@@ -201,15 +201,17 @@ export class FacetService {
         const i18nVars = { products: productCount, variants: variantCount, both, facetCode: facet.code };
         const i18nVars = { products: productCount, variants: variantCount, both, facetCode: facet.code };
         let message = '';
         let message = '';
         let result: DeletionResult;
         let result: DeletionResult;
+        const deletedFacet = new Facet(facet);
 
 
         if (!isInUse) {
         if (!isInUse) {
             await this.connection.getRepository(ctx, Facet).remove(facet);
             await this.connection.getRepository(ctx, Facet).remove(facet);
+            this.eventBus.publish(new FacetEvent(ctx, deletedFacet, 'deleted', id));
             result = DeletionResult.DELETED;
             result = DeletionResult.DELETED;
         } else if (force) {
         } else if (force) {
             await this.connection.getRepository(ctx, Facet).remove(facet);
             await this.connection.getRepository(ctx, Facet).remove(facet);
+            this.eventBus.publish(new FacetEvent(ctx, deletedFacet, 'deleted', id));
             message = ctx.translate('message.facet-force-deleted', i18nVars);
             message = ctx.translate('message.facet-force-deleted', i18nVars);
             result = DeletionResult.DELETED;
             result = DeletionResult.DELETED;
-            this.eventBus.publish(new FacetEvent(ctx, facet, 'deleted', id));
         } else {
         } else {
             message = ctx.translate('message.facet-used', i18nVars);
             message = ctx.translate('message.facet-used', i18nVars);
             result = DeletionResult.NOT_DELETED;
             result = DeletionResult.NOT_DELETED;

+ 4 - 2
packages/core/src/service/services/history.service.ts

@@ -262,8 +262,9 @@ export class HistoryService {
 
 
     async deleteOrderHistoryEntry(ctx: RequestContext, id: ID): Promise<void> {
     async deleteOrderHistoryEntry(ctx: RequestContext, id: ID): Promise<void> {
         const entry = await this.connection.getEntityOrThrow(ctx, OrderHistoryEntry, id);
         const entry = await this.connection.getEntityOrThrow(ctx, OrderHistoryEntry, id);
+        const deletedEntry = new OrderHistoryEntry(entry);
         await this.connection.getRepository(ctx, OrderHistoryEntry).remove(entry);
         await this.connection.getRepository(ctx, OrderHistoryEntry).remove(entry);
-        this.eventBus.publish(new HistoryEntryEvent(ctx, entry, 'deleted', 'order', id));
+        this.eventBus.publish(new HistoryEntryEvent(ctx, deletedEntry, 'deleted', 'order', id));
     }
     }
 
 
     async updateCustomerHistoryEntry<T extends keyof CustomerHistoryEntryData>(
     async updateCustomerHistoryEntry<T extends keyof CustomerHistoryEntryData>(
@@ -288,8 +289,9 @@ export class HistoryService {
 
 
     async deleteCustomerHistoryEntry(ctx: RequestContext, id: ID): Promise<void> {
     async deleteCustomerHistoryEntry(ctx: RequestContext, id: ID): Promise<void> {
         const entry = await this.connection.getEntityOrThrow(ctx, CustomerHistoryEntry, id);
         const entry = await this.connection.getEntityOrThrow(ctx, CustomerHistoryEntry, id);
+        const deletedEntry = new CustomerHistoryEntry(entry);
         await this.connection.getRepository(ctx, CustomerHistoryEntry).remove(entry);
         await this.connection.getRepository(ctx, CustomerHistoryEntry).remove(entry);
-        this.eventBus.publish(new HistoryEntryEvent(ctx, entry, 'deleted', 'customer', id));
+        this.eventBus.publish(new HistoryEntryEvent(ctx, deletedEntry, 'deleted', 'customer', id));
     }
     }
 
 
     private async getAdministratorFromContext(ctx: RequestContext): Promise<Administrator | undefined> {
     private async getAdministratorFromContext(ctx: RequestContext): Promise<Administrator | undefined> {

+ 4 - 1
packages/core/src/service/services/payment-method.service.ts

@@ -137,8 +137,11 @@ export class PaymentMethodService {
                 return { result, message };
                 return { result, message };
             }
             }
             try {
             try {
+                const deletedPaymentMethod = new PaymentMethod(paymentMethod);
                 await this.connection.getRepository(ctx, PaymentMethod).remove(paymentMethod);
                 await this.connection.getRepository(ctx, PaymentMethod).remove(paymentMethod);
-                this.eventBus.publish(new PaymentMethodEvent(ctx, paymentMethod, 'deleted', paymentMethodId));
+                this.eventBus.publish(
+                    new PaymentMethodEvent(ctx, deletedPaymentMethod, 'deleted', paymentMethodId),
+                );
                 return {
                 return {
                     result: DeletionResult.DELETED,
                     result: DeletionResult.DELETED,
                 };
                 };

+ 2 - 1
packages/core/src/service/services/product-option-group.service.ts

@@ -133,6 +133,7 @@ export class ProductOptionGroupService {
         const optionGroup = await this.connection.getEntityOrThrow(ctx, ProductOptionGroup, id, {
         const optionGroup = await this.connection.getEntityOrThrow(ctx, ProductOptionGroup, id, {
             relations: ['options', 'product'],
             relations: ['options', 'product'],
         });
         });
+        const deletedOptionGroup = new ProductOptionGroup(optionGroup);
         const inUseByActiveProducts = await this.isInUseByOtherProducts(ctx, optionGroup, productId);
         const inUseByActiveProducts = await this.isInUseByOtherProducts(ctx, optionGroup, productId);
         if (0 < inUseByActiveProducts) {
         if (0 < inUseByActiveProducts) {
             return {
             return {
@@ -180,7 +181,7 @@ export class ProductOptionGroupService {
                 Logger.error(e.message, undefined, e.stack);
                 Logger.error(e.message, undefined, e.stack);
             }
             }
         }
         }
-        this.eventBus.publish(new ProductOptionGroupEvent(ctx, optionGroup, 'deleted', id));
+        this.eventBus.publish(new ProductOptionGroupEvent(ctx, deletedOptionGroup, 'deleted', id));
         return {
         return {
             result: DeletionResult.DELETED,
             result: DeletionResult.DELETED,
         };
         };

+ 2 - 1
packages/core/src/service/services/product-option.service.ts

@@ -106,6 +106,7 @@ export class ProductOptionService {
      */
      */
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         const productOption = await this.connection.getEntityOrThrow(ctx, ProductOption, id);
         const productOption = await this.connection.getEntityOrThrow(ctx, ProductOption, id);
+        const deletedProductOption = new ProductOption(productOption);
         const inUseByActiveVariants = await this.isInUse(ctx, productOption, 'active');
         const inUseByActiveVariants = await this.isInUse(ctx, productOption, 'active');
         if (0 < inUseByActiveVariants) {
         if (0 < inUseByActiveVariants) {
             return {
             return {
@@ -135,7 +136,7 @@ export class ProductOptionService {
                 Logger.error(e.message, undefined, e.stack);
                 Logger.error(e.message, undefined, e.stack);
             }
             }
         }
         }
-        this.eventBus.publish(new ProductOptionEvent(ctx, productOption, 'deleted', id));
+        this.eventBus.publish(new ProductOptionEvent(ctx, deletedProductOption, 'deleted', id));
         return {
         return {
             result: DeletionResult.DELETED,
             result: DeletionResult.DELETED,
         };
         };

+ 2 - 1
packages/core/src/service/services/role.service.ts

@@ -205,8 +205,9 @@ export class RoleService {
         if (role.code === SUPER_ADMIN_ROLE_CODE || role.code === CUSTOMER_ROLE_CODE) {
         if (role.code === SUPER_ADMIN_ROLE_CODE || role.code === CUSTOMER_ROLE_CODE) {
             throw new InternalServerError(`error.cannot-delete-role`, { roleCode: role.code });
             throw new InternalServerError(`error.cannot-delete-role`, { roleCode: role.code });
         }
         }
+        const deletedRole = new Role(role);
         await this.connection.getRepository(ctx, Role).remove(role);
         await this.connection.getRepository(ctx, Role).remove(role);
-        this.eventBus.publish(new RoleEvent(ctx, role, 'deleted', id));
+        this.eventBus.publish(new RoleEvent(ctx, deletedRole, 'deleted', id));
         return {
         return {
             result: DeletionResult.DELETED,
             result: DeletionResult.DELETED,
         };
         };

+ 2 - 1
packages/core/src/service/services/tax-category.service.ts

@@ -81,8 +81,9 @@ export class TaxCategoryService {
         }
         }
 
 
         try {
         try {
+            const deletedTaxCategory = new TaxCategory(taxCategory);
             await this.connection.getRepository(ctx, TaxCategory).remove(taxCategory);
             await this.connection.getRepository(ctx, TaxCategory).remove(taxCategory);
-            this.eventBus.publish(new TaxCategoryEvent(ctx, taxCategory, 'deleted', id));
+            this.eventBus.publish(new TaxCategoryEvent(ctx, deletedTaxCategory, 'deleted', id));
             return {
             return {
                 result: DeletionResult.DELETED,
                 result: DeletionResult.DELETED,
             };
             };

+ 2 - 1
packages/core/src/service/services/tax-rate.service.ts

@@ -128,9 +128,10 @@ export class TaxRateService {
 
 
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         const taxRate = await this.connection.getEntityOrThrow(ctx, TaxRate, id);
         const taxRate = await this.connection.getEntityOrThrow(ctx, TaxRate, id);
+        const deletedTaxRate = new TaxRate(taxRate);
         try {
         try {
             await this.connection.getRepository(ctx, TaxRate).remove(taxRate);
             await this.connection.getRepository(ctx, TaxRate).remove(taxRate);
-            this.eventBus.publish(new TaxRateEvent(ctx, taxRate, 'deleted', id));
+            this.eventBus.publish(new TaxRateEvent(ctx, deletedTaxRate, 'deleted', id));
             return {
             return {
                 result: DeletionResult.DELETED,
                 result: DeletionResult.DELETED,
             };
             };

+ 2 - 2
packages/core/src/service/services/zone.service.ts

@@ -113,7 +113,7 @@ export class ZoneService {
 
 
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
     async delete(ctx: RequestContext, id: ID): Promise<DeletionResponse> {
         const zone = await this.connection.getEntityOrThrow(ctx, Zone, id);
         const zone = await this.connection.getEntityOrThrow(ctx, Zone, id);
-
+        const deletedZone = new Zone(zone);
         const channelsUsingZone = await this.connection
         const channelsUsingZone = await this.connection
             .getRepository(ctx, Channel)
             .getRepository(ctx, Channel)
             .createQueryBuilder('channel')
             .createQueryBuilder('channel')
@@ -146,7 +146,7 @@ export class ZoneService {
         } else {
         } else {
             await this.connection.getRepository(ctx, Zone).remove(zone);
             await this.connection.getRepository(ctx, Zone).remove(zone);
             await this.zones.refresh(ctx);
             await this.zones.refresh(ctx);
-            this.eventBus.publish(new ZoneEvent(ctx, zone, 'deleted', id));
+            this.eventBus.publish(new ZoneEvent(ctx, deletedZone, 'deleted', id));
             return {
             return {
                 result: DeletionResult.DELETED,
                 result: DeletionResult.DELETED,
                 message: '',
                 message: '',