admin-dashboard-backend/groupTasks/groups/shx-order/crmCustomer.py

104 lines
2.3 KiB
Python

import requests
import sys
import json
# DEFINITIONS
endpoint_url = "https://devdash.ex.umbach.dev/api/v1/crm"
api_key = "uY1DTUog-Iax7-ydc4-dP9V-NON1xsbTGH1I"
headers = {
"X-Api-Key": api_key,
"Content-Type": "application/json"
}
# MAIN
json_object = json.loads(sys.argv[1])
customerEmail = json_object["customerEmail"]
orderId = json_object["orderId"]
if customerEmail is None:
print("customerEmail is required")
sys.exit(1)
customerEmail = customerEmail["value"]
orderId = orderId["value"]
def CreateCrmCustomer():
response = requests.post(
url=f"{endpoint_url}/customer/create",
headers=headers,
json={
"FirstName": "Shopify Customer",
"Email": customerEmail,
"LeadOrigin": "Shopify Customer added by GroupTask",
})
if response.status_code != 200:
print(f"CreateCrmCustomer req error {response.status_code}")
sys.exit(1)
print(response.status_code)
print(response.json())
return response.json()["Id"]
def CheckIfCrmCustomerExists():
print(f"Checking if customer exists: {customerEmail}")
response = requests.post(
url=f"{endpoint_url}/customer",
headers=headers,
json={"Email": customerEmail} # Hier json verwenden, um JSON-Daten zu senden
)
print("CheckIfCrmCustomerExists response status code", response.status_code)
if response.status_code != 200:
print(f"CheckIfCrmCustomerExists req error {response.status_code}")
sys.exit(1)
customerId = ""
if response.json() == []:
print("Customer not found")
customerId = CreateCrmCustomer()
else:
if len(response.json()) > 1:
print("Multiple customers found. Don't know which one to use")
sys.exit(1)
customerId = response.json()[0]["Id"]
CreateCrmActivityLink(customerId=customerId)
def CreateCrmActivityLink(customerId):
print(f"Creating CRM activity link for customer {customerId}")
def req(type):
response = requests.post(
url=f"{endpoint_url}/links",
headers=headers,
json={
"CustomerId": customerId,
"Name": f"Shopify Order #{orderId} - {type}",
"Url": "https://www.example.com"
})
if response.status_code != 200:
print(f"CreateCrmActivityLink req error {response.status_code}")
sys.exit(1)
print(response.status_code)
req("5 € Gutschein")
req("10 € Gutschein")
CheckIfCrmCustomerExists()