realtimex-crm 0.13.7 → 0.13.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "realtimex-crm",
3
- "version": "0.13.7",
3
+ "version": "0.13.8",
4
4
  "description": "RealTimeX CRM - A full-featured CRM built with React, shadcn-admin-kit, and Supabase. Fork of Atomic CRM with RealTimeX App SDK integration.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -0,0 +1,62 @@
1
+ -- Fix ambiguous column reference in compute_company_internal_heartbeat by renaming parameter
2
+
3
+ DROP FUNCTION IF EXISTS compute_company_internal_heartbeat(bigint);
4
+
5
+ CREATE OR REPLACE FUNCTION compute_company_internal_heartbeat(p_company_id bigint)
6
+ RETURNS void AS $$
7
+ DECLARE
8
+ score integer := 0;
9
+ status text;
10
+ days_since_note integer;
11
+ days_since_deal integer;
12
+ days_since_task integer;
13
+ BEGIN
14
+ -- Get recency metrics
15
+ SELECT
16
+ EXTRACT(EPOCH FROM (now() - MAX(cn.date)))/86400,
17
+ EXTRACT(EPOCH FROM (now() - MAX(d.updated_at)))/86400,
18
+ EXTRACT(EPOCH FROM (now() - MAX(t.due_date)))/86400
19
+ INTO days_since_note, days_since_deal, days_since_task
20
+ FROM companies c
21
+ LEFT JOIN "companyNotes" cn ON c.id = cn.company_id
22
+ LEFT JOIN deals d ON c.id = d.company_id
23
+ LEFT JOIN contacts co ON c.id = co.company_id
24
+ LEFT JOIN tasks t ON co.id = t.contact_id
25
+ WHERE c.id = p_company_id;
26
+
27
+ -- Scoring algorithm (simple recency-based, 0-100 scale)
28
+ score := 100;
29
+
30
+ -- Deduct points based on days since last activity
31
+ IF days_since_note IS NOT NULL THEN
32
+ score := score - LEAST(days_since_note::integer, 50);
33
+ END IF;
34
+
35
+ IF days_since_deal IS NOT NULL THEN
36
+ score := score - LEAST(days_since_deal::integer, 30);
37
+ END IF;
38
+
39
+ IF days_since_task IS NOT NULL THEN
40
+ score := score - LEAST(days_since_task::integer, 20);
41
+ END IF;
42
+
43
+ -- Ensure score stays within bounds
44
+ score := GREATEST(0, LEAST(100, score));
45
+
46
+ -- Map score to status
47
+ status := CASE
48
+ WHEN score >= 76 THEN 'engaged'
49
+ WHEN score >= 51 THEN 'quiet'
50
+ WHEN score >= 26 THEN 'at_risk'
51
+ ELSE 'unresponsive'
52
+ END;
53
+
54
+ -- Update company heartbeat fields
55
+ UPDATE companies
56
+ SET
57
+ internal_heartbeat_score = score,
58
+ internal_heartbeat_status = status,
59
+ internal_heartbeat_updated_at = now()
60
+ WHERE id = p_company_id;
61
+ END;
62
+ $$ LANGUAGE plpgsql;