Skip to content

Commit 7b21c85

Browse files
committed
Fix webhooks
1 parent 642440d commit 7b21c85

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

landolfio/inventory_frontend/views.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,19 +360,33 @@ def get_queryset(self):
360360
)
361361
elif warning_type == "missing_sources":
362362
# Filter assets linked to Moneybird with no sources (financially unlinked)
363-
# Check for empty sources array or no moneybird_data
364-
warning_filters |= Q(moneybird_asset_id__isnull=False) & (
365-
Q(moneybird_data__isnull=True)
366-
| Q(moneybird_data__sources__isnull=True)
367-
| Q(moneybird_data__sources=[])
368-
)
363+
# SQLite uses json_array_length, PostgreSQL uses jsonb_array_length
364+
# For now, get all linked assets and filter in Python
365+
pass # Will be handled after applying warning_filters
369366
elif warning_type == "no_photos":
370367
# Filter assets with no attachments (no photos)
371368
warning_filters |= Q(attachments__isnull=True)
372369

373370
if warning_filters:
374371
queryset = queryset.filter(warning_filters)
375372

373+
# Handle missing_sources separately (requires Python-side filtering for JSON array)
374+
if "missing_sources" in warnings:
375+
# Only check assets that are linked to Moneybird
376+
linked_assets = queryset.filter(moneybird_asset_id__isnull=False)
377+
asset_ids_with_missing_sources = []
378+
379+
for asset in linked_assets:
380+
# Check if sources is missing, null, or empty array
381+
sources = asset.moneybird_data.get('sources') if asset.moneybird_data else None
382+
if not sources or len(sources) == 0:
383+
asset_ids_with_missing_sources.append(asset.id)
384+
385+
if asset_ids_with_missing_sources:
386+
queryset = queryset.filter(id__in=asset_ids_with_missing_sources)
387+
else:
388+
queryset = queryset.none()
389+
376390
# Apply property filters
377391
property_filters = self._get_property_filters()
378392
for property_filter in property_filters:

landolfio/moneybird/management/commands/deletemoneybirdwebhook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Command(BaseCommand):
77
help = "Delete webhook at Moneybird"
88

99
def add_arguments(self, parser):
10-
parser.add_argument("webhook_id", nargs="+", type=int)
10+
parser.add_argument("webhook_id", type=int)
1111

1212
def handle(self, *args, **options):
1313
webhook_id = options["webhook_id"]

landolfio/moneybird/webhooks/register.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ def create_webhook():
2828
logging.info("No events to register a webhook for.")
2929
return
3030

31-
events = [event.value for event in events]
31+
# Convert enum values to strings if needed
32+
events = [event.value if hasattr(event, 'value') else event for event in events]
3233

3334
administration = get_moneybird_administration()
34-
response = administration.post("webhooks", data={"url": url, "events": events})
35+
response = administration.post("webhooks", data={"url": url, "enabled_events": events})
3536
logging.info(f"Registered webhook with id {response['id']}")
3637
return response
3738

landolfio/website/settings/common.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,9 @@
139139
"inventory.resource_types.AssetResourceType",
140140
]
141141
MONEYBIRD_WEBHOOK_EVENTS = [
142-
WebhookEvent.CONTACT,
143-
WebhookEvent.SUBSCRIPTION_CANCELLED,
144-
WebhookEvent.SUBSCRIPTION_CREATED,
145-
WebhookEvent.SUBSCRIPTION_UPDATED,
146-
WebhookEvent.SUBSCRIPTION_EDITED,
147-
WebhookEvent.SUBSCRIPTION_DESTROYED,
148-
WebhookEvent.COMPANY_ASSETS_ASSET,
142+
"contact",
143+
"subscription",
144+
"company_assets", # Matches all company_assets_* events
149145
]
150146
MONEYBIRD_WEBHOOK_ID = os.environ.get("MONEYBIRD_WEBHOOK_ID")
151147
MONEYBIRD_WEBHOOK_TOKEN = os.environ.get("MONEYBIRD_WEBHOOK_TOKEN")

0 commit comments

Comments
 (0)