""" M2M GraphQL Schema for internal service-to-service calls. This endpoint is called by Temporal worker to create KYCProfile records. No authentication required (internal network only). """ import graphene from ..models import KYCApplication, KYCProfile class CreateKycProfileMutation(graphene.Mutation): """Create KYCProfile record from existing KYCApplication. Called after KYC approval to create long-term monitoring profile. """ class Arguments: kyc_application_id = graphene.String(required=True) success = graphene.Boolean() profile_uuid = graphene.String() message = graphene.String() def mutate(self, info, kyc_application_id: str): try: # Find the KYCApplication kyc_application = KYCApplication.objects.get(uuid=kyc_application_id) # Check if profile already exists for this user/team existing = KYCProfile.objects.filter( user_id=kyc_application.user_id, team_name=kyc_application.team_name, ).first() if existing: return CreateKycProfileMutation( success=True, profile_uuid=str(existing.uuid), message="Profile already exists", ) # Create KYCProfile by copying fields from KYCApplication profile = KYCProfile.objects.create( user_id=kyc_application.user_id, team_name=kyc_application.team_name, country_code=kyc_application.country_code, workflow_status='active', # Approved = active for profile score=kyc_application.score, contact_person=kyc_application.contact_person, contact_email=kyc_application.contact_email, contact_phone=kyc_application.contact_phone, content_type=kyc_application.content_type, object_id=kyc_application.object_id, approved_by=kyc_application.approved_by, approved_at=kyc_application.approved_at, ) return CreateKycProfileMutation( success=True, profile_uuid=str(profile.uuid), message="Profile created", ) except KYCApplication.DoesNotExist: return CreateKycProfileMutation( success=False, profile_uuid="", message=f"KYCApplication not found: {kyc_application_id}", ) except Exception as e: return CreateKycProfileMutation( success=False, profile_uuid="", message=str(e), ) class M2MQuery(graphene.ObjectType): """M2M Query - health check only.""" health = graphene.String() def resolve_health(self, info): return "ok" class M2MMutation(graphene.ObjectType): """M2M Mutations for internal service calls.""" # New name create_kyc_profile = CreateKycProfileMutation.Field() # Old name for backwards compatibility create_kyc_monitoring = CreateKycProfileMutation.Field() m2m_schema = graphene.Schema(query=M2MQuery, mutation=M2MMutation)