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
+1 -1
View File
@@ -46,7 +46,7 @@ interface Order {
customerCellphone: string; customerCellphone: string;
billingAddressId: number; billingAddressId: number;
deliveryAddressId: number; deliveryAddressId: number;
flyer_ids: string; flyerIds: string;
market: string; market: string;
locale: string; locale: string;
} }
+38 -8
View File
@@ -1,5 +1,11 @@
import { SQLDataSource } from 'datasource-sql'; import { SQLDataSource } from 'datasource-sql';
import * as sql from './sql'; import * as sql from './sql';
import {
Product,
ProductGroup,
PrintProduct,
ProductBlacklist,
} from './product-types';
const MINUTE = 60; const MINUTE = 60;
@@ -8,16 +14,40 @@ export class ProductAPI extends SQLDataSource {
super(config); super(config);
} }
async getProducts() { getBlacklisting(row: any): Array<ProductBlacklist> {
const query = sql.products(true, true, 2); 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 [ // console.log(JSON.stringify(data, null, 2));
{ id: 1, artNo: 'SE' },
{ id: 2, artNo: 'DK' }, return data;
];
} }
} }
+37
View File
@@ -0,0 +1,37 @@
export enum ProductGroup {
PHOTO_WALLPAPER = 1,
CANVAS = 2,
WALLPAPER = 3,
DO_IT_YOURSELF_FRAME = 5,
DESIGNER_WALLPAPER = 6,
POSTER = 7,
FRAMED_PRINT = 8,
}
export interface ProductBlacklist {
id: number;
groupId: number;
group: ProductGroup;
marketId: String;
inserted: Date;
updated: Date;
}
export interface PrintProduct {
id: number;
groupId: number;
group: ProductGroup;
inserted: Date;
updated?: Date;
}
export interface Product {
id: number;
path: string;
visible: boolean;
browsable: boolean;
inserted: Date;
updated?: Date;
printProducts: [PrintProduct];
blacklisting: [ProductBlacklist];
}
+28 -27
View File
@@ -6,46 +6,48 @@ export function products(
limit: number = 100, limit: number = 100,
) { ) {
const query = /* sql */ ` const query = /* sql */ `
SELECT products.productid,
SELECT products.productid as id,
products.path, products.path,
products.visible, products.visible,
products.browsable, products.browsable,
products.designerid, products.designerid,
products.inserted, products.inserted,
products.updated, products.updated,
json_agg( (
SELECT json_agg(
json_build_object( json_build_object(
'printid', 'printId',
printproducts.printid, "product-printproducts".printid,
'productid', 'productId',
printproducts.productid, "product-printproducts".productid,
'groupid', 'groupId',
printproducts.groupid, "product-printproducts".groupid,
'inserted', 'inserted',
printproducts.inserted, "product-printproducts".inserted,
'updated', 'updated',
printproducts.updated "product-printproducts".updated
) )
) AS print_products, ) FROM "product-printproducts" WHERE "product-printproducts".productid = products.productid
json_agg( ) AS printProducts,
( SELECT json_agg(
json_build_object( json_build_object(
'id', 'id',
blacklist.id, product_blacklist.id,
'product_id', 'productId',
blacklist.product_id, product_blacklist.product_id,
'market_id', 'marketId',
blacklist.market_id, product_blacklist.market_id,
'group_id', 'groupId',
blacklist.group_id, product_blacklist.group_id,
'created_at', 'inserted',
blacklist.created_at, product_blacklist.created_at,
'updated_at', 'updated',
blacklist.updated_at product_blacklist.updated_at
) )
) AS blacklisted ) FROM product_blacklist WHERE product_blacklist.product_id = products.productid
) AS blacklisting
FROM "product-products" products FROM "product-products" products
JOIN "product-printproducts" printproducts ON products.productid = printproducts.productid
JOIN product_blacklist blacklist ON products.productid = blacklist.product_id
${ ${
visible != null visible != null
? /* sql */ ` ? /* sql */ `
@@ -53,7 +55,6 @@ FROM "product-products" products
AND products.browsable = ${browsable ? '1' : '0'}` AND products.browsable = ${browsable ? '1' : '0'}`
: `` : ``
} }
GROUP BY products.productid GROUP BY products.productid
LIMIT ${limit} LIMIT ${limit}
`; `;
+2 -1
View File
@@ -1,6 +1,7 @@
// (parent, args, ctx, info) // (parent, args, ctx, info)
const products = { const products = {
products: (_, __, { dataSources }) => dataSources.productApi.getProducts(), products: (_, args, { dataSources }) =>
dataSources.productApi.getProducts(args),
}; };
export default products; export default products;
+45 -7
View File
@@ -6,15 +6,15 @@ const typeDefs = gql`
type Query { type Query {
orders: [Order] orders: [Order]
order(id: ID!): Order order(id: ID!): Order
products: [Product] products(limit: Int, visible: Boolean, browsable: Boolean): [Product]
} }
type Order { type Order {
id: ID! id: ID!
inserted: DateTime inserted: DateTime!
paid: Boolean paid: Boolean!
confirmed: Boolean confirmed: Boolean!
delivered: Boolean delivered: Boolean!
newsletter: Boolean newsletter: Boolean
creditInvoice: Boolean creditInvoice: Boolean
canceled: Boolean canceled: Boolean
@@ -53,15 +53,53 @@ const typeDefs = gql`
customerCellphone: String customerCellphone: String
billingAddressId: Int billingAddressId: Int
deliveryAddressId: Int deliveryAddressId: Int
flyer_ids: String flyerIds: String
market: String market: String
locale: String locale: String
} }
type Product { type Product {
id: ID! id: ID!
artNo: String! path: String!
visible: Boolean!
browsable: Boolean!
inserted: DateTime!
updated: DateTime
printProducts: [PrintProduct]
blacklisting: [ProductBlacklist]
}
enum ProductGroup {
PHOTO_WALLPAPER
CANVAS
WALLPAPER
DO_IT_YOURSELF_FRAME
DESIGNER_WALLPAPER
POSTER
FRAMED_PRINT
}
type ProductBlacklist {
id: ID!
groupId: Int!
group: ProductGroup!
marketId: Int!
inserted: DateTime!
updated: DateTime!
}
type PrintProduct {
id: ID!
groupId: Int!
group: ProductGroup!
inserted: DateTime!
updated: DateTime
} }
`; `;
/**
*/
export { typeDefs }; export { typeDefs };