Răsfoiți Sursa

fix(core): Fix sorting by price on productVariants list

Fixes #690
Michael Bromley 5 ani în urmă
părinte
comite
e5182b73fa

+ 35 - 0
packages/core/e2e/product.e2e-spec.ts

@@ -372,6 +372,41 @@ describe('Product resolver', () => {
                 },
             ]);
         });
+
+        it('sort by price', async () => {
+            const { productVariants } = await adminClient.query<
+                GetProductVariantList.Query,
+                GetProductVariantList.Variables
+            >(GET_PRODUCT_VARIANT_LIST, {
+                options: {
+                    take: 3,
+                    sort: {
+                        price: SortOrder.ASC,
+                    },
+                },
+            });
+
+            expect(productVariants.items).toEqual([
+                {
+                    id: 'T_23',
+                    name: 'Skipping Rope',
+                    price: 799,
+                    sku: 'B07CNGXVXT',
+                },
+                {
+                    id: 'T_20',
+                    name: 'Tripod',
+                    price: 1498,
+                    sku: 'B00XI87KV8',
+                },
+                {
+                    id: 'T_32',
+                    name: 'Spiky Cactus',
+                    price: 1550,
+                    sku: 'SC011001',
+                },
+            ]);
+        });
     });
 
     describe('productVariant query', () => {

+ 2 - 4
packages/core/src/entity/product-variant/product-variant.entity.ts

@@ -70,8 +70,7 @@ export class ProductVariant
     currencyCode: CurrencyCode;
 
     @Calculated({
-        relations: ['productVariantPrices'],
-        expression: 'productVariantPrices.price',
+        expression: 'productvariant_productVariantPrices.price',
     })
     get price(): number {
         if (this.listPrice == null) {
@@ -81,8 +80,7 @@ export class ProductVariant
     }
 
     @Calculated({
-        relations: ['productVariantPrices'],
-        expression: 'productVariantPrices.price',
+        expression: 'productvariant_productVariantPrices.price',
     })
     get priceWithTax(): number {
         if (this.listPrice == null) {

+ 10 - 5
packages/core/src/service/helpers/list-query-builder/list-query-builder.ts

@@ -113,11 +113,16 @@ export class ListQueryBuilder implements OnApplicationBootstrap {
             if (instruction) {
                 const relations = instruction.relations || [];
                 for (const relation of relations) {
-                    const propertyPath = relation.includes('.') ? relation : `${alias}.${relation}`;
-                    const relationAlias = relation.includes('.')
-                        ? relation.split('.').reverse()[0]
-                        : relation;
-                    qb.innerJoinAndSelect(propertyPath, relationAlias);
+                    const relationIsAlreadyJoined = qb.expressionMap.joinAttributes.find(
+                        ja => ja.entityOrProperty === `${alias}.${relation}`,
+                    );
+                    if (!relationIsAlreadyJoined) {
+                        const propertyPath = relation.includes('.') ? relation : `${alias}.${relation}`;
+                        const relationAlias = relation.includes('.')
+                            ? relation.split('.').reverse()[0]
+                            : relation;
+                        qb.innerJoinAndSelect(propertyPath, relationAlias);
+                    }
                 }
                 if (typeof instruction.query === 'function') {
                     instruction.query(qb);