Browse Source

Add Service to server

master
Hou Samnang 1 month ago
parent
commit
384404dbc7
  1. 6
      .env
  2. 1
      data.json
  3. 8
      data/config.json
  4. BIN
      data/data.xlsx
  5. 1
      exported.json
  6. 6
      package.json
  7. 22
      src/index.js
  8. 11
      src/test.js

6
.env

@ -0,0 +1,6 @@
APP_NAME=custom app name
INPUT_FILE=./data/mydata.xlsx
OUTPUT_PATH=./data/outputs
MAPPER_FILE=./data/mapper.json
SHEET_NAME=Sheet1
ENCODING=utf-8

1
data.json

@ -0,0 +1 @@
[{"productName":"សំណាង"},{"productName":"EGG01"},{"productName":"EGG02"},{"productName":"ភាស"},{"productName":"ភាស"},{"productName":"EGG01"}]

8
data/config.json

@ -0,0 +1,8 @@
{
"data": [
{
"dataIndex": "Product name",
"label": "productName"
}
]
}

BIN
data/Sanagro Product and Catagory list.xlsx → data/data.xlsx

Binary file not shown.

1
exported.json

@ -0,0 +1 @@
[{"productName":"EGG00"},{"productName":"EGG01"},{"productName":"EGG02"},{"productName":"EGG03"},{"productName":"S700-30K"},{"productName":"S700-5K"},{"productName":"S701-30K"},{"productName":"S701-5K"},{"productName":"S703M"},{"productName":"S703P"},{"productName":"S704M"},{"productName":"S704P"},{"productName":"S705M"},{"productName":"S705P"},{"productName":"S705XM"},{"productName":"S705XP"},{"productName":"S706M"},{"productName":"S706P"},{"productName":"S707M"},{"productName":"S707P"},{"productName":"S710C"},{"productName":"S711C"},{"productName":"S712P"},{"productName":"S715C"},{"productName":"S716C"},{"productName":"S716P"},{"productName":"S719C"},{"productName":"S720C"},{"productName":"S730P"},{"productName":"S731P"},{"productName":"S732P"},{"productName":"S733P"},{"productName":"S736P"},{"productName":"S740-30K"},{"productName":"S740-5K"},{"productName":"S741-30K"},{"productName":"S741-5K"},{"productName":"S743M"},{"productName":"S743P"},{"productName":"S744M"},{"productName":"S744P"},{"productName":"S745M"},{"productName":"S745P"},{"productName":"S745XM"},{"productName":"S745XP"},{"productName":"S746M"},{"productName":"S746P"},{"productName":"S747M"},{"productName":"S747P"},{"productName":"S750C"},{"productName":"S751C"},{"productName":"S752P"},{"productName":"S755C"},{"productName":"S756C"},{"productName":"S756P"},{"productName":"S765P"},{"productName":"S766P"},{"productName":"S767P"},{"productName":"S768P"},{"productName":"S790-30K"},{"productName":"S790-5K"},{"productName":"S797-30K"},{"productName":"S797-5K"},{"productName":"S798-30K"},{"productName":"S798-5K"},{"productName":"S799-30K"},{"productName":"S799-5K"},{"productName":"S803M"},{"productName":"S803P"},{"productName":"Bag"},{"productName":"B700-30K"},{"productName":"B700-5K"},{"productName":"B701-30K"},{"productName":"B701-5K"},{"productName":"B703M"},{"productName":"B703P"},{"productName":"B704M"},{"productName":"B704P"},{"productName":"B705M"},{"productName":"B705P"},{"productName":"B705XM"},{"productName":"B705XP"},{"productName":"B706M"},{"productName":"B706P"},{"productName":"B707M"},{"productName":"B707P"},{"productName":"B710C"},{"productName":"B711C"},{"productName":"B712P"},{"productName":"B715C"},{"productName":"B716C"},{"productName":"B716P"},{"productName":"B719C"},{"productName":"B720C"},{"productName":"B730P"},{"productName":"B731P"},{"productName":"B732P"},{"productName":"B733P"},{"productName":"B736P"},{"productName":"B740-30K"},{"productName":"B740-5K"},{"productName":"B741-30K"},{"productName":"B741-5K"},{"productName":"B743M"},{"productName":"B743P"},{"productName":"B744M"},{"productName":"B744P"},{"productName":"B745M"},{"productName":"B745P"},{"productName":"B745XM"},{"productName":"B745XP"},{"productName":"B746M"},{"productName":"B746P"},{"productName":"B747M"},{"productName":"B747P"},{"productName":"B750C"},{"productName":"B751C"},{"productName":"B752P"},{"productName":"B755C"},{"productName":"B756C"},{"productName":"B756P"},{"productName":"B765P"},{"productName":"B766P"},{"productName":"B767P"},{"productName":"B768P"},{"productName":"B790-30K"},{"productName":"B790-5K"},{"productName":"B797-30K"},{"productName":"B797-5K"},{"productName":"B798-30K"},{"productName":"B798-5K"},{"productName":"B799-30K"},{"productName":"B799-5K"},{"productName":"B803M"},{"productName":"B803P"},{"productName":"POO"}]

6
package.json

@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "jest",
"start": "nodemon src/index.js"
"start": "nodemon src/index.js",
"export": "nodemon src/test.js"
},
"repository": {
"type": "git",
@ -25,9 +26,12 @@
},
"homepage": "https://github.com/CUBETIQ/data-migration-nodejs#readme",
"dependencies": {
"axios": "^0.21.1",
"excel2json-xlsx": "^1.0.5",
"express": "^4.17.1",
"jest": "^26.6.3",
"multer": "^1.4.2",
"node-fetch": "^2.6.1",
"nodemon": "^2.0.7",
"read-excel-file": "^5.0.0",
"xlsx": "^0.16.9"

22
src/index.js

@ -1,4 +1,5 @@
const express = require('express')
const axios = require("axios")
// const file = require('fs')
// file.readFile('./data/people.csv', 'utf-8', (err, data) => {
@ -31,6 +32,7 @@ app.listen(port, () => {
})
var multer = require('multer');
const { default: fetch } = require('node-fetch');
var upload = multer();
var XLSX = require('xlsx')
app.use(upload.none());
@ -41,6 +43,26 @@ app.post('/upload', async function (req, res) {
let xlData = XLSX.utils.sheet_to_json(wb.Sheets['Sheet2']);
let data = xlData.map((row) => ({ productName: row['Product name'], smallTypeName: row.smallTypeName, productCategory: { productCategoryId: row['CategoryID'] }, alert: row.alert, cost: row.cost, qtyStock: row.qtystock, sellPrice: row.sellPrice }))
res.send(data)
axios({
url: 'http://localhost:4000/graphql',
method: 'post',
headers: {
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsImlhdCI6MTYxNzA3OTYyOCwiZXhwIjoxNjE3MzM4ODI4fQ.p5oAaT3htRbBgKimfr7hxfjNwjWPc5X1finX2zBZ9aM"
},
data: {
query: `
mutation{
createManyProducts(file: "${JSON.stringify(data)}")
}
`
}
}).then((result) => {
console.log(result.data)
});
})

11
src/test.js

@ -0,0 +1,11 @@
const excel2json = require("excel2json-xlsx")
const data = excel2json(
{
inputFile: "data/data.xlsx",
mapperFile: "data/config.json",
outputFile: "data.json",
sheetName: "Sheet2",
}
)
console.log(data)
Loading…
Cancel
Save