realtimex-crm 0.17.1 → 0.19.0

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.17.1",
3
+ "version": "0.19.0",
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",
@@ -73,65 +73,52 @@ async function createActivity(apiKey: any, req: Request) {
73
73
  const body = await req.json();
74
74
  const { type, ...activityData } = body;
75
75
 
76
- // Activities can be notes or tasks
77
- if (type === "note" || type === "contact_note") {
78
- const { data, error } = await supabaseAdmin
79
- .from("contactNotes")
80
- .insert({
81
- ...activityData,
82
- sales_id: apiKey.sales_id,
83
- })
84
- .select()
85
- .single();
76
+ // Map note type to table and validate
77
+ // Note: Tasks are now handled by /api-v1-tasks endpoint
78
+ let tableName: string;
79
+ let responseType: string;
86
80
 
87
- if (error) {
88
- return createErrorResponse(400, error.message);
89
- }
90
-
91
- return new Response(JSON.stringify({ data, type: "note" }), {
92
- status: 201,
93
- headers: { "Content-Type": "application/json", ...corsHeaders },
94
- });
95
- } else if (type === "task") {
96
- const { data, error } = await supabaseAdmin
97
- .from("tasks")
98
- .insert({
99
- ...activityData,
100
- sales_id: apiKey.sales_id,
101
- })
102
- .select()
103
- .single();
104
-
105
- if (error) {
106
- return createErrorResponse(400, error.message);
107
- }
108
-
109
- return new Response(JSON.stringify({ data, type: "task" }), {
110
- status: 201,
111
- headers: { "Content-Type": "application/json", ...corsHeaders },
112
- });
113
- } else if (type === "deal_note") {
114
- const { data, error } = await supabaseAdmin
115
- .from("dealNotes")
116
- .insert({
117
- ...activityData,
118
- sales_id: apiKey.sales_id,
119
- })
120
- .select()
121
- .single();
81
+ switch (type) {
82
+ case "note":
83
+ case "contact_note":
84
+ tableName = "contactNotes";
85
+ responseType = "contact_note";
86
+ break;
87
+ case "company_note":
88
+ tableName = "companyNotes";
89
+ responseType = "company_note";
90
+ break;
91
+ case "deal_note":
92
+ tableName = "dealNotes";
93
+ responseType = "deal_note";
94
+ break;
95
+ case "task_note":
96
+ tableName = "taskNotes";
97
+ responseType = "task_note";
98
+ break;
99
+ default:
100
+ return createErrorResponse(
101
+ 400,
102
+ "Invalid note type. Must be 'contact_note', 'company_note', 'deal_note', or 'task_note'. For tasks, use /api-v1-tasks endpoint."
103
+ );
104
+ }
122
105
 
123
- if (error) {
124
- return createErrorResponse(400, error.message);
125
- }
106
+ // Insert note
107
+ const { data, error } = await supabaseAdmin
108
+ .from(tableName)
109
+ .insert({
110
+ ...activityData,
111
+ sales_id: apiKey.sales_id,
112
+ })
113
+ .select()
114
+ .single();
126
115
 
127
- return new Response(JSON.stringify({ data, type: "deal_note" }), {
128
- status: 201,
129
- headers: { "Content-Type": "application/json", ...corsHeaders },
130
- });
131
- } else {
132
- return createErrorResponse(
133
- 400,
134
- "Invalid activity type. Must be 'note', 'contact_note', 'task', or 'deal_note'"
135
- );
116
+ if (error) {
117
+ return createErrorResponse(400, error.message);
136
118
  }
119
+
120
+ return new Response(JSON.stringify({ data, type: responseType }), {
121
+ status: 201,
122
+ headers: { "Content-Type": "application/json", ...corsHeaders },
123
+ });
137
124
  }
@@ -0,0 +1,217 @@
1
+ import "jsr:@supabase/functions-js/edge-runtime.d.ts";
2
+ import { supabaseAdmin } from "../_shared/supabaseAdmin.ts";
3
+ import { corsHeaders, createErrorResponse } from "../_shared/utils.ts";
4
+ import {
5
+ validateApiKey,
6
+ checkRateLimit,
7
+ hasScope,
8
+ logApiRequest,
9
+ } from "../_shared/apiKeyAuth.ts";
10
+
11
+ Deno.serve(async (req: Request) => {
12
+ const startTime = Date.now();
13
+
14
+ // Handle CORS
15
+ if (req.method === "OPTIONS") {
16
+ return new Response(null, { status: 204, headers: corsHeaders });
17
+ }
18
+
19
+ // Validate API key
20
+ const authResult = await validateApiKey(req);
21
+ if ("status" in authResult) {
22
+ return authResult; // Error response
23
+ }
24
+ const { apiKey } = authResult;
25
+
26
+ // Check rate limit
27
+ const rateLimitError = checkRateLimit(apiKey.id);
28
+ if (rateLimitError) {
29
+ await logApiRequest(
30
+ apiKey.id,
31
+ "/v1/tasks",
32
+ req.method,
33
+ 429,
34
+ Date.now() - startTime,
35
+ req
36
+ );
37
+ return rateLimitError;
38
+ }
39
+
40
+ try {
41
+ const url = new URL(req.url);
42
+ const pathParts = url.pathname.split("/").filter(Boolean);
43
+ // pathParts: ["api-v1-tasks", "{id}"]
44
+ const taskId = pathParts[1];
45
+
46
+ let response: Response;
47
+
48
+ if (req.method === "GET") {
49
+ if (taskId) {
50
+ response = await getTask(apiKey, taskId);
51
+ } else {
52
+ response = await listTasks(apiKey, req);
53
+ }
54
+ } else if (req.method === "POST") {
55
+ response = await createTask(apiKey, req);
56
+ } else if (req.method === "PATCH" && taskId) {
57
+ response = await updateTask(apiKey, taskId, req);
58
+ } else if (req.method === "DELETE" && taskId) {
59
+ response = await deleteTask(apiKey, taskId);
60
+ } else {
61
+ response = createErrorResponse(404, "Not found");
62
+ }
63
+
64
+ const responseTime = Date.now() - startTime;
65
+ await logApiRequest(
66
+ apiKey.id,
67
+ url.pathname,
68
+ req.method,
69
+ response.status,
70
+ responseTime,
71
+ req
72
+ );
73
+
74
+ return response;
75
+ } catch (error) {
76
+ const responseTime = Date.now() - startTime;
77
+ await logApiRequest(
78
+ apiKey.id,
79
+ new URL(req.url).pathname,
80
+ req.method,
81
+ 500,
82
+ responseTime,
83
+ req,
84
+ error.message
85
+ );
86
+ return createErrorResponse(500, "Internal server error");
87
+ }
88
+ });
89
+
90
+ async function listTasks(apiKey: any, req: Request) {
91
+ if (!hasScope(apiKey, "tasks:read")) {
92
+ return createErrorResponse(403, "Insufficient permissions");
93
+ }
94
+
95
+ const url = new URL(req.url);
96
+ const contactId = url.searchParams.get("contact_id");
97
+ const companyId = url.searchParams.get("company_id");
98
+ const dealId = url.searchParams.get("deal_id");
99
+ const status = url.searchParams.get("status");
100
+
101
+ let query = supabaseAdmin.from("tasks").select("*");
102
+
103
+ if (contactId) {
104
+ query = query.eq("contact_id", parseInt(contactId, 10));
105
+ }
106
+ if (companyId) {
107
+ query = query.eq("company_id", parseInt(companyId, 10));
108
+ }
109
+ if (dealId) {
110
+ query = query.eq("deal_id", parseInt(dealId, 10));
111
+ }
112
+ if (status) {
113
+ query = query.eq("status", status);
114
+ }
115
+
116
+ // If no filters, limit to 50 for safety
117
+ if (!contactId && !companyId && !dealId && !status) {
118
+ query = query.limit(50);
119
+ }
120
+
121
+ const { data, error } = await query;
122
+
123
+ if (error) {
124
+ return createErrorResponse(500, error.message);
125
+ }
126
+
127
+ return new Response(JSON.stringify({ data }), {
128
+ headers: { "Content-Type": "application/json", ...corsHeaders },
129
+ });
130
+ }
131
+
132
+ async function getTask(apiKey: any, taskId: string) {
133
+ if (!hasScope(apiKey, "tasks:read")) {
134
+ return createErrorResponse(403, "Insufficient permissions");
135
+ }
136
+
137
+ const id = parseInt(taskId, 10);
138
+ const { data, error } = await supabaseAdmin
139
+ .from("tasks")
140
+ .select("*")
141
+ .eq("id", id)
142
+ .single();
143
+
144
+ if (error || !data) {
145
+ return createErrorResponse(404, "Task not found");
146
+ }
147
+
148
+ return new Response(JSON.stringify({ data }), {
149
+ headers: { "Content-Type": "application/json", ...corsHeaders },
150
+ });
151
+ }
152
+
153
+ async function createTask(apiKey: any, req: Request) {
154
+ if (!hasScope(apiKey, "tasks:write")) {
155
+ return createErrorResponse(403, "Insufficient permissions");
156
+ }
157
+
158
+ const body = await req.json();
159
+
160
+ const { data, error } = await supabaseAdmin
161
+ .from("tasks")
162
+ .insert({
163
+ ...body,
164
+ sales_id: apiKey.sales_id, // Associate with API key owner
165
+ })
166
+ .select()
167
+ .single();
168
+
169
+ if (error) {
170
+ return createErrorResponse(400, error.message);
171
+ }
172
+
173
+ return new Response(JSON.stringify({ data }), {
174
+ status: 201,
175
+ headers: { "Content-Type": "application/json", ...corsHeaders },
176
+ });
177
+ }
178
+
179
+ async function updateTask(apiKey: any, taskId: string, req: Request) {
180
+ if (!hasScope(apiKey, "tasks:write")) {
181
+ return createErrorResponse(403, "Insufficient permissions");
182
+ }
183
+
184
+ const body = await req.json();
185
+
186
+ const { data, error } = await supabaseAdmin
187
+ .from("tasks")
188
+ .update(body)
189
+ .eq("id", parseInt(taskId, 10))
190
+ .select()
191
+ .single();
192
+
193
+ if (error || !data) {
194
+ return createErrorResponse(404, "Task not found");
195
+ }
196
+
197
+ return new Response(JSON.stringify({ data }), {
198
+ headers: { "Content-Type": "application/json", ...corsHeaders },
199
+ });
200
+ }
201
+
202
+ async function deleteTask(apiKey: any, taskId: string) {
203
+ if (!hasScope(apiKey, "tasks:write")) {
204
+ return createErrorResponse(403, "Insufficient permissions");
205
+ }
206
+
207
+ const { error } = await supabaseAdmin
208
+ .from("tasks")
209
+ .delete()
210
+ .eq("id", parseInt(taskId, 10));
211
+
212
+ if (error) {
213
+ return createErrorResponse(404, "Task not found");
214
+ }
215
+
216
+ return new Response(null, { status: 204, headers: corsHeaders });
217
+ }