Browse Source

fix(core): Correctly resolve activeCustomer order lines

Fixes #374, fixes #375
Michael Bromley 5 years ago
parent
commit
56449b8a90

+ 15 - 3
packages/core/src/api/resolvers/entity/order-line-entity.resolver.ts

@@ -2,14 +2,14 @@ import { Parent, ResolveField, Resolver } from '@nestjs/graphql';
 
 import { Translated } from '../../../common/types/locale-types';
 import { assertFound } from '../../../common/utils';
-import { OrderLine, ProductVariant } from '../../../entity';
-import { ProductVariantService } from '../../../service';
+import { Asset, OrderLine, ProductVariant } from '../../../entity';
+import { AssetService, ProductVariantService } from '../../../service';
 import { RequestContext } from '../../common/request-context';
 import { Ctx } from '../../decorators/request-context.decorator';
 
 @Resolver('OrderLine')
 export class OrderLineEntityResolver {
-    constructor(private productVariantService: ProductVariantService) {}
+    constructor(private productVariantService: ProductVariantService, private assetService: AssetService) {}
 
     @ResolveField()
     async productVariant(
@@ -18,4 +18,16 @@ export class OrderLineEntityResolver {
     ): Promise<Translated<ProductVariant>> {
         return assertFound(this.productVariantService.findOne(ctx, orderLine.productVariant.id));
     }
+
+    @ResolveField()
+    async featuredAsset(
+        @Ctx() ctx: RequestContext,
+        @Parent() orderLine: OrderLine,
+    ): Promise<Asset | undefined> {
+        if (orderLine.featuredAsset) {
+            return orderLine.featuredAsset;
+        } else {
+            return this.assetService.getFeaturedAsset(orderLine);
+        }
+    }
 }

+ 6 - 4
packages/core/src/service/services/asset.service.ts

@@ -68,7 +68,9 @@ export class AssetService {
             }));
     }
 
-    async getFeaturedAsset<T extends EntityWithAssets>(entity: T): Promise<Asset | undefined> {
+    async getFeaturedAsset<T extends Omit<EntityWithAssets, 'assets'>>(
+        entity: T,
+    ): Promise<Asset | undefined> {
         const entityType = Object.getPrototypeOf(entity).constructor;
         const entityWithFeaturedAsset = await this.connection
             .getRepository<EntityWithAssets>(entityType)
@@ -89,7 +91,7 @@ export class AssetService {
                 });
             assets = (entityWithAssets && entityWithAssets.assets) || [];
         }
-        return assets.sort((a, b) => a.position - b.position).map(a => a.asset);
+        return assets.sort((a, b) => a.position - b.position).map((a) => a.asset);
     }
 
     async updateFeaturedAsset<T extends EntityWithAssets>(entity: T, input: EntityAssetInput): Promise<T> {
@@ -119,7 +121,7 @@ export class AssetService {
         if (assetIds && assetIds.length) {
             const assets = await this.connection.getRepository(Asset).findByIds(assetIds);
             const sortedAssets = assetIds
-                .map(id => assets.find(a => idsAreEqual(a.id, id)))
+                .map((id) => assets.find((a) => idsAreEqual(a.id, id)))
                 .filter(notNullOrUndefined);
             await this.removeExistingOrderableAssets(entity);
             entity.assets = await this.createOrderableAssets(entity, sortedAssets);
@@ -295,7 +297,7 @@ export class AssetService {
     private getOrderableAssetType(entity: EntityWithAssets): Type<OrderableAsset> {
         const assetRelation = this.connection
             .getRepository(entity.constructor)
-            .metadata.relations.find(r => r.propertyName === 'assets');
+            .metadata.relations.find((r) => r.propertyName === 'assets');
         if (!assetRelation || typeof assetRelation.type === 'string') {
             throw new InternalServerError('error.could-not-find-matching-orderable-asset');
         }

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

@@ -144,7 +144,13 @@ export class OrderService {
     ): Promise<PaginatedList<Order>> {
         return this.listQueryBuilder
             .build(Order, options, {
-                relations: ['lines', 'lines.productVariant', 'lines.productVariant.options', 'customer'],
+                relations: [
+                    'lines',
+                    'lines.items',
+                    'lines.productVariant',
+                    'lines.productVariant.options',
+                    'customer',
+                ],
             })
             .andWhere('order.customer.id = :customerId', { customerId })
             .getManyAndCount()