Break out ref and copyright to own responses (#79)
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
|||||||
ProductSizeTypes,
|
ProductSizeTypes,
|
||||||
ProductProportionsInput,
|
ProductProportionsInput,
|
||||||
Batch,
|
Batch,
|
||||||
|
Copyright,
|
||||||
} from '../types/product-types';
|
} from '../types/product-types';
|
||||||
import { Maybe } from '../types/types';
|
import { Maybe } from '../types/types';
|
||||||
import {
|
import {
|
||||||
@@ -240,36 +241,38 @@ export class ProductAPI extends BaseSQLDataSource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async searchByBatch(name: string): Promise<Array<Batch>> {
|
async searchByBatch(name: string): Promise<Array<Batch>> {
|
||||||
const query = sql.searchByFieldValue(36); // batch
|
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);
|
||||||
|
|
||||||
const batchRes = await this.knex
|
const rows = await this.knex
|
||||||
.raw(query, name)
|
.raw(query, name)
|
||||||
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
||||||
|
|
||||||
const batches = {};
|
const result = {};
|
||||||
batchRes.forEach((p) => {
|
rows.forEach((p) => {
|
||||||
if (!batches[p.fields.batch]) {
|
if (!result[p.fields[fieldKey]]) {
|
||||||
batches[p.fields.batch] = [];
|
result[p.fields[fieldKey]] = [];
|
||||||
}
|
}
|
||||||
batches[p.fields.batch].push(p);
|
result[p.fields[fieldKey]].push(p);
|
||||||
});
|
});
|
||||||
|
|
||||||
return Object.keys(batches).map((key) => {
|
return Object.keys(result).map((key) => {
|
||||||
return <Batch>{
|
return {
|
||||||
name: key,
|
name: key,
|
||||||
products: batches[key],
|
products: result[key],
|
||||||
};
|
} as unknown as T;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async searchByCopyright(name: string): Promise<Array<Product>> {
|
|
||||||
const query = sql.searchByFieldValue(25); // copyright
|
|
||||||
|
|
||||||
return this.knex
|
|
||||||
.raw(query, name)
|
|
||||||
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
|
|
||||||
}
|
|
||||||
|
|
||||||
async searchByReferences(name: string): Promise<Array<Product>> {
|
async searchByReferences(name: string): Promise<Array<Product>> {
|
||||||
const query = sql.referenceProducts(); // ref1, ref2, ref3
|
const query = sql.referenceProducts(); // ref1, ref2, ref3
|
||||||
|
|
||||||
|
|||||||
@@ -84,8 +84,6 @@ async function getProductsSearchResult(
|
|||||||
const prods = Promise.all([
|
const prods = Promise.all([
|
||||||
prodApi.searchByName(q),
|
prodApi.searchByName(q),
|
||||||
prodApi.searchByArtNo(q),
|
prodApi.searchByArtNo(q),
|
||||||
prodApi.searchByCopyright(q),
|
|
||||||
prodApi.searchByReferences(q),
|
|
||||||
]).then((d) => d.flat());
|
]).then((d) => d.flat());
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -95,6 +93,8 @@ async function getProductsSearchResult(
|
|||||||
keywords: keywApi.searchKeywords(q),
|
keywords: keywApi.searchKeywords(q),
|
||||||
categories: catApi.searchCategories(q),
|
categories: catApi.searchCategories(q),
|
||||||
designers: designerApi.searchDesigners(q),
|
designers: designerApi.searchDesigners(q),
|
||||||
|
copyrights: prodApi.searchByCopyright(q),
|
||||||
|
references: prodApi.searchByReferences(q),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,6 +103,8 @@ type ProductsSearchResult {
|
|||||||
keywords: [Keyword]
|
keywords: [Keyword]
|
||||||
categories: [Category]
|
categories: [Category]
|
||||||
designers: [Designer]
|
designers: [Designer]
|
||||||
|
copyrights: [Copyright]
|
||||||
|
references: [Product]
|
||||||
}
|
}
|
||||||
|
|
||||||
input InteriorsFilterInput {
|
input InteriorsFilterInput {
|
||||||
@@ -277,6 +279,11 @@ type Batch {
|
|||||||
products: [Product]
|
products: [Product]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Copyright {
|
||||||
|
name: String!
|
||||||
|
products: [Product]
|
||||||
|
}
|
||||||
|
|
||||||
type Product {
|
type Product {
|
||||||
id: ID!
|
id: ID!
|
||||||
stockid: Int
|
stockid: Int
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ export interface ProductsSearchResult {
|
|||||||
keywords: Promise<Array<Keyword>>;
|
keywords: Promise<Array<Keyword>>;
|
||||||
categories: Promise<Array<Category>>;
|
categories: Promise<Array<Category>>;
|
||||||
designers: Promise<Array<Designer>>;
|
designers: Promise<Array<Designer>>;
|
||||||
|
copyrights: Promise<Array<Copyright>>;
|
||||||
|
references: Promise<Array<Product>>;
|
||||||
}
|
}
|
||||||
export interface ProductListResult {
|
export interface ProductListResult {
|
||||||
pagination: PaginationResult;
|
pagination: PaginationResult;
|
||||||
@@ -128,6 +130,11 @@ export interface Batch {
|
|||||||
products: Promise<Array<Product>>;
|
products: Promise<Array<Product>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface Copyright {
|
||||||
|
name: string;
|
||||||
|
products: Promise<Array<Product>>;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Product {
|
export interface Product {
|
||||||
id: number;
|
id: number;
|
||||||
stockid: number;
|
stockid: number;
|
||||||
@@ -136,6 +143,9 @@ export interface Product {
|
|||||||
browsable: boolean;
|
browsable: boolean;
|
||||||
inserted: Date;
|
inserted: Date;
|
||||||
updated?: Date;
|
updated?: Date;
|
||||||
|
ref1: string;
|
||||||
|
ref2: string;
|
||||||
|
ref3: string;
|
||||||
orientation?: Orientation;
|
orientation?: Orientation;
|
||||||
printProducts: [PrintProduct];
|
printProducts: [PrintProduct];
|
||||||
blacklisting: [ProductBlacklist];
|
blacklisting: [ProductBlacklist];
|
||||||
|
|||||||
Reference in New Issue
Block a user