@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,99 +1 @@
1
- // src/entities/task.entity.ts
2
- import {
3
- defineEntity,
4
- defineEntityEnum,
5
- field,
6
- index
7
- } from "@contractspec/lib.schema";
8
- var TaskTypeEnum = defineEntityEnum({
9
- name: "TaskType",
10
- values: ["CALL", "EMAIL", "MEETING", "TODO", "FOLLOW_UP", "OTHER"],
11
- schema: "crm",
12
- description: "Type of CRM task."
13
- });
14
- var TaskPriorityEnum = defineEntityEnum({
15
- name: "TaskPriority",
16
- values: ["LOW", "NORMAL", "HIGH", "URGENT"],
17
- schema: "crm",
18
- description: "Priority of a task."
19
- });
20
- var TaskStatusEnum = defineEntityEnum({
21
- name: "TaskStatus",
22
- values: ["PENDING", "IN_PROGRESS", "COMPLETED", "CANCELLED"],
23
- schema: "crm",
24
- description: "Status of a task."
25
- });
26
- var TaskEntity = defineEntity({
27
- name: "Task",
28
- description: "A task or follow-up activity.",
29
- schema: "crm",
30
- map: "task",
31
- fields: {
32
- id: field.id(),
33
- title: field.string({ description: "Task title" }),
34
- description: field.string({ isOptional: true }),
35
- type: field.enum("TaskType", { default: "TODO" }),
36
- priority: field.enum("TaskPriority", { default: "NORMAL" }),
37
- status: field.enum("TaskStatus", { default: "PENDING" }),
38
- dueDate: field.dateTime({ isOptional: true }),
39
- reminderAt: field.dateTime({ isOptional: true }),
40
- contactId: field.string({ isOptional: true }),
41
- dealId: field.string({ isOptional: true }),
42
- companyId: field.string({ isOptional: true }),
43
- organizationId: field.foreignKey(),
44
- assignedTo: field.foreignKey({ description: "User assigned to this task" }),
45
- createdBy: field.foreignKey(),
46
- completedAt: field.dateTime({ isOptional: true }),
47
- completedBy: field.string({ isOptional: true }),
48
- createdAt: field.createdAt(),
49
- updatedAt: field.updatedAt(),
50
- contact: field.belongsTo("Contact", ["contactId"], ["id"]),
51
- deal: field.belongsTo("Deal", ["dealId"], ["id"]),
52
- company: field.belongsTo("Company", ["companyId"], ["id"])
53
- },
54
- indexes: [
55
- index.on(["organizationId", "assignedTo", "status"]),
56
- index.on(["dueDate", "status"]),
57
- index.on(["contactId"]),
58
- index.on(["dealId"])
59
- ],
60
- enums: [TaskTypeEnum, TaskPriorityEnum, TaskStatusEnum]
61
- });
62
- var ActivityEntity = defineEntity({
63
- name: "Activity",
64
- description: "An activity/interaction logged in the CRM.",
65
- schema: "crm",
66
- map: "activity",
67
- fields: {
68
- id: field.id(),
69
- type: field.enum("TaskType"),
70
- subject: field.string(),
71
- description: field.string({ isOptional: true }),
72
- contactId: field.string({ isOptional: true }),
73
- dealId: field.string({ isOptional: true }),
74
- companyId: field.string({ isOptional: true }),
75
- organizationId: field.foreignKey(),
76
- performedBy: field.foreignKey(),
77
- outcome: field.string({ isOptional: true }),
78
- occurredAt: field.dateTime(),
79
- duration: field.int({
80
- isOptional: true,
81
- description: "Duration in minutes"
82
- }),
83
- createdAt: field.createdAt(),
84
- contact: field.belongsTo("Contact", ["contactId"], ["id"]),
85
- deal: field.belongsTo("Deal", ["dealId"], ["id"]),
86
- company: field.belongsTo("Company", ["companyId"], ["id"])
87
- },
88
- indexes: [
89
- index.on(["contactId", "occurredAt"]),
90
- index.on(["dealId", "occurredAt"])
91
- ]
92
- });
93
- export {
94
- TaskTypeEnum,
95
- TaskStatusEnum,
96
- TaskPriorityEnum,
97
- TaskEntity,
98
- ActivityEntity
99
- };
1
+ import{defineEntity as B,defineEntityEnum as z,field as j,index as q}from"@contractspec/lib.schema";var C=z({name:"TaskType",values:["CALL","EMAIL","MEETING","TODO","FOLLOW_UP","OTHER"],schema:"crm",description:"Type of CRM task."}),D=z({name:"TaskPriority",values:["LOW","NORMAL","HIGH","URGENT"],schema:"crm",description:"Priority of a task."}),F=z({name:"TaskStatus",values:["PENDING","IN_PROGRESS","COMPLETED","CANCELLED"],schema:"crm",description:"Status of a task."}),H=B({name:"Task",description:"A task or follow-up activity.",schema:"crm",map:"task",fields:{id:j.id(),title:j.string({description:"Task title"}),description:j.string({isOptional:!0}),type:j.enum("TaskType",{default:"TODO"}),priority:j.enum("TaskPriority",{default:"NORMAL"}),status:j.enum("TaskStatus",{default:"PENDING"}),dueDate:j.dateTime({isOptional:!0}),reminderAt:j.dateTime({isOptional:!0}),contactId:j.string({isOptional:!0}),dealId:j.string({isOptional:!0}),companyId:j.string({isOptional:!0}),organizationId:j.foreignKey(),assignedTo:j.foreignKey({description:"User assigned to this task"}),createdBy:j.foreignKey(),completedAt:j.dateTime({isOptional:!0}),completedBy:j.string({isOptional:!0}),createdAt:j.createdAt(),updatedAt:j.updatedAt(),contact:j.belongsTo("Contact",["contactId"],["id"]),deal:j.belongsTo("Deal",["dealId"],["id"]),company:j.belongsTo("Company",["companyId"],["id"])},indexes:[q.on(["organizationId","assignedTo","status"]),q.on(["dueDate","status"]),q.on(["contactId"]),q.on(["dealId"])],enums:[C,D,F]}),I=B({name:"Activity",description:"An activity/interaction logged in the CRM.",schema:"crm",map:"activity",fields:{id:j.id(),type:j.enum("TaskType"),subject:j.string(),description:j.string({isOptional:!0}),contactId:j.string({isOptional:!0}),dealId:j.string({isOptional:!0}),companyId:j.string({isOptional:!0}),organizationId:j.foreignKey(),performedBy:j.foreignKey(),outcome:j.string({isOptional:!0}),occurredAt:j.dateTime(),duration:j.int({isOptional:!0,description:"Duration in minutes"}),createdAt:j.createdAt(),contact:j.belongsTo("Contact",["contactId"],["id"]),deal:j.belongsTo("Deal",["dealId"],["id"]),company:j.belongsTo("Company",["companyId"],["id"])},indexes:[q.on(["contactId","occurredAt"]),q.on(["dealId","occurredAt"])]});export{C as TaskTypeEnum,F as TaskStatusEnum,D as TaskPriorityEnum,H as TaskEntity,I as ActivityEntity};
@@ -1,31 +1 @@
1
- // src/events/contact.event.ts
2
- import { defineEvent } from "@contractspec/lib.contracts-spec";
3
- import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
- var ContactCreatedPayload = defineSchemaModel({
5
- name: "ContactCreatedPayload",
6
- description: "Payload when a contact is created",
7
- fields: {
8
- contactId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
9
- email: { type: ScalarTypeEnum.EmailAddress(), isOptional: true },
10
- organizationId: {
11
- type: ScalarTypeEnum.String_unsecure(),
12
- isOptional: false
13
- },
14
- ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
15
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
16
- }
17
- });
18
- var ContactCreatedEvent = defineEvent({
19
- meta: {
20
- key: "contact.created",
21
- version: "1.0.0",
22
- description: "A new contact has been created.",
23
- stability: "stable",
24
- owners: ["@crm-team"],
25
- tags: ["contact", "created"]
26
- },
27
- payload: ContactCreatedPayload
28
- });
29
- export {
30
- ContactCreatedEvent
31
- };
1
+ import{defineEvent as j}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as k,ScalarTypeEnum as g}from"@contractspec/lib.schema";var q=k({name:"ContactCreatedPayload",description:"Payload when a contact is created",fields:{contactId:{type:g.String_unsecure(),isOptional:!1},email:{type:g.EmailAddress(),isOptional:!0},organizationId:{type:g.String_unsecure(),isOptional:!1},ownerId:{type:g.String_unsecure(),isOptional:!1},createdAt:{type:g.DateTime(),isOptional:!1}}}),z=j({meta:{key:"contact.created",version:"1.0.0",description:"A new contact has been created.",stability:"stable",owners:["@crm-team"],tags:["contact","created"]},payload:q});export{z as ContactCreatedEvent};
@@ -1,101 +1 @@
1
- // src/events/deal.event.ts
2
- import { defineEvent } from "@contractspec/lib.contracts-spec";
3
- import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
- var DealCreatedPayload = defineSchemaModel({
5
- name: "DealCreatedPayload",
6
- description: "Payload when a deal is created",
7
- fields: {
8
- dealId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
9
- name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
10
- value: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
11
- pipelineId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
12
- stageId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
13
- ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
14
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
15
- }
16
- });
17
- var DealMovedPayload = defineSchemaModel({
18
- name: "DealMovedEventPayload",
19
- description: "Payload when a deal is moved to another stage",
20
- fields: {
21
- dealId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
22
- fromStageId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
23
- toStageId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
24
- movedBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
25
- movedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
26
- }
27
- });
28
- var DealWonPayload = defineSchemaModel({
29
- name: "DealWonEventPayload",
30
- description: "Payload when a deal is won",
31
- fields: {
32
- dealId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
33
- value: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
34
- currency: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
35
- contactId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
36
- companyId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
37
- ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
38
- wonAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
39
- }
40
- });
41
- var DealLostPayload = defineSchemaModel({
42
- name: "DealLostEventPayload",
43
- description: "Payload when a deal is lost",
44
- fields: {
45
- dealId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
46
- value: { type: ScalarTypeEnum.Float_unsecure(), isOptional: false },
47
- reason: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
48
- ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
49
- lostAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
50
- }
51
- });
52
- var DealCreatedEvent = defineEvent({
53
- meta: {
54
- key: "deal.created",
55
- version: "1.0.0",
56
- description: "A new deal has been created.",
57
- stability: "stable",
58
- owners: ["@crm-team"],
59
- tags: ["deal", "created"]
60
- },
61
- payload: DealCreatedPayload
62
- });
63
- var DealMovedEvent = defineEvent({
64
- meta: {
65
- key: "deal.moved",
66
- version: "1.0.0",
67
- description: "A deal has been moved to a different stage.",
68
- stability: "stable",
69
- owners: ["@crm-team"],
70
- tags: ["deal", "moved"]
71
- },
72
- payload: DealMovedPayload
73
- });
74
- var DealWonEvent = defineEvent({
75
- meta: {
76
- key: "deal.won",
77
- version: "1.0.0",
78
- description: "A deal has been won.",
79
- stability: "stable",
80
- owners: ["@crm-team"],
81
- tags: ["deal", "won"]
82
- },
83
- payload: DealWonPayload
84
- });
85
- var DealLostEvent = defineEvent({
86
- meta: {
87
- key: "deal.lost",
88
- version: "1.0.0",
89
- description: "A deal has been lost.",
90
- stability: "stable",
91
- owners: ["@crm-team"],
92
- tags: ["deal", "lost"]
93
- },
94
- payload: DealLostPayload
95
- });
96
- export {
97
- DealWonEvent,
98
- DealMovedEvent,
99
- DealLostEvent,
100
- DealCreatedEvent
101
- };
1
+ import{defineEvent as j}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as k,ScalarTypeEnum as g}from"@contractspec/lib.schema";var q=k({name:"DealCreatedPayload",description:"Payload when a deal is created",fields:{dealId:{type:g.String_unsecure(),isOptional:!1},name:{type:g.String_unsecure(),isOptional:!1},value:{type:g.Float_unsecure(),isOptional:!1},pipelineId:{type:g.String_unsecure(),isOptional:!1},stageId:{type:g.String_unsecure(),isOptional:!1},ownerId:{type:g.String_unsecure(),isOptional:!1},createdAt:{type:g.DateTime(),isOptional:!1}}}),w=k({name:"DealMovedEventPayload",description:"Payload when a deal is moved to another stage",fields:{dealId:{type:g.String_unsecure(),isOptional:!1},fromStageId:{type:g.String_unsecure(),isOptional:!1},toStageId:{type:g.String_unsecure(),isOptional:!1},movedBy:{type:g.String_unsecure(),isOptional:!1},movedAt:{type:g.DateTime(),isOptional:!1}}}),x=k({name:"DealWonEventPayload",description:"Payload when a deal is won",fields:{dealId:{type:g.String_unsecure(),isOptional:!1},value:{type:g.Float_unsecure(),isOptional:!1},currency:{type:g.String_unsecure(),isOptional:!1},contactId:{type:g.String_unsecure(),isOptional:!0},companyId:{type:g.String_unsecure(),isOptional:!0},ownerId:{type:g.String_unsecure(),isOptional:!1},wonAt:{type:g.DateTime(),isOptional:!1}}}),z=k({name:"DealLostEventPayload",description:"Payload when a deal is lost",fields:{dealId:{type:g.String_unsecure(),isOptional:!1},value:{type:g.Float_unsecure(),isOptional:!1},reason:{type:g.String_unsecure(),isOptional:!1},ownerId:{type:g.String_unsecure(),isOptional:!1},lostAt:{type:g.DateTime(),isOptional:!1}}}),F=j({meta:{key:"deal.created",version:"1.0.0",description:"A new deal has been created.",stability:"stable",owners:["@crm-team"],tags:["deal","created"]},payload:q}),G=j({meta:{key:"deal.moved",version:"1.0.0",description:"A deal has been moved to a different stage.",stability:"stable",owners:["@crm-team"],tags:["deal","moved"]},payload:w}),H=j({meta:{key:"deal.won",version:"1.0.0",description:"A deal has been won.",stability:"stable",owners:["@crm-team"],tags:["deal","won"]},payload:x}),I=j({meta:{key:"deal.lost",version:"1.0.0",description:"A deal has been lost.",stability:"stable",owners:["@crm-team"],tags:["deal","lost"]},payload:z});export{H as DealWonEvent,G as DealMovedEvent,I as DealLostEvent,F as DealCreatedEvent};
@@ -1,158 +1 @@
1
- // src/events/contact.event.ts
2
- import { defineEvent } from "@contractspec/lib.contracts-spec";
3
- import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
- var ContactCreatedPayload = defineSchemaModel({
5
- name: "ContactCreatedPayload",
6
- description: "Payload when a contact is created",
7
- fields: {
8
- contactId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
9
- email: { type: ScalarTypeEnum.EmailAddress(), isOptional: true },
10
- organizationId: {
11
- type: ScalarTypeEnum.String_unsecure(),
12
- isOptional: false
13
- },
14
- ownerId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
15
- createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
16
- }
17
- });
18
- var ContactCreatedEvent = defineEvent({
19
- meta: {
20
- key: "contact.created",
21
- version: "1.0.0",
22
- description: "A new contact has been created.",
23
- stability: "stable",
24
- owners: ["@crm-team"],
25
- tags: ["contact", "created"]
26
- },
27
- payload: ContactCreatedPayload
28
- });
29
-
30
- // src/events/deal.event.ts
31
- import { defineEvent as defineEvent2 } from "@contractspec/lib.contracts-spec";
32
- import { defineSchemaModel as defineSchemaModel2, ScalarTypeEnum as ScalarTypeEnum2 } from "@contractspec/lib.schema";
33
- var DealCreatedPayload = defineSchemaModel2({
34
- name: "DealCreatedPayload",
35
- description: "Payload when a deal is created",
36
- fields: {
37
- dealId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
38
- name: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
39
- value: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
40
- pipelineId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
41
- stageId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
42
- ownerId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
43
- createdAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
44
- }
45
- });
46
- var DealMovedPayload = defineSchemaModel2({
47
- name: "DealMovedEventPayload",
48
- description: "Payload when a deal is moved to another stage",
49
- fields: {
50
- dealId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
51
- fromStageId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
52
- toStageId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
53
- movedBy: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
54
- movedAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
55
- }
56
- });
57
- var DealWonPayload = defineSchemaModel2({
58
- name: "DealWonEventPayload",
59
- description: "Payload when a deal is won",
60
- fields: {
61
- dealId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
62
- value: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
63
- currency: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
64
- contactId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
65
- companyId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: true },
66
- ownerId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
67
- wonAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
68
- }
69
- });
70
- var DealLostPayload = defineSchemaModel2({
71
- name: "DealLostEventPayload",
72
- description: "Payload when a deal is lost",
73
- fields: {
74
- dealId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
75
- value: { type: ScalarTypeEnum2.Float_unsecure(), isOptional: false },
76
- reason: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
77
- ownerId: { type: ScalarTypeEnum2.String_unsecure(), isOptional: false },
78
- lostAt: { type: ScalarTypeEnum2.DateTime(), isOptional: false }
79
- }
80
- });
81
- var DealCreatedEvent = defineEvent2({
82
- meta: {
83
- key: "deal.created",
84
- version: "1.0.0",
85
- description: "A new deal has been created.",
86
- stability: "stable",
87
- owners: ["@crm-team"],
88
- tags: ["deal", "created"]
89
- },
90
- payload: DealCreatedPayload
91
- });
92
- var DealMovedEvent = defineEvent2({
93
- meta: {
94
- key: "deal.moved",
95
- version: "1.0.0",
96
- description: "A deal has been moved to a different stage.",
97
- stability: "stable",
98
- owners: ["@crm-team"],
99
- tags: ["deal", "moved"]
100
- },
101
- payload: DealMovedPayload
102
- });
103
- var DealWonEvent = defineEvent2({
104
- meta: {
105
- key: "deal.won",
106
- version: "1.0.0",
107
- description: "A deal has been won.",
108
- stability: "stable",
109
- owners: ["@crm-team"],
110
- tags: ["deal", "won"]
111
- },
112
- payload: DealWonPayload
113
- });
114
- var DealLostEvent = defineEvent2({
115
- meta: {
116
- key: "deal.lost",
117
- version: "1.0.0",
118
- description: "A deal has been lost.",
119
- stability: "stable",
120
- owners: ["@crm-team"],
121
- tags: ["deal", "lost"]
122
- },
123
- payload: DealLostPayload
124
- });
125
-
126
- // src/events/task.event.ts
127
- import { defineEvent as defineEvent3 } from "@contractspec/lib.contracts-spec";
128
- import { defineSchemaModel as defineSchemaModel3, ScalarTypeEnum as ScalarTypeEnum3 } from "@contractspec/lib.schema";
129
- var TaskCompletedPayload = defineSchemaModel3({
130
- name: "TaskCompletedPayload",
131
- description: "Payload when a task is completed",
132
- fields: {
133
- taskId: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
134
- type: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
135
- assignedTo: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
136
- completedBy: { type: ScalarTypeEnum3.String_unsecure(), isOptional: false },
137
- completedAt: { type: ScalarTypeEnum3.DateTime(), isOptional: false }
138
- }
139
- });
140
- var TaskCompletedEvent = defineEvent3({
141
- meta: {
142
- key: "task.completed",
143
- version: "1.0.0",
144
- description: "A task has been completed.",
145
- stability: "stable",
146
- owners: ["@crm-team"],
147
- tags: ["task", "lifecycle"]
148
- },
149
- payload: TaskCompletedPayload
150
- });
151
- export {
152
- TaskCompletedEvent,
153
- DealWonEvent,
154
- DealMovedEvent,
155
- DealLostEvent,
156
- DealCreatedEvent,
157
- ContactCreatedEvent
158
- };
1
+ import{defineEvent as z}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as A,ScalarTypeEnum as g}from"@contractspec/lib.schema";var B=A({name:"ContactCreatedPayload",description:"Payload when a contact is created",fields:{contactId:{type:g.String_unsecure(),isOptional:!1},email:{type:g.EmailAddress(),isOptional:!0},organizationId:{type:g.String_unsecure(),isOptional:!1},ownerId:{type:g.String_unsecure(),isOptional:!1},createdAt:{type:g.DateTime(),isOptional:!1}}}),R=z({meta:{key:"contact.created",version:"1.0.0",description:"A new contact has been created.",stability:"stable",owners:["@crm-team"],tags:["contact","created"]},payload:B});import{defineEvent as q}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as w,ScalarTypeEnum as x}from"@contractspec/lib.schema";var F=w({name:"DealCreatedPayload",description:"Payload when a deal is created",fields:{dealId:{type:x.String_unsecure(),isOptional:!1},name:{type:x.String_unsecure(),isOptional:!1},value:{type:x.Float_unsecure(),isOptional:!1},pipelineId:{type:x.String_unsecure(),isOptional:!1},stageId:{type:x.String_unsecure(),isOptional:!1},ownerId:{type:x.String_unsecure(),isOptional:!1},createdAt:{type:x.DateTime(),isOptional:!1}}}),G=w({name:"DealMovedEventPayload",description:"Payload when a deal is moved to another stage",fields:{dealId:{type:x.String_unsecure(),isOptional:!1},fromStageId:{type:x.String_unsecure(),isOptional:!1},toStageId:{type:x.String_unsecure(),isOptional:!1},movedBy:{type:x.String_unsecure(),isOptional:!1},movedAt:{type:x.DateTime(),isOptional:!1}}}),H=w({name:"DealWonEventPayload",description:"Payload when a deal is won",fields:{dealId:{type:x.String_unsecure(),isOptional:!1},value:{type:x.Float_unsecure(),isOptional:!1},currency:{type:x.String_unsecure(),isOptional:!1},contactId:{type:x.String_unsecure(),isOptional:!0},companyId:{type:x.String_unsecure(),isOptional:!0},ownerId:{type:x.String_unsecure(),isOptional:!1},wonAt:{type:x.DateTime(),isOptional:!1}}}),I=w({name:"DealLostEventPayload",description:"Payload when a deal is lost",fields:{dealId:{type:x.String_unsecure(),isOptional:!1},value:{type:x.Float_unsecure(),isOptional:!1},reason:{type:x.String_unsecure(),isOptional:!1},ownerId:{type:x.String_unsecure(),isOptional:!1},lostAt:{type:x.DateTime(),isOptional:!1}}}),Y=q({meta:{key:"deal.created",version:"1.0.0",description:"A new deal has been created.",stability:"stable",owners:["@crm-team"],tags:["deal","created"]},payload:F}),Z=q({meta:{key:"deal.moved",version:"1.0.0",description:"A deal has been moved to a different stage.",stability:"stable",owners:["@crm-team"],tags:["deal","moved"]},payload:G}),_=q({meta:{key:"deal.won",version:"1.0.0",description:"A deal has been won.",stability:"stable",owners:["@crm-team"],tags:["deal","won"]},payload:H}),$=q({meta:{key:"deal.lost",version:"1.0.0",description:"A deal has been lost.",stability:"stable",owners:["@crm-team"],tags:["deal","lost"]},payload:I});import{defineEvent as J}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as K,ScalarTypeEnum as j}from"@contractspec/lib.schema";var N=K({name:"TaskCompletedPayload",description:"Payload when a task is completed",fields:{taskId:{type:j.String_unsecure(),isOptional:!1},type:{type:j.String_unsecure(),isOptional:!1},assignedTo:{type:j.String_unsecure(),isOptional:!1},completedBy:{type:j.String_unsecure(),isOptional:!1},completedAt:{type:j.DateTime(),isOptional:!1}}}),C=J({meta:{key:"task.completed",version:"1.0.0",description:"A task has been completed.",stability:"stable",owners:["@crm-team"],tags:["task","lifecycle"]},payload:N});export{C as TaskCompletedEvent,_ as DealWonEvent,Z as DealMovedEvent,$ as DealLostEvent,Y as DealCreatedEvent,R as ContactCreatedEvent};
@@ -1,28 +1 @@
1
- // src/events/task.event.ts
2
- import { defineEvent } from "@contractspec/lib.contracts-spec";
3
- import { defineSchemaModel, ScalarTypeEnum } from "@contractspec/lib.schema";
4
- var TaskCompletedPayload = defineSchemaModel({
5
- name: "TaskCompletedPayload",
6
- description: "Payload when a task is completed",
7
- fields: {
8
- taskId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
9
- type: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
10
- assignedTo: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
11
- completedBy: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
12
- completedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
13
- }
14
- });
15
- var TaskCompletedEvent = defineEvent({
16
- meta: {
17
- key: "task.completed",
18
- version: "1.0.0",
19
- description: "A task has been completed.",
20
- stability: "stable",
21
- owners: ["@crm-team"],
22
- tags: ["task", "lifecycle"]
23
- },
24
- payload: TaskCompletedPayload
25
- });
26
- export {
27
- TaskCompletedEvent
28
- };
1
+ import{defineEvent as j}from"@contractspec/lib.contracts-spec";import{defineSchemaModel as q,ScalarTypeEnum as g}from"@contractspec/lib.schema";var w=q({name:"TaskCompletedPayload",description:"Payload when a task is completed",fields:{taskId:{type:g.String_unsecure(),isOptional:!1},type:{type:g.String_unsecure(),isOptional:!1},assignedTo:{type:g.String_unsecure(),isOptional:!1},completedBy:{type:g.String_unsecure(),isOptional:!1},completedAt:{type:g.DateTime(),isOptional:!1}}}),A=j({meta:{key:"task.completed",version:"1.0.0",description:"A task has been completed.",stability:"stable",owners:["@crm-team"],tags:["task","lifecycle"]},payload:w});export{A as TaskCompletedEvent};
@@ -1,39 +1 @@
1
- // src/example.ts
2
- import { defineExample } from "@contractspec/lib.contracts-spec";
3
- var example = defineExample({
4
- meta: {
5
- key: "crm-pipeline",
6
- version: "1.0.0",
7
- title: "CRM Pipeline",
8
- description: "Sales CRM with contacts, companies, deals, pipelines, and tasks.",
9
- kind: "template",
10
- visibility: "public",
11
- stability: "experimental",
12
- owners: ["@platform.core"],
13
- tags: ["crm", "sales", "pipeline", "deals"]
14
- },
15
- docs: {
16
- rootDocId: "docs.examples.crm-pipeline"
17
- },
18
- entrypoints: {
19
- packageName: "@contractspec/example.crm-pipeline",
20
- feature: "./feature",
21
- contracts: "./contracts",
22
- presentations: "./presentations",
23
- handlers: "./handlers",
24
- docs: "./docs"
25
- },
26
- surfaces: {
27
- templates: true,
28
- sandbox: {
29
- enabled: true,
30
- modes: ["playground", "specs", "builder", "markdown", "evolution"]
31
- },
32
- studio: { enabled: true, installable: true },
33
- mcp: { enabled: true }
34
- }
35
- });
36
- var example_default = example;
37
- export {
38
- example_default as default
39
- };
1
+ import{defineExample as g}from"@contractspec/lib.contracts-spec";var h=g({meta:{key:"crm-pipeline",version:"1.0.0",title:"CRM Pipeline",description:"Sales CRM with contacts, companies, deals, pipelines, and tasks.",kind:"template",visibility:"public",stability:"experimental",owners:["@platform.core"],tags:["crm","sales","pipeline","deals"]},docs:{rootDocId:"docs.examples.crm-pipeline"},entrypoints:{packageName:"@contractspec/example.crm-pipeline",feature:"./feature",contracts:"./contracts",presentations:"./presentations",handlers:"./handlers",docs:"./docs"},surfaces:{templates:!0,sandbox:{enabled:!0,modes:["playground","specs","builder","markdown","evolution"]},studio:{enabled:!0,installable:!0},mcp:{enabled:!0}}}),q=h;export{q as default};
@@ -1,171 +1,2 @@
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
- export {
170
- createCrmHandlers
171
- };
1
+ import{web as L}from"@contractspec/lib.runtime-sandbox";var{generateId:O}=L;function Q(k){return{id:k.id,projectId:k.projectId,name:k.name,value:k.value,currency:k.currency,pipelineId:k.pipelineId,stageId:k.stageId,status:k.status,contactId:k.contactId??void 0,companyId:k.companyId??void 0,ownerId:k.ownerId,expectedCloseDate:k.expectedCloseDate?new Date(k.expectedCloseDate):void 0,wonSource:k.wonSource??void 0,lostReason:k.lostReason??void 0,notes:k.notes??void 0,createdAt:new Date(k.createdAt),updatedAt:new Date(k.updatedAt)}}var Y={name:"name",value:"value",status:"status",expectedCloseDate:"expectedCloseDate",updatedAt:"updatedAt"};function T(k){async function Z(q){let{projectId:F,pipelineId:z,stageId:G,status:H,ownerId:W,search:X,limit:A=20,offset:E=0,sortBy:M="value",sortDirection:N="desc"}=q,J="WHERE projectId = ?",K=[F];if(z)J+=" AND pipelineId = ?",K.push(z);if(G)J+=" AND stageId = ?",K.push(G);if(H&&H!=="all")J+=" AND status = ?",K.push(H);if(W)J+=" AND ownerId = ?",K.push(W);if(X)J+=" AND name LIKE ?",K.push(`%${X}%`);let R=(await k.query(`SELECT COUNT(*) as count FROM crm_deal ${J}`,K)).rows[0]?.count??0,U=(await k.query(`SELECT COALESCE(SUM(value), 0) as total FROM crm_deal ${J}`,K)).rows[0]?.total??0,y=Y[M]??Y.value,B=N==="asc"?"ASC":"DESC";return{deals:(await k.query(`SELECT * FROM crm_deal ${J} ORDER BY ${y} ${B} LIMIT ? OFFSET ?`,[...K,A,E])).rows.map(Q),total:R,totalValue:U}}async function $(q,F){let z=O("deal"),G=new Date().toISOString();await k.execute(`INSERT INTO crm_deal (id, projectId, pipelineId, stageId, name, value, currency, status, contactId, companyId, ownerId, expectedCloseDate, createdAt, updatedAt)
2
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,[z,F.projectId,q.pipelineId,q.stageId,q.name,q.value,q.currency??"USD","OPEN",q.contactId??null,q.companyId??null,F.ownerId,q.expectedCloseDate?.toISOString()??null,G,G]);let H=(await k.query("SELECT * FROM crm_deal WHERE id = ?",[z])).rows;if(!H[0])throw Error("Failed to create deal");return Q(H[0])}async function j(q){let F=new Date().toISOString();if(!(await k.query("SELECT * FROM crm_deal WHERE id = ?",[q.dealId])).rows[0])throw Error("NOT_FOUND");if(!(await k.query("SELECT * FROM crm_stage WHERE id = ?",[q.stageId])).rows[0])throw Error("INVALID_STAGE");await k.execute("UPDATE crm_deal SET stageId = ?, updatedAt = ? WHERE id = ?",[q.stageId,F,q.dealId]);let H=(await k.query("SELECT * FROM crm_deal WHERE id = ?",[q.dealId])).rows;return Q(H[0])}async function v(q){let F=new Date().toISOString();if(!(await k.query("SELECT * FROM crm_deal WHERE id = ?",[q.dealId])).rows[0])throw Error("NOT_FOUND");await k.execute("UPDATE crm_deal SET status = 'WON', wonSource = ?, notes = ?, updatedAt = ? WHERE id = ?",[q.wonSource??null,q.notes??null,F,q.dealId]);let G=(await k.query("SELECT * FROM crm_deal WHERE id = ?",[q.dealId])).rows;return Q(G[0])}async function P(q){let F=new Date().toISOString();if(!(await k.query("SELECT * FROM crm_deal WHERE id = ?",[q.dealId])).rows[0])throw Error("NOT_FOUND");await k.execute("UPDATE crm_deal SET status = 'LOST', lostReason = ?, notes = ?, updatedAt = ? WHERE id = ?",[q.lostReason,q.notes??null,F,q.dealId]);let G=(await k.query("SELECT * FROM crm_deal WHERE id = ?",[q.dealId])).rows;return Q(G[0])}async function V(q){let F=(await k.query("SELECT * FROM crm_deal WHERE projectId = ? AND pipelineId = ? AND status = 'OPEN' ORDER BY value DESC",[q.projectId,q.pipelineId])).rows,z=(await k.query("SELECT * FROM crm_stage WHERE pipelineId = ? ORDER BY position",[q.pipelineId])).rows,G={};for(let H of z)G[H.id]=F.filter((W)=>W.stageId===H.id).map(Q);return G}async function f(q){return(await k.query("SELECT * FROM crm_stage WHERE pipelineId = ? ORDER BY position",[q.pipelineId])).rows.map((z)=>({id:z.id,pipelineId:z.pipelineId,name:z.name,position:z.position}))}return{listDeals:Z,createDeal:$,moveDeal:j,winDeal:v,loseDeal:P,getDealsByStage:V,getPipelineStages:f}}export{T as createCrmHandlers};