Break out ref and copyright to own responses (#79)

This commit is contained in:
Niklas Fondberg
2021-10-27 13:15:16 +02:00
committed by GitHub
parent 56ad07edf8
commit 68e7414e32
4 changed files with 46 additions and 26 deletions
+27 -24
View File
@@ -18,6 +18,7 @@ import {
ProductSizeTypes,
ProductProportionsInput,
Batch,
Copyright,
} from '../types/product-types';
import { Maybe } from '../types/types';
import {
@@ -240,34 +241,36 @@ export class ProductAPI extends BaseSQLDataSource {
}
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],
};
});
return this.searchByFieldValue<Batch>(name, 36, 'batch');
}
async searchByCopyright(name: string): Promise<Array<Copyright>> {
return this.searchByFieldValue<Copyright>(name, 25, 'copyright');
}
async searchByFieldValue<T>(
name: string,
fieldNo: number,
fieldKey: string,
): Promise<Array<T>> {
const query = sql.searchByFieldValue(fieldNo);
async searchByCopyright(name: string): Promise<Array<Product>> {
const query = sql.searchByFieldValue(25); // copyright
return this.knex
const rows = await this.knex
.raw(query, name)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
const result = {};
rows.forEach((p) => {
if (!result[p.fields[fieldKey]]) {
result[p.fields[fieldKey]] = [];
}
result[p.fields[fieldKey]].push(p);
});
return Object.keys(result).map((key) => {
return {
name: key,
products: result[key],
} as unknown as T;
});
}
async searchByReferences(name: string): Promise<Array<Product>> {
+2 -2
View File
@@ -84,8 +84,6 @@ async function getProductsSearchResult(
const prods = Promise.all([
prodApi.searchByName(q),
prodApi.searchByArtNo(q),
prodApi.searchByCopyright(q),
prodApi.searchByReferences(q),
]).then((d) => d.flat());
return {
@@ -95,6 +93,8 @@ async function getProductsSearchResult(
keywords: keywApi.searchKeywords(q),
categories: catApi.searchCategories(q),
designers: designerApi.searchDesigners(q),
copyrights: prodApi.searchByCopyright(q),
references: prodApi.searchByReferences(q),
};
}
+7
View File
@@ -103,6 +103,8 @@ type ProductsSearchResult {
keywords: [Keyword]
categories: [Category]
designers: [Designer]
copyrights: [Copyright]
references: [Product]
}
input InteriorsFilterInput {
@@ -277,6 +279,11 @@ type Batch {
products: [Product]
}
type Copyright {
name: String!
products: [Product]
}
type Product {
id: ID!
stockid: Int
+10
View File
@@ -55,6 +55,8 @@ export interface ProductsSearchResult {
keywords: Promise<Array<Keyword>>;
categories: Promise<Array<Category>>;
designers: Promise<Array<Designer>>;
copyrights: Promise<Array<Copyright>>;
references: Promise<Array<Product>>;
}
export interface ProductListResult {
pagination: PaginationResult;
@@ -128,6 +130,11 @@ export interface Batch {
products: Promise<Array<Product>>;
}
export interface Copyright {
name: string;
products: Promise<Array<Product>>;
}
export interface Product {
id: number;
stockid: number;
@@ -136,6 +143,9 @@ export interface Product {
browsable: boolean;
inserted: Date;
updated?: Date;
ref1: string;
ref2: string;
ref3: string;
orientation?: Orientation;
printProducts: [PrintProduct];
blacklisting: [ProductBlacklist];