Browse Source

fix(core): Manage transactions outside of orderService.modifyOrder function. (#1533)

This will allow calling orderService.modifyOrder() function along with other logic, inside the same transaction.
Alexander Shitikov 3 years ago
parent
commit
e7072742c1

+ 16 - 4
packages/core/src/api/resolvers/admin/order.resolver.ts

@@ -24,8 +24,9 @@ import {
     TransitionPaymentToStateResult,
 } from '@vendure/common/lib/generated-types';
 import { PaginatedList } from '@vendure/common/lib/shared-types';
+import { TransactionalConnection } from '../../../connection';
 
-import { ErrorResultUnion } from '../../../common/error/error-result';
+import { ErrorResultUnion, isGraphQlErrorResult } from '../../../common/error/error-result';
 import { Fulfillment } from '../../../entity/fulfillment/fulfillment.entity';
 import { Order } from '../../../entity/order/order.entity';
 import { Payment } from '../../../entity/payment/payment.entity';
@@ -34,7 +35,6 @@ import { FulfillmentState } from '../../../service/helpers/fulfillment-state-mac
 import { OrderState } from '../../../service/helpers/order-state-machine/order-state';
 import { PaymentState } from '../../../service/helpers/payment-state-machine/payment-state';
 import { OrderService } from '../../../service/services/order.service';
-import { ShippingMethodService } from '../../../service/services/shipping-method.service';
 import { RequestContext } from '../../common/request-context';
 import { Allow } from '../../decorators/allow.decorator';
 import { Ctx } from '../../decorators/request-context.decorator';
@@ -42,7 +42,10 @@ import { Transaction } from '../../decorators/transaction.decorator';
 
 @Resolver()
 export class OrderResolver {
-    constructor(private orderService: OrderService, private shippingMethodService: ShippingMethodService) {}
+    constructor(
+        private orderService: OrderService, 
+        private connection: TransactionalConnection
+    ) {}
 
     @Query()
     @Allow(Permission.ReadOrder)
@@ -165,7 +168,16 @@ export class OrderResolver {
     @Mutation()
     @Allow(Permission.UpdateOrder)
     async modifyOrder(@Ctx() ctx: RequestContext, @Args() args: MutationModifyOrderArgs) {
-        return this.orderService.modifyOrder(ctx, args.input);
+        await this.connection.startTransaction(ctx);
+        const result = await this.orderService.modifyOrder(ctx, args.input);
+
+        if (args.input.dryRun || isGraphQlErrorResult(result)) {
+            await this.connection.rollBackTransaction(ctx);
+        } else {
+            await this.connection.commitOpenTransaction(ctx);
+        }
+
+        return result
     }
 
     @Transaction()

+ 10 - 8
packages/core/src/service/services/order.service.ts

@@ -863,23 +863,26 @@ export class OrderService {
      * * Shipping or billing address changes
      *
      * Setting the `dryRun` input property to `true` will apply all changes, including updating the price of the
-     * Order, but will not actually persist any of those changes to the database.
+     * Order, except history entry and additional payment actions. 
+     * 
+     * __Using dryRun option, you must wrap function call in transaction manually.__
+     * 
      */
     async modifyOrder(
         ctx: RequestContext,
         input: ModifyOrderInput,
     ): Promise<ErrorResultUnion<ModifyOrderResult, Order>> {
-        await this.connection.startTransaction(ctx);
         const order = await this.getOrderOrThrow(ctx, input.orderId);
         const result = await this.orderModifier.modifyOrder(ctx, input, order);
-        if (input.dryRun) {
-            await this.connection.rollBackTransaction(ctx);
-            return isGraphQlErrorResult(result) ? result : result.order;
-        }
+
         if (isGraphQlErrorResult(result)) {
-            await this.connection.rollBackTransaction(ctx);
             return result;
         }
+
+        if (input.dryRun) {
+            return result.order;
+        }
+
         await this.historyService.createHistoryEntryForOrder({
             ctx,
             orderId: input.orderId,
@@ -888,7 +891,6 @@ export class OrderService {
                 modificationId: result.modification.id,
             },
         });
-        await this.connection.commitOpenTransaction(ctx);
         return this.getOrderOrThrow(ctx, input.orderId);
     }