@fayz-ai/plugin-crm 0.1.1
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/dist/CrmContext.d.ts +32 -0
- package/dist/CrmContext.d.ts.map +1 -0
- package/dist/CrmPage.d.ts +13 -0
- package/dist/CrmPage.d.ts.map +1 -0
- package/dist/cascade.d.ts +13 -0
- package/dist/cascade.d.ts.map +1 -0
- package/dist/components/CrmGeneralSettings.d.ts +3 -0
- package/dist/components/CrmGeneralSettings.d.ts.map +1 -0
- package/dist/components/CrmOnboarding.d.ts +5 -0
- package/dist/components/CrmOnboarding.d.ts.map +1 -0
- package/dist/components/PipelineSettings.d.ts +3 -0
- package/dist/components/PipelineSettings.d.ts.map +1 -0
- package/dist/components/QuoteStatusDropdown.d.ts +8 -0
- package/dist/components/QuoteStatusDropdown.d.ts.map +1 -0
- package/dist/data/index.d.ts +3 -0
- package/dist/data/index.d.ts.map +1 -0
- package/dist/data/mock.d.ts +9 -0
- package/dist/data/mock.d.ts.map +1 -0
- package/dist/data/supabase.d.ts +9 -0
- package/dist/data/supabase.d.ts.map +1 -0
- package/dist/data/types.d.ts +32 -0
- package/dist/data/types.d.ts.map +1 -0
- package/dist/index.cjs +4601 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +72 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4595 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/tenant.d.ts +3 -0
- package/dist/lib/tenant.d.ts.map +1 -0
- package/dist/locales/en.d.ts +2 -0
- package/dist/locales/en.d.ts.map +1 -0
- package/dist/locales/index.d.ts +2 -0
- package/dist/locales/index.d.ts.map +1 -0
- package/dist/locales/pt-BR.d.ts +2 -0
- package/dist/locales/pt-BR.d.ts.map +1 -0
- package/dist/registries.d.ts +3 -0
- package/dist/registries.d.ts.map +1 -0
- package/dist/schema/index.d.ts +1036 -0
- package/dist/schema/index.d.ts.map +1 -0
- package/dist/store.d.ts +43 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/types.d.ts +238 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/views/ActivityListView.d.ts +3 -0
- package/dist/views/ActivityListView.d.ts.map +1 -0
- package/dist/views/DashboardView.d.ts +9 -0
- package/dist/views/DashboardView.d.ts.map +1 -0
- package/dist/views/DealSidebar.d.ts +9 -0
- package/dist/views/DealSidebar.d.ts.map +1 -0
- package/dist/views/LeadDetailView.d.ts +8 -0
- package/dist/views/LeadDetailView.d.ts.map +1 -0
- package/dist/views/LeadFormView.d.ts +5 -0
- package/dist/views/LeadFormView.d.ts.map +1 -0
- package/dist/views/LeadListView.d.ts +6 -0
- package/dist/views/LeadListView.d.ts.map +1 -0
- package/dist/views/PipelineView.d.ts +8 -0
- package/dist/views/PipelineView.d.ts.map +1 -0
- package/dist/views/QuoteDetailView.d.ts +8 -0
- package/dist/views/QuoteDetailView.d.ts.map +1 -0
- package/dist/views/QuoteFormView.d.ts +7 -0
- package/dist/views/QuoteFormView.d.ts.map +1 -0
- package/dist/views/QuoteListView.d.ts +7 -0
- package/dist/views/QuoteListView.d.ts.map +1 -0
- package/dist/views/dashboardWidgets.d.ts +11 -0
- package/dist/views/dashboardWidgets.d.ts.map +1 -0
- package/package.json +55 -0
- package/src/CrmContext.tsx +30 -0
- package/src/CrmPage.tsx +152 -0
- package/src/README.md +97 -0
- package/src/cascade.ts +24 -0
- package/src/components/CrmGeneralSettings.tsx +36 -0
- package/src/components/CrmOnboarding.tsx +52 -0
- package/src/components/PipelineSettings.tsx +45 -0
- package/src/components/QuoteStatusDropdown.tsx +214 -0
- package/src/data/index.ts +2 -0
- package/src/data/mock.ts +426 -0
- package/src/data/supabase.ts +634 -0
- package/src/data/types.ts +50 -0
- package/src/index.ts +240 -0
- package/src/lib/tenant.ts +4 -0
- package/src/locales/en.ts +292 -0
- package/src/locales/index.ts +7 -0
- package/src/locales/pt-BR.ts +292 -0
- package/src/migrations/001_crm_base.sql +73 -0
- package/src/migrations/002_activities.sql +33 -0
- package/src/migrations/003_crm_views_rls.sql +61 -0
- package/src/migrations/004_seed_default_pipeline.sql +31 -0
- package/src/registries.ts +88 -0
- package/src/schema/index.ts +102 -0
- package/src/store.ts +375 -0
- package/src/types.ts +280 -0
- package/src/views/ActivityListView.tsx +183 -0
- package/src/views/DashboardView.tsx +12 -0
- package/src/views/DealSidebar.tsx +414 -0
- package/src/views/LeadDetailView.tsx +264 -0
- package/src/views/LeadFormView.tsx +56 -0
- package/src/views/LeadListView.tsx +81 -0
- package/src/views/PipelineView.tsx +299 -0
- package/src/views/QuoteDetailView.tsx +408 -0
- package/src/views/QuoteFormView.tsx +489 -0
- package/src/views/QuoteListView.tsx +101 -0
- package/src/views/dashboardWidgets.tsx +248 -0
package/src/data/mock.ts
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
import type { CrmDataProvider } from './types'
|
|
2
|
+
import type {
|
|
3
|
+
Pipeline, PipelineStage, Lead, Deal, Activity, Quote, QuoteItem,
|
|
4
|
+
CreateLeadInput, CreateDealInput, CreateActivityInput, CreateQuoteInput,
|
|
5
|
+
LeadQuery, DealQuery, ActivityQuery, QuoteQuery,
|
|
6
|
+
PaginatedResult, CrmSummary, FunnelStage,
|
|
7
|
+
LeadStatus, DealStatus, QuoteStatus,
|
|
8
|
+
} from '../types'
|
|
9
|
+
import { deriveLeadStatus, deriveQuoteStatus } from '../cascade'
|
|
10
|
+
|
|
11
|
+
let nextId = 1
|
|
12
|
+
function uid(): string { return String(nextId++) }
|
|
13
|
+
function now(): string { return new Date().toISOString() }
|
|
14
|
+
function today(): string { return new Date().toISOString().slice(0, 10) }
|
|
15
|
+
|
|
16
|
+
function paginate<T>(items: T[], page?: number, pageSize?: number): PaginatedResult<T> {
|
|
17
|
+
const p = page ?? 1
|
|
18
|
+
const ps = pageSize ?? 50
|
|
19
|
+
const start = (p - 1) * ps
|
|
20
|
+
return { data: items.slice(start, start + ps), total: items.length }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface MockStore {
|
|
24
|
+
pipelines: Pipeline[]
|
|
25
|
+
stages: PipelineStage[]
|
|
26
|
+
leads: Lead[]
|
|
27
|
+
deals: Deal[]
|
|
28
|
+
activities: Activity[]
|
|
29
|
+
quotes: Quote[]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function createStore(seedStages?: Array<{ name: string; color: string; probability: number }>): MockStore {
|
|
33
|
+
const tenantId = 'mock-tenant'
|
|
34
|
+
const pipelineId = uid()
|
|
35
|
+
|
|
36
|
+
const defaultStages = seedStages ?? [
|
|
37
|
+
{ name: 'New', color: '#6366f1', probability: 10 },
|
|
38
|
+
{ name: 'Contacted', color: '#3b82f6', probability: 25 },
|
|
39
|
+
{ name: 'Qualified', color: '#f59e0b', probability: 50 },
|
|
40
|
+
{ name: 'Proposal', color: '#f97316', probability: 75 },
|
|
41
|
+
{ name: 'Negotiation', color: '#8b5cf6', probability: 90 },
|
|
42
|
+
{ name: 'Won', color: '#22c55e', probability: 100 },
|
|
43
|
+
{ name: 'Lost', color: '#ef4444', probability: 0 },
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
const stages: PipelineStage[] = defaultStages.map((s, i) => ({
|
|
47
|
+
id: uid(),
|
|
48
|
+
pipelineId,
|
|
49
|
+
name: s.name,
|
|
50
|
+
order: i,
|
|
51
|
+
color: s.color,
|
|
52
|
+
probability: s.probability,
|
|
53
|
+
isWon: s.name === 'Won',
|
|
54
|
+
isLost: s.name === 'Lost',
|
|
55
|
+
tenantId,
|
|
56
|
+
createdAt: now(),
|
|
57
|
+
}))
|
|
58
|
+
|
|
59
|
+
return {
|
|
60
|
+
pipelines: [{
|
|
61
|
+
id: pipelineId,
|
|
62
|
+
name: 'Sales Pipeline',
|
|
63
|
+
isDefault: true,
|
|
64
|
+
isActive: true,
|
|
65
|
+
tenantId,
|
|
66
|
+
createdAt: now(),
|
|
67
|
+
updatedAt: now(),
|
|
68
|
+
}],
|
|
69
|
+
stages,
|
|
70
|
+
leads: [],
|
|
71
|
+
deals: [],
|
|
72
|
+
activities: [],
|
|
73
|
+
quotes: [],
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createMockCrmProvider(options?: {
|
|
78
|
+
dealStages?: Array<{ name: string; color: string; probability: number }>
|
|
79
|
+
}): CrmDataProvider {
|
|
80
|
+
const store = createStore(options?.dealStages)
|
|
81
|
+
const tenantId = 'mock-tenant'
|
|
82
|
+
|
|
83
|
+
// --- Cascade helper: sync lead + quote statuses when a deal moves ---
|
|
84
|
+
function cascadeFromStage(deal: Deal, stage: PipelineStage) {
|
|
85
|
+
// Cascade to lead
|
|
86
|
+
if (deal.leadId) {
|
|
87
|
+
const lead = store.leads.find((l) => l.id === deal.leadId)
|
|
88
|
+
if (lead) {
|
|
89
|
+
lead.status = deriveLeadStatus(stage)
|
|
90
|
+
lead.updatedAt = now()
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// Cascade to quotes
|
|
94
|
+
for (const q of store.quotes) {
|
|
95
|
+
if (q.dealId === deal.id) {
|
|
96
|
+
q.status = deriveQuoteStatus(stage, q.status)
|
|
97
|
+
q.updatedAt = now()
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const provider: CrmDataProvider = {
|
|
103
|
+
async getPipelines() { return store.pipelines },
|
|
104
|
+
async getPipelineStages(pipelineId) { return store.stages.filter((s) => s.pipelineId === pipelineId).sort((a, b) => a.order - b.order) },
|
|
105
|
+
|
|
106
|
+
// Leads
|
|
107
|
+
async getLeads(query: LeadQuery) {
|
|
108
|
+
let results = [...store.leads]
|
|
109
|
+
if (query.status) {
|
|
110
|
+
const statuses = Array.isArray(query.status) ? query.status : [query.status]
|
|
111
|
+
results = results.filter((l) => statuses.includes(l.status))
|
|
112
|
+
}
|
|
113
|
+
if (query.sourceId) results = results.filter((l) => l.sourceId === query.sourceId)
|
|
114
|
+
if (query.search) {
|
|
115
|
+
const s = query.search.toLowerCase()
|
|
116
|
+
results = results.filter((l) => l.name.toLowerCase().includes(s) || l.email?.toLowerCase().includes(s) || l.company?.toLowerCase().includes(s))
|
|
117
|
+
}
|
|
118
|
+
results.sort((a, b) => b.createdAt.localeCompare(a.createdAt))
|
|
119
|
+
return paginate(results, query.page, query.pageSize)
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
async getLeadById(id) { return store.leads.find((l) => l.id === id) ?? null },
|
|
123
|
+
|
|
124
|
+
async createLead(input: CreateLeadInput) {
|
|
125
|
+
const lead: Lead = {
|
|
126
|
+
id: uid(), name: input.name, email: input.email, phone: input.phone,
|
|
127
|
+
company: input.company, sourceId: input.sourceId, status: 'new',
|
|
128
|
+
assignedToId: input.assignedToId, value: input.value,
|
|
129
|
+
tags: input.tags ?? [], notes: input.notes, tenantId,
|
|
130
|
+
createdAt: now(), updatedAt: now(),
|
|
131
|
+
}
|
|
132
|
+
store.leads.push(lead)
|
|
133
|
+
return lead
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
async updateLead(id, data) {
|
|
137
|
+
const lead = store.leads.find((l) => l.id === id)
|
|
138
|
+
if (!lead) throw new Error(`Lead ${id} not found`)
|
|
139
|
+
Object.assign(lead, data, { updatedAt: now() })
|
|
140
|
+
return lead
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
async convertLeadToDeal(leadId, dealInput) {
|
|
144
|
+
const lead = store.leads.find((l) => l.id === leadId)
|
|
145
|
+
if (!lead) throw new Error(`Lead ${leadId} not found`)
|
|
146
|
+
lead.status = 'converted'
|
|
147
|
+
lead.updatedAt = now()
|
|
148
|
+
const deal = await provider.createDeal({ ...dealInput, leadId })
|
|
149
|
+
return deal
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
// Deals
|
|
153
|
+
async getDeals(query: DealQuery) {
|
|
154
|
+
let results = [...store.deals]
|
|
155
|
+
if (query.pipelineId) results = results.filter((d) => d.pipelineId === query.pipelineId)
|
|
156
|
+
if (query.stageId) results = results.filter((d) => d.stageId === query.stageId)
|
|
157
|
+
if (query.status) {
|
|
158
|
+
const statuses = Array.isArray(query.status) ? query.status : [query.status]
|
|
159
|
+
results = results.filter((d) => statuses.includes(d.status))
|
|
160
|
+
}
|
|
161
|
+
if (query.search) {
|
|
162
|
+
const s = query.search.toLowerCase()
|
|
163
|
+
results = results.filter((d) => d.title.toLowerCase().includes(s) || d.contactName?.toLowerCase().includes(s))
|
|
164
|
+
}
|
|
165
|
+
results.sort((a, b) => b.createdAt.localeCompare(a.createdAt))
|
|
166
|
+
return paginate(results, query.page, query.pageSize)
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
async getDealById(id) { return store.deals.find((d) => d.id === id) ?? null },
|
|
170
|
+
|
|
171
|
+
async getDealsByStage(pipelineId) {
|
|
172
|
+
const stages = store.stages.filter((s) => s.pipelineId === pipelineId).sort((a, b) => a.order - b.order)
|
|
173
|
+
const map = new Map<string, Deal[]>()
|
|
174
|
+
for (const stage of stages) {
|
|
175
|
+
map.set(stage.id, store.deals.filter((d) => d.stageId === stage.id && d.status === 'open'))
|
|
176
|
+
}
|
|
177
|
+
return map
|
|
178
|
+
},
|
|
179
|
+
|
|
180
|
+
async createDeal(input: CreateDealInput) {
|
|
181
|
+
const stage = store.stages.find((s) => s.id === input.stageId)
|
|
182
|
+
const deal: Deal = {
|
|
183
|
+
id: uid(), leadId: input.leadId, title: input.title, value: input.value,
|
|
184
|
+
pipelineId: input.pipelineId, stageId: input.stageId,
|
|
185
|
+
stageName: stage?.name, stageColor: stage?.color,
|
|
186
|
+
probability: stage?.probability ?? 0, expectedCloseDate: input.expectedCloseDate,
|
|
187
|
+
assignedToId: input.assignedToId, contactId: input.contactId,
|
|
188
|
+
contactName: input.contactName, status: 'open',
|
|
189
|
+
tags: input.tags ?? [], notes: input.notes, tenantId,
|
|
190
|
+
createdAt: now(), updatedAt: now(),
|
|
191
|
+
}
|
|
192
|
+
store.deals.push(deal)
|
|
193
|
+
return deal
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
async updateDeal(id, data) {
|
|
197
|
+
const deal = store.deals.find((d) => d.id === id)
|
|
198
|
+
if (!deal) throw new Error(`Deal ${id} not found`)
|
|
199
|
+
Object.assign(deal, data, { updatedAt: now() })
|
|
200
|
+
return deal
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
async moveDealToStage(dealId, stageId) {
|
|
204
|
+
const deal = store.deals.find((d) => d.id === dealId)
|
|
205
|
+
if (!deal) throw new Error(`Deal ${dealId} not found`)
|
|
206
|
+
const stage = store.stages.find((s) => s.id === stageId)
|
|
207
|
+
if (!stage) throw new Error(`Stage ${stageId} not found`)
|
|
208
|
+
deal.stageId = stageId
|
|
209
|
+
deal.stageName = stage.name
|
|
210
|
+
deal.stageColor = stage.color
|
|
211
|
+
deal.probability = stage.probability ?? 0
|
|
212
|
+
if (stage.isWon) deal.status = 'won'
|
|
213
|
+
if (stage.isLost) deal.status = 'lost'
|
|
214
|
+
deal.updatedAt = now()
|
|
215
|
+
|
|
216
|
+
// Cascade to lead + quotes
|
|
217
|
+
cascadeFromStage(deal, stage)
|
|
218
|
+
|
|
219
|
+
return deal
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
// Activities
|
|
223
|
+
async getActivities(query: ActivityQuery) {
|
|
224
|
+
let results = [...store.activities]
|
|
225
|
+
if (query.dealId) results = results.filter((a) => a.dealId === query.dealId)
|
|
226
|
+
if (query.leadId) results = results.filter((a) => a.leadId === query.leadId)
|
|
227
|
+
if (query.activityType) results = results.filter((a) => a.activityType === query.activityType)
|
|
228
|
+
if (query.completed !== undefined) {
|
|
229
|
+
results = query.completed ? results.filter((a) => a.completedAt) : results.filter((a) => !a.completedAt)
|
|
230
|
+
}
|
|
231
|
+
results.sort((a, b) => b.createdAt.localeCompare(a.createdAt))
|
|
232
|
+
return paginate(results, query.page, query.pageSize)
|
|
233
|
+
},
|
|
234
|
+
|
|
235
|
+
async createActivity(input: CreateActivityInput) {
|
|
236
|
+
const activity: Activity = {
|
|
237
|
+
id: uid(), dealId: input.dealId, leadId: input.leadId,
|
|
238
|
+
contactId: input.contactId, activityType: input.activityType,
|
|
239
|
+
title: input.title, description: input.description,
|
|
240
|
+
dueDate: input.dueDate, assignedToId: input.assignedToId,
|
|
241
|
+
tenantId, createdAt: now(),
|
|
242
|
+
}
|
|
243
|
+
store.activities.push(activity)
|
|
244
|
+
return activity
|
|
245
|
+
},
|
|
246
|
+
|
|
247
|
+
async completeActivity(id) {
|
|
248
|
+
const activity = store.activities.find((a) => a.id === id)
|
|
249
|
+
if (!activity) throw new Error(`Activity ${id} not found`)
|
|
250
|
+
activity.completedAt = now()
|
|
251
|
+
return activity
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
// Quotes
|
|
255
|
+
async getQuotes(query: QuoteQuery) {
|
|
256
|
+
let results = [...store.quotes]
|
|
257
|
+
if (query.status) {
|
|
258
|
+
const statuses = Array.isArray(query.status) ? query.status : [query.status]
|
|
259
|
+
results = results.filter((q) => statuses.includes(q.status))
|
|
260
|
+
}
|
|
261
|
+
if (query.contactId) results = results.filter((q) => q.contactId === query.contactId)
|
|
262
|
+
if (query.search) {
|
|
263
|
+
const s = query.search.toLowerCase()
|
|
264
|
+
results = results.filter((q) => q.quoteNumber.includes(s) || q.contactName?.toLowerCase().includes(s))
|
|
265
|
+
}
|
|
266
|
+
results.sort((a, b) => b.createdAt.localeCompare(a.createdAt))
|
|
267
|
+
return paginate(results, query.page, query.pageSize)
|
|
268
|
+
},
|
|
269
|
+
|
|
270
|
+
async getQuoteById(id) { return store.quotes.find((q) => q.id === id) ?? null },
|
|
271
|
+
|
|
272
|
+
async createQuote(input: CreateQuoteInput) {
|
|
273
|
+
const quoteId = uid()
|
|
274
|
+
const items: QuoteItem[] = input.items.map((item) => ({
|
|
275
|
+
id: uid(), quoteId, ...item, createdAt: now(),
|
|
276
|
+
}))
|
|
277
|
+
const totalAmount = items.reduce((sum, i) => sum + i.totalAmount, 0)
|
|
278
|
+
const quote: Quote = {
|
|
279
|
+
id: quoteId, dealId: input.dealId, leadId: input.leadId,
|
|
280
|
+
contactId: input.contactId,
|
|
281
|
+
contactName: input.contactName, quoteNumber: `Q-${String(store.quotes.length + 1).padStart(4, '0')}`,
|
|
282
|
+
quoteDate: input.quoteDate, validUntil: input.validUntil,
|
|
283
|
+
status: 'draft', totalAmount, observations: input.observations,
|
|
284
|
+
paymentConditions: input.paymentConditions, items, tenantId,
|
|
285
|
+
createdAt: now(), updatedAt: now(),
|
|
286
|
+
}
|
|
287
|
+
store.quotes.push(quote)
|
|
288
|
+
return quote
|
|
289
|
+
},
|
|
290
|
+
|
|
291
|
+
async updateQuote(id, input: CreateQuoteInput) {
|
|
292
|
+
const quote = store.quotes.find((q) => q.id === id)
|
|
293
|
+
if (!quote) throw new Error(`Quote ${id} not found`)
|
|
294
|
+
const items: QuoteItem[] = input.items.map((item) => ({
|
|
295
|
+
id: uid(), quoteId: id, ...item, createdAt: now(),
|
|
296
|
+
}))
|
|
297
|
+
const totalAmount = items.reduce((sum, i) => sum + i.totalAmount, 0)
|
|
298
|
+
quote.contactId = input.contactId
|
|
299
|
+
quote.contactName = input.contactName
|
|
300
|
+
quote.dealId = input.dealId
|
|
301
|
+
quote.leadId = input.leadId
|
|
302
|
+
quote.quoteDate = input.quoteDate
|
|
303
|
+
quote.validUntil = input.validUntil
|
|
304
|
+
quote.observations = input.observations
|
|
305
|
+
quote.paymentConditions = input.paymentConditions
|
|
306
|
+
quote.items = items
|
|
307
|
+
quote.totalAmount = totalAmount
|
|
308
|
+
quote.updatedAt = now()
|
|
309
|
+
return quote
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
async sendQuote(id) {
|
|
313
|
+
const quote = store.quotes.find((q) => q.id === id)
|
|
314
|
+
if (!quote) throw new Error(`Quote ${id} not found`)
|
|
315
|
+
quote.status = 'sent'
|
|
316
|
+
quote.updatedAt = now()
|
|
317
|
+
return quote
|
|
318
|
+
},
|
|
319
|
+
|
|
320
|
+
async approveQuote(id) {
|
|
321
|
+
const quote = store.quotes.find((q) => q.id === id)
|
|
322
|
+
if (!quote) throw new Error(`Quote ${id} not found`)
|
|
323
|
+
quote.status = 'approved'
|
|
324
|
+
quote.updatedAt = now()
|
|
325
|
+
|
|
326
|
+
// Cascade: move deal to Won stage
|
|
327
|
+
if (quote.dealId) {
|
|
328
|
+
const deal = store.deals.find((d) => d.id === quote.dealId)
|
|
329
|
+
if (deal) {
|
|
330
|
+
const wonStage = store.stages.find((s) => s.pipelineId === deal.pipelineId && s.isWon)
|
|
331
|
+
if (wonStage) {
|
|
332
|
+
await provider.moveDealToStage(deal.id, wonStage.id)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Generate mock invoice
|
|
338
|
+
quote.convertedInvoiceId = uid()
|
|
339
|
+
|
|
340
|
+
// Convert lead to client
|
|
341
|
+
if (quote.leadId) {
|
|
342
|
+
const lead = store.leads.find((l) => l.id === quote.leadId)
|
|
343
|
+
if (lead) {
|
|
344
|
+
lead.metadata = { ...lead.metadata, convertedFromLead: true }
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return quote
|
|
349
|
+
},
|
|
350
|
+
|
|
351
|
+
async rejectQuote(id, reason) {
|
|
352
|
+
const quote = store.quotes.find((q) => q.id === id)
|
|
353
|
+
if (!quote) throw new Error(`Quote ${id} not found`)
|
|
354
|
+
quote.status = 'rejected'
|
|
355
|
+
quote.rejectionReason = reason
|
|
356
|
+
quote.updatedAt = now()
|
|
357
|
+
|
|
358
|
+
// Cascade: move deal to Lost stage
|
|
359
|
+
if (quote.dealId) {
|
|
360
|
+
const deal = store.deals.find((d) => d.id === quote.dealId)
|
|
361
|
+
if (deal) {
|
|
362
|
+
const lostStage = store.stages.find((s) => s.pipelineId === deal.pipelineId && s.isLost)
|
|
363
|
+
if (lostStage) {
|
|
364
|
+
await provider.moveDealToStage(deal.id, lostStage.id)
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return quote
|
|
369
|
+
},
|
|
370
|
+
|
|
371
|
+
async expireQuote(id) {
|
|
372
|
+
const quote = store.quotes.find((q) => q.id === id)
|
|
373
|
+
if (!quote) throw new Error(`Quote ${id} not found`)
|
|
374
|
+
quote.status = 'expired'
|
|
375
|
+
quote.updatedAt = now()
|
|
376
|
+
return quote
|
|
377
|
+
},
|
|
378
|
+
|
|
379
|
+
// Cross-entity lookups
|
|
380
|
+
async getQuotesByDealId(dealId) {
|
|
381
|
+
return store.quotes.filter((q) => q.dealId === dealId)
|
|
382
|
+
},
|
|
383
|
+
|
|
384
|
+
async getDealByLeadId(leadId) {
|
|
385
|
+
return store.deals.find((d) => d.leadId === leadId) ?? null
|
|
386
|
+
},
|
|
387
|
+
|
|
388
|
+
// Summary
|
|
389
|
+
async getSummary(): Promise<CrmSummary> {
|
|
390
|
+
const monthStart = today().slice(0, 7) + '-01'
|
|
391
|
+
const monthLeads = store.leads.filter((l) => l.createdAt >= monthStart)
|
|
392
|
+
const wonDeals = store.deals.filter((d) => d.status === 'won')
|
|
393
|
+
const wonThisMonth = wonDeals.filter((d) => d.updatedAt >= monthStart)
|
|
394
|
+
const openDeals = store.deals.filter((d) => d.status === 'open')
|
|
395
|
+
const totalConverted = store.leads.filter((l) => l.status === 'converted').length
|
|
396
|
+
const pendingActivities = store.activities.filter((a) => !a.completedAt)
|
|
397
|
+
const overdueActivities = pendingActivities.filter((a) => a.dueDate && a.dueDate < today())
|
|
398
|
+
|
|
399
|
+
return {
|
|
400
|
+
totalLeads: store.leads.length,
|
|
401
|
+
newLeadsThisMonth: monthLeads.length,
|
|
402
|
+
totalDeals: store.deals.length,
|
|
403
|
+
openDealsValue: openDeals.reduce((s, d) => s + d.value, 0),
|
|
404
|
+
wonDealsThisMonth: wonThisMonth.length,
|
|
405
|
+
wonDealsValueThisMonth: wonThisMonth.reduce((s, d) => s + d.value, 0),
|
|
406
|
+
conversionRate: store.leads.length > 0 ? (totalConverted / store.leads.length) * 100 : 0,
|
|
407
|
+
averageDealValue: wonDeals.length > 0 ? wonDeals.reduce((s, d) => s + d.value, 0) / wonDeals.length : 0,
|
|
408
|
+
pendingActivities: pendingActivities.length,
|
|
409
|
+
overdueActivities: overdueActivities.length,
|
|
410
|
+
}
|
|
411
|
+
},
|
|
412
|
+
|
|
413
|
+
async getFunnel(pipelineId) {
|
|
414
|
+
const stages = store.stages.filter((s) => s.pipelineId === pipelineId && !s.isLost).sort((a, b) => a.order - b.order)
|
|
415
|
+
return stages.map((stage) => {
|
|
416
|
+
const deals = store.deals.filter((d) => d.stageId === stage.id)
|
|
417
|
+
return {
|
|
418
|
+
stageId: stage.id, stageName: stage.name, stageColor: stage.color,
|
|
419
|
+
dealCount: deals.length, totalValue: deals.reduce((s, d) => s + d.value, 0),
|
|
420
|
+
}
|
|
421
|
+
})
|
|
422
|
+
},
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return provider
|
|
426
|
+
}
|