diff --git a/backend/leads/csv_import.py b/backend/leads/csv_import.py index 4c70bf9c..47b509cc 100644 --- a/backend/leads/csv_import.py +++ b/backend/leads/csv_import.py @@ -397,7 +397,10 @@ def _get_or_create_tag(name: str, org, cache: dict[str, Tags]) -> Tags: # SELECT-then-INSERT race when two imports name the same new tag. slug = Tags.slug_for(name) if slug not in cache: - cache[slug], _ = Tags.objects.get_or_create( - slug=slug, org=org, defaults={"name": name} - ) + tag, _ = Tags.objects.get_or_create(slug=slug, org=org, defaults={"name": name}) + if not tag.is_active: + # Same as the tags API: naming an archived tag brings it back. + tag.is_active = True + tag.save(update_fields=["is_active"]) + cache[slug] = tag return cache[slug] diff --git a/backend/leads/tests/test_lead_import.py b/backend/leads/tests/test_lead_import.py index 182b50be..bdaed0d2 100644 --- a/backend/leads/tests/test_lead_import.py +++ b/backend/leads/tests/test_lead_import.py @@ -591,3 +591,11 @@ class TestTagSlugs: tag = Tags.objects.get(org=org_a) assert tag.slug == "trade-show" assert set(Lead.objects.values_list("tags", flat=True)) == {tag.id} + + def test_archived_tag_is_reactivated(self, admin_client, org_a): + Tags.objects.create(name="VIP", slug="vip", org=org_a, is_active=False) + response = _post(admin_client, COMMIT, self._tagged("a@example.com", "VIP")) + assert response.status_code == 200, response.json() + tag = Lead.objects.get().tags.get() + assert tag.is_active + assert Tags.objects.filter(org=org_a).count() == 1