2021-03-27 12:20:52 +07:00
|
|
|
const express = require('express')
|
2021-03-30 17:04:31 +07:00
|
|
|
const axios = require("axios")
|
2021-03-26 13:26:14 +07:00
|
|
|
// const file = require('fs')
|
2021-03-26 08:42:51 +07:00
|
|
|
|
2021-03-26 13:26:14 +07:00
|
|
|
// file.readFile('./data/people.csv', 'utf-8', (err, data) => {
|
|
|
|
// if (err) {
|
|
|
|
// console.error(err)
|
|
|
|
// return
|
|
|
|
// }
|
2021-03-26 08:42:51 +07:00
|
|
|
|
2021-03-26 13:26:14 +07:00
|
|
|
// console.log(data)
|
2021-03-26 08:42:51 +07:00
|
|
|
|
2021-03-26 13:26:14 +07:00
|
|
|
// const rows = data.split(/\r?\n/);
|
|
|
|
// const header = rows[0].split(',').map(str => str.trim())
|
|
|
|
// console.log(header)
|
2021-03-26 08:42:51 +07:00
|
|
|
|
2021-03-26 13:26:14 +07:00
|
|
|
// });
|
2021-03-26 08:42:51 +07:00
|
|
|
|
2021-03-27 12:20:52 +07:00
|
|
|
// var XLSX = require('xlsx')
|
|
|
|
// var workbook = XLSX.readFile('./data/Sanagro Product and Catagory list.xlsx');
|
|
|
|
// var xlData = XLSX.utils.sheet_to_json(workbook.Sheets['Sheet1']);
|
|
|
|
// var data = xlData.map((row) => ({ id: row.No, name: row['Product name'] }))
|
|
|
|
|
|
|
|
var app = express();
|
|
|
|
app.get('/', function (req, res) {
|
|
|
|
res.sendFile('/upload.html', { root: __dirname })
|
|
|
|
});
|
|
|
|
|
2021-03-30 11:28:16 +07:00
|
|
|
let port = 5000
|
2021-03-27 12:20:52 +07:00
|
|
|
app.listen(port, () => {
|
|
|
|
console.log(`Example app listening at http://localhost:${port}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
var multer = require('multer');
|
2021-03-30 17:04:31 +07:00
|
|
|
const { default: fetch } = require('node-fetch');
|
2021-03-27 12:20:52 +07:00
|
|
|
var upload = multer();
|
2021-03-26 13:26:14 +07:00
|
|
|
var XLSX = require('xlsx')
|
2021-03-27 12:20:52 +07:00
|
|
|
app.use(upload.none());
|
|
|
|
app.post('/upload', async function (req, res) {
|
|
|
|
let input = req.body.myFile;
|
2021-03-30 11:28:16 +07:00
|
|
|
let wb = XLSX.read(input, { type: 'binary' });
|
2021-03-27 12:20:52 +07:00
|
|
|
|
2021-03-31 18:08:19 +07:00
|
|
|
let xlData = XLSX.utils.sheet_to_json(wb.Sheets['Sheet1']);
|
2021-03-31 10:11:48 +07:00
|
|
|
let data = xlData.map((row) => ({ productName: row['Product name'], smallTypeName: row.SmallTypeName, productCategory: { productCategoryId: row['CategoryID'], newCategory: row['Category'] }, alert: row.alert, cost: row.cost, qtyStock: row.qtystock, sellPrice: row.sellPrice }))
|
2021-03-27 12:20:52 +07:00
|
|
|
res.send(data)
|
2021-03-30 17:04:31 +07:00
|
|
|
|
|
|
|
|
2021-03-31 10:11:48 +07:00
|
|
|
var fileData = JSON.stringify(data)
|
|
|
|
console.log(fileData)
|
|
|
|
|
2021-03-30 17:04:31 +07:00
|
|
|
axios({
|
|
|
|
url: 'http://localhost:4000/graphql',
|
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImlhdCI6MTYxNzA3OTYyOCwiZXhwIjoxNjE3MzM4ODI4fQ.p5oAaT3htRbBgKimfr7hxfjNwjWPc5X1finX2zBZ9aM"
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
query: `
|
|
|
|
mutation{
|
2021-03-31 10:11:48 +07:00
|
|
|
createManyProducts(file: ${JSON.stringify(fileData)})
|
2021-03-30 17:04:31 +07:00
|
|
|
}
|
2021-03-31 10:11:48 +07:00
|
|
|
`,
|
2021-03-30 17:04:31 +07:00
|
|
|
}
|
|
|
|
}).then((result) => {
|
|
|
|
console.log(result.data)
|
|
|
|
});
|
2021-03-27 12:20:52 +07:00
|
|
|
})
|
2021-03-30 11:28:16 +07:00
|
|
|
|
2021-03-27 12:20:52 +07:00
|
|
|
|
2021-03-30 17:04:31 +07:00
|
|
|
|
|
|
|
|