From e2235cba9b9c7916a5fe4783a53d388948d7f37a Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Wed, 29 May 2019 14:53:03 +0200 Subject: [PATCH] feat(core): Normalize product slug values Relates to #103 --- packages/core/e2e/product.e2e-spec.ts | 22 ++++++++++++++++++- .../src/service/services/product.service.ts | 14 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/core/e2e/product.e2e-spec.ts b/packages/core/e2e/product.e2e-spec.ts index 923fdd2c7f..216ca51612 100644 --- a/packages/core/e2e/product.e2e-spec.ts +++ b/packages/core/e2e/product.e2e-spec.ts @@ -179,7 +179,7 @@ describe('Product resolver', () => { { languageCode: LanguageCode.en, name: 'en Baked Potato', - slug: 'en-baked-potato', + slug: 'en Baked Potato', description: 'A baked potato', }, { @@ -250,6 +250,26 @@ describe('Product resolver', () => { expect(result.updateProduct).toMatchSnapshot(); }); + it('slug is normalized to be url-safe', async () => { + const result = await client.query( + UPDATE_PRODUCT, + { + input: { + id: newProduct.id, + translations: [ + { + languageCode: LanguageCode.en, + name: 'en Mashed Potato', + slug: 'A (very) nice potato!!1', + description: 'A blob of mashed potato', + }, + ], + }, + }, + ); + expect(result.updateProduct.slug).toBe('a-very-nice-potato1'); + }); + it('updateProduct accepts partial input', async () => { const result = await client.query( UPDATE_PRODUCT, diff --git a/packages/core/src/service/services/product.service.ts b/packages/core/src/service/services/product.service.ts index a311e59298..89347bdbba 100644 --- a/packages/core/src/service/services/product.service.ts +++ b/packages/core/src/service/services/product.service.ts @@ -6,6 +6,7 @@ import { DeletionResult, UpdateProductInput, } from '@vendure/common/lib/generated-types'; +import { normalizeString } from '@vendure/common/lib/normalize-string'; import { ID, PaginatedList } from '@vendure/common/lib/shared-types'; import { Connection } from 'typeorm'; @@ -99,6 +100,7 @@ export class ProductService { } async create(ctx: RequestContext, input: CreateProductInput): Promise> { + this.normalizeSlugs(input); const product = await this.translatableSaver.create({ input, entityType: Product, @@ -117,6 +119,7 @@ export class ProductService { async update(ctx: RequestContext, input: UpdateProductInput): Promise> { await getEntityOrThrow(this.connection, Product, input.id); + this.normalizeSlugs(input); const product = await this.translatableSaver.update({ input, entityType: Product, @@ -183,4 +186,15 @@ export class ProductService { } return product; } + + private normalizeSlugs(input: T): T { + if (input.translations) { + input.translations.forEach(t => { + if (t.slug) { + t.slug = normalizeString(t.slug, '-'); + } + }); + } + return input; + } }