Merge branch 'master' into 2369-rework-stock-product-handling
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
import { SQLDataSource } from 'datasource-sql';
|
||||
import { Keyword, KeywordType } from '../types/keyword-types';
|
||||
import { TextsAPI } from './texts-api';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
export class KeywordAPI extends SQLDataSource {
|
||||
constructor(config) {
|
||||
textsApi: TextsAPI;
|
||||
constructor(config, textsApi) {
|
||||
super(config);
|
||||
this.textsApi = textsApi;
|
||||
}
|
||||
|
||||
getType(data: any) {
|
||||
@@ -98,18 +101,8 @@ ORDER BY keywords.value
|
||||
});
|
||||
}
|
||||
}
|
||||
await this.textsApi.translateKeyword(keywordId, name);
|
||||
|
||||
return keywordId;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
category keywords:
|
||||
SELECT category_keyword.category_id, keywords.value
|
||||
FROM category_keyword
|
||||
JOIN keywords ON category_keyword.keyword_id = keywords.id;
|
||||
|
||||
texts:
|
||||
FROM categorydata cd
|
||||
JOIN categorydatakeys cdk ON cd.datakey_id = cdk.id
|
||||
*/
|
||||
|
||||
@@ -194,6 +194,18 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
return res.find(Boolean);
|
||||
}
|
||||
|
||||
async getProductByPath(path: string): Promise<Maybe<Product>> {
|
||||
const query = sql.productByPath(path);
|
||||
|
||||
const res = await this.knex.raw(query).then((data) =>
|
||||
data.rows.map((row) => {
|
||||
const prod = this.createProductFromRow(row);
|
||||
return prod;
|
||||
}),
|
||||
);
|
||||
return res.find(Boolean);
|
||||
}
|
||||
|
||||
async getDesignerProducts(designerId: number): Promise<Array<Product>> {
|
||||
const query = sql.designerProducts();
|
||||
|
||||
@@ -381,7 +393,7 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
|
||||
/**
|
||||
* @function addProduct
|
||||
* @description Adds a new product to system, will use batchname and name to
|
||||
* @description Adds a new product to system, will use name to
|
||||
* set a unique path, artNo is the productId with e infront.
|
||||
*/
|
||||
async addProduct(
|
||||
@@ -390,8 +402,7 @@ export class ProductAPI extends BaseSQLDataSource {
|
||||
designerId: number,
|
||||
): Promise<number> {
|
||||
const fixedBatch = !!batch ? batch : '';
|
||||
const path = batch ? fixedBatch + ' ' + name : name;
|
||||
const productPath = slug(path);
|
||||
const productPath = slug(name);
|
||||
const safePath = await this.getUniqueProductPath(productPath, null);
|
||||
|
||||
const idres = await this.knex
|
||||
|
||||
@@ -119,6 +119,15 @@ export function product(id: number) {
|
||||
);
|
||||
}
|
||||
|
||||
export function productByPath(path: string) {
|
||||
return (
|
||||
baseQuery +
|
||||
/* sql */ `
|
||||
WHERE products.path = '${path}'
|
||||
`
|
||||
);
|
||||
}
|
||||
|
||||
export function categoryProducts(categoryId: number) {
|
||||
return (
|
||||
baseQuery +
|
||||
|
||||
@@ -14,6 +14,31 @@ export class TextsAPI extends SQLDataSource {
|
||||
this.marketLocaleApi = marketLocaleApi;
|
||||
}
|
||||
|
||||
async translateKeyword(keywordId: number, keyword: string): Promise<void> {
|
||||
const locales = await this.marketLocaleApi.getLocales();
|
||||
for (let i = 0; i < locales.length; i++) {
|
||||
let langCode = this.getLanguageCode(locales[i]);
|
||||
// If we have a fallback locale we skip translating
|
||||
if (locales[i].fallbackId) {
|
||||
continue;
|
||||
}
|
||||
const input: TranslateInput = {
|
||||
text: keyword,
|
||||
targetLanguageCode: langCode,
|
||||
sourceLanguageCode: 'en',
|
||||
};
|
||||
const translated = await translateText(input);
|
||||
|
||||
const res = await this.knex('keywords_i18n')
|
||||
.insert({
|
||||
value: translated,
|
||||
keyword_id: keywordId,
|
||||
locale_id: locales[i].id,
|
||||
})
|
||||
.returning('*');
|
||||
}
|
||||
}
|
||||
|
||||
async translateProductName(
|
||||
name: string,
|
||||
oldPath: string,
|
||||
|
||||
+1
-1
@@ -40,7 +40,7 @@ const imageServerApi = new ImageServerApi();
|
||||
const interiorsLambdaApi = new InteriorsLambdaAPI();
|
||||
const interiorApi = new InteriorAPI(knexConfig, imageServerApi);
|
||||
const printProductDefaultsApi = new PrintProductDefaultsAPI(knexConfig);
|
||||
const keywordApi = new KeywordAPI(knexConfig);
|
||||
const keywordApi = new KeywordAPI(knexConfig, textsApi);
|
||||
const orderApi = new OrderAPI(knexConfig);
|
||||
const productApi = new ProductAPI(
|
||||
knexConfig,
|
||||
|
||||
@@ -121,6 +121,14 @@ async function getProduct(_, { id }, { dataSources, auth }): Promise<Product> {
|
||||
return (<ProductAPI>dataSources.productApi).getProduct(id);
|
||||
}
|
||||
|
||||
async function getProductByPath(
|
||||
_,
|
||||
{ path },
|
||||
{ dataSources },
|
||||
): Promise<Product> {
|
||||
return (<ProductAPI>dataSources.productApi).getProductByPath(path);
|
||||
}
|
||||
|
||||
export const productTypeDefs = {
|
||||
ProductBlacklist,
|
||||
Product,
|
||||
@@ -130,6 +138,7 @@ export const productTypeDefs = {
|
||||
export const productQueryTypeDefs = {
|
||||
products: getProductsResult,
|
||||
product: getProduct,
|
||||
productByPath: getProductByPath,
|
||||
productsSearch: getProductsSearchResult,
|
||||
};
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ type Query {
|
||||
filter: ProductsFilterInput
|
||||
): ProductsResult
|
||||
product(id: Int!): Product
|
||||
productByPath(path: String!): Product
|
||||
productsSearch(q: String!): ProductsSearchResult
|
||||
categories: [Category]
|
||||
category(id: Int!): Category
|
||||
|
||||
Reference in New Issue
Block a user