Skip to content

Commit

Permalink
fix: ensure consistency of the way event types are specified and urls…
Browse files Browse the repository at this point in the history
… for products are created in published events
  • Loading branch information
adrianmcphee committed Nov 25, 2024
1 parent e659aa5 commit ed94970
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
24 changes: 19 additions & 5 deletions apps/capabilities/product_management/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import httpx
import time
import json
from django.urls import reverse

from apps.capabilities.talent.models import Person, Expertise
from apps.capabilities.security.services import RoleService
from apps.common import utils
from apps.event_hub.events import EventTypes
from .models import Bounty, Challenge, Product, Idea, Bug, IdeaVote, ProductContributorAgreementTemplate, ProductArea, Initiative
from apps.capabilities.commerce.models import Organisation
from . import forms
Expand Down Expand Up @@ -342,7 +344,7 @@ def create_product(form_data: dict, person: Person, organisation: Organisation =

# Publish event
event_bus = get_event_bus()
event_bus.publish('product.created', {
event_bus.publish(EventTypes.PRODUCT_CREATED, {
'organisation_id': product.organisation_id if product.organisation else None,
'person_id': product.person_id if product.person else None,
'name': product.name,
Expand All @@ -360,13 +362,25 @@ def create_product(form_data: dict, person: Person, organisation: Organisation =
logger.error(f"Error creating product: {str(e)}", exc_info=True)
raise InvalidInputError(f"Failed to create product: {str(e)}")

@staticmethod
def update_product(product: Product, form_data: Dict) -> Tuple[bool, Optional[str]]:
@classmethod
@transaction.atomic
def update_product(cls, product: Product, data: Dict, person: Person) -> Product:
try:
for key, value in form_data.items():
for key, value in data.items():
setattr(product, key, value)
product.save()
return True, None

# After successful update
event_bus = get_event_bus()
event_bus.publish(EventTypes.PRODUCT_UPDATED, {
'organisation_id': product.organisation_id if product.organisation else None,
'person_id': product.person_id if product.person else None,
'name': product.name,
'product_id': product.id,
'url': product.get_absolute_url()
})

return product
except Exception as e:
logger.error(f"Error updating product: {e}")
return False, str(e)
Expand Down
10 changes: 9 additions & 1 deletion apps/engagement/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ class EngagementConfig(AppConfig):

def ready(self):
from apps.event_hub.services.factory import get_event_bus
from .events import handle_product_created
from .events import (
handle_product_created,
handle_product_updated,
# Add other handlers as needed
)

event_bus = get_event_bus()

# Register all event handlers
event_bus.register_listener(EventTypes.PRODUCT_CREATED, handle_product_created)
event_bus.register_listener(EventTypes.PRODUCT_UPDATED, handle_product_updated)
# Add other event registrations as needed

0 comments on commit ed94970

Please sign in to comment.