Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Created January 16, 2019 15:33
Show Gist options
  • Save daliborgogic/447e18487abdecd4bfb4f55808665c59 to your computer and use it in GitHub Desktop.
Save daliborgogic/447e18487abdecd4bfb4f55808665c59 to your computer and use it in GitHub Desktop.
require('dotenv').config()
const csv = require('csvtojson')
const { asyncForEach } = require('./helpers')
const { read, write } = require('./fs')
const fetch = require('node-fetch')
const { DOMAIN, USERNAME, PASSWORD } = process.env
const convert = async (source, code, type) => {
try {
const input = await csv({
// https://github.com/Keyang/node-csvtojson#parameters
fork: true,
includeColumns: /KLASIF|KLASIF2||VALUTA|CENA|IDENT|NAZIV|DOBAVITELJ|MPCENA|EM|KODA|FIELDSF|OPIS|SLIKA|DAVEK/,
checkColumn: true
}).fromFile(source)
// {
// "IDENT": "3504",
// "NAZIV": "opl iv crvena tresnja Sj 35/031/01-5.64-",
// "KLASIF": "IVE",
// "KLASIF2": "",
// "KODA": "35/031/01",
// "VRSTAMS": "200",
// "DOBAVITELJ": "DOMOTERC",
// "DAVEK": "20",
// "VALUTA": "EUR",
// "PRODCENA": "24.59000 Din.",
// "MPCENA": "20.4907",
// "VPCENA": "16.7",
// "NABACENA": "0",
// "EM": "m2",
// "EMTOEM2": "1",
// "EM2": "m2",
// "TS": "R1",
// "CENA": "0",
// "FIELDSA": "D001",
// "FIELDSD": "IV06",
// "FIELDSE": "39",
// "FIELDSF": "Iverica",
// "SLIKA": "",
// "OPIS": ""
// },
let output = []
let kategorija
input.forEach( async x => {
if (x.KLASIF === code) {
kategorija = x.KLASIF
output.push({
title: x.NAZIV,
status: 'publish',
fields: {
id: x.IDENT,
jedinica_mere: x.EM.toLowerCase() || null,
pdv: x.DAVEK,
valuta: x.VALUTA,
sifra: x.KODA || null,
mp_cena: x.MPCENA,
vp_cena: x.VPCENA,
opis: x.OPIS,
tag: x.KLASIF2.toUpperCase() || null,
kategorija: x.KLASIF || null,
proizvodjac: x.DOBAVITELJ.toUpperCase() || null
},
})
}
})
await write(`./dist/${kategorija.toLowerCase()}.json`, JSON.stringify(output))
const getCategory = await read(`./dist/${kategorija.toLowerCase()}.json`)
const category = JSON.parse(getCategory)
await asyncForEach(category, async x => {
try {
const id = await(await fetch(`${DOMAIN}/wp-json/wp/v2/${type}?filter[meta_key]=id&filter[meta_value]=${x.fields.id}`)).json()
await fetch(DOMAIN + '/wp-json/wp/v2/' + type, {
method: id.length !== 0 ? 'put' : 'post',
headers: {
'Authorization': 'Basic ' + Buffer.from(USERNAME + ':' + PASSWORD).toString('base64'),
'Content-Type': 'application/json'
},
body: JSON.stringify(x)
})
console.log(id.length !== 0 ? 'UPDATED ' : 'CREATED ', x.fields.id)
// DELETE!!!
// if(id.length !== 0) {
// const deleted = await( await fetch(`${DOMAIN}/wp-json/wp/v2/${type}/${id[0].id}?force=true`, {
// method: 'DELETE',
// headers: {
// 'Authorization': 'Basic ' + Buffer.from(USERNAME + ':' + PASSWORD).toString('base64'),
// 'Content-Type': 'application/json'
// }
// })).json()
// console.log('DELETED ', id , deleted)
// }
} catch (error) {
console.error(error)
}
})
} catch (error) {
console.error('COVERT ERROR ', error)
}
}
module.exports = convert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment