58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
from typing import cast
|
|
import strawberry
|
|
from strawberry.types import Info
|
|
from core.graphql.pubsub import pubsub
|
|
from core.graphql.inputs.labor import LaborInput, LaborUpdateInput
|
|
from core.graphql.types.labor import LaborType
|
|
from core.models.labor import Labor
|
|
from core.graphql.utils import create_object, update_object, delete_object
|
|
from core.services.events import (
|
|
publish_labor_rate_created, publish_labor_rate_updated, publish_labor_rate_deleted,
|
|
)
|
|
|
|
|
|
@strawberry.type
|
|
class Mutation:
|
|
@strawberry.mutation(description="Create a new labor rate")
|
|
async def create_labor(self, input: LaborInput, info: Info) -> LaborType:
|
|
instance = await create_object(input, Labor)
|
|
await pubsub.publish("labor_created", instance.id)
|
|
|
|
# Publish event for notifications
|
|
profile = getattr(info.context.request, 'profile', None)
|
|
await publish_labor_rate_created(
|
|
rate_id=str(instance.id),
|
|
triggered_by=profile
|
|
)
|
|
|
|
return cast(LaborType, instance)
|
|
|
|
@strawberry.mutation(description="Update an existing labor rate")
|
|
async def update_labor(self, input: LaborUpdateInput, info: Info) -> LaborType:
|
|
instance = await update_object(input, Labor)
|
|
await pubsub.publish("labor_updated", instance.id)
|
|
|
|
# Publish event for notifications
|
|
profile = getattr(info.context.request, 'profile', None)
|
|
await publish_labor_rate_updated(
|
|
rate_id=str(instance.id),
|
|
triggered_by=profile
|
|
)
|
|
|
|
return cast(LaborType, instance)
|
|
|
|
@strawberry.mutation(description="Delete an existing labor rate")
|
|
async def delete_labor(self, id: strawberry.ID, info: Info) -> strawberry.ID:
|
|
instance = await delete_object(id, Labor)
|
|
if not instance:
|
|
raise ValueError(f"Labor with ID {id} does not exist")
|
|
await pubsub.publish("labor_deleted", id)
|
|
|
|
# Publish event for notifications
|
|
profile = getattr(info.context.request, 'profile', None)
|
|
await publish_labor_rate_deleted(
|
|
rate_id=str(id),
|
|
triggered_by=profile
|
|
)
|
|
|
|
return id |