Skip to content

Commit 89594fd

Browse files
committed
Fix webhook assets 2
1 parent 3047e05 commit 89594fd

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

landolfio/inventory/resource_types.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,34 @@ def process_webhook_event(
4242
data: dict,
4343
event: WebhookEvent,
4444
):
45-
logger.info(f"Processing {event} for {cls.entity_type_name} {resource_id}")
45+
logger.info(
46+
f"Processing {event.value} for {cls.entity_type_name} {resource_id}"
47+
)
4648

49+
# If no data, the asset was deleted
4750
if not data:
4851
return cls.delete_from_moneybird(resource_id)
4952

53+
# Check if asset already exists locally
5054
try:
5155
asset = cls.get_queryset().get(moneybird_asset_id=resource_id)
52-
logger.info(f"Asset {resource_id} already exists locally, refreshing")
56+
logger.info(
57+
f"Asset {resource_id} already exists locally, refreshing from Moneybird"
58+
)
5359
asset.refresh_from_moneybird()
54-
asset.update_on_moneybird()
60+
61+
# If this is a created event and the name doesn't match, update Moneybird
62+
if event.value == "company_assets_asset_created":
63+
moneybird_name = data.get("name", "")
64+
if moneybird_name and moneybird_name != asset.name:
65+
logger.info(
66+
f"Asset name mismatch: Moneybird has '{moneybird_name}', local has '{asset.name}'. Updating Moneybird."
67+
)
68+
asset.update_on_moneybird()
69+
5570
return asset
5671
except cls.model.DoesNotExist:
72+
# Asset doesn't exist locally - try to find by name and link
5773
asset_name = data.get("name", "")
5874
if asset_name:
5975
try:
@@ -64,7 +80,15 @@ def process_webhook_event(
6480
f"Found existing asset with name '{asset_name}', linking to Moneybird asset {resource_id}"
6581
)
6682
asset.moneybird_asset_id = resource_id
67-
asset._refresh_from_moneybird(data)
83+
asset.refresh_from_moneybird()
84+
85+
# Check if name needs updating on Moneybird
86+
if asset_name != asset.name:
87+
logger.info(
88+
f"Linked asset name differs from Moneybird name. Updating Moneybird from '{asset_name}' to '{asset.name}'."
89+
)
90+
asset.update_on_moneybird()
91+
6892
return asset
6993
except cls.model.DoesNotExist:
7094
logger.info(

landolfio/moneybird/webhooks/processing.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,22 @@ def process_webhook_payload(payload: MoneybirdResource) -> None:
3838
# Try to get exact entity type match first
3939
resource_type = get_moneybird_resource_type_for_entity(entity_type)
4040

41-
# If not found and entity_type starts with company_assets_, route to asset resource type
42-
if resource_type is None and entity_type.startswith("company_assets_"):
41+
# If not found and entity_type starts with CompanyAssets::, route to asset resource type
42+
if resource_type is None and entity_type.startswith("CompanyAssets::"):
4343
resource_type = get_moneybird_resource_type_for_entity("company_assets_asset")
4444
if resource_type:
45-
logging.info(f"Routing {entity_type} webhook to AssetResourceType")
45+
logging.info(
46+
f"Routing {entity_type} webhook (event: {event.value}, entity_id: {entity_id}) to AssetResourceType"
47+
)
48+
# For CompanyAssets sub-entities (disposal, source, value_change), extract asset_id from entity_data
49+
if (
50+
entity_type != "CompanyAssets::Asset"
51+
and entity_data
52+
and "asset_id" in entity_data
53+
):
54+
asset_id = entity_data["asset_id"]
55+
logging.info(f"Extracted asset_id {asset_id} from {entity_type} entity")
56+
entity_id = asset_id
4657

4758
if resource_type is None:
4859
logging.warning(

landolfio/website/settings/common.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@
141141
MONEYBIRD_WEBHOOK_EVENTS = [
142142
"contact",
143143
"subscription",
144-
"company_assets", # Matches all company_assets_* events
144+
"company_assets_asset_created",
145+
"company_assets_asset_updated",
146+
"company_assets_asset_destroyed",
147+
"company_assets_disposal",
148+
"company_assets_source",
149+
"company_assets_value_changes",
150+
"company_assets_value_change_plan",
145151
]
146152
MONEYBIRD_WEBHOOK_ID = os.environ.get("MONEYBIRD_WEBHOOK_ID")
147153
MONEYBIRD_WEBHOOK_TOKEN = os.environ.get("MONEYBIRD_WEBHOOK_TOKEN")

0 commit comments

Comments
 (0)