Browse Source

fix(core): Fix featuredAsset error when adding item to Order

Fixes #756
Michael Bromley 4 years ago
parent
commit
e635f252ed

+ 21 - 10
packages/core/src/service/services/asset.service.ts

@@ -24,6 +24,7 @@ import { RequestContext } from '../../api/common/request-context';
 import { isGraphQlErrorResult } from '../../common/error/error-result';
 import { ForbiddenError, InternalServerError } from '../../common/error/errors';
 import { MimeTypeError } from '../../common/error/generated-graphql-admin-errors';
+import { ChannelAware } from '../../common/types/common-types';
 import { getAssetType, idsAreEqual } from '../../common/utils';
 import { ConfigService } from '../../config/config.service';
 import { Logger } from '../../config/logger/vendure-logger';
@@ -117,16 +118,26 @@ export class AssetService {
         ctx: RequestContext,
         entity: T,
     ): Promise<Asset | undefined> {
-        const entityType: Type<EntityWithAssets> = Object.getPrototypeOf(entity).constructor;
-        const entityWithFeaturedAsset = await this.connection.findOneInChannel(
-            ctx,
-            entityType,
-            entity.id,
-            ctx.channelId,
-            {
-                relations: ['featuredAsset'],
-            },
-        );
+        const entityType: Type<T> = Object.getPrototypeOf(entity).constructor;
+        let entityWithFeaturedAsset: T | undefined;
+
+        if (this.channelService.isChannelAware(entity)) {
+            entityWithFeaturedAsset = await this.connection.findOneInChannel(
+                ctx,
+                entityType as Type<T & ChannelAware>,
+                entity.id,
+                ctx.channelId,
+                {
+                    relations: ['featuredAsset'],
+                },
+            );
+        } else {
+            entityWithFeaturedAsset = await this.connection
+                .getRepository(ctx, entityType)
+                .findOne(entity.id, {
+                    relations: ['featuredAsset'],
+                });
+        }
         return (entityWithFeaturedAsset && entityWithFeaturedAsset.featuredAsset) || undefined;
     }
 

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

@@ -10,7 +10,6 @@ import {
 } from '@vendure/common/lib/generated-types';
 import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
 import { ID, Type } from '@vendure/common/lib/shared-types';
-import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
 import { unique } from '@vendure/common/lib/unique';
 
 import { RequestContext } from '../../api/common/request-context';
@@ -213,6 +212,13 @@ export class ChannelService {
         };
     }
 
+    public isChannelAware(entity: VendureEntity): entity is VendureEntity & ChannelAware {
+        const entityType = Object.getPrototypeOf(entity).constructor;
+        return !!this.connection.rawConnection
+            .getMetadata(entityType)
+            .relations.find(r => r.type === Channel && r.propertyName === 'channels');
+    }
+
     /**
      * There must always be a default Channel. If none yet exists, this method creates one.
      * Also ensures the default Channel token matches the defaultChannelToken config setting.

+ 2 - 2
packages/core/src/service/transaction/transactional-connection.ts

@@ -141,7 +141,7 @@ export class TransactionalConnection {
             const { channelId, ...optionsWithoutChannelId } = options;
             entity = await this.findOneInChannel(
                 ctx,
-                entityType,
+                entityType as Type<T & ChannelAware>,
                 id,
                 options.channelId,
                 optionsWithoutChannelId,
@@ -163,7 +163,7 @@ export class TransactionalConnection {
      * Like the TypeOrm `Repository.findOne()` method, but limits the results to
      * the given Channel.
      */
-    findOneInChannel<T extends ChannelAware | VendureEntity>(
+    findOneInChannel<T extends ChannelAware & VendureEntity>(
         ctx: RequestContext,
         entity: Type<T>,
         id: ID,