Add category locale names (#112)

This commit is contained in:
Niklas Fondberg
2022-04-21 11:59:35 +02:00
committed by GitHub
parent 60d4d312f1
commit 9e2ff31b65
4 changed files with 21 additions and 2 deletions
+16 -2
View File
@@ -1,9 +1,9 @@
import { SQLDataSource } from 'datasource-sql';
import { Category } from '../types/category-types';
import { BaseSQLDataSource } from './BaseSQLDataSource';
const MINUTE = 60;
export class CategoryAPI extends SQLDataSource {
export class CategoryAPI extends BaseSQLDataSource {
constructor(config) {
super(config);
}
@@ -48,6 +48,20 @@ WHERE product_category.product_id = ?
.cache(MINUTE);
}
async getLocaleNameForId(id: number): Promise<JSON> {
const res = await this.cachedRaw(
`
SELECT json_object_agg(ec.locale, ec.name) as locale_names
FROM v_esales_categorytree_i18n ec
WHERE ec.id = ${id}
`,
)
.cache(MINUTE * 5)
.then((data) => data.rows);
return res[0].locale_names;
}
/**
* TODO: should we have these?
* category keywords
+3
View File
@@ -13,6 +13,9 @@ const Category = {
return (<ProductAPI>dataSources.productApi).getCategoryProducts(id);
}
},
async localeNames({ id }, _, { dataSources, auth }) {
return (<CategoryAPI>dataSources.categoryApi).getLocaleNameForId(id);
},
};
async function getCategories(_, _args, { dataSources }) {
+1
View File
@@ -216,6 +216,7 @@ type Category {
Default to false
"""
products(includeAllSubCategories: Boolean): [Product]
localeNames: JSON
}
type Batch {
+1
View File
@@ -8,4 +8,5 @@ export interface Category {
childCount: number;
lft: number;
rgt: number;
localeNames: Array<Record<string, string>>;
}