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

fix(core): Slugify product created with fast importer service (#2091)

prasanna malla 2 лет назад
Родитель
Сommit
8e9f4d64b4

+ 52 - 0
packages/core/e2e/fast-importer.e2e-spec.ts

@@ -0,0 +1,52 @@
+import { CreateProductInput, ProductTranslationInput } from '@vendure/common/lib/generated-types';
+import { createTestEnvironment } from '@vendure/testing';
+import path from 'path';
+
+import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
+import { initialData } from '../mock-data/data-sources/initial-data';
+import { FastImporterService, LanguageCode } from '../src';
+
+import { GetProductWithVariants } from './graphql/generated-e2e-admin-types';
+import { GET_PRODUCT_WITH_VARIANTS } from './graphql/shared-definitions';
+
+describe('FastImporterService resolver', () => {
+    const { server, adminClient } = createTestEnvironment(testConfig());
+
+    let fastImporterService: FastImporterService;
+
+    beforeAll(async () => {
+        await server.init({
+            initialData,
+            productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
+        });
+        await adminClient.asSuperAdmin();
+        fastImporterService = server.app.get(FastImporterService);
+    }, TEST_SETUP_TIMEOUT_MS);
+
+    afterAll(async () => {
+        await server.destroy();
+    });
+
+    it('creates normalized slug', async () => {
+        const productTranslation: ProductTranslationInput = {
+            languageCode: LanguageCode.en,
+            name: 'test product',
+            slug: 'test product',
+            description: 'test description',
+        };
+        const createProductInput: CreateProductInput = {
+            translations: [productTranslation],
+        };
+        await fastImporterService.initialize();
+        const productId = await fastImporterService.createProduct(createProductInput);
+
+        const { product } = await adminClient.query<
+            GetProductWithVariants.Query,
+            GetProductWithVariants.Variables
+        >(GET_PRODUCT_WITH_VARIANTS, {
+            id: productId as string,
+        });
+
+        expect(product?.slug).toMatch('test-product');
+    });
+});

+ 15 - 3
packages/core/src/data-import/providers/importer/fast-importer.service.ts

@@ -5,6 +5,7 @@ import {
     CreateProductOptionInput,
     CreateProductVariantInput,
 } from '@vendure/common/lib/generated-types';
+import { normalizeString } from '@vendure/common/lib/normalize-string';
 import { ID } from '@vendure/common/lib/shared-types';
 import { unique } from '@vendure/common/lib/unique';
 
@@ -71,6 +72,11 @@ export class FastImporterService {
 
     async createProduct(input: CreateProductInput): Promise<ID> {
         this.ensureInitialized();
+        // https://github.com/vendure-ecommerce/vendure/issues/2053
+        // normalizes slug without validation for faster performance
+        input.translations.map(translation => {
+            translation.slug = normalizeString(translation.slug as string, '-');
+        });
         const product = await this.translatableSaver.create({
             ctx: this.importCtx,
             input,
@@ -95,7 +101,9 @@ export class FastImporterService {
                         position: i,
                     }),
             );
-            await this.connection.getRepository(this.importCtx, ProductAsset).save(productAssets, { reload: false });
+            await this.connection
+                .getRepository(this.importCtx, ProductAsset)
+                .save(productAssets, { reload: false });
         }
         return product.id;
     }
@@ -177,7 +185,9 @@ export class FastImporterService {
                         position: i,
                     }),
             );
-            await this.connection.getRepository(this.importCtx, ProductVariantAsset).save(variantAssets, { reload: false });
+            await this.connection
+                .getRepository(this.importCtx, ProductVariantAsset)
+                .save(variantAssets, { reload: false });
         }
         if (input.stockOnHand != null && input.stockOnHand !== 0) {
             await this.stockMovementService.adjustProductVariantStock(
@@ -194,7 +204,9 @@ export class FastImporterService {
                 channelId,
             });
             variantPrice.variant = createdVariant;
-            await this.connection.getRepository(this.importCtx, ProductVariantPrice).save(variantPrice, { reload: false });
+            await this.connection
+                .getRepository(this.importCtx, ProductVariantPrice)
+                .save(variantPrice, { reload: false });
         }
 
         return createdVariant.id;

+ 1 - 0
packages/dev-server/.gitignore

@@ -9,3 +9,4 @@ load-testing/results/**/*.csv
 load-testing/static/assets
 dev-config-override.ts
 vendure.log
+scripts/dev-test

+ 6 - 6
packages/dev-server/dev-config.ts

@@ -102,12 +102,12 @@ function getDbConfig(): ConnectionOptions {
             return {
                 synchronize: true,
                 type: 'postgres',
-                host: process.env.DB_HOST,
-                port: Number(process.env.DB_PORT),
-                username: process.env.DB_USERNAME,
-                password: process.env.DB_PASSWORD,
-                database: process.env.DB_NAME,
-                schema: process.env.DB_SCHEMA,
+                host: process.env.DB_HOST || 'localhost',
+                port: Number(process.env.DB_PORT) || 5432,
+                username: process.env.DB_USERNAME || 'postgres',
+                password: process.env.DB_PASSWORD || 'postgres',
+                database: process.env.DB_NAME || 'vendure',
+                schema: process.env.DB_SCHEMA || 'public',
             };
         case 'sqlite':
             console.log('Using sqlite connection');