diff --git a/custom_objects/rollup_engine.py b/custom_objects/rollup_engine.py index 18278e6..6ea24ea 100644 --- a/custom_objects/rollup_engine.py +++ b/custom_objects/rollup_engine.py @@ -1,7 +1,7 @@ from decimal import Decimal from django.db.models import Sum, Count, Min, Max, Avg from django.core.exceptions import ValidationError -from custom_objects.models import CustomFieldDefinition, CustomRecord +from custom_objects.models import CustomFieldDefinition, CustomRecord, CustomObjectDefinition STANDARD_REVERSE_RELATIONS = { 'Account': { @@ -97,6 +97,72 @@ def recalculate_rollups_for_record(parent_entity_or_object, parent_id): parent_record.save(update_fields=['data']) +def should_recalculate_child_rollups(child_name, instance, created): + """ + Early-exit check: + Returns True if: + - This is a new record (created=True) + - Or if any field relevant to active roll-up definitions (target field, filter criteria, or parent FK) + actually changed on the instance compared to its pre_save snapshot. + """ + if created: + return True + + old_state = getattr(instance, '_rollup_old_state', None) + if not old_state: + return True + + from custom_objects.models import CustomFieldDefinition + from django.db.models import Q + + active_rollups = CustomFieldDefinition.objects.filter( + Q(field_type='rollup_summary') | Q(field_type='rollup') + ).filter( + Q(rollup_summary_object__iexact=child_name) + ) + + if not active_rollups.exists(): + return False + + # Check if any relevant field changed + for rf in active_rollups: + # Check target aggregation field + if rf.rollup_field: + curr_val = getattr(instance, rf.rollup_field, None) + if isinstance(instance, dict): + curr_val = instance.get(rf.rollup_field) + elif hasattr(instance, 'data') and isinstance(instance.data, dict) and rf.rollup_field in instance.data: + curr_val = instance.data.get(rf.rollup_field) + elif hasattr(instance, 'custom_fields') and isinstance(instance.custom_fields, dict) and rf.rollup_field in instance.custom_fields: + curr_val = instance.custom_fields.get(rf.rollup_field) + + if rf.rollup_field in old_state and curr_val != old_state[rf.rollup_field]: + return True + + # Check filter criteria fields + filters = rf.rollup_filter or {} + for f_key in filters: + curr_val = getattr(instance, f_key, None) + if hasattr(instance, 'data') and isinstance(instance.data, dict) and f_key in instance.data: + curr_val = instance.data.get(f_key) + elif hasattr(instance, 'custom_fields') and isinstance(instance.custom_fields, dict) and f_key in instance.custom_fields: + curr_val = instance.custom_fields.get(f_key) + + if f_key in old_state and curr_val != old_state[f_key]: + return True + + # Check if parent relation FK changed + for fk in ['account_id', 'contact_id', 'opportunity_id', 'lead_id', 'parent_id', 'object_id']: + if fk in old_state: + curr_fk = getattr(instance, fk, None) + if hasattr(instance, 'data') and isinstance(instance.data, dict) and fk in instance.data: + curr_fk = instance.data.get(fk) + if curr_fk != old_state[fk]: + return True + + return False + + def compute_rollup_value(parent_instance, rollup_field_def): """ Computes aggregation for a given rollup field definition against parent instance. @@ -155,4 +221,55 @@ def compute_rollup_value(parent_instance, rollup_field_def): res = qs.aggregate(val=Avg(target_field))['val'] return float(res) if isinstance(res, (int, float, Decimal)) else 0.0 + # Custom object records rollup (e.g. child CustomRecord pointing to parent) + child_cobj = CustomObjectDefinition.objects.filter( + name__iexact=child_entity + ).first() + if child_cobj: + parent_class_name = parent_instance.__class__.__name__ + filter_kwargs = {'custom_object': child_cobj} + + # Polymorphic parent link: check either parent standard object id or parent custom record id + if parent_class_name == 'CustomRecord': + filter_kwargs['data__parent_id'] = parent_instance.id + else: + filter_kwargs[f"data__{parent_class_name.lower()}_id"] = parent_instance.id + + qs = CustomRecord.objects.filter(**filter_kwargs) + + # Apply in-memory filtering for custom record JSON data + records = [] + for r in qs: + match = True + for fk, fv in filter_criteria.items(): + if r.data.get(fk) != fv: + match = False + break + if match: + records.append(r) + + if rollup_type == 'COUNT': + return len(records) + + vals = [] + for r in records: + v = r.data.get(target_field) + if v is not None: + try: + vals.append(float(v)) + except (ValueError, TypeError): + pass + + if not vals: + return 0.0 if rollup_type in ('SUM', 'AVG') else 0 + + if rollup_type == 'SUM': + return sum(vals) + elif rollup_type == 'MIN': + return min(vals) + elif rollup_type == 'MAX': + return max(vals) + elif rollup_type == 'AVG': + return sum(vals) / len(vals) + return 0 diff --git a/custom_objects/signals.py b/custom_objects/signals.py index f4eeb3f..b5964d7 100644 --- a/custom_objects/signals.py +++ b/custom_objects/signals.py @@ -4,7 +4,35 @@ from custom_objects.models import CustomRecord from crm_core.models import Account, Contact, Lead, Opportunity, Activity from outreach.models import Campaign from custom_objects.formula_engine import evaluate_all_formulas_for_instance -from custom_objects.rollup_engine import recalculate_rollups_for_record +from custom_objects.rollup_engine import recalculate_rollups_for_record, should_recalculate_child_rollups + + +@receiver(pre_save, sender=Opportunity) +@receiver(pre_save, sender=Contact) +@receiver(pre_save, sender=Activity) +@receiver(pre_save, sender=CustomRecord) +def capture_pre_save_rollup_state(sender, instance, **kwargs): + """ + Captures snapshot of fields before saving to enable early exit optimization on post_save. + """ + if instance.pk: + try: + orig = sender.objects.get(pk=instance.pk) + state = {} + # Capture standard model attributes + for f in instance._meta.fields: + state[f.name] = getattr(orig, f.name, None) + # Capture data attributes if CustomRecord + if hasattr(orig, 'data') and isinstance(orig.data, dict): + for k, v in orig.data.items(): + state[k] = v + # Capture custom_fields if present + if hasattr(orig, 'custom_fields') and isinstance(orig.custom_fields, dict): + for k, v in orig.custom_fields.items(): + state[k] = v + instance._rollup_old_state = state + except Exception: + instance._rollup_old_state = None @receiver(pre_save, sender=Account) @@ -20,8 +48,17 @@ def evaluate_record_formulas(sender, instance, **kwargs): # Roll-up summary triggers on child objects @receiver(post_save, sender=Opportunity) +def trigger_opportunity_post_save(sender, instance, created, **kwargs): + if not should_recalculate_child_rollups('Opportunity', instance, created): + return + if instance.account_id: + recalculate_rollups_for_record('Account', instance.account_id) + if instance.primary_contact_id: + recalculate_rollups_for_record('Contact', instance.primary_contact_id) + + @receiver(post_delete, sender=Opportunity) -def trigger_opportunity_rollup(sender, instance, **kwargs): +def trigger_opportunity_post_delete(sender, instance, **kwargs): if instance.account_id: recalculate_rollups_for_record('Account', instance.account_id) if instance.primary_contact_id: @@ -29,15 +66,32 @@ def trigger_opportunity_rollup(sender, instance, **kwargs): @receiver(post_save, sender=Contact) +def trigger_contact_post_save(sender, instance, created, **kwargs): + if not should_recalculate_child_rollups('Contact', instance, created): + return + if instance.account_id: + recalculate_rollups_for_record('Account', instance.account_id) + + @receiver(post_delete, sender=Contact) -def trigger_contact_rollup(sender, instance, **kwargs): +def trigger_contact_post_delete(sender, instance, **kwargs): if instance.account_id: recalculate_rollups_for_record('Account', instance.account_id) @receiver(post_save, sender=Activity) +def trigger_activity_post_save(sender, instance, created, **kwargs): + if not should_recalculate_child_rollups('Activity', instance, created): + return + _recalc_activity_parents(instance) + + @receiver(post_delete, sender=Activity) -def trigger_activity_rollup(sender, instance, **kwargs): +def trigger_activity_post_delete(sender, instance, **kwargs): + _recalc_activity_parents(instance) + + +def _recalc_activity_parents(instance): if hasattr(instance, 'content_type') and instance.content_type: model_name = instance.content_type.model.capitalize() recalculate_rollups_for_record(model_name, instance.object_id) @@ -49,3 +103,35 @@ def trigger_activity_rollup(sender, instance, **kwargs): recalculate_rollups_for_record('Opportunity', instance.opportunity_id) if getattr(instance, 'lead_id', None): recalculate_rollups_for_record('Lead', instance.lead_id) + + +# Roll-up summary triggers for CustomRecord children +@receiver(post_save, sender=CustomRecord) +def trigger_custom_record_post_save(sender, instance, created, **kwargs): + cobj_name = instance.custom_object.name + if not should_recalculate_child_rollups(cobj_name, instance, created): + return + _recalc_custom_record_parents(instance) + + +@receiver(post_delete, sender=CustomRecord) +def trigger_custom_record_post_delete(sender, instance, **kwargs): + _recalc_custom_record_parents(instance) + + +def _recalc_custom_record_parents(instance): + data = instance.data or {} + # Recalculate parent CustomRecord if linked via parent_id + if 'parent_id' in data: + # Check custom object definition if available + recalculate_rollups_for_record(None, data['parent_id']) + + # Recalculate standard parent entities if linked via standard fk (e.g. account_id, contact_id) + if 'account_id' in data: + recalculate_rollups_for_record('Account', data['account_id']) + if 'contact_id' in data: + recalculate_rollups_for_record('Contact', data['contact_id']) + if 'opportunity_id' in data: + recalculate_rollups_for_record('Opportunity', data['opportunity_id']) + if 'lead_id' in data: + recalculate_rollups_for_record('Lead', data['lead_id']) diff --git a/tests/test_rollup_engine.py b/tests/test_rollup_engine.py index 8b837f2..72e81ca 100644 --- a/tests/test_rollup_engine.py +++ b/tests/test_rollup_engine.py @@ -82,3 +82,47 @@ class TestRollupEngine: opp2.save() acc.refresh_from_db() assert float(acc.custom_fields.get('won_opp_amount__c')) == 8000.0 + + def test_custom_record_parent_rollup_and_early_exit(self): + from custom_objects.models import CustomObjectDefinition, CustomRecord + u = User.objects.create_user('ru_user3', 'ru3@example.com', 'p') + acc = Account.objects.create(name='Custom Child Parent Acc', owner=u) + + child_cobj = CustomObjectDefinition.objects.create( + name='InvoiceItem__c', + label='Invoice Item', + created_by=u + ) + + # Roll-up on Account from CustomRecord child + CustomFieldDefinition.objects.create( + entity_name='Account', + name='total_invoiced__c', + label='Total Invoiced', + field_type='rollup', + rollup_summary_object='InvoiceItem__c', + rollup_type='SUM', + rollup_field='amount', + rollup_filter={'status': 'paid'} + ) + + item1 = CustomRecord.objects.create( + custom_object=child_cobj, + owner=u, + data={'account_id': acc.id, 'amount': 150.50, 'status': 'paid'} + ) + + acc.refresh_from_db() + assert float(acc.custom_fields.get('total_invoiced__c')) == 150.50 + + # Update unrelated field on child (should early exit and not trigger lock/save) + item1.data['notes'] = 'Unrelated update' + item1.save() + acc.refresh_from_db() + assert float(acc.custom_fields.get('total_invoiced__c')) == 150.50 + + # Update relevant field + item1.data['amount'] = 200.00 + item1.save() + acc.refresh_from_db() + assert float(acc.custom_fields.get('total_invoiced__c')) == 200.00