Add product

This commit is contained in:
Niklas Fondberg
2021-04-08 15:09:31 +02:00
parent e0f629d54d
commit f60a51a94b
11 changed files with 184 additions and 171 deletions
+26 -2
View File
@@ -1,6 +1,12 @@
import { SQLDataSource } from 'datasource-sql';
import { camelcase } from 'stringcase';
import { Address, ContactInformation, Order, OrderRow } from './order-types';
import {
Address,
ContactInformation,
Market,
Order,
OrderRow,
} from './order-types';
import { Maybe } from './types';
const MINUTE = 60;
@@ -9,6 +15,24 @@ export class OrderAPI extends SQLDataSource {
super(config);
}
async getMarketById(id: number): Promise<Market> {
return await this.knex
.select('*')
.from('markets')
.where('id', id)
.first()
.cache(MINUTE);
}
async getMarketByName(name: string): Promise<Market> {
return await this.knex
.select('*')
.from('markets')
.where('name', name)
.first()
.cache(MINUTE);
}
async getAddress(addressId: number): Promise<Maybe<Address>> {
const row = await this.knex
.select('*')
@@ -103,7 +127,6 @@ export class OrderAPI extends SQLDataSource {
}
const orderRow = details.reduce((obj, item) => {
obj[camelcase(fields[item.fieldid])] = item.value;
obj['insertedDate'] = item.inserted;
return obj;
}, {});
@@ -120,6 +143,7 @@ export class OrderAPI extends SQLDataSource {
}
/**
* row
{
printId: '73963',
inserted: 2021-02-04T10:11:41.093Z,
+9 -1
View File
@@ -1,3 +1,11 @@
export interface Market {
id: number;
name: string;
vat: number;
currency: string;
priceAdjustment: number;
}
export interface Address {
id: number;
firstname: string;
@@ -71,7 +79,7 @@ export interface Order {
export interface OrderRow {
id: number;
insertedDate: Date;
inserted: Date;
orderId: number;
productId: number;
productGroup: string;
+13 -5
View File
@@ -39,21 +39,29 @@ export class ProductAPI extends SQLDataSource {
limit: Maybe<number>,
visible: Maybe<boolean>,
browsable: Maybe<boolean>,
): Promise<Product> {
// console.log(limit, browsable, visible);
): Promise<Array<Product>> {
limit = limit ?? 100;
const query = sql.products(limit, visible, browsable);
const data = await this.knex.raw(query).then((data) =>
return await this.knex.raw(query).then((data) =>
data.rows.map((row) => {
row.blacklisting = this.getBlacklisting(row);
row.printProducts = this.getPrintProducts(row);
return row;
}),
);
}
// console.log(JSON.stringify(data, null, 2));
async getProduct(id: number): Promise<Maybe<Product>> {
const query = sql.product(id);
return data;
const res = await this.knex.raw(query).then((data) =>
data.rows.map((row) => {
row.blacklisting = this.getBlacklisting(row);
row.printProducts = this.getPrintProducts(row);
return row;
}),
);
return res.find(Boolean);
}
}
+58 -45
View File
@@ -2,54 +2,59 @@ import { Maybe } from '../types';
// Get syntax highlighting with vscode by installing "Comment tagged templates2
const baseQuery = /* sql */ `
SELECT products.productid as id,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
(
SELECT json_agg(
json_build_object(
'printId',
"product-printproducts".printid,
'productId',
"product-printproducts".productid,
'groupId',
"product-printproducts".groupid,
'inserted',
"product-printproducts".inserted,
'updated',
"product-printproducts".updated
)
) FROM "product-printproducts" WHERE "product-printproducts".productid = products.productid
) AS printProducts,
( SELECT json_agg(
json_build_object(
'id',
product_blacklist.id,
'productId',
product_blacklist.product_id,
'marketId',
product_blacklist.market_id,
'groupId',
product_blacklist.group_id,
'inserted',
product_blacklist.created_at,
'updated',
product_blacklist.updated_at
)
) FROM product_blacklist WHERE product_blacklist.product_id = products.productid
) AS blacklisting
FROM "product-products" products
`;
export function products(
limit: number = 100,
visible: Maybe<boolean>,
browsable: Maybe<boolean>,
) {
const query = /* sql */ `
SELECT products.productid as id,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
(
SELECT json_agg(
json_build_object(
'printId',
"product-printproducts".printid,
'productId',
"product-printproducts".productid,
'groupId',
"product-printproducts".groupid,
'inserted',
"product-printproducts".inserted,
'updated',
"product-printproducts".updated
)
) FROM "product-printproducts" WHERE "product-printproducts".productid = products.productid
) AS printProducts,
( SELECT json_agg(
json_build_object(
'id',
product_blacklist.id,
'productId',
product_blacklist.product_id,
'marketId',
product_blacklist.market_id,
'groupId',
product_blacklist.group_id,
'inserted',
product_blacklist.created_at,
'updated',
product_blacklist.updated_at
)
) FROM product_blacklist WHERE product_blacklist.product_id = products.productid
) AS blacklisting
FROM "product-products" products
return (
baseQuery +
/* sql */ `
${
visible != null
? /* sql */ `
@@ -58,7 +63,15 @@ export function products(
: ``
}
LIMIT ${limit}
`;
`
);
}
return query;
export function product(id: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid = ${id}
`
);
}