This commit extends the SuperOffice connector to support the creation and linking of Sale \(Opportunity\) and Project \(Campaign\) entities, providing a comprehensive foundation for both sales and marketing automation workflows.
Key achievements:
- **`SUPEROFFICE_INTEGRATION_PLAN.md`**: Updated to include strategic mapping of D365 concepts \(Opportunity, Campaign\) to SuperOffice entities \(Sale, Project\).
- **`connector-superoffice/superoffice_client.py`**:
- Implemented `create_sale` method to generate new opportunities, correctly mapping `Title` to SuperOffices
123 lines
4.8 KiB
Python
123 lines
4.8 KiB
Python
import os
|
|
import logging
|
|
from dotenv import load_dotenv
|
|
from auth_handler import AuthHandler
|
|
from superoffice_client import SuperOfficeClient
|
|
|
|
# Setup logging
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def main():
|
|
# Load .env from the root directory
|
|
# If running from /app, .env is in the same directory.
|
|
# If running from /app/connector-superoffice, it's in ../.env
|
|
if os.path.exists(".env"):
|
|
load_dotenv(".env", override=True)
|
|
elif os.path.exists("../.env"):
|
|
load_dotenv("../.env", override=True)
|
|
|
|
logger.info("Starting SuperOffice Connector S2S POC...")
|
|
|
|
try:
|
|
# Initialize Auth
|
|
auth = AuthHandler()
|
|
|
|
# Initialize Client
|
|
client = SuperOfficeClient(auth)
|
|
|
|
# 1. Test Connection
|
|
logger.info("Step 1: Testing connection...")
|
|
user_info = client.test_connection()
|
|
if user_info:
|
|
logger.info(f"Connected successfully as: {user_info.get('FullName')}")
|
|
else:
|
|
logger.error("Connection test failed.")
|
|
return
|
|
|
|
# 2. Search for our demo company
|
|
demo_company_name = "Gemini Test Company [2ff88f42]"
|
|
logger.info(f"Step 2: Searching for company '{demo_company_name}'...")
|
|
contact = client.find_contact_by_criteria(name=demo_company_name)
|
|
|
|
target_contact_id = None
|
|
|
|
if contact:
|
|
target_contact_id = contact.get('ContactId')
|
|
logger.info(f"Found existing demo company: {contact.get('Name')} (ID: {target_contact_id})")
|
|
else:
|
|
logger.info(f"Demo company not found. Creating new one...")
|
|
demo_company_url = "https://www.gemini-test-company.com"
|
|
demo_company_orgnr = "DE123456789"
|
|
|
|
new_contact = client.create_contact(
|
|
name=demo_company_name,
|
|
url=demo_company_url,
|
|
org_nr=demo_company_orgnr
|
|
)
|
|
if new_contact:
|
|
target_contact_id = new_contact.get('ContactId')
|
|
logger.info(f"Created new demo company with ID: {target_contact_id}")
|
|
|
|
# 3. Create a Person linked to this company
|
|
if target_contact_id:
|
|
logger.info(f"Step 3: Creating Person for Contact ID {target_contact_id}...")
|
|
|
|
# Create Max Mustermann
|
|
person = client.create_person(
|
|
first_name="Max",
|
|
last_name="Mustermann",
|
|
contact_id=target_contact_id,
|
|
email="max.mustermann@gemini-test.com"
|
|
)
|
|
|
|
if person:
|
|
logger.info("SUCCESS: Person created!")
|
|
person_id = person.get('PersonId')
|
|
logger.info(f"Name: {person.get('Firstname')} {person.get('Lastname')}")
|
|
logger.info(f"Person ID: {person_id}")
|
|
logger.info(f"Linked to Contact ID: {person.get('Contact').get('ContactId')}")
|
|
|
|
# 4. Create a Sale for this company
|
|
logger.info(f"Step 4: Creating Sale for Contact ID {target_contact_id}...")
|
|
sale = client.create_sale(
|
|
title=f"Robotics Automation Opportunity - {demo_company_name}",
|
|
contact_id=target_contact_id,
|
|
person_id=person_id,
|
|
amount=50000.0
|
|
)
|
|
if sale:
|
|
logger.info("SUCCESS: Sale created!")
|
|
logger.info(f"Sale Title: {sale.get('Heading')}")
|
|
logger.info(f"Sale ID: {sale.get('SaleId')}")
|
|
else:
|
|
logger.error("Failed to create sale.")
|
|
|
|
# 5. Create a Project for this company and add the person to it
|
|
logger.info(f"Step 5: Creating Project for Contact ID {target_contact_id} and adding Person ID {person_id}...")
|
|
project = client.create_project(
|
|
name=f"Marketing Campaign Q1 [2ff88f42]",
|
|
contact_id=target_contact_id,
|
|
person_id=person_id
|
|
)
|
|
if project:
|
|
logger.info("SUCCESS: Project created and person added!")
|
|
logger.info(f"Project Name: {project.get('Name')}")
|
|
logger.info(f"Project ID: {project.get('ProjectId')}")
|
|
else:
|
|
logger.error("Failed to create project.")
|
|
|
|
else:
|
|
logger.error("Failed to create person.")
|
|
else:
|
|
logger.error("Skipping person creation because company could not be found or created.")
|
|
|
|
except Exception as e:
|
|
logger.error(f"An error occurred: {e}", exc_info=True)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|