""" functions to fetch dta from other services"""
from django.db import connections, connection
from rest_framework.exceptions import NotFound



def get_cost_center(code):
    """function to user_details"""
    try:
        with connection.cursor() as cursor:
            cursor.execute("""
                SELECT Id, Code, Description   from CostCenter where Code=%s
            """, [
                code
            ])
            cost_center_data = cursor.fetchone()
            
    except Exception as e:
        raise NotFound({
            "status_code": 500,
            "status": "error",
            "message": f"User details not found: {str(e)}",
            "data": None
        })
    if cost_center_data:
        return {
            'id': cost_center_data[0],
            'code': cost_center_data[1],
            'description': cost_center_data[2],
        }
    else:
        return None