from typing import cast import strawberry from strawberry.types import Info from core.graphql.pubsub import pubsub from core.graphql.inputs.revenue import RevenueInput, RevenueUpdateInput from core.graphql.types.revenue import RevenueType from core.models.revenue import Revenue from core.graphql.utils import create_object, update_object, delete_object from core.services.events import ( publish_revenue_rate_created, publish_revenue_rate_updated, publish_revenue_rate_deleted, ) @strawberry.type class Mutation: @strawberry.mutation(description="Create a new revenue rate") async def create_revenue(self, input: RevenueInput, info: Info) -> RevenueType: instance = await create_object(input, Revenue) await pubsub.publish("revenue_created", instance.id) # Publish event for notifications profile = getattr(info.context.request, 'profile', None) await publish_revenue_rate_created( rate_id=str(instance.id), triggered_by=profile ) return cast(RevenueType, instance) @strawberry.mutation(description="Update an existing revenue rate") async def update_revenue(self, input: RevenueUpdateInput, info: Info) -> RevenueType: instance = await update_object(input, Revenue) await pubsub.publish("revenue_updated", instance.id) # Publish event for notifications profile = getattr(info.context.request, 'profile', None) await publish_revenue_rate_updated( rate_id=str(instance.id), triggered_by=profile ) return cast(RevenueType, instance) @strawberry.mutation(description="Delete an existing revenue rate") async def delete_revenue(self, id: strawberry.ID, info: Info) -> strawberry.ID: instance = await delete_object(id, Revenue) if not instance: raise ValueError(f"Revenue with ID {id} does not exist") await pubsub.publish("revenue_deleted", id) # Publish event for notifications profile = getattr(info.context.request, 'profile', None) await publish_revenue_rate_deleted( rate_id=str(id), triggered_by=profile ) return id