Browse Source

fix(core): Correctly invalidate Zone cache on Country changes

Michael Bromley 5 years ago
parent
commit
f4101b7a07

+ 17 - 17
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -3605,17 +3605,6 @@ export type GetChannelsQuery = { __typename?: 'Query' } & {
     channels: Array<{ __typename?: 'Channel' } & Pick<Channel, 'id' | 'code' | 'token'>>;
 };
 
-export type UpdateChannelMutationVariables = {
-    input: UpdateChannelInput;
-};
-
-export type UpdateChannelMutation = { __typename?: 'Mutation' } & {
-    updateChannel: { __typename?: 'Channel' } & Pick<
-        Channel,
-        'id' | 'code' | 'defaultLanguageCode' | 'currencyCode'
-    >;
-};
-
 export type DeleteChannelMutationVariables = {
     id: Scalars['ID'];
 };
@@ -4714,6 +4703,17 @@ export type DeleteAssetMutation = { __typename?: 'Mutation' } & {
     deleteAsset: { __typename?: 'DeletionResponse' } & Pick<DeletionResponse, 'result' | 'message'>;
 };
 
+export type UpdateChannelMutationVariables = {
+    input: UpdateChannelInput;
+};
+
+export type UpdateChannelMutation = { __typename?: 'Mutation' } & {
+    updateChannel: { __typename?: 'Channel' } & Pick<
+        Channel,
+        'id' | 'code' | 'defaultLanguageCode' | 'currencyCode'
+    >;
+};
+
 export type UpdateOptionGroupMutationVariables = {
     input: UpdateProductOptionGroupInput;
 };
@@ -5505,12 +5505,6 @@ export namespace GetChannels {
     export type Channels = NonNullable<GetChannelsQuery['channels'][0]>;
 }
 
-export namespace UpdateChannel {
-    export type Variables = UpdateChannelMutationVariables;
-    export type Mutation = UpdateChannelMutation;
-    export type UpdateChannel = UpdateChannelMutation['updateChannel'];
-}
-
 export namespace DeleteChannel {
     export type Variables = DeleteChannelMutationVariables;
     export type Mutation = DeleteChannelMutation;
@@ -6236,6 +6230,12 @@ export namespace DeleteAsset {
     export type DeleteAsset = DeleteAssetMutation['deleteAsset'];
 }
 
+export namespace UpdateChannel {
+    export type Variables = UpdateChannelMutationVariables;
+    export type Mutation = UpdateChannelMutation;
+    export type UpdateChannel = UpdateChannelMutation['updateChannel'];
+}
+
 export namespace UpdateOptionGroup {
     export type Variables = UpdateOptionGroupMutationVariables;
     export type Mutation = UpdateOptionGroupMutation;

+ 6 - 0
packages/core/src/service/services/country.service.ts

@@ -22,12 +22,15 @@ import { TranslatableSaver } from '../helpers/translatable-saver/translatable-sa
 import { getEntityOrThrow } from '../helpers/utils/get-entity-or-throw';
 import { translateDeep } from '../helpers/utils/translate-entity';
 
+import { ZoneService } from './zone.service';
+
 @Injectable()
 export class CountryService {
     constructor(
         @InjectConnection() private connection: Connection,
         private listQueryBuilder: ListQueryBuilder,
         private translatableSaver: TranslatableSaver,
+        private zoneService: ZoneService,
     ) {}
 
     findAll(
@@ -71,6 +74,7 @@ export class CountryService {
             entityType: Country,
             translationType: CountryTranslation,
         });
+        await this.zoneService.updateZonesCache();
         return assertFound(this.findOne(ctx, country.id));
     }
 
@@ -80,6 +84,7 @@ export class CountryService {
             entityType: Country,
             translationType: CountryTranslation,
         });
+        await this.zoneService.updateZonesCache();
         return assertFound(this.findOne(ctx, country.id));
     }
 
@@ -97,6 +102,7 @@ export class CountryService {
                 message: ctx.translate('message.country-used-in-addresses', { count: addressesUsingCountry }),
             };
         } else {
+            await this.zoneService.updateZonesCache();
             await this.connection.getRepository(Country).remove(country);
             return {
                 result: DeletionResult.DELETED,

+ 5 - 1
packages/core/src/service/services/zone.service.ts

@@ -139,7 +139,11 @@ export class ZoneService implements OnModuleInit {
         return this.connection.getRepository(Country).findByIds(ids);
     }
 
-    private async updateZonesCache() {
+    /**
+     * TODO: This is not good for multi-instance deployments. A better solution will
+     * need to be found without adversely affecting performance.
+     */
+    async updateZonesCache() {
         this.zones = await this.connection.getRepository(Zone).find({
             relations: ['members'],
         });