101 lines
3.2 KiB
JavaScript
101 lines
3.2 KiB
JavaScript
const csv = require("csv-parser")
|
||
const fs = require("fs")
|
||
const path = require("path")
|
||
const {createClient} = require("@supabase/supabase-js")
|
||
|
||
const supabase = createClient("https://uwppvcxflrcsibuzsbil.supabase.co","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InV3cHB2Y3hmbHJjc2lidXpzYmlsIiwicm9sZSI6InNlcnZpY2Vfcm9sZSIsImlhdCI6MTcwMDkzODE5NCwiZXhwIjoyMDE2NTE0MTk0fQ.6hOkD1J8XBkVJUm-swv0ngLQ74xrEYr28EEbo0rUrts")
|
||
const tenant = 6
|
||
let rows = []
|
||
let vendors = []
|
||
let contacts = []
|
||
fs.createReadStream(path.join(__dirname, "./Lieferanten.csv"), "utf-8")
|
||
.pipe(csv({ separator: ";" }))
|
||
.on("data", (data) => rows.push(data))
|
||
.on("end", async function () {
|
||
console.log("finished");
|
||
console.log(rows)
|
||
|
||
vendors = rows.map(i => {
|
||
|
||
let item = {
|
||
vendorNumber : Number(i['Lieferanten-Nr.']),
|
||
tenant: tenant,
|
||
name: i.Firmenname,
|
||
infoData: {
|
||
zip: i["PLZ"],
|
||
city: i["Ort"],
|
||
street: i["Namenszusatz"],
|
||
tel: i["Telefon 1"],
|
||
}
|
||
}
|
||
|
||
if(i["Land"] === "D") {
|
||
item.infoData.country = "Deutschland"
|
||
} else if(i["Land"] === "IRL") {
|
||
item.infoData.country = "Irland"
|
||
} else if(i["Land"] === "B") {
|
||
item.infoData.country = "Belgien"
|
||
} else if(i["Land"] === "F") {
|
||
item.infoData.country = "Frankreich"
|
||
} else if(i["Land"] === "E") {
|
||
item.infoData.country = "Spanien"
|
||
} else if(i["Land"] === "USA") {
|
||
item.infoData.country = "USA"
|
||
} else if(i["Land"] === "NL") {
|
||
item.infoData.country = "Niederlande"
|
||
} else if(i["Land"] === "S") {
|
||
item.infoData.country = "Schweden"
|
||
}
|
||
|
||
|
||
return item
|
||
|
||
})
|
||
console.log(vendors)
|
||
|
||
rows.forEach(i => {
|
||
if(i["Haupt-AP"]) {
|
||
let item = {
|
||
firstName: i["Haupt-AP"].split(" ")[1],
|
||
lastName: i["Haupt-AP"].split(" ")[2],
|
||
fullName: i["Haupt-AP"].split(" ")[1] + i["Haupt-AP"].split(" ")[2],
|
||
salutation: i["Haupt-AP"].split(" ")[0],
|
||
tenant: tenant,
|
||
vendorNumber: i['Lieferanten-Nr.']
|
||
}
|
||
|
||
contacts.push(item)
|
||
}
|
||
|
||
|
||
})
|
||
console.log(contacts)
|
||
|
||
|
||
const {data:vendorsData,error: vendorsError} = await supabase.from("vendors").insert(vendors).select()
|
||
console.log(vendorsData)
|
||
console.log(vendorsError)
|
||
|
||
contacts = contacts.map(i => {
|
||
let item = {
|
||
...i,
|
||
vendor: vendorsData.find(v => v.vendorNumber === i.vendorNumber).id
|
||
}
|
||
|
||
delete item.vendorNumber
|
||
|
||
return item
|
||
})
|
||
|
||
const {data:contactsData,error: contactsError} = await supabase.from("contacts").insert(contacts).select()
|
||
console.log(contactsData)
|
||
console.log(contactsError)
|
||
|
||
|
||
//console.log(rows)
|
||
})
|
||
.on("error", function (error) {
|
||
console.log(error.message);
|
||
});
|
||
|