Break out ref and copyright to own responses (#79)
This commit is contained in:
@@ -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>> {
|
||||
|
||||
Reference in New Issue
Block a user