import graphene import json from graphene import Field, Boolean, List, String, JSONString, Int from backend.core.commands import ( CreateServiceCommand, UpdateServiceCommand, DeleteServiceCommand, CompleteServiceCommand, CancelServiceCommand, AssignTeamMembersCommand, GetServicesByDateRangeCommand, BulkScheduleServicesCommand ) from backend.core.repositories import ServiceRepository, AccountRepository, ProfileRepository, ScheduleRepository from backend.graphql_api.types import ServiceType from backend.graphql_api.inputs import ServiceCreateInput, ServiceUpdateInput class CreateServiceMutation(graphene.Mutation): """Mutation to create a new service.""" class Arguments: input = ServiceCreateInput(required=True) service = Field(ServiceType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: service_repo = ServiceRepository() account_repo = AccountRepository() profile_repo = ProfileRepository() command = CreateServiceCommand( service_repo=service_repo, account_repo=account_repo, profile_repo=profile_repo, account_id=input.account_id, date=input.date, status=input.status, team_member_ids=input.team_member_ids, notes=input.notes, deadline_start=input.deadline_start, deadline_end=input.deadline_end ) result = command.execute() return { 'service': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'service': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while creating the service" } class UpdateServiceMutation(graphene.Mutation): """Mutation to update an existing service.""" class Arguments: input = ServiceUpdateInput(required=True) service = Field(ServiceType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, input): try: service_repo = ServiceRepository() account_repo = AccountRepository() profile_repo = ProfileRepository() command = UpdateServiceCommand( service_repo=service_repo, account_repo=account_repo, profile_repo=profile_repo, service_id=input.id, status=input.status, date=input.date, team_member_ids=input.team_member_ids, notes=input.notes, deadline_start=input.deadline_start, deadline_end=input.deadline_end ) result = command.execute() return { 'service': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'service': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while updating the service" } class DeleteServiceMutation(graphene.Mutation): """Mutation to delete a service.""" class Arguments: id = graphene.ID(required=True) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id): try: service_repo = ServiceRepository() command = DeleteServiceCommand( service_repo=service_repo, service_id=id ) result = command.execute() return { 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'success': False, 'errors': [str(e)], 'message': "An error occurred while deleting the service" } class CompleteServiceMutation(graphene.Mutation): """Mutation to mark a service as complete.""" class Arguments: id = graphene.ID(required=True) completion_notes = graphene.String() service = Field(ServiceType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id, completion_notes=None): try: service_repo = ServiceRepository() command = CompleteServiceCommand( service_repo=service_repo, service_id=id, completion_notes=completion_notes ) result = command.execute() return { 'service': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'service': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while completing the service" } class CancelServiceMutation(graphene.Mutation): """Mutation to cancel a service.""" class Arguments: id = graphene.ID(required=True) cancellation_reason = graphene.String() service = Field(ServiceType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, id, cancellation_reason=None): try: service_repo = ServiceRepository() command = CancelServiceCommand( service_repo=service_repo, service_id=id, cancellation_reason=cancellation_reason ) result = command.execute() return { 'service': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'service': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while cancelling the service" } class AssignTeamMembersMutation(graphene.Mutation): """Mutation to assign team members to a service.""" class Arguments: service_id = graphene.ID(required=True) team_member_ids = List(graphene.ID, required=True) service = Field(ServiceType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, service_id, team_member_ids): try: service_repo = ServiceRepository() profile_repo = ProfileRepository() command = AssignTeamMembersCommand( service_repo=service_repo, profile_repo=profile_repo, service_id=service_id, team_member_ids=team_member_ids ) result = command.execute() return { 'service': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'service': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while assigning team members" } class GetServicesByDateRangeMutation(graphene.Mutation): """Mutation to get services within a date range.""" class Arguments: start_date = graphene.String(required=True) end_date = graphene.String(required=True) account_id = graphene.ID() team_member_id = graphene.ID() services = List(ServiceType) success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, start_date, end_date, account_id=None, team_member_id=None): try: service_repo = ServiceRepository() command = GetServicesByDateRangeCommand( service_repo=service_repo, start_date=start_date, end_date=end_date, account_id=account_id, team_member_id=team_member_id ) result = command.execute() return { 'services': result.data, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'services': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while retrieving services" } class BulkScheduleServicesMutation(graphene.Mutation): """Mutation to bulk schedule services for multiple accounts.""" class Arguments: account_ids = List(graphene.ID, required=True) year = Int(required=True) month = Int(required=True) results = JSONString() success = Boolean() errors = List(String) message = String() @staticmethod def mutate(root, info, account_ids, year, month): try: account_repo = AccountRepository() schedule_repo = ScheduleRepository() command = BulkScheduleServicesCommand( account_repo=account_repo, schedule_repo=schedule_repo, account_ids=account_ids, year=year, month=month ) result = command.execute() # Convert result.data to JSON string to ensure it's serializable serialized_results = json.dumps(result.data, default=str) return { 'results': serialized_results, 'success': result.success, 'errors': result.errors, 'message': result.message } except Exception as e: return { 'results': None, 'success': False, 'errors': [str(e)], 'message': "An error occurred while bulk scheduling services" }