Browse Source

fix(core): Add property resolver for Collection.featuredAsset

Michael Bromley 6 years ago
parent
commit
cd367a7833

+ 2 - 1
packages/asset-server-plugin/src/plugin.ts

@@ -221,10 +221,11 @@ export class AssetServerPlugin implements VendurePlugin {
 
     private createAssetStorageStrategy() {
         const toAbsoluteUrlFn = (request: Request, identifier: string): string => {
+            const prefix = `${request.protocol}://${request.get('host')}/${this.options.route}/`;
             if (!identifier) {
                 return '';
             }
-            return `${request.protocol}://${request.get('host')}/${this.options.route}/${identifier}`;
+            return identifier.startsWith(prefix) ? identifier : `${prefix}${identifier}`;
         };
         return new LocalAssetStorageStrategy(this.options.assetUploadDir, toAbsoluteUrlFn);
     }

+ 2 - 1
packages/core/e2e/config/testing-asset-storage-strategy.ts

@@ -19,7 +19,8 @@ export class TestingAssetStorageStrategy implements AssetStorageStrategy {
     }
 
     toAbsoluteUrl(reqest: Request, identifier: string): string {
-        return `test-url/${identifier}`;
+        const prefix = `test-url/`;
+        return identifier.startsWith(prefix) ? identifier : `${prefix}${identifier}`;
     }
 
     writeFileFromBuffer(fileName: string, data: Buffer): Promise<string> {

+ 11 - 1
packages/core/src/api/resolvers/entity/collection-entity.resolver.ts

@@ -4,7 +4,8 @@ import { PaginatedList } from '@vendure/common/lib/shared-types';
 
 import { ListQueryOptions } from '../../../common/types/common-types';
 import { Translated } from '../../../common/types/locale-types';
-import { Collection, Product, ProductVariant } from '../../../entity';
+import { Asset, Collection, Product, ProductVariant } from '../../../entity';
+import { AssetService } from '../../../service/services/asset.service';
 import { CollectionService } from '../../../service/services/collection.service';
 import { ProductVariantService } from '../../../service/services/product-variant.service';
 import { ApiType } from '../../common/get-api-type';
@@ -17,6 +18,7 @@ export class CollectionEntityResolver {
     constructor(
         private productVariantService: ProductVariantService,
         private collectionService: CollectionService,
+        private assetService: AssetService,
     ) {}
 
     @ResolveProperty()
@@ -62,4 +64,12 @@ export class CollectionEntityResolver {
         }
         return this.collectionService.getChildren(ctx, collection.id) as any;
     }
+
+    @ResolveProperty()
+    async featuredAsset(@Ctx() ctx: RequestContext, @Parent() collection: Collection): Promise<Asset | undefined> {
+        if (collection.featuredAsset) {
+            return collection.featuredAsset;
+        }
+        return this.assetService.getFeaturedAsset(collection);
+    }
 }