Added products
This commit is contained in:
@@ -46,7 +46,7 @@ interface Order {
|
||||
customerCellphone: string;
|
||||
billingAddressId: number;
|
||||
deliveryAddressId: number;
|
||||
flyer_ids: string;
|
||||
flyerIds: string;
|
||||
market: string;
|
||||
locale: string;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
@@ -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}
|
||||
`;
|
||||
|
||||
@@ -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;
|
||||
|
||||
+45
-7
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user