def add_customer(customer_id, name, lastname, home_address, phone_number,
                 email_address, status, credit_limit):
    """ Add a new customer to the database """
    logging.info("add_customer()")
    # Convert inputs into a dictionary
    customer_dict = [
        {
            'customer_id': customer_id,
            'name': name,
            'lastname': lastname,
            'home_address': home_address,
            'phone_number': phone_number,
            'email_address': email_address,
            'status': status,
            'credit_limit': credit_limit
        },
    ]
    # Insert the given customer data into the database
    try:
        with database.atomic():
            Customer.insert_many(customer_dict).execute()
    except IntegrityError as err_msg:
        logging.error(
            f"Failed to add customer {customer_id}: {name} {lastname} to the database: {err_msg}"
        )
        raise IntegrityError
def delete_customer(customer_id):
    """ Delete a customer from the database using customer_id """
    logging.info("delete_customer()")
    # Try to find given customer in the database, just want to see logging error
    search_customer(customer_id)
    # Try to delete whether it is found or not
    with database.atomic():
        Customer.delete_by_id(customer_id)
def update_customer_credit(customer_id, credit_limit):
    """ Update an existing customer's credit limit in the database """
    logging.info("update_customer_credit()")
    try:
        with database.atomic():
            person = Customer.get_by_id(customer_id)
            person.credit_limit = credit_limit
            Customer.bulk_update([person], fields=[Customer.credit_limit])
    except DoesNotExist:
        logging.error(
            f"Can't find customer data with id = {customer_id} in the database."
        )
        raise ValueError
def add_customer(customer_id, name, lastname, home_address, phone_number,
                 email_address, status, credit_limit):
    """ Add a new customer to the database """
    logging.info("add_customer()")
    # Insert the given customer data into the database
    try:
        with database.atomic():
            Customer.insert_many(
                get_customer_generator(customer_id, name, lastname,
                                       home_address, phone_number,
                                       email_address, status,
                                       credit_limit)).execute()
    except (OperationalError, IntegrityError) as err_msg:
        logging.error(
            f"Failed to add customer {customer_id}: {name} {lastname} to the database: {err_msg}"
        )
        raise IntegrityError
def search_customer(customer_id):
    """ Search for a customer in the database using customer_id """
    logging.info("search_customer()")
    result = {}
    try:
        with database.atomic():
            person = Customer.get_by_id(customer_id)
            result = {
                'name': person.name,
                'lastname': person.lastname,
                'email_address': person.email_address,
                'phone_number': person.phone_number
            }
    except DoesNotExist:
        logging.error(
            f"Can't find customer data with id = {customer_id} in the database."
        )
    finally:
        return result
def add_customer(customer_id, name, last_name, home_address, phone_number, email_address, status,
                 credit_limit):
    """
        Adds a new customer to the database. Throw an exception if the customer already exists.
        Checks the input data to ensure its valid.
        Customer ID is used as the primary  key.
    """
    with database.atomic():
        try:
            new_customer = Customer.create(customer_id=customer_id,
                                           name=name,
                                           last_name=last_name,
                                           home_address=home_address,
                                           phone_number=phone_number,
                                           email_address=email_address,
                                           status=status,
                                           credit_limit=credit_limit)
            new_customer.save()
            logging.info('New Customer: %s, created and saved to the database.', customer_id)
        except peewee.IntegrityError as err:
            print(err)
            logging.warning('Error adding to db, could not add customer: %s', customer_id)
def list_active_customers():
    """ Return the total number of customers in the database whose status is currently active """
    logging.info("list_active_customers()")
    with database.atomic():
        active_customers = Customer.filter(status=True)
        return len(active_customers)