Browse Source

fix(core): Return all assets when querying product by slug

Fixes #820
Michael Bromley 4 years ago
parent
commit
acb3fb00d1

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

@@ -252,6 +252,33 @@ describe('Product resolver', () => {
             expect(product.slug).toBe('curvy-monitor');
             expect(product.slug).toBe('curvy-monitor');
         });
         });
 
 
+        // https://github.com/vendure-ecommerce/vendure/issues/820
+        it('by slug with multiple assets', async () => {
+            const { product: product1 } = await adminClient.query<
+                GetProductSimple.Query,
+                GetProductSimple.Variables
+            >(GET_PRODUCT_SIMPLE, { id: 'T_1' });
+            const result = await adminClient.query<UpdateProduct.Mutation, UpdateProduct.Variables>(
+                UPDATE_PRODUCT,
+                {
+                    input: {
+                        id: product1!.id,
+                        assetIds: ['T_1', 'T_2', 'T_3'],
+                    },
+                },
+            );
+            const { product } = await adminClient.query<
+                GetProductWithVariants.Query,
+                GetProductWithVariants.Variables
+            >(GET_PRODUCT_WITH_VARIANTS, { slug: product1!.slug });
+
+            if (!product) {
+                fail('Product not found');
+                return;
+            }
+            expect(product.assets.map(a => a.id)).toEqual(['T_1', 'T_2', 'T_3']);
+        });
+
         // https://github.com/vendure-ecommerce/vendure/issues/538
         // https://github.com/vendure-ecommerce/vendure/issues/538
         it('falls back to default language slug', async () => {
         it('falls back to default language slug', async () => {
             const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(
             const { product } = await adminClient.query<GetProductSimple.Query, GetProductSimple.Variables>(

+ 1 - 1
packages/core/src/service/services/product.service.ts

@@ -153,11 +153,11 @@ export class ProductService {
             .andWhere('product.deletedAt IS NULL')
             .andWhere('product.deletedAt IS NULL')
             .andWhere('channel.id = :channelId', { channelId: ctx.channelId })
             .andWhere('channel.id = :channelId', { channelId: ctx.channelId })
             .addSelect(
             .addSelect(
+                // tslint:disable-next-line:max-line-length
                 `CASE product_translations.languageCode WHEN '${ctx.languageCode}' THEN 2 WHEN '${ctx.channel.defaultLanguageCode}' THEN 1 ELSE 0 END`,
                 `CASE product_translations.languageCode WHEN '${ctx.languageCode}' THEN 2 WHEN '${ctx.channel.defaultLanguageCode}' THEN 1 ELSE 0 END`,
                 'sort_order',
                 'sort_order',
             )
             )
             .orderBy('sort_order', 'DESC')
             .orderBy('sort_order', 'DESC')
-            .limit(1)
             .getOne()
             .getOne()
             .then(product =>
             .then(product =>
                 product
                 product