@contractspec/example.crm-pipeline 3.7.17 → 3.7.18

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.
Files changed (135) hide show
  1. package/.turbo/turbo-build.log +135 -135
  2. package/CHANGELOG.md +20 -0
  3. package/dist/browser/crm-pipeline.feature.js +1 -82
  4. package/dist/browser/deal/deal.enum.js +1 -18
  5. package/dist/browser/deal/deal.operation.js +1 -396
  6. package/dist/browser/deal/deal.schema.js +1 -141
  7. package/dist/browser/deal/deal.test-spec.js +1 -58
  8. package/dist/browser/deal/index.js +1 -408
  9. package/dist/browser/docs/crm-pipeline.docblock.js +5 -49
  10. package/dist/browser/docs/index.js +5 -49
  11. package/dist/browser/entities/company.entity.js +1 -52
  12. package/dist/browser/entities/contact.entity.js +1 -66
  13. package/dist/browser/entities/deal.entity.js +1 -107
  14. package/dist/browser/entities/index.js +1 -343
  15. package/dist/browser/entities/task.entity.js +1 -99
  16. package/dist/browser/events/contact.event.js +1 -31
  17. package/dist/browser/events/deal.event.js +1 -101
  18. package/dist/browser/events/index.js +1 -158
  19. package/dist/browser/events/task.event.js +1 -28
  20. package/dist/browser/example.js +1 -39
  21. package/dist/browser/handlers/crm.handlers.js +2 -171
  22. package/dist/browser/handlers/deal.handlers.js +1 -293
  23. package/dist/browser/handlers/index.js +2 -467
  24. package/dist/browser/handlers/mock-data.js +1 -165
  25. package/dist/browser/index.js +8 -3461
  26. package/dist/browser/operations/index.js +1 -407
  27. package/dist/browser/presentations/dashboard.presentation.js +1 -55
  28. package/dist/browser/presentations/index.js +1 -290
  29. package/dist/browser/presentations/pipeline.presentation.js +1 -236
  30. package/dist/browser/seeders/index.js +1 -22
  31. package/dist/browser/ui/CrmDashboard.js +1 -1547
  32. package/dist/browser/ui/CrmDealCard.js +1 -50
  33. package/dist/browser/ui/CrmPipelineBoard.js +1 -160
  34. package/dist/browser/ui/hooks/index.js +1 -197
  35. package/dist/browser/ui/hooks/useDealList.js +1 -95
  36. package/dist/browser/ui/hooks/useDealMutations.js +1 -100
  37. package/dist/browser/ui/index.js +4 -2205
  38. package/dist/browser/ui/modals/CreateDealModal.js +1 -211
  39. package/dist/browser/ui/modals/DealActionsModal.js +1 -428
  40. package/dist/browser/ui/modals/index.js +1 -638
  41. package/dist/browser/ui/overlays/demo-overlays.js +1 -55
  42. package/dist/browser/ui/overlays/index.js +1 -55
  43. package/dist/browser/ui/renderers/index.js +4 -849
  44. package/dist/browser/ui/renderers/pipeline.markdown.js +4 -575
  45. package/dist/browser/ui/renderers/pipeline.renderer.js +1 -275
  46. package/dist/browser/ui/tables/DealListTab.js +1 -390
  47. package/dist/crm-pipeline.feature.js +1 -82
  48. package/dist/deal/deal.enum.js +1 -18
  49. package/dist/deal/deal.operation.js +1 -396
  50. package/dist/deal/deal.schema.js +1 -141
  51. package/dist/deal/deal.test-spec.js +1 -58
  52. package/dist/deal/index.js +1 -408
  53. package/dist/docs/crm-pipeline.docblock.js +5 -49
  54. package/dist/docs/index.js +5 -49
  55. package/dist/entities/company.entity.js +1 -52
  56. package/dist/entities/contact.entity.js +1 -66
  57. package/dist/entities/deal.entity.js +1 -107
  58. package/dist/entities/index.js +1 -343
  59. package/dist/entities/task.entity.js +1 -99
  60. package/dist/events/contact.event.js +1 -31
  61. package/dist/events/deal.event.js +1 -101
  62. package/dist/events/index.js +1 -158
  63. package/dist/events/task.event.js +1 -28
  64. package/dist/example.js +1 -39
  65. package/dist/handlers/crm.handlers.js +2 -171
  66. package/dist/handlers/deal.handlers.js +1 -293
  67. package/dist/handlers/index.js +2 -467
  68. package/dist/handlers/mock-data.js +1 -165
  69. package/dist/index.js +8 -3461
  70. package/dist/node/crm-pipeline.feature.js +1 -82
  71. package/dist/node/deal/deal.enum.js +1 -18
  72. package/dist/node/deal/deal.operation.js +1 -396
  73. package/dist/node/deal/deal.schema.js +1 -141
  74. package/dist/node/deal/deal.test-spec.js +1 -58
  75. package/dist/node/deal/index.js +1 -408
  76. package/dist/node/docs/crm-pipeline.docblock.js +5 -49
  77. package/dist/node/docs/index.js +5 -49
  78. package/dist/node/entities/company.entity.js +1 -52
  79. package/dist/node/entities/contact.entity.js +1 -66
  80. package/dist/node/entities/deal.entity.js +1 -107
  81. package/dist/node/entities/index.js +1 -343
  82. package/dist/node/entities/task.entity.js +1 -99
  83. package/dist/node/events/contact.event.js +1 -31
  84. package/dist/node/events/deal.event.js +1 -101
  85. package/dist/node/events/index.js +1 -158
  86. package/dist/node/events/task.event.js +1 -28
  87. package/dist/node/example.js +1 -39
  88. package/dist/node/handlers/crm.handlers.js +2 -171
  89. package/dist/node/handlers/deal.handlers.js +1 -293
  90. package/dist/node/handlers/index.js +2 -467
  91. package/dist/node/handlers/mock-data.js +1 -165
  92. package/dist/node/index.js +8 -3461
  93. package/dist/node/operations/index.js +1 -407
  94. package/dist/node/presentations/dashboard.presentation.js +1 -55
  95. package/dist/node/presentations/index.js +1 -290
  96. package/dist/node/presentations/pipeline.presentation.js +1 -236
  97. package/dist/node/seeders/index.js +1 -22
  98. package/dist/node/ui/CrmDashboard.js +1 -1547
  99. package/dist/node/ui/CrmDealCard.js +1 -50
  100. package/dist/node/ui/CrmPipelineBoard.js +1 -160
  101. package/dist/node/ui/hooks/index.js +1 -197
  102. package/dist/node/ui/hooks/useDealList.js +1 -95
  103. package/dist/node/ui/hooks/useDealMutations.js +1 -100
  104. package/dist/node/ui/index.js +4 -2205
  105. package/dist/node/ui/modals/CreateDealModal.js +1 -211
  106. package/dist/node/ui/modals/DealActionsModal.js +1 -428
  107. package/dist/node/ui/modals/index.js +1 -638
  108. package/dist/node/ui/overlays/demo-overlays.js +1 -55
  109. package/dist/node/ui/overlays/index.js +1 -55
  110. package/dist/node/ui/renderers/index.js +4 -849
  111. package/dist/node/ui/renderers/pipeline.markdown.js +4 -575
  112. package/dist/node/ui/renderers/pipeline.renderer.js +1 -275
  113. package/dist/node/ui/tables/DealListTab.js +1 -390
  114. package/dist/operations/index.js +1 -407
  115. package/dist/presentations/dashboard.presentation.js +1 -55
  116. package/dist/presentations/index.js +1 -290
  117. package/dist/presentations/pipeline.presentation.js +1 -236
  118. package/dist/seeders/index.js +1 -22
  119. package/dist/ui/CrmDashboard.js +1 -1547
  120. package/dist/ui/CrmDealCard.js +1 -50
  121. package/dist/ui/CrmPipelineBoard.js +1 -160
  122. package/dist/ui/hooks/index.js +1 -197
  123. package/dist/ui/hooks/useDealList.js +1 -95
  124. package/dist/ui/hooks/useDealMutations.js +1 -100
  125. package/dist/ui/index.js +4 -2205
  126. package/dist/ui/modals/CreateDealModal.js +1 -211
  127. package/dist/ui/modals/DealActionsModal.js +1 -428
  128. package/dist/ui/modals/index.js +1 -638
  129. package/dist/ui/overlays/demo-overlays.js +1 -55
  130. package/dist/ui/overlays/index.js +1 -55
  131. package/dist/ui/renderers/index.js +4 -849
  132. package/dist/ui/renderers/pipeline.markdown.js +4 -575
  133. package/dist/ui/renderers/pipeline.renderer.js +1 -275
  134. package/dist/ui/tables/DealListTab.js +1 -390
  135. package/package.json +13 -13
@@ -1,575 +1,4 @@
1
- // src/handlers/crm.handlers.ts
2
- import { web } from "@contractspec/lib.runtime-sandbox";
3
- var { generateId } = web;
4
- function rowToDeal(row) {
5
- return {
6
- id: row.id,
7
- projectId: row.projectId,
8
- name: row.name,
9
- value: row.value,
10
- currency: row.currency,
11
- pipelineId: row.pipelineId,
12
- stageId: row.stageId,
13
- status: row.status,
14
- contactId: row.contactId ?? undefined,
15
- companyId: row.companyId ?? undefined,
16
- ownerId: row.ownerId,
17
- expectedCloseDate: row.expectedCloseDate ? new Date(row.expectedCloseDate) : undefined,
18
- wonSource: row.wonSource ?? undefined,
19
- lostReason: row.lostReason ?? undefined,
20
- notes: row.notes ?? undefined,
21
- createdAt: new Date(row.createdAt),
22
- updatedAt: new Date(row.updatedAt)
23
- };
24
- }
25
- var DEAL_SORT_COLUMNS = {
26
- name: "name",
27
- value: "value",
28
- status: "status",
29
- expectedCloseDate: "expectedCloseDate",
30
- updatedAt: "updatedAt"
31
- };
32
- function createCrmHandlers(db) {
33
- async function listDeals(input) {
34
- const {
35
- projectId,
36
- pipelineId,
37
- stageId,
38
- status,
39
- ownerId,
40
- search,
41
- limit = 20,
42
- offset = 0,
43
- sortBy = "value",
44
- sortDirection = "desc"
45
- } = input;
46
- let whereClause = "WHERE projectId = ?";
47
- const params = [projectId];
48
- if (pipelineId) {
49
- whereClause += " AND pipelineId = ?";
50
- params.push(pipelineId);
51
- }
52
- if (stageId) {
53
- whereClause += " AND stageId = ?";
54
- params.push(stageId);
55
- }
56
- if (status && status !== "all") {
57
- whereClause += " AND status = ?";
58
- params.push(status);
59
- }
60
- if (ownerId) {
61
- whereClause += " AND ownerId = ?";
62
- params.push(ownerId);
63
- }
64
- if (search) {
65
- whereClause += " AND name LIKE ?";
66
- params.push(`%${search}%`);
67
- }
68
- const countResult = (await db.query(`SELECT COUNT(*) as count FROM crm_deal ${whereClause}`, params)).rows;
69
- const total = countResult[0]?.count ?? 0;
70
- const valueResult = (await db.query(`SELECT COALESCE(SUM(value), 0) as total FROM crm_deal ${whereClause}`, params)).rows;
71
- const totalValue = valueResult[0]?.total ?? 0;
72
- const orderByColumn = DEAL_SORT_COLUMNS[sortBy] ?? DEAL_SORT_COLUMNS.value;
73
- const orderByDirection = sortDirection === "asc" ? "ASC" : "DESC";
74
- const dealRows = (await db.query(`SELECT * FROM crm_deal ${whereClause} ORDER BY ${orderByColumn} ${orderByDirection} LIMIT ? OFFSET ?`, [...params, limit, offset])).rows;
75
- return {
76
- deals: dealRows.map(rowToDeal),
77
- total,
78
- totalValue
79
- };
80
- }
81
- async function createDeal(input, context) {
82
- const id = generateId("deal");
83
- const now = new Date().toISOString();
84
- await db.execute(`INSERT INTO crm_deal (id, projectId, pipelineId, stageId, name, value, currency, status, contactId, companyId, ownerId, expectedCloseDate, createdAt, updatedAt)
85
- VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
86
- id,
87
- context.projectId,
88
- input.pipelineId,
89
- input.stageId,
90
- input.name,
91
- input.value,
92
- input.currency ?? "USD",
93
- "OPEN",
94
- input.contactId ?? null,
95
- input.companyId ?? null,
96
- context.ownerId,
97
- input.expectedCloseDate?.toISOString() ?? null,
98
- now,
99
- now
100
- ]);
101
- const rows = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [id])).rows;
102
- if (!rows[0]) {
103
- throw new Error("Failed to create deal");
104
- }
105
- return rowToDeal(rows[0]);
106
- }
107
- async function moveDeal(input) {
108
- const now = new Date().toISOString();
109
- const existing = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [input.dealId])).rows;
110
- if (!existing[0]) {
111
- throw new Error("NOT_FOUND");
112
- }
113
- const stage = (await db.query(`SELECT * FROM crm_stage WHERE id = ?`, [input.stageId])).rows;
114
- if (!stage[0]) {
115
- throw new Error("INVALID_STAGE");
116
- }
117
- await db.execute(`UPDATE crm_deal SET stageId = ?, updatedAt = ? WHERE id = ?`, [input.stageId, now, input.dealId]);
118
- const rows = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [input.dealId])).rows;
119
- return rowToDeal(rows[0]);
120
- }
121
- async function winDeal(input) {
122
- const now = new Date().toISOString();
123
- const existing = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [input.dealId])).rows;
124
- if (!existing[0]) {
125
- throw new Error("NOT_FOUND");
126
- }
127
- await db.execute(`UPDATE crm_deal SET status = 'WON', wonSource = ?, notes = ?, updatedAt = ? WHERE id = ?`, [input.wonSource ?? null, input.notes ?? null, now, input.dealId]);
128
- const rows = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [input.dealId])).rows;
129
- return rowToDeal(rows[0]);
130
- }
131
- async function loseDeal(input) {
132
- const now = new Date().toISOString();
133
- const existing = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [input.dealId])).rows;
134
- if (!existing[0]) {
135
- throw new Error("NOT_FOUND");
136
- }
137
- await db.execute(`UPDATE crm_deal SET status = 'LOST', lostReason = ?, notes = ?, updatedAt = ? WHERE id = ?`, [input.lostReason, input.notes ?? null, now, input.dealId]);
138
- const rows = (await db.query(`SELECT * FROM crm_deal WHERE id = ?`, [input.dealId])).rows;
139
- return rowToDeal(rows[0]);
140
- }
141
- async function getDealsByStage(input) {
142
- const deals = (await db.query(`SELECT * FROM crm_deal WHERE projectId = ? AND pipelineId = ? AND status = 'OPEN' ORDER BY value DESC`, [input.projectId, input.pipelineId])).rows;
143
- const stages = (await db.query(`SELECT * FROM crm_stage WHERE pipelineId = ? ORDER BY position`, [input.pipelineId])).rows;
144
- const grouped = {};
145
- for (const stage of stages) {
146
- grouped[stage.id] = deals.filter((d) => d.stageId === stage.id).map(rowToDeal);
147
- }
148
- return grouped;
149
- }
150
- async function getPipelineStages(input) {
151
- const rows = (await db.query(`SELECT * FROM crm_stage WHERE pipelineId = ? ORDER BY position`, [input.pipelineId])).rows;
152
- return rows.map((row) => ({
153
- id: row.id,
154
- pipelineId: row.pipelineId,
155
- name: row.name,
156
- position: row.position
157
- }));
158
- }
159
- return {
160
- listDeals,
161
- createDeal,
162
- moveDeal,
163
- winDeal,
164
- loseDeal,
165
- getDealsByStage,
166
- getPipelineStages
167
- };
168
- }
169
-
170
- // src/handlers/mock-data.ts
171
- var MOCK_STAGES = [
172
- { id: "stage-1", name: "Lead", position: 1, pipelineId: "pipeline-1" },
173
- { id: "stage-2", name: "Qualified", position: 2, pipelineId: "pipeline-1" },
174
- { id: "stage-3", name: "Proposal", position: 3, pipelineId: "pipeline-1" },
175
- { id: "stage-4", name: "Negotiation", position: 4, pipelineId: "pipeline-1" },
176
- { id: "stage-5", name: "Closed", position: 5, pipelineId: "pipeline-1" }
177
- ];
178
- var MOCK_DEALS = [
179
- {
180
- id: "deal-1",
181
- name: "Enterprise License - Acme Corp",
182
- value: 75000,
183
- currency: "USD",
184
- pipelineId: "pipeline-1",
185
- stageId: "stage-3",
186
- status: "OPEN",
187
- contactId: "contact-1",
188
- companyId: "company-1",
189
- ownerId: "user-1",
190
- expectedCloseDate: new Date("2024-05-15T00:00:00Z"),
191
- createdAt: new Date("2024-02-01T10:00:00Z"),
192
- updatedAt: new Date("2024-04-10T14:30:00Z")
193
- },
194
- {
195
- id: "deal-2",
196
- name: "Startup Plan - TechStart Inc",
197
- value: 12000,
198
- currency: "USD",
199
- pipelineId: "pipeline-1",
200
- stageId: "stage-2",
201
- status: "OPEN",
202
- contactId: "contact-2",
203
- companyId: "company-2",
204
- ownerId: "user-2",
205
- expectedCloseDate: new Date("2024-04-30T00:00:00Z"),
206
- createdAt: new Date("2024-03-15T09:00:00Z"),
207
- updatedAt: new Date("2024-04-08T11:15:00Z")
208
- },
209
- {
210
- id: "deal-3",
211
- name: "Professional Services - Global Ltd",
212
- value: 45000,
213
- currency: "USD",
214
- pipelineId: "pipeline-1",
215
- stageId: "stage-4",
216
- status: "OPEN",
217
- contactId: "contact-3",
218
- companyId: "company-3",
219
- ownerId: "user-1",
220
- expectedCloseDate: new Date("2024-04-20T00:00:00Z"),
221
- createdAt: new Date("2024-01-20T08:00:00Z"),
222
- updatedAt: new Date("2024-04-12T16:45:00Z")
223
- },
224
- {
225
- id: "deal-4",
226
- name: "Annual Contract - SmallBiz Co",
227
- value: 8500,
228
- currency: "USD",
229
- pipelineId: "pipeline-1",
230
- stageId: "stage-1",
231
- status: "OPEN",
232
- contactId: "contact-4",
233
- companyId: "company-4",
234
- ownerId: "user-3",
235
- createdAt: new Date("2024-04-05T12:00:00Z"),
236
- updatedAt: new Date("2024-04-05T12:00:00Z")
237
- },
238
- {
239
- id: "deal-5",
240
- name: "Custom Integration - MegaCorp",
241
- value: 125000,
242
- currency: "USD",
243
- pipelineId: "pipeline-1",
244
- stageId: "stage-5",
245
- status: "WON",
246
- contactId: "contact-5",
247
- companyId: "company-5",
248
- ownerId: "user-1",
249
- expectedCloseDate: new Date("2024-03-31T00:00:00Z"),
250
- createdAt: new Date("2023-11-10T10:00:00Z"),
251
- updatedAt: new Date("2024-03-28T09:00:00Z")
252
- },
253
- {
254
- id: "deal-6",
255
- name: "Pilot Project - NewCo",
256
- value: 5000,
257
- currency: "USD",
258
- pipelineId: "pipeline-1",
259
- stageId: "stage-2",
260
- status: "LOST",
261
- contactId: "contact-6",
262
- companyId: "company-6",
263
- ownerId: "user-2",
264
- createdAt: new Date("2024-01-15T14:00:00Z"),
265
- updatedAt: new Date("2024-02-28T10:30:00Z")
266
- }
267
- ];
268
- var MOCK_COMPANIES = [
269
- {
270
- id: "company-1",
271
- name: "Acme Corporation",
272
- domain: "acme.com",
273
- industry: "Technology",
274
- size: "1000-5000",
275
- website: "https://acme.com",
276
- createdAt: new Date("2024-01-01T00:00:00Z")
277
- },
278
- {
279
- id: "company-2",
280
- name: "TechStart Inc",
281
- domain: "techstart.io",
282
- industry: "Software",
283
- size: "10-50",
284
- website: "https://techstart.io",
285
- createdAt: new Date("2024-02-15T00:00:00Z")
286
- },
287
- {
288
- id: "company-3",
289
- name: "Global Ltd",
290
- domain: "global.com",
291
- industry: "Consulting",
292
- size: "500-1000",
293
- website: "https://global.com",
294
- createdAt: new Date("2023-12-01T00:00:00Z")
295
- }
296
- ];
297
- var MOCK_CONTACTS = [
298
- {
299
- id: "contact-1",
300
- firstName: "John",
301
- lastName: "Smith",
302
- email: "john.smith@acme.com",
303
- phone: "+1-555-0101",
304
- title: "VP of Engineering",
305
- companyId: "company-1",
306
- createdAt: new Date("2024-01-05T00:00:00Z")
307
- },
308
- {
309
- id: "contact-2",
310
- firstName: "Sarah",
311
- lastName: "Johnson",
312
- email: "sarah@techstart.io",
313
- phone: "+1-555-0102",
314
- title: "CEO",
315
- companyId: "company-2",
316
- createdAt: new Date("2024-02-20T00:00:00Z")
317
- },
318
- {
319
- id: "contact-3",
320
- firstName: "Michael",
321
- lastName: "Brown",
322
- email: "michael.brown@global.com",
323
- phone: "+1-555-0103",
324
- title: "CTO",
325
- companyId: "company-3",
326
- createdAt: new Date("2023-12-10T00:00:00Z")
327
- }
328
- ];
329
-
330
- // src/handlers/deal.handlers.ts
331
- async function mockListDealsHandler(input) {
332
- const {
333
- pipelineId,
334
- stageId,
335
- status,
336
- ownerId,
337
- search,
338
- limit = 20,
339
- offset = 0
340
- } = input;
341
- let filtered = [...MOCK_DEALS];
342
- if (pipelineId) {
343
- filtered = filtered.filter((d) => d.pipelineId === pipelineId);
344
- }
345
- if (stageId) {
346
- filtered = filtered.filter((d) => d.stageId === stageId);
347
- }
348
- if (status && status !== "all") {
349
- filtered = filtered.filter((d) => d.status === status);
350
- }
351
- if (ownerId) {
352
- filtered = filtered.filter((d) => d.ownerId === ownerId);
353
- }
354
- if (search) {
355
- const q = search.toLowerCase();
356
- filtered = filtered.filter((d) => d.name.toLowerCase().includes(q));
357
- }
358
- filtered.sort((a, b) => b.value - a.value);
359
- const total = filtered.length;
360
- const totalValue = filtered.reduce((sum, d) => sum + d.value, 0);
361
- const deals = filtered.slice(offset, offset + limit);
362
- return {
363
- deals,
364
- total,
365
- totalValue
366
- };
367
- }
368
- async function mockCreateDealHandler(input, context) {
369
- const now = new Date;
370
- const deal = {
371
- id: `deal-${Date.now()}`,
372
- name: input.name,
373
- value: input.value,
374
- currency: input.currency ?? "USD",
375
- pipelineId: input.pipelineId,
376
- stageId: input.stageId,
377
- status: "OPEN",
378
- contactId: input.contactId,
379
- companyId: input.companyId,
380
- ownerId: context.ownerId,
381
- expectedCloseDate: input.expectedCloseDate,
382
- createdAt: now,
383
- updatedAt: now
384
- };
385
- MOCK_DEALS.push(deal);
386
- return deal;
387
- }
388
- async function mockMoveDealHandler(input) {
389
- const dealIndex = MOCK_DEALS.findIndex((d) => d.id === input.dealId);
390
- if (dealIndex === -1) {
391
- throw new Error("NOT_FOUND");
392
- }
393
- const deal = MOCK_DEALS[dealIndex];
394
- if (!deal) {
395
- throw new Error("NOT_FOUND");
396
- }
397
- const stage = MOCK_STAGES.find((s) => s.id === input.stageId);
398
- if (!stage) {
399
- throw new Error("INVALID_STAGE");
400
- }
401
- const updatedDeal = {
402
- ...deal,
403
- stageId: input.stageId,
404
- updatedAt: new Date
405
- };
406
- MOCK_DEALS[dealIndex] = updatedDeal;
407
- return updatedDeal;
408
- }
409
- async function mockWinDealHandler(input) {
410
- const dealIndex = MOCK_DEALS.findIndex((d) => d.id === input.dealId);
411
- if (dealIndex === -1) {
412
- throw new Error("NOT_FOUND");
413
- }
414
- const deal = MOCK_DEALS[dealIndex];
415
- if (!deal) {
416
- throw new Error("NOT_FOUND");
417
- }
418
- const updatedDeal = {
419
- ...deal,
420
- status: "WON",
421
- updatedAt: new Date
422
- };
423
- MOCK_DEALS[dealIndex] = updatedDeal;
424
- return updatedDeal;
425
- }
426
- async function mockLoseDealHandler(input) {
427
- const dealIndex = MOCK_DEALS.findIndex((d) => d.id === input.dealId);
428
- if (dealIndex === -1) {
429
- throw new Error("NOT_FOUND");
430
- }
431
- const deal = MOCK_DEALS[dealIndex];
432
- if (!deal) {
433
- throw new Error("NOT_FOUND");
434
- }
435
- const updatedDeal = {
436
- ...deal,
437
- status: "LOST",
438
- updatedAt: new Date
439
- };
440
- MOCK_DEALS[dealIndex] = updatedDeal;
441
- return updatedDeal;
442
- }
443
- async function mockGetDealsByStageHandler(input) {
444
- const deals = MOCK_DEALS.filter((d) => d.pipelineId === input.pipelineId && d.status === "OPEN");
445
- const grouped = {};
446
- for (const stage of MOCK_STAGES) {
447
- grouped[stage.id] = deals.filter((d) => d.stageId === stage.id);
448
- }
449
- return grouped;
450
- }
451
- async function mockGetPipelineStagesHandler(input) {
452
- return MOCK_STAGES.filter((s) => s.pipelineId === input.pipelineId);
453
- }
454
- // src/ui/renderers/pipeline.markdown.ts
455
- function formatCurrency(value, currency = "USD") {
456
- return new Intl.NumberFormat("en-US", {
457
- style: "currency",
458
- currency,
459
- minimumFractionDigits: 0
460
- }).format(value);
461
- }
462
- var crmPipelineMarkdownRenderer = {
463
- target: "markdown",
464
- render: async (desc, _ctx) => {
465
- if (desc.source.type !== "component" || desc.source.componentKey !== "PipelineKanbanView") {
466
- throw new Error("crmPipelineMarkdownRenderer: not PipelineKanbanView");
467
- }
468
- const pipelineId = "pipeline-1";
469
- const [dealsResult, stages] = await Promise.all([
470
- mockListDealsHandler({ pipelineId, limit: 50 }),
471
- mockGetPipelineStagesHandler({ pipelineId })
472
- ]);
473
- const deals = dealsResult.deals;
474
- const stageList = stages;
475
- const dealsByStage = {};
476
- for (const stage of stageList) {
477
- dealsByStage[stage.id] = deals.filter((d) => d.stageId === stage.id && d.status === "OPEN");
478
- }
479
- const lines = [
480
- "# CRM Pipeline",
481
- "",
482
- `**Total Value**: ${formatCurrency(dealsResult.totalValue)}`,
483
- `**Total Deals**: ${dealsResult.total}`,
484
- ""
485
- ];
486
- for (const stage of stageList.sort((a, b) => a.position - b.position)) {
487
- const stageDeals = dealsByStage[stage.id] ?? [];
488
- const stageValue = stageDeals.reduce((sum, d) => sum + d.value, 0);
489
- lines.push(`## ${stage.name}`);
490
- lines.push(`_${stageDeals.length} deals · ${formatCurrency(stageValue)}_`);
491
- lines.push("");
492
- if (stageDeals.length === 0) {
493
- lines.push("_No deals_");
494
- } else {
495
- for (const deal of stageDeals) {
496
- lines.push(`- **${deal.name}** - ${formatCurrency(deal.value, deal.currency)}`);
497
- }
498
- }
499
- lines.push("");
500
- }
501
- return {
502
- mimeType: "text/markdown",
503
- body: lines.join(`
504
- `)
505
- };
506
- }
507
- };
508
- var crmDashboardMarkdownRenderer = {
509
- target: "markdown",
510
- render: async (desc, _ctx) => {
511
- if (desc.source.type !== "component" || desc.source.componentKey !== "CrmDashboard") {
512
- throw new Error("crmDashboardMarkdownRenderer: not CrmDashboard");
513
- }
514
- const pipelineId = "pipeline-1";
515
- const [dealsResult, stages] = await Promise.all([
516
- mockListDealsHandler({ pipelineId, limit: 100 }),
517
- mockGetPipelineStagesHandler({ pipelineId })
518
- ]);
519
- const deals = dealsResult.deals;
520
- const stageList = stages;
521
- const openDeals = deals.filter((d) => d.status === "OPEN");
522
- const wonDeals = deals.filter((d) => d.status === "WON");
523
- const lostDeals = deals.filter((d) => d.status === "LOST");
524
- const openValue = openDeals.reduce((sum, d) => sum + d.value, 0);
525
- const wonValue = wonDeals.reduce((sum, d) => sum + d.value, 0);
526
- const lines = [
527
- "# CRM Dashboard",
528
- "",
529
- "> Sales pipeline overview and key metrics",
530
- "",
531
- "## Summary",
532
- "",
533
- "| Metric | Value |",
534
- "|--------|-------|",
535
- `| Total Deals | ${dealsResult.total} |`,
536
- `| Pipeline Value | ${formatCurrency(dealsResult.totalValue)} |`,
537
- `| Open Deals | ${openDeals.length} (${formatCurrency(openValue)}) |`,
538
- `| Won Deals | ${wonDeals.length} (${formatCurrency(wonValue)}) |`,
539
- `| Lost Deals | ${lostDeals.length} |`,
540
- "",
541
- "## Pipeline Stages",
542
- ""
543
- ];
544
- lines.push("| Stage | Deals | Value |");
545
- lines.push("|-------|-------|-------|");
546
- for (const stage of stageList.sort((a, b) => a.position - b.position)) {
547
- const stageDeals = openDeals.filter((d) => d.stageId === stage.id);
548
- const stageValue = stageDeals.reduce((sum, d) => sum + d.value, 0);
549
- lines.push(`| ${stage.name} | ${stageDeals.length} | ${formatCurrency(stageValue)} |`);
550
- }
551
- lines.push("");
552
- lines.push("## Recent Deals");
553
- lines.push("");
554
- const recentDeals = deals.slice(0, 10);
555
- if (recentDeals.length === 0) {
556
- lines.push("_No deals yet._");
557
- } else {
558
- lines.push("| Deal | Value | Stage | Status |");
559
- lines.push("|------|-------|-------|--------|");
560
- for (const deal of recentDeals) {
561
- const stage = stageList.find((s) => s.id === deal.stageId);
562
- lines.push(`| ${deal.name} | ${formatCurrency(deal.value, deal.currency)} | ${stage?.name ?? "-"} | ${deal.status} |`);
563
- }
564
- }
565
- return {
566
- mimeType: "text/markdown",
567
- body: lines.join(`
568
- `)
569
- };
570
- }
571
- };
572
- export {
573
- crmPipelineMarkdownRenderer,
574
- crmDashboardMarkdownRenderer
575
- };
1
+ import{web as I}from"@contractspec/lib.runtime-sandbox";var{generateId:f}=I;function x(H){return{id:H.id,projectId:H.projectId,name:H.name,value:H.value,currency:H.currency,pipelineId:H.pipelineId,stageId:H.stageId,status:H.status,contactId:H.contactId??void 0,companyId:H.companyId??void 0,ownerId:H.ownerId,expectedCloseDate:H.expectedCloseDate?new Date(H.expectedCloseDate):void 0,wonSource:H.wonSource??void 0,lostReason:H.lostReason??void 0,notes:H.notes??void 0,createdAt:new Date(H.createdAt),updatedAt:new Date(H.updatedAt)}}var A={name:"name",value:"value",status:"status",expectedCloseDate:"expectedCloseDate",updatedAt:"updatedAt"};function p(H){async function F(W){let{projectId:Q,pipelineId:z,stageId:Y,status:k,ownerId:j,search:X,limit:P=20,offset:R=0,sortBy:T="value",sortDirection:L="desc"}=W,G="WHERE projectId = ?",B=[Q];if(z)G+=" AND pipelineId = ?",B.push(z);if(Y)G+=" AND stageId = ?",B.push(Y);if(k&&k!=="all")G+=" AND status = ?",B.push(k);if(j)G+=" AND ownerId = ?",B.push(j);if(X)G+=" AND name LIKE ?",B.push(`%${X}%`);let E=(await H.query(`SELECT COUNT(*) as count FROM crm_deal ${G}`,B)).rows[0]?.count??0,M=(await H.query(`SELECT COALESCE(SUM(value), 0) as total FROM crm_deal ${G}`,B)).rows[0]?.total??0,_=A[T]??A.value,O=L==="asc"?"ASC":"DESC";return{deals:(await H.query(`SELECT * FROM crm_deal ${G} ORDER BY ${_} ${O} LIMIT ? OFFSET ?`,[...B,P,R])).rows.map(x),total:E,totalValue:M}}async function J(W,Q){let z=f("deal"),Y=new Date().toISOString();await H.execute(`INSERT INTO crm_deal (id, projectId, pipelineId, stageId, name, value, currency, status, contactId, companyId, ownerId, expectedCloseDate, createdAt, updatedAt)
2
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[z,Q.projectId,W.pipelineId,W.stageId,W.name,W.value,W.currency??"USD","OPEN",W.contactId??null,W.companyId??null,Q.ownerId,W.expectedCloseDate?.toISOString()??null,Y,Y]);let k=(await H.query("SELECT * FROM crm_deal WHERE id = ?",[z])).rows;if(!k[0])throw Error("Failed to create deal");return x(k[0])}async function v(W){let Q=new Date().toISOString();if(!(await H.query("SELECT * FROM crm_deal WHERE id = ?",[W.dealId])).rows[0])throw Error("NOT_FOUND");if(!(await H.query("SELECT * FROM crm_stage WHERE id = ?",[W.stageId])).rows[0])throw Error("INVALID_STAGE");await H.execute("UPDATE crm_deal SET stageId = ?, updatedAt = ? WHERE id = ?",[W.stageId,Q,W.dealId]);let k=(await H.query("SELECT * FROM crm_deal WHERE id = ?",[W.dealId])).rows;return x(k[0])}async function Z(W){let Q=new Date().toISOString();if(!(await H.query("SELECT * FROM crm_deal WHERE id = ?",[W.dealId])).rows[0])throw Error("NOT_FOUND");await H.execute("UPDATE crm_deal SET status = 'WON', wonSource = ?, notes = ?, updatedAt = ? WHERE id = ?",[W.wonSource??null,W.notes??null,Q,W.dealId]);let Y=(await H.query("SELECT * FROM crm_deal WHERE id = ?",[W.dealId])).rows;return x(Y[0])}async function $(W){let Q=new Date().toISOString();if(!(await H.query("SELECT * FROM crm_deal WHERE id = ?",[W.dealId])).rows[0])throw Error("NOT_FOUND");await H.execute("UPDATE crm_deal SET status = 'LOST', lostReason = ?, notes = ?, updatedAt = ? WHERE id = ?",[W.lostReason,W.notes??null,Q,W.dealId]);let Y=(await H.query("SELECT * FROM crm_deal WHERE id = ?",[W.dealId])).rows;return x(Y[0])}async function U(W){let Q=(await H.query("SELECT * FROM crm_deal WHERE projectId = ? AND pipelineId = ? AND status = 'OPEN' ORDER BY value DESC",[W.projectId,W.pipelineId])).rows,z=(await H.query("SELECT * FROM crm_stage WHERE pipelineId = ? ORDER BY position",[W.pipelineId])).rows,Y={};for(let k of z)Y[k.id]=Q.filter((j)=>j.stageId===k.id).map(x);return Y}async function N(W){return(await H.query("SELECT * FROM crm_stage WHERE pipelineId = ? ORDER BY position",[W.pipelineId])).rows.map((z)=>({id:z.id,pipelineId:z.pipelineId,name:z.name,position:z.position}))}return{listDeals:F,createDeal:J,moveDeal:v,winDeal:Z,loseDeal:$,getDealsByStage:U,getPipelineStages:N}}var y=[{id:"stage-1",name:"Lead",position:1,pipelineId:"pipeline-1"},{id:"stage-2",name:"Qualified",position:2,pipelineId:"pipeline-1"},{id:"stage-3",name:"Proposal",position:3,pipelineId:"pipeline-1"},{id:"stage-4",name:"Negotiation",position:4,pipelineId:"pipeline-1"},{id:"stage-5",name:"Closed",position:5,pipelineId:"pipeline-1"}],q=[{id:"deal-1",name:"Enterprise License - Acme Corp",value:75000,currency:"USD",pipelineId:"pipeline-1",stageId:"stage-3",status:"OPEN",contactId:"contact-1",companyId:"company-1",ownerId:"user-1",expectedCloseDate:new Date("2024-05-15T00:00:00Z"),createdAt:new Date("2024-02-01T10:00:00Z"),updatedAt:new Date("2024-04-10T14:30:00Z")},{id:"deal-2",name:"Startup Plan - TechStart Inc",value:12000,currency:"USD",pipelineId:"pipeline-1",stageId:"stage-2",status:"OPEN",contactId:"contact-2",companyId:"company-2",ownerId:"user-2",expectedCloseDate:new Date("2024-04-30T00:00:00Z"),createdAt:new Date("2024-03-15T09:00:00Z"),updatedAt:new Date("2024-04-08T11:15:00Z")},{id:"deal-3",name:"Professional Services - Global Ltd",value:45000,currency:"USD",pipelineId:"pipeline-1",stageId:"stage-4",status:"OPEN",contactId:"contact-3",companyId:"company-3",ownerId:"user-1",expectedCloseDate:new Date("2024-04-20T00:00:00Z"),createdAt:new Date("2024-01-20T08:00:00Z"),updatedAt:new Date("2024-04-12T16:45:00Z")},{id:"deal-4",name:"Annual Contract - SmallBiz Co",value:8500,currency:"USD",pipelineId:"pipeline-1",stageId:"stage-1",status:"OPEN",contactId:"contact-4",companyId:"company-4",ownerId:"user-3",createdAt:new Date("2024-04-05T12:00:00Z"),updatedAt:new Date("2024-04-05T12:00:00Z")},{id:"deal-5",name:"Custom Integration - MegaCorp",value:125000,currency:"USD",pipelineId:"pipeline-1",stageId:"stage-5",status:"WON",contactId:"contact-5",companyId:"company-5",ownerId:"user-1",expectedCloseDate:new Date("2024-03-31T00:00:00Z"),createdAt:new Date("2023-11-10T10:00:00Z"),updatedAt:new Date("2024-03-28T09:00:00Z")},{id:"deal-6",name:"Pilot Project - NewCo",value:5000,currency:"USD",pipelineId:"pipeline-1",stageId:"stage-2",status:"LOST",contactId:"contact-6",companyId:"company-6",ownerId:"user-2",createdAt:new Date("2024-01-15T14:00:00Z"),updatedAt:new Date("2024-02-28T10:30:00Z")}],r=[{id:"company-1",name:"Acme Corporation",domain:"acme.com",industry:"Technology",size:"1000-5000",website:"https://acme.com",createdAt:new Date("2024-01-01T00:00:00Z")},{id:"company-2",name:"TechStart Inc",domain:"techstart.io",industry:"Software",size:"10-50",website:"https://techstart.io",createdAt:new Date("2024-02-15T00:00:00Z")},{id:"company-3",name:"Global Ltd",domain:"global.com",industry:"Consulting",size:"500-1000",website:"https://global.com",createdAt:new Date("2023-12-01T00:00:00Z")}],u=[{id:"contact-1",firstName:"John",lastName:"Smith",email:"john.smith@acme.com",phone:"+1-555-0101",title:"VP of Engineering",companyId:"company-1",createdAt:new Date("2024-01-05T00:00:00Z")},{id:"contact-2",firstName:"Sarah",lastName:"Johnson",email:"sarah@techstart.io",phone:"+1-555-0102",title:"CEO",companyId:"company-2",createdAt:new Date("2024-02-20T00:00:00Z")},{id:"contact-3",firstName:"Michael",lastName:"Brown",email:"michael.brown@global.com",phone:"+1-555-0103",title:"CTO",companyId:"company-3",createdAt:new Date("2023-12-10T00:00:00Z")}];async function h(H){let{pipelineId:F,stageId:J,status:v,ownerId:Z,search:$,limit:U=20,offset:N=0}=H,W=[...q];if(F)W=W.filter((k)=>k.pipelineId===F);if(J)W=W.filter((k)=>k.stageId===J);if(v&&v!=="all")W=W.filter((k)=>k.status===v);if(Z)W=W.filter((k)=>k.ownerId===Z);if($){let k=$.toLowerCase();W=W.filter((j)=>j.name.toLowerCase().includes(k))}W.sort((k,j)=>j.value-k.value);let Q=W.length,z=W.reduce((k,j)=>k+j.value,0);return{deals:W.slice(N,N+U),total:Q,totalValue:z}}async function m(H,F){let J=new Date,v={id:`deal-${Date.now()}`,name:H.name,value:H.value,currency:H.currency??"USD",pipelineId:H.pipelineId,stageId:H.stageId,status:"OPEN",contactId:H.contactId,companyId:H.companyId,ownerId:F.ownerId,expectedCloseDate:H.expectedCloseDate,createdAt:J,updatedAt:J};return q.push(v),v}async function S(H){let F=q.findIndex(($)=>$.id===H.dealId);if(F===-1)throw Error("NOT_FOUND");let J=q[F];if(!J)throw Error("NOT_FOUND");if(!y.find(($)=>$.id===H.stageId))throw Error("INVALID_STAGE");let Z={...J,stageId:H.stageId,updatedAt:new Date};return q[F]=Z,Z}async function C(H){let F=q.findIndex((Z)=>Z.id===H.dealId);if(F===-1)throw Error("NOT_FOUND");let J=q[F];if(!J)throw Error("NOT_FOUND");let v={...J,status:"WON",updatedAt:new Date};return q[F]=v,v}async function c(H){let F=q.findIndex((Z)=>Z.id===H.dealId);if(F===-1)throw Error("NOT_FOUND");let J=q[F];if(!J)throw Error("NOT_FOUND");let v={...J,status:"LOST",updatedAt:new Date};return q[F]=v,v}async function D(H){let F=q.filter((v)=>v.pipelineId===H.pipelineId&&v.status==="OPEN"),J={};for(let v of y)J[v.id]=F.filter((Z)=>Z.stageId===v.id);return J}async function K(H){return y.filter((F)=>F.pipelineId===H.pipelineId)}function V(H,F="USD"){return new Intl.NumberFormat("en-US",{style:"currency",currency:F,minimumFractionDigits:0}).format(H)}var WH={target:"markdown",render:async(H,F)=>{if(H.source.type!=="component"||H.source.componentKey!=="PipelineKanbanView")throw Error("crmPipelineMarkdownRenderer: not PipelineKanbanView");let J="pipeline-1",[v,Z]=await Promise.all([h({pipelineId:J,limit:50}),K({pipelineId:J})]),$=v.deals,U=Z,N={};for(let Q of U)N[Q.id]=$.filter((z)=>z.stageId===Q.id&&z.status==="OPEN");let W=["# CRM Pipeline","",`**Total Value**: ${V(v.totalValue)}`,`**Total Deals**: ${v.total}`,""];for(let Q of U.sort((z,Y)=>z.position-Y.position)){let z=N[Q.id]??[],Y=z.reduce((k,j)=>k+j.value,0);if(W.push(`## ${Q.name}`),W.push(`_${z.length} deals · ${V(Y)}_`),W.push(""),z.length===0)W.push("_No deals_");else for(let k of z)W.push(`- **${k.name}** - ${V(k.value,k.currency)}`);W.push("")}return{mimeType:"text/markdown",body:W.join(`
3
+ `)}}},kH={target:"markdown",render:async(H,F)=>{if(H.source.type!=="component"||H.source.componentKey!=="CrmDashboard")throw Error("crmDashboardMarkdownRenderer: not CrmDashboard");let J="pipeline-1",[v,Z]=await Promise.all([h({pipelineId:J,limit:100}),K({pipelineId:J})]),$=v.deals,U=Z,N=$.filter((X)=>X.status==="OPEN"),W=$.filter((X)=>X.status==="WON"),Q=$.filter((X)=>X.status==="LOST"),z=N.reduce((X,P)=>X+P.value,0),Y=W.reduce((X,P)=>X+P.value,0),k=["# CRM Dashboard","","> Sales pipeline overview and key metrics","","## Summary","","| Metric | Value |","|--------|-------|",`| Total Deals | ${v.total} |`,`| Pipeline Value | ${V(v.totalValue)} |`,`| Open Deals | ${N.length} (${V(z)}) |`,`| Won Deals | ${W.length} (${V(Y)}) |`,`| Lost Deals | ${Q.length} |`,"","## Pipeline Stages",""];k.push("| Stage | Deals | Value |"),k.push("|-------|-------|-------|");for(let X of U.sort((P,R)=>P.position-R.position)){let P=N.filter((T)=>T.stageId===X.id),R=P.reduce((T,L)=>T+L.value,0);k.push(`| ${X.name} | ${P.length} | ${V(R)} |`)}k.push(""),k.push("## Recent Deals"),k.push("");let j=$.slice(0,10);if(j.length===0)k.push("_No deals yet._");else{k.push("| Deal | Value | Stage | Status |"),k.push("|------|-------|-------|--------|");for(let X of j){let P=U.find((R)=>R.id===X.stageId);k.push(`| ${X.name} | ${V(X.value,X.currency)} | ${P?.name??"-"} | ${X.status} |`)}}return{mimeType:"text/markdown",body:k.join(`
4
+ `)}}};export{WH as crmPipelineMarkdownRenderer,kH as crmDashboardMarkdownRenderer};