Added products

This commit is contained in:
Niklas Fondberg
2021-04-07 16:06:55 +02:00
parent 949c641410
commit 59e385c144
6 changed files with 163 additions and 56 deletions
+38 -8
View File
@@ -1,5 +1,11 @@
import { SQLDataSource } from 'datasource-sql';
import * as sql from './sql';
import {
Product,
ProductGroup,
PrintProduct,
ProductBlacklist,
} from './product-types';
const MINUTE = 60;
@@ -8,16 +14,40 @@ export class ProductAPI extends SQLDataSource {
super(config);
}
async getProducts() {
const query = sql.products(true, true, 2);
getBlacklisting(row: any): Array<ProductBlacklist> {
if (!row.blacklisting) {
return [];
}
return row.blacklisting.map((bl: any) => {
bl.group = ProductGroup[bl.groupId];
return bl;
});
}
const data = await this.knex.raw(query);
getPrintProducts(row: any): Array<PrintProduct> {
if (!row.printProducts) {
return [];
}
return row.printProducts.map((pp: any) => {
pp.group = ProductGroup[pp.groupId];
return pp;
});
}
console.log(JSON.stringify(data.rows[0], null, 2));
async getProducts(args: any): Promise<Product> {
const query = sql.products(true, true, 10);
// console.log(query);
const data = await this.knex.raw(query).then((data) => {
// console.log(data);
return data.rows.map((row) => {
row.blacklisting = this.getBlacklisting(row);
row.printProducts = this.getPrintProducts(row);
return row;
});
});
return [
{ id: 1, artNo: 'SE' },
{ id: 2, artNo: 'DK' },
];
// console.log(JSON.stringify(data, null, 2));
return data;
}
}