You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// backend - server.js - Routes API requests from the frontend to Kintone// Express Server Setupimportexpress,{json}from'express';importcorsfrom'cors';import*asdotenvfrom'dotenv'dotenv.config({path: '../../.env'})constPORT=50000;constapp=express();// Get Kintone credentials from a .env fileconstsubdomain=process.env.SUBDOMAIN;constappID=process.env.APPID;constapiToken=process.env.APITOKEN;// Parse incoming requests with JSON payloadsapp.use(express.json());// Set Cross-Origin Resource Sharing (CORS) to frontend React Appapp.use(cors());constcorsOptions={origin: 'http://localhost:3000'};// Kintone's record(s) endpointsconstmultipleRecordsEndpoint=`https://${subdomain}.kintone.com/k/v1/records.json?app=${appID}`letcheckItemStock=async()=>{constfetchOptions={method: 'GET',headers: {'X-Cybozu-API-Token': apiToken}}constresponse=awaitfetch(multipleRecordsEndpoint,fetchOptions);returnresponse.json();}letcompareRequestAndStock=async(stock,request)=>{letvalidItems=[];request.forEach(requestedItem=>{letstockItem=stock.find((item)=>item.Record_number.value===requestedItem.id.toString())if(stockItem.count.value>=requestedItem.count){letnewItem=requestedItem;newItem.count=stockItem.count.value-requestedItem.count;validItems.push(newItem)}});returnvalidItems;}// This route executes when a PUT request lands on localhost:50000/putDataapp.put('/putData',cors(corsOptions),async(req,res)=>{letitemStock=awaitcheckItemStock();letfilteredRequest=req.body.filter((item)=>item.count>=1)letitemsToUpdate=awaitcompareRequestAndStock(itemStock.records,filteredRequest);if(itemsToUpdate.length!=filteredRequest.length){res.status(404).json({success: false,response: "not in stock"});return;}constrequestBody={'app': appID,'records': []};itemsToUpdate.forEach(item=>{requestBody.records.push({'id': item.id,'record': {'count': {'value': item.count}}},)});constoptions={method: 'PUT',headers: {'X-Cybozu-API-Token': apiToken,'Content-Type': 'application/json',},body: JSON.stringify(requestBody)}constresponse=awaitfetch(multipleRecordsEndpoint,options);constjsonResponse=awaitresponse.json();res.status(200).json({success: true,response: jsonResponse})return});app.listen(PORT,()=>{console.log(`\n Backend server listening at http://localhost:${PORT} \n Confirm if Kintone records are being retrieved at \n http://localhost:${PORT}/putData`);});