realtimex-crm 0.18.0 → 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.18.0",
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,7 +73,8 @@ async function createActivity(apiKey: any, req: Request) {
73
73
  const body = await req.json();
74
74
  const { type, ...activityData } = body;
75
75
 
76
- // Map activity type to table and validate
76
+ // Map note type to table and validate
77
+ // Note: Tasks are now handled by /api-v1-tasks endpoint
77
78
  let tableName: string;
78
79
  let responseType: string;
79
80
 
@@ -95,18 +96,14 @@ async function createActivity(apiKey: any, req: Request) {
95
96
  tableName = "taskNotes";
96
97
  responseType = "task_note";
97
98
  break;
98
- case "task":
99
- tableName = "tasks";
100
- responseType = "task";
101
- break;
102
99
  default:
103
100
  return createErrorResponse(
104
101
  400,
105
- "Invalid activity type. Must be 'contact_note', 'company_note', 'deal_note', 'task_note', or 'task'"
102
+ "Invalid note type. Must be 'contact_note', 'company_note', 'deal_note', or 'task_note'. For tasks, use /api-v1-tasks endpoint."
106
103
  );
107
104
  }
108
105
 
109
- // Insert activity
106
+ // Insert note
110
107
  const { data, error } = await supabaseAdmin
111
108
  .from(tableName)
112
109
  .insert({
@@ -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
+ }