226 lines
4.9 KiB
JavaScript
226 lines
4.9 KiB
JavaScript
import axios from "axios"
|
|
import {createClient} from "@supabase/supabase-js";
|
|
|
|
|
|
let key = process.env.SUPABASE_KEY
|
|
let url = process.env.SUPABASE_URL
|
|
let interval = process.env.INTERVAL
|
|
|
|
const supabase = createClient(url,key)
|
|
|
|
let currentTenant = 2
|
|
|
|
|
|
|
|
|
|
const fetchProjects = async () => {
|
|
|
|
let offset = 0;
|
|
let projectMatches = [];
|
|
|
|
|
|
let query = `
|
|
query {
|
|
project_matches(offset: 0) {
|
|
id
|
|
project_nr
|
|
name
|
|
display_id
|
|
is_deleted
|
|
file_uploads {
|
|
url
|
|
url_download
|
|
type
|
|
category
|
|
image_category
|
|
filename
|
|
modified
|
|
size
|
|
|
|
|
|
}
|
|
|
|
partner_notes
|
|
|
|
volume
|
|
|
|
|
|
measure { # Gewerk
|
|
short
|
|
name
|
|
}
|
|
|
|
customer { # Kunde
|
|
id
|
|
first_name
|
|
last_name
|
|
email
|
|
address {
|
|
basic_address
|
|
city
|
|
street
|
|
zipcode
|
|
full_address
|
|
}
|
|
birth_date
|
|
is_deleted
|
|
full_name
|
|
nr
|
|
partner_notes
|
|
type
|
|
}
|
|
|
|
contact { # Ansprechpartner
|
|
id
|
|
full_name
|
|
nr
|
|
type
|
|
first_name
|
|
last_name
|
|
email
|
|
phone_home
|
|
phone_mobile
|
|
birth_date
|
|
category
|
|
}
|
|
|
|
address { #Projektadresse
|
|
street
|
|
city
|
|
zipcode
|
|
full_address
|
|
}
|
|
|
|
current_project_match_status { # Projektstatus
|
|
status_code
|
|
name
|
|
}
|
|
}
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const response = await axios({
|
|
url: "https://login.hero-software.de/api/external/v7/graphql",
|
|
data: {query},
|
|
method: "POST",
|
|
headers: {
|
|
"content-type":"application/json",
|
|
"Authorization":"Bearer ac_wEDTAs73QLyb94RAnp265XcbgVtj6PKd"
|
|
}
|
|
})
|
|
|
|
projectMatches = response.data.data.project_matches
|
|
//projectMatches = projectMatches.splice(0,10)
|
|
|
|
//console.log(projectMatches)
|
|
|
|
let customerObjs = []
|
|
let projectObjs = []
|
|
|
|
|
|
projectMatches.forEach(item => {
|
|
//Kunden erstellen
|
|
|
|
let customerObj = {
|
|
customerNumber: item.customer.nr,
|
|
name: item.customer.full_name,
|
|
tenant: currentTenant,
|
|
infoData: {
|
|
city: item.customer.address.city,
|
|
zip: item.customer.address.zipcode,
|
|
street: item.customer.address.street,
|
|
email: item.customer.email
|
|
},
|
|
active: true,
|
|
notes: item.customer.partner_notes,
|
|
type: item.customer.type == 'private' ? "Privat" : "Firma",
|
|
heroId: item.customer.nr,
|
|
}
|
|
|
|
if(!customerObjs.find(i => i.heroId === customerObj.heroId)) customerObjs.push(customerObj)
|
|
|
|
//Kontakte erstellen
|
|
|
|
/*let contactObj = {
|
|
firstName: item.contact.first_name,
|
|
lastName: item.contact.last_name,
|
|
email: item.contact.email,
|
|
phoneMobile: item.contact.phone_mobile,
|
|
phoneHome: item.contact.phone_home,
|
|
heroId: item.contact.nr,
|
|
tenant: currentTenant
|
|
}*/
|
|
|
|
//Projekte erstellen
|
|
|
|
let projectObj = {
|
|
name: item.name,
|
|
heroId: item.project_nr,
|
|
notes: item.partner_notes,
|
|
tenant: currentTenant,
|
|
measure: item.measure.name
|
|
}
|
|
|
|
projectObjs.push(projectObj)
|
|
|
|
})
|
|
|
|
console.log(projectObjs.length)
|
|
console.log(customerObjs.length)
|
|
|
|
|
|
const existingProjects = (await supabase.from("projects").select("heroId")).data
|
|
let existingCustomers = (await supabase.from("customers").select("heroId")).data
|
|
|
|
projectObjs = projectObjs.filter(item => !existingProjects.find(i => i.heroId === item.heroId))
|
|
customerObjs = customerObjs.filter(item => !existingCustomers.find(i => i.heroId === item.heroId))
|
|
|
|
console.log(projectObjs.length)
|
|
console.log(customerObjs.length)
|
|
|
|
|
|
const {data:customerData, error:customerError } = await supabase
|
|
.from("customers")
|
|
.insert(customerObjs)
|
|
.select()
|
|
|
|
if(customerError) {
|
|
console.log(customerError)
|
|
} else if(customerData) {
|
|
console.log(customerData)
|
|
}
|
|
|
|
existingCustomers = (await supabase.from("customers").select("heroId,id").eq("tenant", currentTenant)).data
|
|
|
|
console.log(existingCustomers)
|
|
|
|
projectObjs.forEach((item,index) => {
|
|
let heroProject = projectMatches.find(prj => prj.project_nr === item.heroId)
|
|
let heroCustomerNr = heroProject.customer.nr
|
|
console.log(heroCustomerNr)
|
|
let customerId = existingCustomers.find(customer => String(customer.heroId) === String(heroCustomerNr)).id
|
|
console.log(customerId)
|
|
projectObjs[index].customer = customerId
|
|
})
|
|
|
|
//console.log(projectObjs)
|
|
|
|
|
|
const {data:projectData, error:projectError } = await supabase
|
|
.from("projects")
|
|
.insert(projectObjs)
|
|
.select()
|
|
|
|
if(projectError) {
|
|
console.log(projectError)
|
|
} else if(projectData) {
|
|
console.log(projectData)
|
|
}
|
|
|
|
}
|
|
|
|
fetchProjects()
|