Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
uploads csv via ftp
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Mazurok committed May 20, 2019
1 parent 5bb5537 commit 35404ce
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ USE_HTTPS=true
SHOPIFY_WEBHOOK_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SHOP_DOMAIN=myshop.myshopify.com
FORCE_DB_SYNC=false
FTP_HOST=ftp.emons.de
FTP_PORT=21
FTP_SECURE=false
FTP_USER=username
FTP_PASS=password
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dotenv": "^8.0.0",
"express": "^4.17.0",
"fastest-validator": "^0.6.17",
"ftp": "^0.3.10",
"mysql2": "^1.6.5",
"nested-property": "0.0.7",
"sequelize": "^5.8.6"
Expand Down
28 changes: 28 additions & 0 deletions src/ftp-upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require('./env');

const uploadCSV = (csv, orderId) => {
const Client = require('ftp');

const c = new Client();
c.on('ready', function () {
c.put(Buffer.from(csv, 'utf8'), `/in/${orderId}.csv`, function (error) {
if (error) {
console.error(error);
return false;
}
c.end();
return true;
});
});
c.connect({
host: process.env.FTP_HOST,
port: process.env.FTP_PORT || 21,
secure: process.env.FTP_SECURE === "true" || false, // Set to true for both control and data connection encryption, 'control' for control connection encryption only, or 'implicit' for implicitly encrypted control connection (this mode is deprecated in modern times, but usually uses port 990)
user: process.env.FTP_USER,
password: process.env.FTP_PASS,
});
};

module.exports = {
uploadCSV
};
20 changes: 14 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const crypto = require('crypto');
const Validator = require("fastest-validator");
const csvWriter = require('csv-writer');
const nestedProperty = require("nested-property");
const { uploadCSV } = require("./ftp-upload");
const { addOrder } = require("./db");

const app = express();
Expand All @@ -33,7 +34,7 @@ app.post('/', (req, res) => {

if (checkOrder(json) === true) {
buildCSV(parseOrder(json), rawJSON)
.then(csv => console.log(csv))
.then(data => console.log(uploadCSV(data.csv, data.orderId)))
.catch(error => console.error(error));
} else {
console.log('data is not ok')
Expand Down Expand Up @@ -124,9 +125,9 @@ function buildCSV(inputData, json) {

const lineItems = (inputData.lineItems || []).map((item, index) => ({
header: '#P',
invoice: inputData.invoice,
index: index + 1,

invoice: inputData.invoice.toString().substring(0, 50),
index: parseInt((index + 1).toString().substring(0, 9)),
// TODO: add more
}));

const headerSchema = {
Expand All @@ -144,7 +145,11 @@ function buildCSV(inputData, json) {
phone: { type: "string", min: 0, max: 50 },
};

const lineItemsSchema = {};
const lineItemsSchema = {
header: { type: "string", min: 2, max: 2 },
invoice: { type: "string", min: 1, max: 50 },
index: { type: "number", positive: true, integer: true, min: 0, max: 999999999 },
};

const v = new Validator();
let headerCSVStringifier;
Expand Down Expand Up @@ -189,7 +194,10 @@ function buildCSV(inputData, json) {
.then(orderId => {
header.id = orderId;
const headerCSV = headerCSVStringifier.stringifyRecords([header]);
resolve(headerCSV + lineItemsCSV);
resolve({
csv: headerCSV + lineItemsCSV,
orderId,
});
})
.catch(error => {
console.error(error);
Expand Down

0 comments on commit 35404ce

Please sign in to comment.