Browse Source

fix(server): Fix errors resolving deep order properties for active user

Michael Bromley 7 years ago
parent
commit
b44dc7eaf3

+ 1 - 1
server/src/api/resolvers/customer.resolver.ts

@@ -72,7 +72,7 @@ export class CustomerResolver {
     ): Promise<PaginatedList<Order>> {
         this.checkOwnerPermissions(ctx, customer);
         const customerId = this.idCodecService.decode(customer.id);
-        return this.orderService.findByCustomerId(customerId, args.options || undefined);
+        return this.orderService.findByCustomerId(ctx, customerId, args.options || undefined);
     }
 
     @Mutation()

+ 3 - 1
server/src/api/resolvers/order.resolver.ts

@@ -18,6 +18,7 @@ import {
 } from '../../../../shared/generated-types';
 import { PaginatedList } from '../../../../shared/shared-types';
 import { ForbiddenError, InternalServerError } from '../../common/error/errors';
+import { idsAreEqual } from '../../common/utils';
 import { Order } from '../../entity/order/order.entity';
 import { OrderState } from '../../service/helpers/order-state-machine/order-state';
 import { AuthService } from '../../service/services/auth.service';
@@ -51,7 +52,8 @@ export class OrderResolver {
     async order(@Ctx() ctx: RequestContext, @Args() args: OrderQueryArgs): Promise<Order | undefined> {
         const order = await this.orderService.findOne(ctx, args.id);
         if (order && ctx.authorizedAsOwnerOnly) {
-            if (ctx.session && ctx.session.activeOrder && ctx.session.activeOrder.id === order.id) {
+            const orderUserId = order.customer && order.customer.user && order.customer.user.id;
+            if (idsAreEqual(ctx.activeUserId, orderUserId)) {
                 return order;
             } else {
                 return;

+ 15 - 2
server/src/service/services/order.service.ts

@@ -89,12 +89,25 @@ export class OrderService {
         return order;
     }
 
-    async findByCustomerId(customerId: ID, options?: ListQueryOptions<Order>): Promise<PaginatedList<Order>> {
+    async findByCustomerId(
+        ctx: RequestContext,
+        customerId: ID,
+        options?: ListQueryOptions<Order>,
+    ): Promise<PaginatedList<Order>> {
         return this.listQueryBuilder
-            .build(Order, options, { relations: ['lines', 'lines.productVariant', 'customer'] })
+            .build(Order, options, {
+                relations: ['lines', 'lines.productVariant', 'lines.productVariant.options', 'customer'],
+            })
             .andWhere('order.customer.id = :customerId', { customerId })
             .getManyAndCount()
             .then(([items, totalItems]) => {
+                items.forEach(item => {
+                    item.lines.forEach(line => {
+                        line.productVariant = translateDeep(line.productVariant, ctx.languageCode, [
+                            'options',
+                        ]);
+                    });
+                });
                 return {
                     items,
                     totalItems,