const express = require("express"); const ikea = require("ikea-availability-checker"); const app = express(); const PORT = 3001; app.use(express.static("public")); app.get("/api/stock", async (req, res) => { const { store, product } = req.query; if (!product || !store) { return res.status(400).json({ error: "Missing product or store parameter" }); } try { const result = await ikea.availability(store, product); const Filterdresult = ({ productId: result.productId || null, stock: result.stock, probability: result.probability, store: result.store.name, buCode: result.store.buCode, country: result.store.country, }); res.json(Filterdresult); } catch (err) { res.status(500).json({ error: "Failed to fetch stock", details: err.message }); } }); app.get("/api/countrystock", async (req, res) => { const { countryCode, product } = req.query; if (!product || !countryCode) { return res.status(400).json({ error: "Missing product or countryCode parameter" }); } try { const stores = ikea.stores.findByCountryCode(countryCode); const result = await ikea.availabilities(stores, [product]); const Filterdresult = result.map(item => ({ productId: item.productId || null, stock: item.stock, probability: item.probability, store: item.store.name, buCode: item.store.buCode, country: item.store.country, })); res.json(Filterdresult); } catch (err) { res.status(500).json({ error: "Failed to fetch stock", details: err.message }); } }); app.get("/api/stores", async (req, res) => { const { countryCode } = req.query; if (!countryCode) { return res.status(400).json({ error: "Missing product or store parameter" }); } try { const result = await ikea.stores.findByCountryCode(countryCode); const Filterdresult = result.map(item => ({ buCode: item.buCode || null, name: item.name, country: item.country })); res.json(Filterdresult); } catch (err) { res.status(500).json({ error: "Failed to fetch stock", details: err.message }); } }); //For ChangeDetection.io instance app.get("/api/change", async (req, res) => { const { product, store } = req.query; if (!product || !store) { return res.status(400).send("Missing product or store"); } try { const result = await ikea.availability(store, product); const quantity = result.stock ?? 0; let inStock; if( quantity > 0 ) { inStock = "InStock" ; visualStock = "In stock" ; } else{ inStock = "OutOfStock" ; visualStock = "Out of stock" ; } res.send(`
Status: ${visualStock}
`); } catch (err) { res.type("text").send("error"); } }); app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); });