Skip to content

Commit

Permalink
Raise exception when deleting cat that is linked to template(s) (#2219)
Browse files Browse the repository at this point in the history
- Refactored dao_delete_template_category_by_id so it's easier to understand
  • Loading branch information
whabanks authored Jul 15, 2024
1 parent ad135c8 commit bab5f15
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
33 changes: 18 additions & 15 deletions app/dao/template_categories_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from app import db
from app.dao.dao_utils import transactional
from app.errors import InvalidRequest
from app.models import Template, TemplateCategory


Expand Down Expand Up @@ -58,23 +59,25 @@ def dao_delete_template_category_by_id(template_category_id, cascade=False):
template_category = dao_get_template_category_by_id(template_category_id)
templates = Template.query.filter_by(template_category_id=template_category_id).all()

if not templates or cascade:
if templates and not cascade:
raise InvalidRequest(
"Cannot delete categories associated with templates. Dissociate the category from templates first.", 400
)

if templates and cascade:
# When there are templates and we are cascading, we set the category to a default
# that matches the template's previous category's priority
if cascade:
for template in templates:
# Get the a default category that matches the previous priority of the template, based on template type
default_category_id = _get_default_category_id(
template_category.sms_process_type
if template.template_type == "sms"
else template_category.email_process_type
)
template.template_category_id = default_category_id
template.updated_at = datetime.utcnow()
db.session.add(template)
db.session.commit()

db.session.delete(template_category)
for template in templates:
# Get the a default category that matches the previous priority of the template, based on template type
default_category_id = _get_default_category_id(
template_category.sms_process_type if template.template_type == "sms" else template_category.email_process_type
)
template.template_category_id = default_category_id
template.updated_at = datetime.utcnow()
db.session.add(template)
db.session.commit()

db.session.delete(template_category)


def _get_default_category_id(process_type):
Expand Down
13 changes: 3 additions & 10 deletions app/template/template_category_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
dao_update_template_category,
)
from app.errors import register_errors
from app.models import Template, TemplateCategory
from app.models import TemplateCategory
from app.schemas import template_category_schema

template_category_blueprint = Blueprint(
Expand Down Expand Up @@ -92,13 +92,6 @@ def delete_template_category(template_category_id):
Returns:
(flask.Response): The response message and http status code.
"""

if request.args.get("cascade") == "True":
dao_delete_template_category_by_id(template_category_id, cascade=True)
return "", 204

if Template.query.filter_by(template_category_id=template_category_id).count() > 0:
return jsonify(message="Cannot delete a template category with templates assigned to it."), 400
else:
dao_delete_template_category_by_id(template_category_id)
cascade = True if request.args.get("cascade") == "True" else False
dao_delete_template_category_by_id(template_category_id, cascade=cascade)
return "", 204
4 changes: 3 additions & 1 deletion tests/app/dao/test_template_categories_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
dao_update_template_category,
)
from app.dao.templates_dao import dao_create_template
from app.errors import InvalidRequest
from app.models import BULK, NORMAL, Template, TemplateCategory
from tests.app.conftest import create_sample_template

Expand Down Expand Up @@ -375,7 +376,8 @@ def test_dao_delete_template_category_by_id_should_not_allow_deletion_when_assoc
):
create_sample_template(notify_db, notify_db_session, template_category=sample_template_category)

dao_delete_template_category_by_id(sample_template_category.id)
with pytest.raises(InvalidRequest):
dao_delete_template_category_by_id(sample_template_category.id)

assert TemplateCategory.query.count() == 1

Expand Down
2 changes: 1 addition & 1 deletion tests/app/template/test_template_category_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_get_template_categories(
"cascade, expected_status_code, expected_msg",
[
("True", 204, ""),
("False", 400, "Cannot delete a template category with templates assigned to it."),
("False", 400, "Cannot delete categories associated with templates. Dissociate the category from templates first."),
],
)
def test_delete_template_category_cascade(
Expand Down

0 comments on commit bab5f15

Please sign in to comment.