import { dutchieClient } from "./dutchieGraphql/client"; import { MenuSectionFilterType, MenuType, ProductFragmentFragment, } from "./dutchieGraphql/sdk"; export const extractErrorMessage = (message: string | undefined): string => { try { if (message) { const start = message.indexOf('{"'); if (start > 2) { return message.substring(0, start - 2); } else { return message; } } else { return "Network Error"; } } catch (e) { return message || "Network Error"; } }; export const weightToMg = (weight: string | undefined) => { if (weight) { const mgRex = new RegExp("([0-9.]+)mg"); const mgRst = weight.match(mgRex); if (mgRst) { return parseFloat(mgRst[1]); } else { const gReg = new RegExp("([0-9.]+)g"); const gRst = weight.match(gReg); if (gRst) { return parseFloat(gRst[1]) * 1000; } else { const fracRex = new RegExp("([0-9]+)/([0-9]+)oz"); const fracRst = weight.match(fracRex); if (fracRst) { return (parseInt(fracRst[1]) * 28350) / parseInt(fracRst[2]); } else { const ozRex = new RegExp("([0-9.]+)oz"); const ozRst = weight.match(ozRex); if (ozRst) { return parseFloat(ozRst[1]) * 28350; } else { return 0; } } } } } else { return 0; } }; const pause = async (ms: number) => { return new Promise((resolve) => setTimeout(resolve, ms)); }; const DUTCHIE_MENU_PER_PAGE = 200; export const fetchAllDutchieProducts = async ( retailerERPId: string, menuType: MenuType ) => { const rv: ProductFragmentFragment[] = []; const publicDutchieClient = dutchieClient(); while (true) { const offset = rv.length; const dutchieQuery = { retailerId: retailerERPId, menuType: menuType, pagination: { limit: DUTCHIE_MENU_PER_PAGE, offset, }, }; const menuResp = await publicDutchieClient.MenuQuery(dutchieQuery); const { products, productsCount } = menuResp.data?.menu || {}; if (products && products.length > 0 && productsCount) { for (let idx = 0; idx < products.length; idx++) { rv.push(products[idx]); } if (rv.length >= productsCount) { break; } } else { break; } await pause(50); } return rv; }; export const fetchAllDutchieSpecialProductIds = async ( retailerERPId: string, specialERPId: string[] ) => { const rv: string[] = []; const publicDutchieClient = dutchieClient(); while (true) { const offset = rv.length; const dutchieQuery = { retailerId: retailerERPId, menuSectionFilter: { specialId: specialERPId, type: MenuSectionFilterType.Specials, }, pagination: { limit: DUTCHIE_MENU_PER_PAGE, offset, }, }; const menuResp = await publicDutchieClient.fetchSpecialProducts( dutchieQuery ); const { products, productsCount } = menuResp.data?.menu || {}; if (products && products.length > 0 && productsCount) { for (let idx = 0; idx < products.length; idx++) { rv.push(products[idx].id); } if (rv.length >= productsCount) { break; } } else { break; } await pause(50); } return rv; };