Skip to content

Commit

Permalink
✨ Fix Testimonial Auto-Share Sequence During Creation (#1179)
Browse files Browse the repository at this point in the history
* ✨ fix(api): ensure testimonial save and publish sequence correctly

Refactored testimonial save logic to ensure proper sequence. Fixed bug
where 'published_at' and community additions occurred before save,
potentially causing issues.

Benefits:
- Ensures required fields are set before saving
- Avoids premature community addition 🚀

* added tests for auto share setting utility function

* Refactor get_auto_shared_with_list function to handle missing testimonial or community
  • Loading branch information
abdullai-t authored Oct 16, 2024
1 parent 408c74f commit 194d0a8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/api/store/testimonial.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ def get_auto_shared_with_list(testimonial):
3. The auto-share settings contain tags that match the tags of the testimonial.
The function collects these matching communities and returns a list of them. This list will be used to share the testimonial with those communities.
"""
if not testimonial:
if not testimonial or not testimonial.community:
print("No testimonial or community")
return []

testimonial_community = testimonial.community
if not testimonial_community:
return []

community_zip_codes = set(testimonial_community.locations.values_list('zipcode', flat=True))
communities_list = set()
Expand Down Expand Up @@ -197,11 +196,6 @@ def create_testimonial(self, context: Context, args) -> Tuple[dict, MassEnergize
user = UserProfile.objects.filter(email=user_email).first()
if user:
new_testimonial.user = user

if is_published:
new_testimonial.published_at = parse_datetime_to_aware()
add_auto_shared_communities_to_testimonial(new_testimonial)


if action:
testimonial_action = Action.objects.get(id=action)
Expand Down Expand Up @@ -242,6 +236,11 @@ def create_testimonial(self, context: Context, args) -> Tuple[dict, MassEnergize

new_testimonial.save()

if is_published:
new_testimonial.published_at = parse_datetime_to_aware()
add_auto_shared_communities_to_testimonial(new_testimonial)
new_testimonial.save()

if audience:
new_testimonial.audience.set(audience)

Expand Down
8 changes: 8 additions & 0 deletions src/api/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
Location, Media,
Menu, Message,
RealEstateUnit,
Tag,
Team,
Testimonial,
TestimonialAutoShareSettings, UserActionRel,
Expand Down Expand Up @@ -496,5 +497,12 @@ def make_section(**kwargs):
"media": kwargs.get("media"),
})
return section


def make_tag(**kwargs):
return Tag.objects.create(**{
**kwargs,
"name": kwargs.get("name") or f"New Tag-{datetime.now().timestamp()}",
})


68 changes: 68 additions & 0 deletions src/api/tests/unit/utils/test_shareable_testimonial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from django.test import TestCase
from api.store.testimonial import get_auto_shared_with_list
from api.tests.common import make_tag, makeCommunity, makeLocation, makeTestimonial
from database.models import TestimonialAutoShareSettings, LocationType

class TestGetAutoSharedWithList(TestCase):

def setUp(self):
loc1 = makeLocation(state="MA", zipcode="02112", city="Boston")
loc4 = makeLocation(state="NY", zipcode="02115", city="City")

self.c1 = makeCommunity(name="Community - test 1", locations=[loc1.id] )
self.c2 = makeCommunity(name="Community - test 2",locations=[loc4.id])
self.c3 = makeCommunity(name="Community - test 3",)
self.c4 = makeCommunity(name="Community - test 4", )

self.testimonial = makeTestimonial(title="SHareable Testimonial")


self.tag1 = make_tag(name="Solar")
self.tag2 = make_tag(name="Waste")

def test_get_auto_shared_with_list_no_testimonial(self):
result = get_auto_shared_with_list(None)
self.assertEqual(result, [])


def test_get_auto_shared_with_list_no_community(self):
testimonial = makeTestimonial(title="SHareable Testimonial 2223")
result = get_auto_shared_with_list(testimonial)
self.assertEqual(result, [])

def test_get_auto_shared_with_list_no_auto_share_settings(self):
self.testimonial.community = makeCommunity(name="Community - test Within")
result = get_auto_shared_with_list(self.testimonial)
self.assertEqual(result.first(), None)

def test_get_auto_shared_with_list_geographical_range(self):
self.testimonial.community = self.c1
self.testimonial.save()
TestimonialAutoShareSettings.objects.create(
community=self.c3,
share_from_location_type=LocationType.STATE.value[0],
share_from_location_value='MA'
)
result = get_auto_shared_with_list(self.testimonial)
self.assertIn(self.c3, list(result))

def test_get_auto_shared_with_list_tags(self):
self.testimonial.community = self.c1
self.testimonial.tags.add(self.tag1)
self.testimonial.save()
auto_share_settings = TestimonialAutoShareSettings.objects.create(
community=self.c4,
)
auto_share_settings.excluded_tags.add(self.tag1)
result = get_auto_shared_with_list(self.testimonial)
self.assertIn(self.c4, list(result))

def test_get_auto_shared_with_list_communities(self):
self.testimonial.community = self.c1
self.testimonial.save()
auto_share_settings = TestimonialAutoShareSettings.objects.create(
community=self.c2,
)
auto_share_settings.share_from_communities.set([self.c1])
result = get_auto_shared_with_list(self.testimonial)
self.assertIn(self.c2, list(result))

0 comments on commit 194d0a8

Please sign in to comment.