Skip to content

Commit

Permalink
feat(core): Normalize product slug values
Browse files Browse the repository at this point in the history
Relates to #103
  • Loading branch information
michaelbromley committed May 29, 2019
1 parent 29509d8 commit e2235cb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/core/e2e/product.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
{
Expand Down Expand Up @@ -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<UpdateProduct.Mutation, UpdateProduct.Variables>(
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<UpdateProduct.Mutation, UpdateProduct.Variables>(
UPDATE_PRODUCT,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/service/services/product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -99,6 +100,7 @@ export class ProductService {
}

async create(ctx: RequestContext, input: CreateProductInput): Promise<Translated<Product>> {
this.normalizeSlugs(input);
const product = await this.translatableSaver.create({
input,
entityType: Product,
Expand All @@ -117,6 +119,7 @@ export class ProductService {

async update(ctx: RequestContext, input: UpdateProductInput): Promise<Translated<Product>> {
await getEntityOrThrow(this.connection, Product, input.id);
this.normalizeSlugs(input);
const product = await this.translatableSaver.update({
input,
entityType: Product,
Expand Down Expand Up @@ -183,4 +186,15 @@ export class ProductService {
}
return product;
}

private normalizeSlugs<T extends CreateProductInput | UpdateProductInput>(input: T): T {
if (input.translations) {
input.translations.forEach(t => {
if (t.slug) {
t.slug = normalizeString(t.slug, '-');
}
});
}
return input;
}
}

0 comments on commit e2235cb

Please sign in to comment.