diff --git a/src/datasources/order-api.ts b/src/datasources/order-api.ts index cc57c02..a72372e 100644 --- a/src/datasources/order-api.ts +++ b/src/datasources/order-api.ts @@ -46,7 +46,7 @@ interface Order { customerCellphone: string; billingAddressId: number; deliveryAddressId: number; - flyer_ids: string; + flyerIds: string; market: string; locale: string; } diff --git a/src/datasources/product-api.ts b/src/datasources/product-api.ts index 2727f9b..836357b 100644 --- a/src/datasources/product-api.ts +++ b/src/datasources/product-api.ts @@ -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 { + 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 { + 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 { + 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; } } diff --git a/src/datasources/product-types.ts b/src/datasources/product-types.ts new file mode 100644 index 0000000..58450de --- /dev/null +++ b/src/datasources/product-types.ts @@ -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]; +} diff --git a/src/datasources/sql/products-sql.ts b/src/datasources/sql/products-sql.ts index 8e28b64..2ff9f34 100644 --- a/src/datasources/sql/products-sql.ts +++ b/src/datasources/sql/products-sql.ts @@ -6,46 +6,48 @@ export function products( limit: number = 100, ) { const query = /* sql */ ` -SELECT products.productid, - products.path, - products.visible, - products.browsable, - products.designerid, - products.inserted, - products.updated, - json_agg( + + SELECT products.productid as id, + products.path, + products.visible, + products.browsable, + products.designerid, + products.inserted, + products.updated, + ( + SELECT json_agg( json_build_object( - 'printid', - printproducts.printid, - 'productid', - printproducts.productid, - 'groupid', - printproducts.groupid, - 'inserted', - printproducts.inserted, - 'updated', - printproducts.updated - ) - ) AS print_products, - json_agg( + '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', - blacklist.id, - 'product_id', - blacklist.product_id, - 'market_id', - blacklist.market_id, - 'group_id', - blacklist.group_id, - 'created_at', - blacklist.created_at, - 'updated_at', - blacklist.updated_at - ) - ) AS blacklisted -FROM "product-products" products - JOIN "product-printproducts" printproducts ON products.productid = printproducts.productid - JOIN product_blacklist blacklist ON products.productid = blacklist.product_id + '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 ${ visible != null ? /* sql */ ` @@ -53,7 +55,6 @@ FROM "product-products" products AND products.browsable = ${browsable ? '1' : '0'}` : `` } - GROUP BY products.productid LIMIT ${limit} `; diff --git a/src/resolvers/products-resolver.ts b/src/resolvers/products-resolver.ts index 00d91de..0df18cf 100644 --- a/src/resolvers/products-resolver.ts +++ b/src/resolvers/products-resolver.ts @@ -1,6 +1,7 @@ // (parent, args, ctx, info) const products = { - products: (_, __, { dataSources }) => dataSources.productApi.getProducts(), + products: (_, args, { dataSources }) => + dataSources.productApi.getProducts(args), }; export default products; diff --git a/src/schema.ts b/src/schema.ts index d50ae52..8ee0694 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -6,15 +6,15 @@ const typeDefs = gql` type Query { orders: [Order] order(id: ID!): Order - products: [Product] + products(limit: Int, visible: Boolean, browsable: Boolean): [Product] } type Order { id: ID! - inserted: DateTime - paid: Boolean - confirmed: Boolean - delivered: Boolean + inserted: DateTime! + paid: Boolean! + confirmed: Boolean! + delivered: Boolean! newsletter: Boolean creditInvoice: Boolean canceled: Boolean @@ -53,15 +53,53 @@ const typeDefs = gql` customerCellphone: String billingAddressId: Int deliveryAddressId: Int - flyer_ids: String + flyerIds: String market: String locale: String } type Product { 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 };