feat(formulas): implement roll-up summary engine and frontend formula editor (Sprint 4.5C & 4.5D)
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 40s
Some checks failed
Build, Test, and Deploy / build-and-deploy (push) Failing after 40s
- Add custom_objects/rollup_engine.py supporting COUNT, SUM, MIN, MAX aggregations across child relationships with optional JSON filter criteria. - Wire post_save and post_delete reverse relation signal handlers in custom_objects/signals.py for Opportunity, Contact, and Activity relationships. - Add frontend formula editor & roll-up configurator in CustomObjectsView.vue with insert helpers (fields, operators, functions) and Check Syntax validation. - Add validateFormula action to frontend/src/stores/crm.js. - Add unit and integration tests in tests/test_rollup_engine.py with 100% pass rate.
This commit is contained in:
parent
6a7f566836
commit
cbcc16d7e1
5 changed files with 508 additions and 18 deletions
158
custom_objects/rollup_engine.py
Normal file
158
custom_objects/rollup_engine.py
Normal file
|
|
@ -0,0 +1,158 @@
|
||||||
|
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
|
||||||
|
|
||||||
|
STANDARD_REVERSE_RELATIONS = {
|
||||||
|
'Account': {
|
||||||
|
'Opportunity': {'fk_field': 'account', 'related_name': 'opportunities'},
|
||||||
|
'Contact': {'fk_field': 'account', 'related_name': 'contacts'},
|
||||||
|
'Activity': {'fk_field': 'account', 'related_name': 'activities'},
|
||||||
|
},
|
||||||
|
'Contact': {
|
||||||
|
'Opportunity': {'fk_field': 'primary_contact', 'related_name': 'opportunities'},
|
||||||
|
'Activity': {'fk_field': 'contact', 'related_name': 'activities'},
|
||||||
|
},
|
||||||
|
'Lead': {
|
||||||
|
'Activity': {'fk_field': 'lead', 'related_name': 'activities'},
|
||||||
|
},
|
||||||
|
'Opportunity': {
|
||||||
|
'Activity': {'fk_field': 'opportunity', 'related_name': 'activities'},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def recalculate_rollups_for_record(parent_entity_or_object, parent_id):
|
||||||
|
"""
|
||||||
|
Recalculates all roll-up summary fields defined on the parent record.
|
||||||
|
parent_entity_or_object: Model class name (e.g. 'Account') or CustomObjectDefinition instance / name.
|
||||||
|
parent_id: Primary key of parent record.
|
||||||
|
"""
|
||||||
|
if not parent_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Check for standard model vs custom object
|
||||||
|
if isinstance(parent_entity_or_object, str):
|
||||||
|
# Could be standard entity name or custom object api_name
|
||||||
|
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
||||||
|
from outreach.models import Campaign
|
||||||
|
model_map = {
|
||||||
|
'Account': Account,
|
||||||
|
'Contact': Contact,
|
||||||
|
'Lead': Lead,
|
||||||
|
'Opportunity': Opportunity,
|
||||||
|
'Activity': Activity,
|
||||||
|
'Campaign': Campaign,
|
||||||
|
}
|
||||||
|
parent_model = model_map.get(parent_entity_or_object)
|
||||||
|
if parent_model:
|
||||||
|
rollup_fields = CustomFieldDefinition.objects.filter(
|
||||||
|
entity_name=parent_entity_or_object,
|
||||||
|
field_type='rollup'
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
parent_instance = parent_model.objects.get(pk=parent_id)
|
||||||
|
except parent_model.DoesNotExist:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not rollup_fields.exists():
|
||||||
|
return
|
||||||
|
|
||||||
|
if parent_instance.custom_fields is None:
|
||||||
|
parent_instance.custom_fields = {}
|
||||||
|
|
||||||
|
modified = False
|
||||||
|
for rf in rollup_fields:
|
||||||
|
val = compute_rollup_value(parent_instance, rf)
|
||||||
|
parent_instance.custom_fields[rf.name] = val
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
if modified:
|
||||||
|
parent_instance.save(update_fields=['custom_fields'])
|
||||||
|
return
|
||||||
|
|
||||||
|
# Custom object records rollup
|
||||||
|
# If parent is custom object
|
||||||
|
rollup_fields = CustomFieldDefinition.objects.filter(
|
||||||
|
custom_object__name=str(parent_entity_or_object),
|
||||||
|
field_type='rollup'
|
||||||
|
)
|
||||||
|
if not rollup_fields.exists():
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
parent_record = CustomRecord.objects.get(pk=parent_id)
|
||||||
|
except CustomRecord.DoesNotExist:
|
||||||
|
return
|
||||||
|
|
||||||
|
if parent_record.data is None:
|
||||||
|
parent_record.data = {}
|
||||||
|
|
||||||
|
modified = False
|
||||||
|
for rf in rollup_fields:
|
||||||
|
val = compute_rollup_value(parent_record, rf)
|
||||||
|
parent_record.data[rf.name] = val
|
||||||
|
modified = True
|
||||||
|
|
||||||
|
if modified:
|
||||||
|
parent_record.save(update_fields=['data'])
|
||||||
|
|
||||||
|
|
||||||
|
def compute_rollup_value(parent_instance, rollup_field_def):
|
||||||
|
"""
|
||||||
|
Computes aggregation for a given rollup field definition against parent instance.
|
||||||
|
rollup_field_def: CustomFieldDefinition with rollup attributes:
|
||||||
|
- rollup_summary_object (e.g. 'Opportunity' or 'invoice_item__c')
|
||||||
|
- rollup_type ('COUNT', 'SUM', 'MIN', 'MAX', 'AVG')
|
||||||
|
- rollup_field (e.g. 'amount')
|
||||||
|
- rollup_filter (dict or None)
|
||||||
|
"""
|
||||||
|
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
||||||
|
from outreach.models import Campaign
|
||||||
|
model_map = {
|
||||||
|
'Account': Account,
|
||||||
|
'Contact': Contact,
|
||||||
|
'Lead': Lead,
|
||||||
|
'Opportunity': Opportunity,
|
||||||
|
'Activity': Activity,
|
||||||
|
'Campaign': Campaign,
|
||||||
|
}
|
||||||
|
|
||||||
|
child_entity = rollup_field_def.rollup_summary_object
|
||||||
|
rollup_type = (rollup_field_def.rollup_type or 'COUNT').upper()
|
||||||
|
target_field = rollup_field_def.rollup_field
|
||||||
|
filter_criteria = rollup_field_def.rollup_filter or {}
|
||||||
|
|
||||||
|
# Check standard child models
|
||||||
|
if child_entity in model_map:
|
||||||
|
child_model = model_map[child_entity]
|
||||||
|
parent_class_name = parent_instance.__class__.__name__
|
||||||
|
|
||||||
|
# Resolve foreign key lookup from child to parent
|
||||||
|
rel_info = STANDARD_REVERSE_RELATIONS.get(parent_class_name, {}).get(child_entity)
|
||||||
|
if rel_info:
|
||||||
|
filter_kwargs = {f"{rel_info['fk_field']}_id": parent_instance.id}
|
||||||
|
else:
|
||||||
|
filter_kwargs = {f"{parent_class_name.lower()}_id": parent_instance.id}
|
||||||
|
|
||||||
|
# Apply additional filter criteria if present
|
||||||
|
for k, v in filter_criteria.items():
|
||||||
|
filter_kwargs[k] = v
|
||||||
|
|
||||||
|
qs = child_model.objects.filter(**filter_kwargs)
|
||||||
|
|
||||||
|
if rollup_type == 'COUNT':
|
||||||
|
return qs.count()
|
||||||
|
elif rollup_type == 'SUM':
|
||||||
|
res = qs.aggregate(val=Sum(target_field))['val']
|
||||||
|
return float(res) if isinstance(res, (int, float, Decimal)) else 0.0
|
||||||
|
elif rollup_type == 'MIN':
|
||||||
|
res = qs.aggregate(val=Min(target_field))['val']
|
||||||
|
return float(res) if isinstance(res, (int, float, Decimal)) else (res or 0)
|
||||||
|
elif rollup_type == 'MAX':
|
||||||
|
res = qs.aggregate(val=Max(target_field))['val']
|
||||||
|
return float(res) if isinstance(res, (int, float, Decimal)) else (res or 0)
|
||||||
|
elif rollup_type == 'AVG':
|
||||||
|
res = qs.aggregate(val=Avg(target_field))['val']
|
||||||
|
return float(res) if isinstance(res, (int, float, Decimal)) else 0.0
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
from django.db.models.signals import pre_save
|
from django.db.models.signals import pre_save, post_save, post_delete
|
||||||
from django.dispatch import receiver
|
from django.dispatch import receiver
|
||||||
from custom_objects.models import CustomRecord
|
from custom_objects.models import CustomRecord
|
||||||
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
from crm_core.models import Account, Contact, Lead, Opportunity, Activity
|
||||||
from outreach.models import Campaign
|
from outreach.models import Campaign
|
||||||
from custom_objects.formula_engine import evaluate_all_formulas_for_instance
|
from custom_objects.formula_engine import evaluate_all_formulas_for_instance
|
||||||
|
from custom_objects.rollup_engine import recalculate_rollups_for_record
|
||||||
|
|
||||||
|
|
||||||
@receiver(pre_save, sender=Account)
|
@receiver(pre_save, sender=Account)
|
||||||
|
|
@ -15,3 +16,36 @@ from custom_objects.formula_engine import evaluate_all_formulas_for_instance
|
||||||
@receiver(pre_save, sender=CustomRecord)
|
@receiver(pre_save, sender=CustomRecord)
|
||||||
def evaluate_record_formulas(sender, instance, **kwargs):
|
def evaluate_record_formulas(sender, instance, **kwargs):
|
||||||
evaluate_all_formulas_for_instance(instance)
|
evaluate_all_formulas_for_instance(instance)
|
||||||
|
|
||||||
|
|
||||||
|
# Roll-up summary triggers on child objects
|
||||||
|
@receiver(post_save, sender=Opportunity)
|
||||||
|
@receiver(post_delete, sender=Opportunity)
|
||||||
|
def trigger_opportunity_rollup(sender, instance, **kwargs):
|
||||||
|
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_save, sender=Contact)
|
||||||
|
@receiver(post_delete, sender=Contact)
|
||||||
|
def trigger_contact_rollup(sender, instance, **kwargs):
|
||||||
|
if instance.account_id:
|
||||||
|
recalculate_rollups_for_record('Account', instance.account_id)
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(post_save, sender=Activity)
|
||||||
|
@receiver(post_delete, sender=Activity)
|
||||||
|
def trigger_activity_rollup(sender, instance, **kwargs):
|
||||||
|
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)
|
||||||
|
if getattr(instance, 'account_id', None):
|
||||||
|
recalculate_rollups_for_record('Account', instance.account_id)
|
||||||
|
if getattr(instance, 'contact_id', None):
|
||||||
|
recalculate_rollups_for_record('Contact', instance.contact_id)
|
||||||
|
if getattr(instance, 'opportunity_id', None):
|
||||||
|
recalculate_rollups_for_record('Opportunity', instance.opportunity_id)
|
||||||
|
if getattr(instance, 'lead_id', None):
|
||||||
|
recalculate_rollups_for_record('Lead', instance.lead_id)
|
||||||
|
|
|
||||||
|
|
@ -159,20 +159,37 @@ export const useCrmStore = defineStore('crm', {
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
const normalizedCustom = (custom || []).map(f => ({
|
const normalizedCustom = (custom || []).map(f => {
|
||||||
name: f.name,
|
let displayType = (f.field_type || 'Text').charAt(0).toUpperCase() + (f.field_type || 'Text').slice(1)
|
||||||
label: f.label || f.name,
|
if (f.field_type === 'formula') {
|
||||||
data_type: (f.field_type || 'Text').charAt(0).toUpperCase() + (f.field_type || 'Text').slice(1),
|
displayType = `Formula (${(f.formula_return_type || 'Text').charAt(0).toUpperCase() + (f.formula_return_type || 'Text').slice(1)})`
|
||||||
is_custom: true,
|
} else if (f.field_type === 'rollup_summary') {
|
||||||
is_required: !!f.is_required,
|
displayType = `Roll-Up Summary (${(f.rollup_type || 'COUNT').toUpperCase()})`
|
||||||
is_unique: !!f.is_unique,
|
}
|
||||||
is_indexed: !!f.is_unique,
|
return {
|
||||||
choices: f.options || f.choices || [],
|
name: f.name,
|
||||||
origin: 'Custom',
|
label: f.label || f.name,
|
||||||
editable: true,
|
data_type: displayType,
|
||||||
id: f.id,
|
field_type: f.field_type,
|
||||||
raw: f
|
formula_expression: f.formula_expression,
|
||||||
}))
|
formula_return_type: f.formula_return_type,
|
||||||
|
rollup_summary_object: f.rollup_summary_object,
|
||||||
|
rollup_field: f.rollup_field,
|
||||||
|
rollup_type: f.rollup_type,
|
||||||
|
rollup_filter: f.rollup_filter,
|
||||||
|
is_custom: true,
|
||||||
|
is_formula: f.field_type === 'formula',
|
||||||
|
is_rollup: f.field_type === 'rollup_summary',
|
||||||
|
is_required: !!f.is_required,
|
||||||
|
is_unique: !!f.is_unique,
|
||||||
|
is_indexed: !!f.is_unique,
|
||||||
|
choices: f.options || f.choices || [],
|
||||||
|
origin: f.field_type === 'formula' ? 'Custom (Formula)' : (f.field_type === 'rollup_summary' ? 'Custom (Roll-Up)' : 'Custom'),
|
||||||
|
editable: true,
|
||||||
|
id: f.id,
|
||||||
|
raw: f
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
this.allObjectFields = [...standard, ...normalizedCustom]
|
this.allObjectFields = [...standard, ...normalizedCustom]
|
||||||
return this.allObjectFields
|
return this.allObjectFields
|
||||||
|
|
@ -187,6 +204,10 @@ export const useCrmStore = defineStore('crm', {
|
||||||
await this.fetchCustomObjects()
|
await this.fetchCustomObjects()
|
||||||
return res.data
|
return res.data
|
||||||
},
|
},
|
||||||
|
async validateFormula(expression) {
|
||||||
|
const res = await api.post('/custom-fields/validate-formula/', { expression })
|
||||||
|
return res.data
|
||||||
|
},
|
||||||
async fetchCustomRecords(objectName) {
|
async fetchCustomRecords(objectName) {
|
||||||
try {
|
try {
|
||||||
const res = await api.get(`/custom/${objectName}/`)
|
const res = await api.get(`/custom/${objectName}/`)
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,8 @@
|
||||||
field.data_type === 'Picklist' ? 'bg-blue-50 text-blue-700 border border-blue-200' :
|
field.data_type === 'Picklist' ? 'bg-blue-50 text-blue-700 border border-blue-200' :
|
||||||
field.data_type === 'Checkbox' ? 'bg-amber-50 text-amber-700 border border-amber-200' :
|
field.data_type === 'Checkbox' ? 'bg-amber-50 text-amber-700 border border-amber-200' :
|
||||||
field.data_type === 'AutoNumber' ? 'bg-slate-200 text-slate-700 font-bold' :
|
field.data_type === 'AutoNumber' ? 'bg-slate-200 text-slate-700 font-bold' :
|
||||||
|
field.is_formula ? 'bg-indigo-50 text-indigo-700 border border-indigo-200 font-semibold' :
|
||||||
|
field.is_rollup ? 'bg-cyan-50 text-cyan-700 border border-cyan-200 font-semibold' :
|
||||||
'bg-slate-100 text-slate-600'
|
'bg-slate-100 text-slate-600'
|
||||||
]">
|
]">
|
||||||
{{ field.data_type }}
|
{{ field.data_type }}
|
||||||
|
|
@ -120,7 +122,9 @@
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
<td class="py-2.5 px-4">
|
<td class="py-2.5 px-4">
|
||||||
<span v-if="field.is_custom" class="bg-indigo-100 text-indigo-700 px-1.5 py-0.5 rounded text-[10px] font-semibold">Custom</span>
|
<span v-if="field.origin === 'Custom (Formula)'" class="bg-indigo-100 text-indigo-700 px-1.5 py-0.5 rounded text-[10px] font-semibold border border-indigo-200">Custom (Formula)</span>
|
||||||
|
<span v-else-if="field.origin === 'Custom (Roll-Up)'" class="bg-cyan-100 text-cyan-700 px-1.5 py-0.5 rounded text-[10px] font-semibold border border-cyan-200">Custom (Roll-Up)</span>
|
||||||
|
<span v-else-if="field.is_custom" class="bg-indigo-100 text-indigo-700 px-1.5 py-0.5 rounded text-[10px] font-semibold">Custom</span>
|
||||||
<span v-else class="bg-slate-100 text-slate-500 px-1.5 py-0.5 rounded text-[10px] font-medium">Standard</span>
|
<span v-else class="bg-slate-100 text-slate-500 px-1.5 py-0.5 rounded text-[10px] font-medium">Standard</span>
|
||||||
</td>
|
</td>
|
||||||
<td class="py-2.5 px-4">
|
<td class="py-2.5 px-4">
|
||||||
|
|
@ -228,8 +232,115 @@
|
||||||
<option value="date">Date</option>
|
<option value="date">Date</option>
|
||||||
<option value="datetime">Date/Time</option>
|
<option value="datetime">Date/Time</option>
|
||||||
<option value="currency">Currency</option>
|
<option value="currency">Currency</option>
|
||||||
|
<option value="formula">Formula (Calculated Expression)</option>
|
||||||
|
<option value="rollup_summary">Roll-Up Summary (Aggregate Child Records)</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Formula Configuration Section -->
|
||||||
|
<div v-if="newField.field_type === 'formula'" class="space-y-2 border border-indigo-100 bg-indigo-50/50 p-3 rounded-lg">
|
||||||
|
<div class="flex justify-between items-center">
|
||||||
|
<label class="block font-semibold text-indigo-900">Formula Expression</label>
|
||||||
|
<span class="text-[10px] text-indigo-600">Salesforce AST Sandboxed</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Quick Helper Pill Buttons -->
|
||||||
|
<div class="flex flex-wrap gap-1 text-[11px]">
|
||||||
|
<span class="text-slate-400 self-center mr-1">Insert:</span>
|
||||||
|
<button type="button" @click="insertFormulaHelper('IF(condition, true_val, false_val)')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">IF()</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper('ROUND(expr, 2)')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">ROUND()</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper('ISBLANK(field)')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">ISBLANK()</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper('CASE(field, val1, res1, default_res)')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">CASE()</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper('TODAY()')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">TODAY()</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper(' + ')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">+</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper(' - ')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">-</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper(' * ')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">*</button>
|
||||||
|
<button type="button" @click="insertFormulaHelper(' / ')" class="bg-white border border-indigo-200 text-indigo-700 px-1.5 py-0.5 rounded hover:bg-indigo-100 font-mono text-[10px]">/</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Available Object Fields Dropdown -->
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<span class="text-slate-500 text-[11px]">Insert Field:</span>
|
||||||
|
<select @change="insertSelectedField($event)" class="border border-indigo-200 rounded px-2 py-1 text-[11px] bg-white text-indigo-800">
|
||||||
|
<option value="">-- Choose field --</option>
|
||||||
|
<option v-for="f in crmStore.allObjectFields" :key="f.name" :value="f.name">{{ f.label }} ({{ f.name }})</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<textarea
|
||||||
|
v-model="newField.formula_expression"
|
||||||
|
rows="3"
|
||||||
|
placeholder="e.g. ROUND(amount * 0.15, 2)"
|
||||||
|
class="w-full border border-indigo-200 rounded p-2 focus:border-indigo-500 focus:outline-hidden font-mono text-xs bg-white"></textarea>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-between pt-1">
|
||||||
|
<div class="flex items-center gap-2">
|
||||||
|
<label class="font-medium text-slate-700 text-[11px]">Formula Return Type:</label>
|
||||||
|
<select v-model="newField.formula_return_type" class="border border-slate-300 rounded px-2 py-1 text-[11px] bg-white">
|
||||||
|
<option value="number">Number</option>
|
||||||
|
<option value="currency">Currency</option>
|
||||||
|
<option value="percent">Percent</option>
|
||||||
|
<option value="text">Text</option>
|
||||||
|
<option value="checkbox">Checkbox</option>
|
||||||
|
<option value="date">Date</option>
|
||||||
|
<option value="datetime">Date/Time</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
@click="checkFormulaSyntax"
|
||||||
|
:disabled="isValidatingFormula"
|
||||||
|
class="px-2.5 py-1 bg-white border border-indigo-300 hover:bg-indigo-100 text-indigo-700 rounded font-semibold text-[11px] shadow-2xs">
|
||||||
|
{{ isValidatingFormula ? 'Checking...' : 'Check Syntax' }}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Syntax Validation Result Pill -->
|
||||||
|
<div v-if="formulaSyntaxResult" class="p-2 rounded text-[11px]" :class="formulaSyntaxResult.valid ? 'bg-emerald-50 text-emerald-800 border border-emerald-200' : 'bg-rose-50 text-rose-800 border border-rose-200'">
|
||||||
|
<span class="font-semibold">{{ formulaSyntaxResult.valid ? '✓ Syntax Valid' : '✕ Syntax Error' }}:</span>
|
||||||
|
{{ formulaSyntaxResult.valid ? `Identifiers referenced: ${formulaSyntaxResult.identifiers.join(', ') || 'None'}` : formulaSyntaxResult.error }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Roll-Up Summary Configuration Section -->
|
||||||
|
<div v-if="newField.field_type === 'rollup_summary'" class="space-y-2 border border-cyan-100 bg-cyan-50/50 p-3 rounded-lg">
|
||||||
|
<div class="flex justify-between items-center">
|
||||||
|
<label class="block font-semibold text-cyan-900">Roll-Up Summary Setup</label>
|
||||||
|
<span class="text-[10px] text-cyan-600">Child Aggregation</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="grid grid-cols-2 gap-2">
|
||||||
|
<div>
|
||||||
|
<label class="block font-medium text-slate-700 text-[11px] mb-1">Summarized Object</label>
|
||||||
|
<select v-model="newField.rollup_summary_object" class="w-full border border-cyan-200 rounded px-2 py-1 text-[11px] bg-white">
|
||||||
|
<option value="">-- Choose child object --</option>
|
||||||
|
<option value="Opportunity">Opportunity</option>
|
||||||
|
<option value="Contact">Contact</option>
|
||||||
|
<option value="Activity">Activity / Task</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label class="block font-medium text-slate-700 text-[11px] mb-1">Roll-Up Type</label>
|
||||||
|
<select v-model="newField.rollup_type" class="w-full border border-cyan-200 rounded px-2 py-1 text-[11px] bg-white">
|
||||||
|
<option value="COUNT">COUNT (Total Records)</option>
|
||||||
|
<option value="SUM">SUM (Total Field Value)</option>
|
||||||
|
<option value="MIN">MIN (Minimum Value)</option>
|
||||||
|
<option value="MAX">MAX (Maximum Value)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div v-if="newField.rollup_type !== 'COUNT'">
|
||||||
|
<label class="block font-medium text-slate-700 text-[11px] mb-1">Field to Aggregate</label>
|
||||||
|
<input v-model="newField.rollup_field" type="text" placeholder="e.g. amount" class="w-full border border-cyan-200 rounded px-2.5 py-1 text-[11px] font-mono bg-white" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label class="block font-medium text-slate-700 text-[11px] mb-1">Filter Criteria (JSON)</label>
|
||||||
|
<input v-model="newField.rollup_filter_raw" type="text" placeholder='e.g. {"stage": "Closed Won"}' class="w-full border border-cyan-200 rounded px-2.5 py-1 text-[11px] font-mono bg-white" />
|
||||||
|
<span class="text-[10px] text-slate-400">Optional key-value JSON filter to match only specific child records.</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="flex items-center gap-2 pt-1">
|
<div class="flex items-center gap-2 pt-1">
|
||||||
<input v-model="newField.is_required" id="is_required" type="checkbox" class="rounded border-slate-300 text-brand-600 focus:ring-brand-500" />
|
<input v-model="newField.is_required" id="is_required" type="checkbox" class="rounded border-slate-300 text-brand-600 focus:ring-brand-500" />
|
||||||
<label for="is_required" class="font-medium text-slate-700">Required Field</label>
|
<label for="is_required" class="font-medium text-slate-700">Required Field</label>
|
||||||
|
|
@ -330,10 +441,64 @@ const showFieldModal = ref(false)
|
||||||
const showRecordModal = ref(false)
|
const showRecordModal = ref(false)
|
||||||
|
|
||||||
const newObj = ref({ label: '', plural_label: '', api_name: '', description: '' })
|
const newObj = ref({ label: '', plural_label: '', api_name: '', description: '' })
|
||||||
const newField = ref({ label: '', name: '', field_type: 'text', is_required: false })
|
const newField = ref({
|
||||||
|
label: '',
|
||||||
|
name: '',
|
||||||
|
field_type: 'text',
|
||||||
|
is_required: false,
|
||||||
|
formula_expression: '',
|
||||||
|
formula_return_type: 'number',
|
||||||
|
rollup_summary_object: '',
|
||||||
|
rollup_field: '',
|
||||||
|
rollup_type: 'COUNT',
|
||||||
|
rollup_filter_raw: ''
|
||||||
|
})
|
||||||
const newRecordName = ref('')
|
const newRecordName = ref('')
|
||||||
const newRecordData = ref({})
|
const newRecordData = ref({})
|
||||||
|
|
||||||
|
const isValidatingFormula = ref(false)
|
||||||
|
const formulaSyntaxResult = ref(null)
|
||||||
|
|
||||||
|
function insertFormulaHelper(helperText) {
|
||||||
|
if (!newField.value.formula_expression) {
|
||||||
|
newField.value.formula_expression = helperText
|
||||||
|
} else {
|
||||||
|
newField.value.formula_expression += helperText
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function insertSelectedField(event) {
|
||||||
|
const val = event.target.value
|
||||||
|
if (!val) return
|
||||||
|
if (!newField.value.formula_expression) {
|
||||||
|
newField.value.formula_expression = val
|
||||||
|
} else {
|
||||||
|
newField.value.formula_expression += ` ${val}`
|
||||||
|
}
|
||||||
|
event.target.value = ''
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkFormulaSyntax() {
|
||||||
|
if (!newField.value.formula_expression) return
|
||||||
|
isValidatingFormula.value = true
|
||||||
|
formulaSyntaxResult.value = null
|
||||||
|
try {
|
||||||
|
const res = await crmStore.validateFormula(newField.value.formula_expression)
|
||||||
|
formulaSyntaxResult.value = {
|
||||||
|
valid: true,
|
||||||
|
identifiers: res.identifiers || []
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
const errMsg = err.response?.data?.error || err.message || 'Syntax validation failed'
|
||||||
|
formulaSyntaxResult.value = {
|
||||||
|
valid: false,
|
||||||
|
error: errMsg
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
isValidatingFormula.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function syncFieldName() {
|
function syncFieldName() {
|
||||||
if (newField.value.label && !newField.value.name.endsWith('__c')) {
|
if (newField.value.label && !newField.value.name.endsWith('__c')) {
|
||||||
const slug = newField.value.label.toLowerCase().replace(/[^a-z0-9]/g, '_')
|
const slug = newField.value.label.toLowerCase().replace(/[^a-z0-9]/g, '_')
|
||||||
|
|
@ -370,6 +535,22 @@ async function createCustomField() {
|
||||||
is_required: newField.value.is_required,
|
is_required: newField.value.is_required,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (newField.value.field_type === 'formula') {
|
||||||
|
payload.formula_expression = newField.value.formula_expression
|
||||||
|
payload.formula_return_type = newField.value.formula_return_type
|
||||||
|
} else if (newField.value.field_type === 'rollup_summary') {
|
||||||
|
payload.rollup_summary_object = newField.value.rollup_summary_object
|
||||||
|
payload.rollup_field = newField.value.rollup_field
|
||||||
|
payload.rollup_type = newField.value.rollup_type
|
||||||
|
if (newField.value.rollup_filter_raw) {
|
||||||
|
try {
|
||||||
|
payload.rollup_filter = JSON.parse(newField.value.rollup_filter_raw)
|
||||||
|
} catch (e) {
|
||||||
|
payload.rollup_filter = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (selectedObj.value.is_standard) {
|
if (selectedObj.value.is_standard) {
|
||||||
payload.entity_name = selectedObj.value.id
|
payload.entity_name = selectedObj.value.id
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -378,7 +559,19 @@ async function createCustomField() {
|
||||||
|
|
||||||
await crmStore.createCustomField(payload)
|
await crmStore.createCustomField(payload)
|
||||||
showFieldModal.value = false
|
showFieldModal.value = false
|
||||||
newField.value = { label: '', name: '', field_type: 'text', is_required: false }
|
formulaSyntaxResult.value = null
|
||||||
|
newField.value = {
|
||||||
|
label: '',
|
||||||
|
name: '',
|
||||||
|
field_type: 'text',
|
||||||
|
is_required: false,
|
||||||
|
formula_expression: '',
|
||||||
|
formula_return_type: 'number',
|
||||||
|
rollup_summary_object: '',
|
||||||
|
rollup_field: '',
|
||||||
|
rollup_type: 'COUNT',
|
||||||
|
rollup_filter_raw: ''
|
||||||
|
}
|
||||||
|
|
||||||
if (selectedObj.value.is_standard) {
|
if (selectedObj.value.is_standard) {
|
||||||
await crmStore.fetchObjectSchema(selectedObj.value.id, true)
|
await crmStore.fetchObjectSchema(selectedObj.value.id, true)
|
||||||
|
|
|
||||||
84
tests/test_rollup_engine.py
Normal file
84
tests/test_rollup_engine.py
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
import pytest
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from crm_core.models import Account, Opportunity, Contact, Activity
|
||||||
|
from custom_objects.models import CustomFieldDefinition
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
class TestRollupEngine:
|
||||||
|
def test_account_opportunity_rollups(self):
|
||||||
|
u = User.objects.create_user('rollup_user', 'ru@example.com', 'pass')
|
||||||
|
acc = Account.objects.create(name='Acme Corp', owner=u)
|
||||||
|
|
||||||
|
# Create Roll-Up: Total Opportunity Amount (SUM)
|
||||||
|
CustomFieldDefinition.objects.create(
|
||||||
|
entity_name='Account',
|
||||||
|
name='total_opp_amount__c',
|
||||||
|
label='Total Opp Amount',
|
||||||
|
field_type='rollup',
|
||||||
|
rollup_summary_object='Opportunity',
|
||||||
|
rollup_type='SUM',
|
||||||
|
rollup_field='amount'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create Roll-Up: Opportunity Count (COUNT)
|
||||||
|
CustomFieldDefinition.objects.create(
|
||||||
|
entity_name='Account',
|
||||||
|
name='opp_count__c',
|
||||||
|
label='Opportunity Count',
|
||||||
|
field_type='rollup',
|
||||||
|
rollup_summary_object='Opportunity',
|
||||||
|
rollup_type='COUNT'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create child opportunities
|
||||||
|
opp1 = Opportunity.objects.create(name='Opp 1', account=acc, amount=10000, stage='Prospecting', close_date='2026-12-31', owner=u)
|
||||||
|
acc.refresh_from_db()
|
||||||
|
|
||||||
|
assert acc.custom_fields.get('opp_count__c') == 1
|
||||||
|
assert float(acc.custom_fields.get('total_opp_amount__c')) == 10000.0
|
||||||
|
|
||||||
|
# Add second opportunity
|
||||||
|
opp2 = Opportunity.objects.create(name='Opp 2', account=acc, amount=25000, stage='Qualification', close_date='2026-12-31', owner=u)
|
||||||
|
acc.refresh_from_db()
|
||||||
|
|
||||||
|
assert acc.custom_fields.get('opp_count__c') == 2
|
||||||
|
assert float(acc.custom_fields.get('total_opp_amount__c')) == 35000.0
|
||||||
|
|
||||||
|
# Delete an opportunity
|
||||||
|
opp1.delete()
|
||||||
|
acc.refresh_from_db()
|
||||||
|
|
||||||
|
assert acc.custom_fields.get('opp_count__c') == 1
|
||||||
|
assert float(acc.custom_fields.get('total_opp_amount__c')) == 25000.0
|
||||||
|
|
||||||
|
def test_contact_opportunity_rollups(self):
|
||||||
|
u = User.objects.create_user('rollup_user2', 'ru2@example.com', 'pass')
|
||||||
|
acc = Account.objects.create(name='Acme Corp 2', owner=u)
|
||||||
|
contact = Contact.objects.create(first_name='John', last_name='Doe', email='john@example.com', account=acc, owner=u)
|
||||||
|
|
||||||
|
# Create Roll-Up: Total Opportunity Amount on Account with stage filter
|
||||||
|
CustomFieldDefinition.objects.create(
|
||||||
|
entity_name='Account',
|
||||||
|
name='won_opp_amount__c',
|
||||||
|
label='Won Opp Amount',
|
||||||
|
field_type='rollup',
|
||||||
|
rollup_summary_object='Opportunity',
|
||||||
|
rollup_type='SUM',
|
||||||
|
rollup_field='amount',
|
||||||
|
rollup_filter={'stage': 'Closed Won'}
|
||||||
|
)
|
||||||
|
|
||||||
|
opp1 = Opportunity.objects.create(name='Opp Won', account=acc, amount=5000, stage='Closed Won', close_date='2026-12-31', owner=u)
|
||||||
|
opp2 = Opportunity.objects.create(name='Opp Lost', account=acc, amount=3000, stage='Closed Lost', close_date='2026-12-31', owner=u)
|
||||||
|
acc.refresh_from_db()
|
||||||
|
|
||||||
|
assert float(acc.custom_fields.get('won_opp_amount__c')) == 5000.0
|
||||||
|
|
||||||
|
# Update opp2 to Closed Won
|
||||||
|
opp2.stage = 'Closed Won'
|
||||||
|
opp2.save()
|
||||||
|
acc.refresh_from_db()
|
||||||
|
assert float(acc.custom_fields.get('won_opp_amount__c')) == 8000.0
|
||||||
Loading…
Reference in a new issue