Skip to content

Commit

Permalink
export fdc products
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanelnajjar committed Sep 6, 2023
1 parent 7b7b540 commit 937bbec
Show file tree
Hide file tree
Showing 12 changed files with 15,808 additions and 209 deletions.
15,548 changes: 15,548 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"body-parser": "^1.20.2",
"connect-ensure-login": "^0.1.1",
"connect-sqlite3": "^0.9.13",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express-session": "^1.17.3",
"nock": "^13.3.1",
Expand Down
13 changes: 8 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'path';
import { readFileSync } from 'fs';
import express from 'express';
import serveStatic from 'serve-static';

import cors from 'cors';
import apiRouters from './api-routers.js';
import fdcRouters from './fdc-routers.js';
import shopify from './shopify.js';
Expand Down Expand Up @@ -41,12 +41,17 @@ app.get(
shopify.redirectToShopifyOrAppRoot()
);

app.use('/fdc', express.json(), fdcRouters, errorMiddleware);
app.use('/fdc', cors(), express.json(), fdcRouters, errorMiddleware);
app.use('/api/*', shopify.validateAuthenticatedSession());

app.use('/*', addSessionShopToReqParams);

app.use('/api', express.json(), apiRouters);
app.use(
'/api',
express.json(),
express.urlencoded({ extended: true }),
apiRouters
);

app.use(serveStatic(STATIC_PATH, { index: false }));

Expand All @@ -66,6 +71,4 @@ app.post(
})
);

// add error handler

export default app;
9 changes: 6 additions & 3 deletions web/fdc-modules/products/controllers/get-products.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import ProductUseCases from '../use-cases/index.js';

const getProducts = async (req, res, next) => {
const getProducts = async (req, res) => {
try {
const {shopifySession} = req;
const { shopifySession } = req;
const { nextPageCursor, previousPageCursor } = req.query;

const products = await ProductUseCases.getProducts({
session: shopifySession
session: shopifySession,
nextPageCursor,
previousPageCursor
});

return res.status(200).json({
Expand Down
2 changes: 1 addition & 1 deletion web/fdc-modules/products/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Router} from 'express';
import { Router } from 'express';
import getProducts from './get-products.js';

import checkOfflineSession from '../../../middleware/checkOfflineSession.js';
Expand Down
8 changes: 6 additions & 2 deletions web/fdc-modules/products/use-cases/get-products.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import Shopify from '../../../fdc-repositories/shopify/index.js';

const getProducts = async ({ session }) => {
const products = await Shopify.getProducts({ session });
const getProducts = async ({ session, nextPageCursor, previousPageCursor }) => {
const products = await Shopify.getProducts({
session,
nextPageCursor,
previousPageCursor
});
return products;
};

Expand Down
2 changes: 1 addition & 1 deletion web/fdc-modules/products/use-cases/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import getProducts from './get-products.js';

export default {getProducts};
export default { getProducts };
39 changes: 27 additions & 12 deletions web/fdc-repositories/shopify/get-products.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import shopify from '../../shopify.js';
import { GET_FDC_PRODUCTS_QUERY } from './queries/GET_FDC_PRODUCTS_QUERY.js';
/* eslint-disable no-shadow */
/* eslint-disable arrow-body-style */
/* eslint-disable import/prefer-default-export */
/* eslint-disable max-len */
import { getFDCProductGraphQlQuery } from './queries/GET_FDC_PRODUCTS_QUERY.js';
import getClient from './get-client.js';

// get only products that have fdc inside tags
// this query works only for the first 10 products, we should loop through all products and get the ones that have fdc inside tags
export const getProducts = async ({ session }) => {
export const getProducts = async ({
session,
nextPageCursor,
previousPageCursor
}) => {
const client = getClient(session);

try {
const products = await client.query({
data: GET_FDC_PRODUCTS_QUERY
data: getFDCProductGraphQlQuery({
nextPageCursor,
previousPageCursor
})
});

return products.body.data.products.edges.map(({ node }) => {
return {
...node,
variants: node.variants.edges.map(({ node }) => node)
};
});
return {
pageInfo: products.body.data.products.pageInfo,
list: products.body.data.products.edges.map(({ node }) => {
return {
...node,
variants: {
hasNextPage: node.variants.pageInfo.hasNextPage,
list: node.variants.edges.map(({ node }) => node)
}
};
})
};
} catch (e) {
console.error('eee', JSON.stringify(e, null, 2));
throw e;
throw new Error(e);
}
};
2 changes: 1 addition & 1 deletion web/fdc-repositories/shopify/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getProducts} from './get-products.js';
import { getProducts } from './get-products.js';
import createDraftOrder from './create-draft-order.js';
import checkInventory from './check-inventory.js';
import updateInventoryLevel from './update-inventory-level.js';
Expand Down
74 changes: 43 additions & 31 deletions web/fdc-repositories/shopify/queries/GET_FDC_PRODUCTS_QUERY.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
// The max first value is 250
export const GET_FDC_PRODUCTS_QUERY = `{
products(first: 10, after: null, before: null) {
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
edges {
cursor
node {
id
title
description
variants(first: 5) {
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
edges {
node {
id
title
inventoryItem {
/* eslint-disable import/prefer-default-export */

export const getFDCProductGraphQlQuery = ({
nextPageCursor = 'null',
previousPageCursor = 'null'
}) => {
const after = nextPageCursor === 'null' ? '' : `after: "${nextPageCursor}"`;
const before =
previousPageCursor === 'null' ? '' : `before: "${previousPageCursor}"`;

return `{
products(first: 5, ${after}, ${before} ,query:"tag:fdc") {
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
edges {
cursor
node {
id
handle
title
description
variants(first: 10) {
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
edges {
node {
id
title
price
inventoryItem {
id
}
inventoryPolicy
availableForSale
}
inventoryPolicy
availableForSale
}
}
}
}
}
}
}`;
}`;
};
2 changes: 1 addition & 1 deletion web/fdc-routers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Router} from 'express';
import { Router } from 'express';

import OrdersModules from './fdc-modules/orders/index.js';
import ProductsModules from './fdc-modules/products/index.js';
Expand Down
Loading

0 comments on commit 937bbec

Please sign in to comment.