45 lines
1.6 KiB
JavaScript
45 lines
1.6 KiB
JavaScript
// inventoryController.js
|
|
import buildAddItemRequestXML from "../utils/buildAddItemRequestXML.js";
|
|
import fetch from "node-fetch";
|
|
import fetchEbayUserToken from "../utils/fetchEbayUserToken.js"; // Adjust the import path according to your project structure
|
|
|
|
export const addItem = async (req, res) => {
|
|
const itemDetails = req.body; // Assuming item details are sent in the request body
|
|
itemDetails.userToken = await fetchEbayUserToken(); // Fetch and add the user token to itemDetails
|
|
|
|
const xmlRequest = buildAddItemRequestXML(itemDetails);
|
|
|
|
try {
|
|
const response = await fetch("https://api.ebay.com/ws/api.dll", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "text/xml",
|
|
"X-EBAY-API-SITEID": "0",
|
|
"X-EBAY-API-CALL-NAME": "AddItem",
|
|
"X-EBAY-API-COMPATIBILITY-LEVEL": "967", // Ensure this is the current compatibility level
|
|
"X-EBAY-API-APP-NAME": process.env.EBAY_CLIENT_ID,
|
|
"X-EBAY-API-DEV-NAME": process.env.EBAY_DEV_ID,
|
|
"X-EBAY-API-CERT-NAME": process.env.EBAY_CLIENT_SECRET,
|
|
},
|
|
body: xmlRequest,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`eBay API responded with status ${response.status}`);
|
|
}
|
|
|
|
const responseData = await response.text();
|
|
// Parse the XML response and handle it appropriately
|
|
// You might need an XML parser here to handle the response
|
|
|
|
res.json({ success: true, data: responseData }); // Send back a success response
|
|
} catch (error) {
|
|
console.error("Error adding item to eBay:", error);
|
|
res.status(500).json({
|
|
success: false,
|
|
message: "Failed to add item to eBay",
|
|
error: error.message,
|
|
});
|
|
}
|
|
};
|