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

188 lines
6.8 KiB
Python

import graphene
from graphene import Field, Boolean, List, String
from backend.core.commands import (
CreateCustomerCommand,
UpdateCustomerCommand,
DeleteCustomerCommand,
MarkCustomerInactiveCommand
)
from backend.core.repositories import CustomerRepository, AccountRepository
from backend.graphql_api.types import CustomerType
from backend.graphql_api.inputs import CustomerCreateInput, CustomerUpdateInput
class CreateCustomerMutation(graphene.Mutation):
"""Mutation to create a new customer."""
class Arguments:
input = CustomerCreateInput(required=True)
customer = Field(CustomerType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, input):
try:
customer_repo = CustomerRepository()
command = CreateCustomerCommand(
customer_repo=customer_repo,
name=input.name,
primary_contact_first_name=input.primary_contact_first_name,
primary_contact_last_name=input.primary_contact_last_name,
primary_contact_phone=input.primary_contact_phone,
primary_contact_email=input.primary_contact_email,
secondary_contact_first_name=input.secondary_contact_first_name,
secondary_contact_last_name=input.secondary_contact_last_name,
secondary_contact_phone=input.secondary_contact_phone,
secondary_contact_email=input.secondary_contact_email,
billing_contact_first_name=input.billing_contact_first_name,
billing_contact_last_name=input.billing_contact_last_name,
billing_street_address=input.billing_street_address,
billing_city=input.billing_city,
billing_state=input.billing_state,
billing_zip_code=input.billing_zip_code,
billing_email=input.billing_email,
billing_terms=input.billing_terms,
start_date=input.start_date,
end_date=input.end_date
)
result = command.execute()
return {
'customer': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'customer': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while creating the customer"
}
class UpdateCustomerMutation(graphene.Mutation):
"""Mutation to update an existing customer."""
class Arguments:
id = graphene.ID(required=True)
input = CustomerUpdateInput(required=True)
customer = Field(CustomerType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, id, input):
try:
customer_repo = CustomerRepository()
command = UpdateCustomerCommand(
id=id,
customer_repo=customer_repo,
name=input.name,
primary_contact_first_name=input.primary_contact_first_name,
primary_contact_last_name=input.primary_contact_last_name,
primary_contact_phone=input.primary_contact_phone,
primary_contact_email=input.primary_contact_email,
secondary_contact_first_name=input.secondary_contact_first_name,
secondary_contact_last_name=input.secondary_contact_last_name,
secondary_contact_phone=input.secondary_contact_phone,
secondary_contact_email=input.secondary_contact_email,
billing_contact_first_name=input.billing_contact_first_name,
billing_contact_last_name=input.billing_contact_last_name,
billing_street_address=input.billing_street_address,
billing_city=input.billing_city,
billing_state=input.billing_state,
billing_zip_code=input.billing_zip_code,
billing_email=input.billing_email,
billing_terms=input.billing_terms,
start_date=input.start_date,
end_date=input.end_date
)
result = command.execute()
return {
'customer': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'customer': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while updating the customer"
}
class DeleteCustomerMutation(graphene.Mutation):
"""Mutation to delete a customer."""
class Arguments:
id = graphene.ID(required=True)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, id):
try:
customer_repo = CustomerRepository()
account_repo = AccountRepository()
command = DeleteCustomerCommand(
customer_repo=customer_repo,
account_repo=account_repo,
customer_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 customer"
}
class MarkCustomerInactiveMutation(graphene.Mutation):
"""Mutation to mark a customer as inactive."""
class Arguments:
id = graphene.ID(required=True)
end_date = graphene.String()
customer = Field(CustomerType)
success = Boolean()
errors = List(String)
message = String()
@staticmethod
def mutate(root, info, id, end_date=None):
try:
customer_repo = CustomerRepository()
command = MarkCustomerInactiveCommand(
customer_repo=customer_repo,
customer_id=id,
end_date=end_date
)
result = command.execute()
return {
'customer': result.data,
'success': result.success,
'errors': result.errors,
'message': result.message
}
except Exception as e:
return {
'customer': None,
'success': False,
'errors': [str(e)],
'message': "An error occurred while marking the customer as inactive"
}