2026-01-26 10:30:49 -05:00

318 lines
10 KiB
Python

import graphene
from graphene import Field, Boolean, List, String, JSONString
from backend.core.commands import (
CreateScheduleCommand,
UpdateScheduleCommand,
DeleteScheduleCommand,
EndScheduleCommand,
GetActiveSchedulesCommand,
GenerateServicesCommand,
GetScheduleByAccountCommand,
SearchSchedulesCommand
)
from backend.core.repositories import ScheduleRepository, AccountRepository
from backend.graphql_api.types import ScheduleType
from backend.graphql_api.inputs import ScheduleCreateInput, ScheduleUpdateInput
class CreateScheduleMutation(graphene.Mutation):
"""Mutation to create a new schedule."""
class Arguments:
input = ScheduleCreateInput(required=True)
schedule = Field(ScheduleType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, input):
try:
schedule_repo = ScheduleRepository()
account_repo = AccountRepository()
command = CreateScheduleCommand(
schedule_repo=schedule_repo,
account_repo=account_repo,
account_id=input.account_id,
start_date=input.start_date,
monday_service=input.monday_service,
tuesday_service=input.tuesday_service,
wednesday_service=input.wednesday_service,
thursday_service=input.thursday_service,
friday_service=input.friday_service,
saturday_service=input.saturday_service,
sunday_service=input.sunday_service,
weekend_service=input.weekend_service,
schedule_exception=input.schedule_exception,
end_date=input.end_date
)
result = command.execute()
return {
'schedule': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'schedule': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while creating the schedule"
}
class UpdateScheduleMutation(graphene.Mutation):
"""Mutation to update an existing schedule."""
class Arguments:
input = ScheduleUpdateInput(required=True)
schedule = Field(ScheduleType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, input):
try:
schedule_repo = ScheduleRepository()
account_repo = AccountRepository()
command = UpdateScheduleCommand(
schedule_repo=schedule_repo,
account_repo=account_repo,
schedule_id=input.id,
monday_service=input.monday_service,
tuesday_service=input.tuesday_service,
wednesday_service=input.wednesday_service,
thursday_service=input.thursday_service,
friday_service=input.friday_service,
saturday_service=input.saturday_service,
sunday_service=input.sunday_service,
weekend_service=input.weekend_service,
schedule_exception=input.schedule_exception,
start_date=input.start_date,
end_date=input.end_date
)
result = command.execute()
return {
'schedule': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'schedule': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while updating the schedule"
}
class DeleteScheduleMutation(graphene.Mutation):
"""Mutation to delete a schedule."""
class Arguments:
id = graphene.ID(required=True)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, id):
try:
schedule_repo = ScheduleRepository()
command = DeleteScheduleCommand(
schedule_repo=schedule_repo,
schedule_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 schedule"
}
class EndScheduleMutation(graphene.Mutation):
"""Mutation to end a schedule."""
class Arguments:
id = graphene.ID(required=True)
end_date = graphene.String()
schedule = Field(ScheduleType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, id, end_date=None):
try:
schedule_repo = ScheduleRepository()
command = EndScheduleCommand(
schedule_repo=schedule_repo,
schedule_id=id,
end_date=end_date
)
result = command.execute()
return {
'schedule': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'schedule': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while ending the schedule"
}
class GenerateServicesMutation(graphene.Mutation):
"""Mutation to generate services from a schedule."""
class Arguments:
schedule_id = graphene.ID(required=True)
start_date = graphene.String(required=True)
end_date = graphene.String(required=True)
services = List(JSONString)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, schedule_id, start_date, end_date):
try:
schedule_repo = ScheduleRepository()
command = GenerateServicesCommand(
schedule_repo=schedule_repo,
schedule_id=schedule_id,
start_date=start_date,
end_date=end_date
)
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 generating services"
}
class GetActiveSchedulesMutation(graphene.Mutation):
"""Mutation to get active schedules."""
class Arguments:
account_id = graphene.ID()
schedules = List(ScheduleType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, account_id=None):
try:
schedule_repo = ScheduleRepository()
command = GetActiveSchedulesCommand(
schedule_repo=schedule_repo,
account_id=account_id
)
result = command.execute()
return {
'schedules': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'schedules': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while retrieving active schedules"
}
class GetScheduleByAccountMutation(graphene.Mutation):
"""Mutation to get schedules for an account."""
class Arguments:
account_id = graphene.ID(required=True)
schedules = List(ScheduleType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, account_id):
try:
schedule_repo = ScheduleRepository()
command = GetScheduleByAccountCommand(
schedule_repo=schedule_repo,
account_id=account_id
)
result = command.execute()
return {
'schedules': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'schedules': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while retrieving account schedules"
}
class SearchSchedulesMutation(graphene.Mutation):
"""Mutation to search schedules."""
class Arguments:
search_term = graphene.String(required=True)
schedules = List(ScheduleType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, search_term):
try:
schedule_repo = ScheduleRepository()
command = SearchSchedulesCommand(
schedule_repo=schedule_repo,
search_term=search_term
)
result = command.execute()
return {
'schedules': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'schedules': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while searching schedules"
}