266 lines
8.1 KiB
Python
266 lines
8.1 KiB
Python
import graphene
|
|
from graphene import Field, Boolean, List, String, Float
|
|
|
|
from backend.core.commands import (
|
|
CreateRevenueCommand,
|
|
UpdateRevenueCommand,
|
|
DeleteRevenueCommand,
|
|
EndRevenueCommand,
|
|
GetRevenueByDateRangeCommand,
|
|
CalculateTotalRevenueCommand,
|
|
GetActiveRevenuesCommand
|
|
)
|
|
from backend.core.repositories import RevenueRepository, AccountRepository
|
|
from backend.graphql_api.types import RevenueType
|
|
from backend.graphql_api.inputs import RevenueCreateInput, RevenueUpdateInput
|
|
|
|
|
|
class CreateRevenueMutation(graphene.Mutation):
|
|
"""Mutation to create a new revenue record."""
|
|
class Arguments:
|
|
input = RevenueCreateInput(required=True)
|
|
|
|
revenue = Field(RevenueType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, input):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
account_repo = AccountRepository()
|
|
command = CreateRevenueCommand(
|
|
revenue_repo=revenue_repo,
|
|
account_repo=account_repo,
|
|
account_id=input.account_id,
|
|
amount=input.amount,
|
|
start_date=input.start_date,
|
|
end_date=input.end_date
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'revenue': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'revenue': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while creating the revenue record"
|
|
}
|
|
|
|
|
|
class UpdateRevenueMutation(graphene.Mutation):
|
|
"""Mutation to update an existing revenue record."""
|
|
class Arguments:
|
|
input = RevenueUpdateInput(required=True)
|
|
|
|
revenue = Field(RevenueType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, input):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
account_repo = AccountRepository()
|
|
command = UpdateRevenueCommand(
|
|
revenue_repo=revenue_repo,
|
|
account_repo=account_repo,
|
|
revenue_id=input.id,
|
|
amount=input.amount,
|
|
start_date=input.start_date,
|
|
end_date=input.end_date
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'revenue': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'revenue': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while updating the revenue record"
|
|
}
|
|
|
|
|
|
class DeleteRevenueMutation(graphene.Mutation):
|
|
"""Mutation to delete a revenue record."""
|
|
class Arguments:
|
|
id = graphene.ID(required=True)
|
|
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, id):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
command = DeleteRevenueCommand(
|
|
revenue_repo=revenue_repo,
|
|
revenue_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 revenue record"
|
|
}
|
|
|
|
|
|
class EndRevenueMutation(graphene.Mutation):
|
|
"""Mutation to end a revenue record."""
|
|
class Arguments:
|
|
id = graphene.ID(required=True)
|
|
end_date = graphene.String()
|
|
|
|
revenue = Field(RevenueType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, id, end_date=None):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
command = EndRevenueCommand(
|
|
revenue_repo=revenue_repo,
|
|
revenue_id=id,
|
|
end_date=end_date
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'revenue': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'revenue': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while ending the revenue record"
|
|
}
|
|
|
|
|
|
class CalculateTotalRevenueMutation(graphene.Mutation):
|
|
"""Mutation to calculate total revenue."""
|
|
class Arguments:
|
|
account_id = graphene.ID()
|
|
start_date = graphene.String()
|
|
end_date = graphene.String()
|
|
|
|
total_revenue = Float()
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, account_id=None, start_date=None, end_date=None):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
command = CalculateTotalRevenueCommand(
|
|
revenue_repo=revenue_repo,
|
|
start_date=start_date,
|
|
end_date=end_date,
|
|
account_id=account_id
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'total_revenue': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'total_revenue': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while calculating total revenue"
|
|
}
|
|
|
|
|
|
class GetRevenueByDateRangeMutation(graphene.Mutation):
|
|
"""Mutation to get revenues within a date range."""
|
|
class Arguments:
|
|
start_date = graphene.String(required=True)
|
|
end_date = graphene.String(required=True)
|
|
|
|
revenues = List(RevenueType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info, start_date, end_date):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
command = GetRevenueByDateRangeCommand(
|
|
revenue_repo=revenue_repo,
|
|
start_date=start_date,
|
|
end_date=end_date
|
|
)
|
|
result = command.execute()
|
|
return {
|
|
'revenues': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'revenues': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while retrieving revenues by date range"
|
|
}
|
|
|
|
|
|
class GetActiveRevenuesMutation(graphene.Mutation):
|
|
"""Mutation to get all active revenue records."""
|
|
class Arguments:
|
|
pass
|
|
|
|
revenues = List(RevenueType)
|
|
success = Boolean()
|
|
errors = List(String)
|
|
message = String()
|
|
|
|
@staticmethod
|
|
def mutate(root, info):
|
|
try:
|
|
revenue_repo = RevenueRepository()
|
|
command = GetActiveRevenuesCommand(revenue_repo=revenue_repo)
|
|
result = command.execute()
|
|
return {
|
|
'revenues': result.data,
|
|
'success': result.success,
|
|
'errors': result.errors,
|
|
'message': result.message
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'revenues': None,
|
|
'success': False,
|
|
'errors': [str(e)],
|
|
'message': "An error occurred while retrieving active revenues"
|
|
}
|