Add product search (#64)

This commit is contained in:
Niklas Fondberg
2021-10-11 11:20:48 +02:00
committed by GitHub
parent 8b03ab8246
commit 08149c4a87
10 changed files with 168 additions and 4 deletions
+48
View File
@@ -17,6 +17,7 @@ import {
ProductMaterials,
ProductSizeTypes,
ProductProportionsInput,
Batch,
} from '../types/product-types';
import { Maybe } from '../types/types';
import { booleanStringField, nullOrNumber, roundOrDefaultTo } from './utils';
@@ -179,6 +180,14 @@ export class ProductAPI extends BaseSQLDataSource {
return res.find(Boolean);
}
async getDesignerProducts(designerId: number): Promise<Array<Product>> {
const query = sql.designerProducts();
return this.knex
.raw(query, designerId)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
async getCategoryProducts(categoryId: number): Promise<Array<Product>> {
const query = sql.categoryProducts(categoryId);
@@ -195,6 +204,45 @@ export class ProductAPI extends BaseSQLDataSource {
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
async searchByName(name: string): Promise<Array<Product>> {
const query = sql.searchByFieldValue(2); // name
return this.knex
.raw(query, name)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
async searchByArtNo(name: string): Promise<Array<Product>> {
const query = sql.searchByFieldValue(1); // artNo
return this.knex
.raw(query, name)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
async searchByBatch(name: string): Promise<Array<Batch>> {
const query = sql.searchByFieldValue(36); // batch
const batchRes = await this.knex
.raw(query, name)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
const batches = {};
batchRes.forEach((p) => {
if (!batches[p.fields.batch]) {
batches[p.fields.batch] = [];
}
batches[p.fields.batch].push(p);
});
return Object.keys(batches).map((key) => {
return <Batch>{
name: key,
products: batches[key],
};
});
}
async getRelatedProducts(id: number): Promise<Array<Product>> {
const ids = await this.knex
.raw(