Add category locale names (#112)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 }) {
|
||||
|
||||
@@ -216,6 +216,7 @@ type Category {
|
||||
Default to false
|
||||
"""
|
||||
products(includeAllSubCategories: Boolean): [Product]
|
||||
localeNames: JSON
|
||||
}
|
||||
|
||||
type Batch {
|
||||
|
||||
@@ -8,4 +8,5 @@ export interface Category {
|
||||
childCount: number;
|
||||
lft: number;
|
||||
rgt: number;
|
||||
localeNames: Array<Record<string, string>>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user