<reponame>BitMask-Technologies/route-lift-api<filename>routelift_api/message_templates/utils.py
import logging

from django.core.exceptions import ObjectDoesNotExist

from .models import TemplateSettings, MessageTemplates, NotificationType

# Get an instance of a logger
logger = logging.getLogger(__name__)


def create_message_template_type(message, type, company):
    try:
        send_feedback = MessageTemplates.objects.create(
            message=message,
            type=type,
            company=company
        )
        return send_feedback, "success"

    except Exception as e:
        logger.error("createMessageTemplate@error")
        logger.error(e)
        return None, str(e)


def get_tags_by_notification_type(type):
    try:
        tags = NotificationType.objects.get(type=type).tags
        return tags, "success"
    except Exception as e:
        logger.error("getTagsByNotificationType@error")
        logger.error(e)
        return None, str(e)


def validate_message_template(message, type):
    tags, err = get_tags_by_notification_type(type)
    if not tags:
        return None, "No tags for corresponding type"
    import re
    matches = re.findall(r'{{\w*}}', message)
    if set(tags) != set(matches):
        return None, "Invalid tags used"
    return True, "successfully passed test"


def get_all_message_template():
    """returns all drivers or none"""
    try:
        return MessageTemplates.objects.all(), "success"
    except Exception as e:
        return None, str(e)


def get_message_template_by_id(message_template_id):
    """retrieve message template by id"""
    try:
        message_template = MessageTemplates.objects.get(pk=message_template_id)
        return message_template, "success"

    except ObjectDoesNotExist as e:
        logger.error(f"Driver with ID: {message_template_id} does not exist")
        logger.error(e)
        return None, str(e)


def get_message_template_by_type(type_id):
    """retrieve message template by id"""
    try:
        message_template = MessageTemplates.objects.get(type=type_id)
        return message_template, "success"

    except ObjectDoesNotExist as e:
        logger.error(f"Driver with ID: {type_id} does not exist")
        logger.error(e)
        return None, str(e)


def update_message_template_type(message, notification_type, company, message_template):
    """update message template"""
    try:
        message_template.message = message
        message_template.type = notification_type
        message_template.company = company

        message_template.save()

        return message_template, "success"
    except Exception as e:
        logger.error("updateMessageTemplateRecord@error")
        logger.error(e)
        return None, str(e)


def delete_messages_template(message_template):
    """permanently delete customer"""
    try:
        message_template.delete()
        return True, "success"
    except Exception as e:
        logger.error("deleteMessageTemplate@error")
        logger.error(e)
        return False, str(e)


def valid_boolean_values(bool_values):
    for value in bool_values:
        if type(value) is not bool:
            return None, "failure"
    return True, "success"


def create_template_setting(planned, ongoing, after, cancelled, company):
    try:
        message_settings = TemplateSettings.objects.create(
            planned=planned,
            ongoing=ongoing,
            after=after,
            cancelled=cancelled,
            company=company
        )
        return message_settings, "success"

    except Exception as e:
        logger.error("createMessageSettings@error")
        logger.error(e)
        return None, str(e)


def update_template_setting(planned, ongoing, after, cancelled, company, setting):
    try:
        setting.planned = planned
        setting.ongoing = ongoing
        setting.after = after
        setting.cancelled = cancelled
        setting.company = company
        setting.save()

        return setting, "success"

    except Exception as e:
        logger.error("updateMessageSettings@error")
        logger.error(e)
        return None, str(e)


def get_all_template_setting():
    """returns all saved settings for company notification"""
    try:
        return TemplateSettings.objects.all(), "success"
    except Exception as e:
        return None, str(e)


def get_message_setting_by_company(company_id):
    """retrieve message template by id"""
    try:
        message_template = TemplateSettings.objects.get(company=company_id)
        return message_template, "success"

    except ObjectDoesNotExist as e:
        logger.error(f"Settings with company: {company_id} does not exist")
        logger.error(e)
        return None, str(e)


def delete_message_setting(message_setting):
    """permanently delete customer"""
    try:
        message_setting.delete()
        return True, "success"
    except Exception as e:
        logger.error("deleteMessageTemplate@error")
        logger.error(e)
        return False, str(e)

import logging

from django.core.exceptions import ObjectDoesNotExist

from .models import TemplateSettings, MessageTemplates, NotificationType

# Get an instance of a logger
logger = logging.getLogger(__name__)


def create_message_template_type(message, type, company):
    try:
        send_feedback = MessageTemplates.objects.create(
            message=message,
            type=type,
            company=company
        )
        return send_feedback, "success"

    except Exception as e:
        logger.error("createMessageTemplate@error")
        logger.error(e)
        return None, str(e)


def get_tags_by_notification_type(type):
    try:
        tags = NotificationType.objects.get(type=type).tags
        return tags, "success"
    except Exception as e:
        logger.error("getTagsByNotificationType@error")
        logger.error(e)
        return None, str(e)


def validate_message_template(message, type):
    tags, err = get_tags_by_notification_type(type)
    if not tags:
        return None, "No tags for corresponding type"
    import re
    matches = re.findall(r'{{\w*}}', message)
    if set(tags) != set(matches):
        return None, "Invalid tags used"
    return True, "successfully passed test"


def get_all_message_template():
    """returns all drivers or none"""
    try:
        return MessageTemplates.objects.all(), "success"
    except Exception as e:
        return None, str(e)


def get_message_template_by_id(message_template_id):
    """retrieve message template by id"""
    try:
        message_template = MessageTemplates.objects.get(pk=message_template_id)
        return message_template, "success"

    except ObjectDoesNotExist as e:
        logger.error(f"Driver with ID: {message_template_id} does not exist")
        logger.error(e)
        return None, str(e)


def get_message_template_by_type(type_id):
    """retrieve message template by id"""
    try:
        message_template = MessageTemplates.objects.get(type=type_id)
        return message_template, "success"

    except ObjectDoesNotExist as e:
        logger.error(f"Driver with ID: {type_id} does not exist")
        logger.error(e)
        return None, str(e)


def update_message_template_type(message, notification_type, company, message_template):
    """update message template"""
    try:
        message_template.message = message
        message_template.type = notification_type
        message_template.company = company

        message_template.save()

        return message_template, "success"
    except Exception as e:
        logger.error("updateMessageTemplateRecord@error")
        logger.error(e)
        return None, str(e)


def delete_messages_template(message_template):
    """permanently delete customer"""
    try:
        message_template.delete()
        return True, "success"
    except Exception as e:
        logger.error("deleteMessageTemplate@error")
        logger.error(e)
        return False, str(e)


def valid_boolean_values(bool_values):
    for value in bool_values:
        if type(value) is not bool:
            return None, "failure"
    return True, "success"


def create_template_setting(planned, ongoing, after, cancelled, company):
    try:
        message_settings = TemplateSettings.objects.create(
            planned=planned,
            ongoing=ongoing,
            after=after,
            cancelled=cancelled,
            company=company
        )
        return message_settings, "success"

    except Exception as e:
        logger.error("createMessageSettings@error")
        logger.error(e)
        return None, str(e)


def update_template_setting(planned, ongoing, after, cancelled, company, setting):
    try:
        setting.planned = planned
        setting.ongoing = ongoing
        setting.after = after
        setting.cancelled = cancelled
        setting.company = company
        setting.save()

        return setting, "success"

    except Exception as e:
        logger.error("updateMessageSettings@error")
        logger.error(e)
        return None, str(e)


def get_all_template_setting():
    """returns all saved settings for company notification"""
    try:
        return TemplateSettings.objects.all(), "success"
    except Exception as e:
        return None, str(e)


def get_message_setting_by_company(company_id):
    """retrieve message template by id"""
    try:
        message_template = TemplateSettings.objects.get(company=company_id)
        return message_template, "success"

    except ObjectDoesNotExist as e:
        logger.error(f"Settings with company: {company_id} does not exist")
        logger.error(e)
        return None, str(e)


def delete_message_setting(message_setting):
    """permanently delete customer"""
    try:
        message_setting.delete()
        return True, "success"
    except Exception as e:
        logger.error("deleteMessageTemplate@error")
        logger.error(e)
        return False, str(e)
# Get an instance of a logger returns all drivers or none retrieve message template by id retrieve message template by id update message template permanently delete customer returns all saved settings for company notification retrieve message template by id permanently delete customer