added safe slug to productInfo mutation (#70)
This commit is contained in:
@@ -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
|
// Check for unique-slug
|
||||||
// Get paths from product-products
|
// Get paths from product-products
|
||||||
const pathResponse = await this.knex.raw(
|
const pathResponse = await this.knex.raw(
|
||||||
@@ -317,7 +332,7 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
async addProduct(name: string, batch: string): Promise<number> {
|
async addProduct(name: string, batch: string): Promise<number> {
|
||||||
const path = batch ? batch + ' ' + name : name;
|
const path = batch ? batch + ' ' + name : name;
|
||||||
const productSlug = slug(path);
|
const productSlug = slug(path);
|
||||||
const safeSlug = await this.getUniqueProductSlug(productSlug);
|
const safeSlug = await this.getUniqueProductSlug(productSlug, null);
|
||||||
|
|
||||||
const idres = await this.knex
|
const idres = await this.knex
|
||||||
.table('product-products')
|
.table('product-products')
|
||||||
@@ -338,12 +353,15 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
productId: number,
|
productId: number,
|
||||||
info: ProductInfoInput,
|
info: ProductInfoInput,
|
||||||
): Promise<any[]> {
|
): Promise<any[]> {
|
||||||
|
// Check path and insert new unique path if taken
|
||||||
|
const uniquePath = await this.getUniqueProductSlug(info.path, productId);
|
||||||
|
|
||||||
const promises = [];
|
const promises = [];
|
||||||
promises.push(
|
promises.push(
|
||||||
this.knex
|
this.knex
|
||||||
.table('product-products')
|
.table('product-products')
|
||||||
.update({
|
.update({
|
||||||
path: info.path,
|
path: uniquePath,
|
||||||
browsable: info.browsable ? 1 : 0,
|
browsable: info.browsable ? 1 : 0,
|
||||||
visible: info.visible ? 1 : 0,
|
visible: info.visible ? 1 : 0,
|
||||||
designerid: info.designerId ?? null,
|
designerid: info.designerId ?? null,
|
||||||
|
|||||||
Reference in New Issue
Block a user