-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from yalla-coop/179-products-graphql-2
setup single get product endpoint
- Loading branch information
Showing
5 changed files
with
97 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,62 @@ | ||
import { query } from '../connect.js'; | ||
|
||
async function getVariants() { | ||
return (await query(`SELECT * FROM fdc_variants order by product_id`)).rows | ||
return (await query(`SELECT * FROM fdc_variants order by product_id`)).rows; | ||
} | ||
|
||
async function getVariantsByProductId(productId) { | ||
return ( | ||
await query(`SELECT * FROM fdc_variants where product_id = $1`, [productId]) | ||
).rows; | ||
} | ||
|
||
async function variantCount() { | ||
return Number((await query(`SELECT count(*) FROM fdc_variants`)).rows[0].count); | ||
return Number( | ||
(await query(`SELECT count(*) FROM fdc_variants`)).rows[0].count | ||
); | ||
} | ||
|
||
async function getPagedVariants(lastId, limit) { | ||
return (await query(`SELECT * FROM fdc_variants where product_id > $1 order by product_id limit $2`, [lastId, limit])).rows | ||
return ( | ||
await query( | ||
`SELECT * FROM fdc_variants where product_id > $1 order by product_id limit $2`, | ||
[lastId, limit] | ||
) | ||
).rows; | ||
} | ||
|
||
function indexedByProductId(variants) { | ||
return variants.reduce((accumulator, row) => { | ||
const productId = row.productId; | ||
return { | ||
...accumulator, | ||
[productId]: accumulator[productId] ? [...accumulator[productId], row] : [row] | ||
}; | ||
}, {}); | ||
return variants.reduce((accumulator, row) => { | ||
const productId = row.productId; | ||
return { | ||
...accumulator, | ||
[productId]: accumulator[productId] | ||
? [...accumulator[productId], row] | ||
: [row] | ||
}; | ||
}, {}); | ||
} | ||
|
||
async function getAndAddVariantsToProducts(products) { | ||
return addVariantsToProducts(products, indexedByProductId(await getVariants())) | ||
return addVariantsToProducts( | ||
products, | ||
indexedByProductId(await getVariants()) | ||
); | ||
} | ||
|
||
function addVariantsToProducts(products, variantsByProductId){ | ||
return products.map(product => ({ ...product, fdcVariants: variantsByProductId[product.id] || [] })) | ||
function addVariantsToProducts(products, variantsByProductId) { | ||
return products.map((product) => ({ | ||
...product, | ||
fdcVariants: variantsByProductId[product.id] || [] | ||
})); | ||
} | ||
|
||
export { | ||
getVariants, | ||
getPagedVariants, | ||
indexedByProductId, | ||
variantCount, | ||
getAndAddVariantsToProducts, | ||
addVariantsToProducts | ||
} | ||
getVariants, | ||
getVariantsByProductId, | ||
getPagedVariants, | ||
indexedByProductId, | ||
variantCount, | ||
getAndAddVariantsToProducts, | ||
addVariantsToProducts | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import shopify from '../../../shopify.js'; | ||
import getSession from '../../../utils/getShopifySession.js'; | ||
import { createDFCProductsFromShopify } from '../dfc/dfc-products.js'; | ||
import { | ||
findFDCProducts, | ||
getFdcVariantsByProductIdFromDB | ||
} from './shopify/products.js'; | ||
|
||
const getProduct = async (req, res) => { | ||
try { | ||
const { EnterpriseName, ProductId } = req.params; | ||
|
||
const session = await getSession(`${EnterpriseName}.myshopify.com`); | ||
|
||
if (!session) { | ||
return res.status(401).json({ message: 'Unauthorised' }); | ||
} | ||
|
||
const client = new shopify.api.clients.Graphql({ session }); | ||
|
||
const fdcVariantsFromDB = await getFdcVariantsByProductIdFromDB(ProductId); | ||
|
||
if (Object.keys(fdcVariantsFromDB).length === 0) { | ||
return res.status(200).json('No product found'); | ||
} | ||
|
||
const fdcProduct = await findFDCProducts( | ||
client, | ||
Object.keys(fdcVariantsFromDB) | ||
); | ||
|
||
const dfcProducts = await createDFCProductsFromShopify( | ||
fdcProduct, | ||
fdcVariantsFromDB | ||
); | ||
|
||
return res.status(200).json(dfcProducts); | ||
} catch (error) { | ||
console.error('Unable to load products', error); | ||
throw new Error(error); | ||
} | ||
}; | ||
|
||
export default getProduct; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { Router } from 'express'; | ||
import getProducts from './controllers/get-products.js'; | ||
import getProduct from './controllers/get-product.js'; | ||
|
||
const fdcProductRoutes = Router({ mergeParams: true }); | ||
|
||
fdcProductRoutes.get('/', getProducts); | ||
fdcProductRoutes.get('/:ProductId', getProduct); | ||
|
||
export default fdcProductRoutes; |