-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
84 lines (74 loc) · 2.19 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// backend - server.js - Routes API requests from the frontend to Kintone
// Express Server Setup
import express, { json } from 'express';
import cors from 'cors';
import * as dotenv from 'dotenv'
dotenv.config({
path: '../../.env'
})
const PORT = 50000;
const app = express();
// Get Kintone credentials from a .env file
const subdomain = process.env.SUBDOMAIN;
const appID = process.env.APPID;
const apiToken = process.env.APITOKEN;
// Parse incoming requests with JSON payloads
app.use(express.json());
// Set Cross-Origin Resource Sharing (CORS) to frontend React App
app.use(cors());
const corsOptions = {
origin: 'http://localhost:3000'
};
// Kintone's record(s) endpoints
const multipleRecordsEndpoint = `https://${subdomain}.kintone.com/k/v1/records.json?app=${appID}`
let checkItemStock = async () => {
// TODO!
}
let compareRequestAndStock = async (stock, request) => {
// TODO. This one is hard!
}
// This route executes when a PUT request lands on localhost:50000/putData
app.put('/putData', cors(corsOptions), async (req, res) => {
let itemStock = await checkItemStock();
let filteredRequest = req.body.filter((item) => item.count >= 1)
let itemsToUpdate = await compareRequestAndStock(itemStock.records, filteredRequest);
if (itemsToUpdate.length != filteredRequest.length) {
res.status(404).json({
success: false,
response: "not in stock"
});
return;
}
const requestBody = {
'app': appID,
'records': []
};
itemsToUpdate.forEach(item => {
requestBody.records.push({
'id': item.id,
'record': {
'count': {
'value': item.count
}
}
},)
});
const options = {
method: 'PUT',
headers: {
'X-Cybozu-API-Token': apiToken,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody)
}
const response = await fetch(multipleRecordsEndpoint, options);
const jsonResponse = await response.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`);
});