Просмотр исходного кода

fix(email-plugin): Hydrate lines.featureAsset on order-confirmation email (#3690)

Ariel Barabas 5 месяцев назад
Родитель
Сommit
20c05db690

+ 8 - 11
packages/email-plugin/src/handler/default-email-handlers.ts

@@ -31,8 +31,13 @@ export const orderConfirmationHandler = new EmailEventListener('order-confirmati
             event.toState === 'PaymentSettled' && event.fromState !== 'Modifying' && !!event.order.customer,
     )
     .loadData(async ({ event, injector }) => {
+        const entityHydrator = injector.get(EntityHydrator);
+        await entityHydrator.hydrate(event.ctx, event.order, {
+            relations: ['lines.featuredAsset', 'shippingLines.shippingMethod'],
+        });
+
         transformOrderLineAssetUrls(event.ctx, event.order, injector);
-        const shippingLines = await hydrateShippingLines(event.ctx, event.order, injector);
+        const shippingLines = shippingLinesWithMethod(event.order);
         return { shippingLines };
     })
     .setRecipient(event => event.order.customer!.emailAddress)
@@ -113,24 +118,16 @@ export function transformOrderLineAssetUrls(ctx: RequestContext, order: Order, i
 
 /**
  * @description
- * Ensures that the ShippingLines are hydrated so that we can use the
+ * Ensures that the ShippingLines have a shippingMethod so that we can use the
  * `shippingMethod.name` property in the email template.
  *
  * @docsCategory core plugins/EmailPlugin
  * @docsPage Email utils
  */
-export async function hydrateShippingLines(
-    ctx: RequestContext,
-    order: Order,
-    injector: Injector,
-): Promise<ShippingLine[]> {
+export function shippingLinesWithMethod(order: Order): ShippingLine[] {
     const shippingLines: ShippingLine[] = [];
-    const entityHydrator = injector.get(EntityHydrator);
 
     for (const line of order.shippingLines || []) {
-        await entityHydrator.hydrate(ctx, line, {
-            relations: ['shippingMethod'],
-        });
         if (line.shippingMethod) {
             shippingLines.push(line);
         }

+ 9 - 1
packages/email-plugin/src/plugin.spec.ts

@@ -3,6 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
 import { TypeOrmModule } from '@nestjs/typeorm';
 import { DEFAULT_CHANNEL_CODE } from '@vendure/common/lib/shared-constants';
 import {
+    EntityHydrator,
     EventBus,
     Injector,
     JobQueueService,
@@ -68,7 +69,14 @@ describe('EmailPlugin', () => {
                 }),
             ],
             providers: [MockService],
-        }).compile();
+        })
+            .overrideProvider(EntityHydrator)
+            .useValue({
+                hydrate() {
+                    // noop
+                },
+            })
+            .compile();
 
         Logger.useLogger(testingLogger);
         module.useLogger(new Logger());