Added product admin features
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { SQLDataSource } from 'datasource-sql';
|
||||
import { BaseSQLDataSource } from './BaseSQLDataSource';
|
||||
import { camelcase } from 'stringcase';
|
||||
import * as sql from './sql';
|
||||
import {
|
||||
Product,
|
||||
@@ -6,12 +7,16 @@ import {
|
||||
PrintProduct,
|
||||
ProductType,
|
||||
ProductBlacklist,
|
||||
ProductWallpaperType,
|
||||
ProductFields,
|
||||
Orientation,
|
||||
} from '../types/product-types';
|
||||
import { Maybe } from '../types/types';
|
||||
import { convertToPossibleType } from './utils';
|
||||
|
||||
const MINUTE = 60;
|
||||
|
||||
export class ProductAPI extends SQLDataSource {
|
||||
export class ProductAPI extends BaseSQLDataSource {
|
||||
constructor(config) {
|
||||
super(config);
|
||||
}
|
||||
@@ -21,6 +26,25 @@ export class ProductAPI extends SQLDataSource {
|
||||
// SELECT materialid, material, (price / 100::float) as price FROM "product-materials"
|
||||
// }
|
||||
|
||||
getWallpaperTypes(row: any): Array<ProductWallpaperType> {
|
||||
return row.wallpapertypes?.map((t: number) => ProductWallpaperType[t]);
|
||||
}
|
||||
|
||||
getProductTypes(row: any): Array<ProductType> {
|
||||
if (!row.types) {
|
||||
return [];
|
||||
}
|
||||
return row.types.map((type: any) => {
|
||||
if (type === 'typeillustration') {
|
||||
return ProductType[ProductType.ILLUSTRATION];
|
||||
}
|
||||
if (type === 'typephotography') {
|
||||
return ProductType[ProductType.PHOTO];
|
||||
}
|
||||
return ProductType[ProductType.UNKNOWN];
|
||||
});
|
||||
}
|
||||
|
||||
getBlacklisting(row: any): Array<ProductBlacklist> {
|
||||
if (!row.blacklisting) {
|
||||
return [];
|
||||
@@ -42,26 +66,36 @@ export class ProductAPI extends SQLDataSource {
|
||||
});
|
||||
}
|
||||
|
||||
getProductTypes(row: any): Array<ProductType> {
|
||||
if (!row.types) {
|
||||
return [];
|
||||
getProductFields(row: any): ProductFields {
|
||||
const fields = Object.keys(row.fields).reduce((mem, key) => {
|
||||
mem[camelcase(key)] = convertToPossibleType(row.fields[key]);
|
||||
return mem;
|
||||
}, {});
|
||||
|
||||
return <ProductFields>fields;
|
||||
}
|
||||
|
||||
getOrientation(fields: ProductFields): string {
|
||||
if (!fields.width || !fields.height) {
|
||||
return Orientation[Orientation.UNKNOWN];
|
||||
}
|
||||
return row.types.map((type: any) => {
|
||||
if (type === 'typeillustration') {
|
||||
return ProductType[ProductType.ILLUSTRATION];
|
||||
}
|
||||
if (type === 'typephotography') {
|
||||
return ProductType[ProductType.PHOTO];
|
||||
}
|
||||
return ProductType[ProductType.UNKNOWN];
|
||||
});
|
||||
if (fields.width > fields.height) {
|
||||
return Orientation[Orientation.LANDSCAPE];
|
||||
}
|
||||
if (fields.width < fields.height) {
|
||||
return Orientation[Orientation.PORTRAIT];
|
||||
}
|
||||
return Orientation[Orientation.SQUARE];
|
||||
}
|
||||
|
||||
createProductFromRow(row: any) {
|
||||
row.blacklisting = this.getBlacklisting(row);
|
||||
row.printProducts = this.getPrintProducts(row);
|
||||
row.type = this.getProductTypes(row);
|
||||
row.wallpaperTypes = this.getWallpaperTypes(row);
|
||||
row.fields = this.getProductFields(row);
|
||||
row.designerId = row.designerid;
|
||||
row.orientation = this.getOrientation(row.fields);
|
||||
row.fields_json = row.fields;
|
||||
return row;
|
||||
}
|
||||
@@ -74,7 +108,7 @@ export class ProductAPI extends SQLDataSource {
|
||||
limit = limit ?? 100;
|
||||
const query = sql.products(limit, visible, browsable);
|
||||
|
||||
return await this.knex
|
||||
return this.knex
|
||||
.raw(query)
|
||||
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
||||
}
|
||||
@@ -94,7 +128,7 @@ export class ProductAPI extends SQLDataSource {
|
||||
async getCategoryProducts(categoryId: number): Promise<Array<Product>> {
|
||||
const query = sql.categoryProducts(categoryId);
|
||||
|
||||
return await this.knex
|
||||
return this.knex
|
||||
.raw(query)
|
||||
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
||||
}
|
||||
@@ -102,8 +136,18 @@ export class ProductAPI extends SQLDataSource {
|
||||
async getKeywordProducts(keywordId: number): Promise<Array<Product>> {
|
||||
const query = sql.keywordProducts(keywordId);
|
||||
|
||||
return await this.knex
|
||||
return this.knex
|
||||
.raw(query)
|
||||
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
||||
}
|
||||
|
||||
async getRelatedProducts(id: number): Promise<Array<Product>> {
|
||||
const ids = await this.cachedRaw(
|
||||
`SELECT productid2 FROM "product-products_products" WHERE productid1 = ${id}`,
|
||||
)
|
||||
.cache(MINUTE * 60)
|
||||
.then((data) => data.rows.map((r) => r.productid2));
|
||||
|
||||
return ids.map(async (id) => this.getProduct(id));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user