diff --git a/apps/backend/src/index.ts b/apps/backend/src/index.ts index a26a264b..59308e99 100644 --- a/apps/backend/src/index.ts +++ b/apps/backend/src/index.ts @@ -26,6 +26,10 @@ import { import { retailRouter } from "./controllers/retail"; import { sendUpsolicieatedOnStatus } from "./lib/utils/sendUpsolicieatedOnStatus"; import { loadConfig } from "./lib/utils"; +import 'dotenv/config' +import path from 'path'; +import { sub } from 'date-fns/fp'; +const fs = require('fs'); export const app: Express = express(); @@ -71,6 +75,87 @@ app.use("/services", errorHandlingWrapper(servicesRouter)); app.use("/subscription", errorHandlingWrapper(subscriptionRouter)); app.use("/logistics", errorHandlingWrapper(logisticsRouter)); app.use("/agri", errorHandlingWrapper(agriRouter)); +app.use("/get-data", async (req, res) => { + // console.log("action",req.query,__dirname) + let { action, domain, subdomain,version } = req.query + console.log("---->",action, domain, subdomain,version) + let baseDir="" + switch (domain) { + case "agri": + baseDir = (subdomain === "ONDC:AGR11") ? path.join(__dirname, `../domain-repos/@${domain}/draft-agri_output/api/components/Examples/Agri_Bids_And_Auction/${action}/`) : path.join(__dirname, `../domain-repos/@${domain}/draft-agri_input/api/components/Examples/Agri_Products/${action}/`) + break; + case "logistics": + baseDir = (subdomain === "ONDC:LOG10")?path.join(__dirname, `../domain-repos/@${domain}/draft-2.x/api/components/Examples/B2B_Dom_Logistics/${action}/`):path.join(__dirname, `../domain-repos/@${domain}/draft-2.x/api/components/Examples/B2B_Int_Logistics/${action}/`) + break; + case "services": + switch (subdomain) { + case "ONDC:SRV11": + baseDir = path.join(__dirname, `../domain-repos/@${domain}/draft-services/api/components/Examples/Services_home_service/${action}/`) + break + case "ONDC:SRV13": + baseDir = path.join(__dirname, `../domain-repos/@${domain}/draft-healthcare-service/api/components/Examples/Health_care_services/${action}/`) + break; + case "ONDC:SRV16": + baseDir = path.join(__dirname, `../domain-repos/@${domain}/draft-astro/api/components/Examples/Astro_services/${action}/`) + break; + case "ONDC:SRV14": + baseDir = path.join(__dirname, `../domain-repos/@${domain}/draft-agri-services/api/components/Examples/Agriculture_services/${action}/`) + break + case "ONDC:SRV18": + baseDir = path.join(__dirname, `../domain-repos/@${domain}/draft-agri_bids_and_auction/api/components/Examples/Agri_Bids_And_Auction/${action}/`) + break; + case "ONDC:SRV18": + baseDir = path.join(__dirname, `../domain-repos/@${domain}/draft-agri_equipment/api/components/Examples/Agri_Equipment_Hiring/${action}/`) + break + } + break; + case "retail": + baseDir = (version==="b2c")?path.join(__dirname, `../domain-repos/@${domain}-b2b/b2c_exports_2.0/api/components/Examples/B2C_Exports_Json/${action}/`):path.join(__dirname, `../domain-repos/@${domain}-b2b/release-2.0.2/api/components/Examples/B2B_json/${action}/`) + break; + case "subscription": + baseDir = path.join(__dirname, `../domain-repos/@mec/draft-print_media/api/components/Examples/Print_Content/${action}/`) + break; + } + try { + // Read all files in the directory + const files = fs.readdirSync(baseDir); + + // Find the first JSON file in the directory + const jsonFile = files.find((file:any) => file.endsWith(".json")); + + if (jsonFile) { + // Construct the full file path + const filePath = path.join(baseDir, jsonFile); + console.log("File path:", filePath); + + // Read the content of the JSON file + fs.readFile(filePath, "utf8", (err:any, data:any) => { + if (err) { + console.error("Error reading file:", err); + return res.status(500).send({ error: "Error reading file" }); + } + + try { + // Parse JSON and send as a response + const jsonData = JSON.parse(data); + res.send({ data: jsonData }); + } catch (parseError) { + console.error("Error parsing JSON:", parseError); + res.status(500).send({ error: "Invalid JSON format in the file" }); + } + }); + + } else { + res.send({ data: "No file exist for this" }); + // throw new Error(`No JSON file found in directory: ${baseDir}`); + } + + + } catch (error) { + console.log(error) + } + +}) app.use("/detect_app_installation", (req: Request, res: Response) => { const headers = req.headers; return res.json({ diff --git a/apps/frontend/src/components/MockRequestSection.tsx b/apps/frontend/src/components/MockRequestSection.tsx index 4e1bbebd..4d29544f 100644 --- a/apps/frontend/src/components/MockRequestSection.tsx +++ b/apps/frontend/src/components/MockRequestSection.tsx @@ -12,13 +12,16 @@ import Grid from "@mui/material/Grid"; import Paper from "@mui/material/Paper"; import Stack from "@mui/material/Stack"; import Typography from "@mui/material/Typography"; +import AddIcon from "@mui/icons-material/Add"; import { useEffect, useState } from "react"; import { CurlDisplay } from "."; import { useAction, useDomain, useMock } from "../utils/hooks"; -import { URL_MAPPING } from "../utils"; +import { All_Actions, ALL_SUB_DOMAINS, URL_MAPPING } from "../utils"; import axios, { AxiosError } from "axios"; import { UserGuide } from "./UserGuideSection"; import { VITE_SERVER_URL } from "../utils/env"; +import { Fab } from "@mui/material"; +import { VERSION } from "lodash"; // type MockRequestSectionProp = { // domain: string; @@ -34,6 +37,9 @@ export const MockRequestSection = () => { const { domain } = useDomain(); const [version, setVersion] = useState(""); + const [show, setshow] = useState(false); + const [Action, setAction] = useState(""); + const [Domain, setDomain] = useState(""); const { action, detectAction, logError, scenarios, setLogError } = useAction(); const { setAsyncResponse, setSyncResponse } = useMock(); @@ -41,7 +47,36 @@ export const MockRequestSection = () => { useEffect(() => { setLog(""); setLogError(false); + }, [domain]); + + async function call() { + const response = await axios.get("http://localhost:3000/get-data",{ + params: { + action: Action.toLowerCase(), + domain: domain, + subdomain:Domain, + version:version + } + }); + return response; + } +// console.log(domain,"Domainnn") + useEffect(() => { + console.log("ACTION CHANGED:", Action); + + const fetchData = async () => { + const data = await call(); + if (data.data.data) { + console.log("Response from backend:", data); + setLog(JSON.stringify(data.data.data)) + detectAction(JSON.stringify(data.data.data) , version); + } + }; + + if(Action !="" && Domain !=""){fetchData();} + }, [Action,Domain,version]); + useEffect(() => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore @@ -68,15 +103,40 @@ export const MockRequestSection = () => { } }; + const handleAction = (event: + | React.MouseEvent + | React.KeyboardEvent + | React.FocusEvent + | null, + value: {} | null) => { + console.log("action", event); + setAction(value as string); // Ensure value is a string and set the version + }; + + const handledomain = (event: + | React.MouseEvent + | React.KeyboardEvent + | React.FocusEvent + | null, + value: {} | null) => { + console.log("action", event); + setDomain(value as string); // Ensure value is a string and set the version + }; + const handleLogChange = (e: React.ChangeEvent) => { + console.log("insidelog",e.target.value) setLog(e.target.value); detectAction(e.target.value, version); }; + const handleclick = () => { + setshow(!show); + }; + const handleSubmit = async () => { - let url = `${ - VITE_SERVER_URL - }/${domain.toLowerCase()}/${Object.keys(URL_MAPPING).filter((key) => + let url = `${VITE_SERVER_URL}/${domain.toLowerCase()}/${Object.keys( + URL_MAPPING + ).filter((key) => URL_MAPPING[key as keyof typeof URL_MAPPING].includes(action as string) )}/${action}?mode=mock&version=${version}`; if (activeScenario?.scenario) @@ -116,6 +176,59 @@ export const MockRequestSection = () => { > Mock Server + + {show && (<> + + + )} + + + + + {domain === "retail" && (