From 7aff154eb4417af965e3c32fd66d73369ec41735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arwid=20Thornstr=C3=B6m?= Date: Tue, 12 Oct 2021 11:34:53 +0200 Subject: [PATCH] added safe slug to productInfo mutation (#70) --- src/datasources/product-api.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index d57deb1..22f2aee 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -279,7 +279,22 @@ export class ProductAPI extends BaseSQLDataSource { ); } - async getUniqueProductSlug(slug: string): Promise { + async getUniqueProductSlug( + slug: string, + productId?: number, + ): Promise { + // If productId is set then check if slug is already + // set on that product. + if (productId) { + const productPath = await this.knex.raw( + `SELECT path FROM "product-products" WHERE productid = ?`, + [productId], + ); + if (productPath.rows[0].path === slug) { + return slug; + } + } + // Check for unique-slug // Get paths from product-products const pathResponse = await this.knex.raw( @@ -317,7 +332,7 @@ export class ProductAPI extends BaseSQLDataSource { async addProduct(name: string, batch: string): Promise { const path = batch ? batch + ' ' + name : name; const productSlug = slug(path); - const safeSlug = await this.getUniqueProductSlug(productSlug); + const safeSlug = await this.getUniqueProductSlug(productSlug, null); const idres = await this.knex .table('product-products') @@ -338,12 +353,15 @@ export class ProductAPI extends BaseSQLDataSource { productId: number, info: ProductInfoInput, ): Promise { + // Check path and insert new unique path if taken + const uniquePath = await this.getUniqueProductSlug(info.path, productId); + const promises = []; promises.push( this.knex .table('product-products') .update({ - path: info.path, + path: uniquePath, browsable: info.browsable ? 1 : 0, visible: info.visible ? 1 : 0, designerid: info.designerId ?? null,