added safe slug to productInfo mutation (#70)

This commit is contained in:
Arwid Thornström
2021-10-12 11:34:53 +02:00
committed by GitHub
parent 1fbe972af7
commit 7aff154eb4
+21 -3
View File
@@ -279,7 +279,22 @@ export class ProductAPI extends BaseSQLDataSource {
);
}
async getUniqueProductSlug(slug: string): Promise<string> {
async getUniqueProductSlug(
slug: string,
productId?: number,
): Promise<string> {
// 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<number> {
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<any[]> {
// 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,