@baack-software/baack-ts-crm 0.0.2 → 0.0.4

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 (112) hide show
  1. package/dist/backend.d.ts +29 -1
  2. package/dist/backend.d.ts.map +1 -1
  3. package/dist/backend.js +95 -0
  4. package/dist/backend.js.map +1 -1
  5. package/dist/crm-client.d.ts +55 -13
  6. package/dist/crm-client.d.ts.map +1 -1
  7. package/dist/crm-client.js +99 -15
  8. package/dist/crm-client.js.map +1 -1
  9. package/dist/deal-order.d.ts +42 -0
  10. package/dist/deal-order.d.ts.map +1 -0
  11. package/dist/deal-order.js +80 -0
  12. package/dist/deal-order.js.map +1 -0
  13. package/dist/index.d.ts +1 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -0
  16. package/dist/index.js.map +1 -1
  17. package/dist/invoices.d.ts +7 -0
  18. package/dist/invoices.d.ts.map +1 -1
  19. package/dist/invoices.js +20 -2
  20. package/dist/invoices.js.map +1 -1
  21. package/dist/period.d.ts +15 -2
  22. package/dist/period.d.ts.map +1 -1
  23. package/dist/period.js +27 -1
  24. package/dist/period.js.map +1 -1
  25. package/dist/pipeline.d.ts +6 -0
  26. package/dist/pipeline.d.ts.map +1 -1
  27. package/dist/pipeline.js +18 -0
  28. package/dist/pipeline.js.map +1 -1
  29. package/dist/react/ChoiceCombobox.d.ts +36 -0
  30. package/dist/react/ChoiceCombobox.d.ts.map +1 -0
  31. package/dist/react/ChoiceCombobox.js +105 -0
  32. package/dist/react/ChoiceCombobox.js.map +1 -0
  33. package/dist/react/DealsBoard.d.ts +22 -3
  34. package/dist/react/DealsBoard.d.ts.map +1 -1
  35. package/dist/react/DealsBoard.js +53 -4
  36. package/dist/react/DealsBoard.js.map +1 -1
  37. package/dist/react/Details.d.ts +40 -7
  38. package/dist/react/Details.d.ts.map +1 -1
  39. package/dist/react/Details.js +180 -23
  40. package/dist/react/Details.js.map +1 -1
  41. package/dist/react/Forms.d.ts +148 -17
  42. package/dist/react/Forms.d.ts.map +1 -1
  43. package/dist/react/Forms.js +365 -75
  44. package/dist/react/Forms.js.map +1 -1
  45. package/dist/react/Sales.d.ts +25 -6
  46. package/dist/react/Sales.d.ts.map +1 -1
  47. package/dist/react/Sales.js +114 -30
  48. package/dist/react/Sales.js.map +1 -1
  49. package/dist/react/SidePanel.d.ts +10 -0
  50. package/dist/react/SidePanel.d.ts.map +1 -1
  51. package/dist/react/SidePanel.js +18 -3
  52. package/dist/react/SidePanel.js.map +1 -1
  53. package/dist/react/index.d.ts +1 -0
  54. package/dist/react/index.d.ts.map +1 -1
  55. package/dist/react/index.js +1 -0
  56. package/dist/react/index.js.map +1 -1
  57. package/dist/react/parts.d.ts +12 -2
  58. package/dist/react/parts.d.ts.map +1 -1
  59. package/dist/react/parts.js +25 -2
  60. package/dist/react/parts.js.map +1 -1
  61. package/dist/react/pipeline-choice.d.ts +19 -0
  62. package/dist/react/pipeline-choice.d.ts.map +1 -0
  63. package/dist/react/pipeline-choice.js +39 -0
  64. package/dist/react/pipeline-choice.js.map +1 -0
  65. package/dist/react/text.d.ts +89 -2
  66. package/dist/react/text.d.ts.map +1 -1
  67. package/dist/react/text.js +89 -2
  68. package/dist/react/text.js.map +1 -1
  69. package/dist/react/ui/badge.d.ts +1 -1
  70. package/dist/react/ui/button.d.ts +2 -2
  71. package/dist/react/views.d.ts +33 -11
  72. package/dist/react/views.d.ts.map +1 -1
  73. package/dist/react/views.js +96 -27
  74. package/dist/react/views.js.map +1 -1
  75. package/dist/sales.d.ts +28 -3
  76. package/dist/sales.d.ts.map +1 -1
  77. package/dist/sales.js +33 -10
  78. package/dist/sales.js.map +1 -1
  79. package/dist/store.d.ts +31 -12
  80. package/dist/store.d.ts.map +1 -1
  81. package/dist/store.js +82 -21
  82. package/dist/store.js.map +1 -1
  83. package/dist/target.d.ts +2 -1
  84. package/dist/target.d.ts.map +1 -1
  85. package/dist/target.js +6 -2
  86. package/dist/target.js.map +1 -1
  87. package/dist/types.d.ts +24 -0
  88. package/dist/types.d.ts.map +1 -1
  89. package/dist/types.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/backend.ts +131 -1
  92. package/src/crm-client.ts +162 -19
  93. package/src/deal-order.ts +116 -0
  94. package/src/index.ts +1 -0
  95. package/src/invoices.ts +22 -2
  96. package/src/period.ts +41 -2
  97. package/src/pipeline.ts +21 -0
  98. package/src/react/ChoiceCombobox.tsx +227 -0
  99. package/src/react/DealsBoard.tsx +171 -4
  100. package/src/react/Details.tsx +608 -31
  101. package/src/react/Forms.tsx +1006 -139
  102. package/src/react/Sales.tsx +332 -44
  103. package/src/react/SidePanel.tsx +22 -11
  104. package/src/react/index.ts +1 -0
  105. package/src/react/parts.tsx +56 -4
  106. package/src/react/pipeline-choice.ts +54 -0
  107. package/src/react/text.ts +90 -2
  108. package/src/react/views.tsx +222 -32
  109. package/src/sales.ts +57 -10
  110. package/src/store.ts +136 -27
  111. package/src/target.ts +5 -2
  112. package/src/types.ts +28 -0
package/src/store.ts CHANGED
@@ -2,23 +2,33 @@ import type { Company } from '@baack-software/baack-ts-client';
2
2
  import { urnOf, type RefLike } from '@baack-software/baack-ts-tasks';
3
3
  import { companyAccount, type CompanyAccount } from './account.ts';
4
4
  import type { DealInput, InvoiceInput, LeadInput, RemittanceInput } from './backend.ts';
5
- import type { ConvertOptions, CrmClient, NewLeadOptions } from './crm-client.ts';
5
+ import type {
6
+ ConvertOptions,
7
+ CrmClient,
8
+ InvoicePartyOptions,
9
+ InvoiceSaved,
10
+ NewInvoiceOptions,
11
+ NewLeadOptions,
12
+ } from './crm-client.ts';
6
13
  import { CrmUnavailableError } from './errors.ts';
7
14
  import { leadsBySource, type SourceCount } from './leads.ts';
8
15
  import { addMoney, type MoneyTotals } from './money.ts';
9
16
  import { inRange, periodChange, previousRange, type DateRange } from './period.ts';
10
- import { dealCompany, dealOutcome, pipelineByStage, type StageSummary } from './pipeline.ts';
17
+ import { dealCompany, dealOutcome, defaultPipeline, pipelineByStage, type StageSummary } from './pipeline.ts';
11
18
  import { paymentsByDay, paymentsReceived } from './invoices.ts';
12
- import { salesSummary, type SalesSummary } from './sales.ts';
19
+ import { salesRemittances, salesSummary, type SalesSummary } from './sales.ts';
13
20
  import { salesTarget, type SalesTarget } from './target.ts';
14
21
  import {
15
22
  CRM_CONCEPTS,
23
+ type ContactDetails,
16
24
  type CrmConcept,
25
+ type CrmPerson,
17
26
  type Deal,
18
27
  type DealOutcome,
19
28
  type Invoice,
20
29
  type Lead,
21
30
  type LeadOutcome,
31
+ type NewContact,
22
32
  type Pipeline,
23
33
  type PipelineStage,
24
34
  type Remittance,
@@ -49,13 +59,15 @@ export interface CrmStoreState {
49
59
  remittances: readonly Remittance[];
50
60
  /** Client companies made while the store was open, such as a converted lead's prospect, with their names. */
51
61
  companies: readonly Company[];
62
+ /** People made while the store was open, such as a lead's new contact, with their names. */
63
+ people: readonly CrmPerson[];
52
64
  /** URNs of records with a write in flight. */
53
65
  saving: ReadonlySet<string>;
54
66
  /** The last write the server refused, until the next write. */
55
67
  writeError?: unknown;
56
68
  }
57
69
 
58
- type Records = Pick<CrmStoreState, CrmConcept | 'companies'>;
70
+ type Records = Pick<CrmStoreState, CrmConcept | 'companies' | 'people'>;
59
71
 
60
72
  const idle = Object.fromEntries(CRM_CONCEPTS.map((concept) => [concept, { status: 'idle' }])) as Record<
61
73
  CrmConcept,
@@ -71,11 +83,27 @@ const EMPTY: CrmStoreState = {
71
83
  invoices: [],
72
84
  remittances: [],
73
85
  companies: [],
86
+ people: [],
74
87
  saving: new Set(),
75
88
  };
76
89
 
77
90
  let pendingCount = 0;
78
91
 
92
+ /** The companies and people saving an invoice made, added to those held. */
93
+ const madeForInvoice = ({ companies, people }: Records, result: Omit<InvoiceSaved, 'invoice'>): Partial<Records> => ({
94
+ ...(result.companies.length ? { companies: [...companies, ...result.companies] } : {}),
95
+ ...(result.people.length ? { people: [...people, ...result.people] } : {}),
96
+ });
97
+
98
+ /** The company and person a lead's save made, added to those the store holds. */
99
+ const made = (
100
+ { companies, people }: Records,
101
+ result: { company?: Company | undefined; person?: CrmPerson | undefined },
102
+ ): Partial<Records> => ({
103
+ ...(result.company ? { companies: [...companies, result.company] } : {}),
104
+ ...(result.person ? { people: [...people, result.person] } : {}),
105
+ });
106
+
79
107
  /**
80
108
  * Holds a company's CRM records for a UI: loads every concept, applies changes straight away and rolls them back if
81
109
  * the server refuses, and notifies subscribers. `subscribe` and `getState` fit React's `useSyncExternalStore`.
@@ -168,13 +196,13 @@ export class CrmStore {
168
196
  }
169
197
 
170
198
  #records(): Records {
171
- const { leads, deals, pipelines, invoices, remittances, companies } = this.#state;
172
- return { leads, deals, pipelines, invoices, remittances, companies };
199
+ const { leads, deals, pipelines, invoices, remittances, companies, people } = this.#state;
200
+ return { leads, deals, pipelines, invoices, remittances, companies, people };
173
201
  }
174
202
 
175
203
  /**
176
- * Creates a lead, shown at the top straight away. With `companyName`, the prospect's company is made first and linked
177
- * (see `CrmClient.createLead`).
204
+ * Creates a lead, shown at the top straight away. With `companyName` or `contact`, the prospect's company or contact
205
+ * is made first and linked (see `CrmClient.createLead`).
178
206
  */
179
207
  async createLead(input: LeadInput, options: NewLeadOptions = {}): Promise<Lead> {
180
208
  const temporary = `pending-${++pendingCount}`;
@@ -183,22 +211,48 @@ export class CrmStore {
183
211
  [temporary],
184
212
  ({ leads }) => ({ leads: [placeholder, ...leads] }),
185
213
  () => this.client.createLead(input, options),
186
- ({ leads, companies }, result) => ({
187
- leads: leads.map((candidate) => (candidate.urn === temporary ? result.lead : candidate)),
188
- ...(result.company ? { companies: [...companies, result.company] } : {}),
214
+ (records, result) => ({
215
+ leads: records.leads.map((candidate) => (candidate.urn === temporary ? result.lead : candidate)),
216
+ ...made(records, result),
189
217
  }),
190
218
  );
191
219
  return lead;
192
220
  }
193
221
 
194
- /** Changes a lead. */
195
- updateLead(lead: RefLike, changes: Partial<LeadInput>): Promise<Lead> {
222
+ /**
223
+ * Changes a lead, shown at once. With `companyName` or `contact`, its new company or contact is made first, as for
224
+ * a new lead (see `CrmClient.updateLead`).
225
+ */
226
+ async updateLead(lead: RefLike, changes: Partial<LeadInput>, options: NewLeadOptions = {}): Promise<Lead> {
196
227
  const urn = urnOf(lead);
197
- return this.#write(
228
+ const current = this.#state.leads.find((candidate) => candidate.urn === urn);
229
+ if (!current) throw new Error(`No lead ${urn} in the store`);
230
+ const result = await this.#write(
198
231
  [urn],
199
- ({ leads }) => ({ leads: replace(leads, urn, (current) => ({ ...current, ...changes })) }),
200
- () => this.client.leads.update(urn, changes),
201
- ({ leads }, updated) => ({ leads: replace(leads, urn, () => updated) }),
232
+ ({ leads }) => ({ leads: replace(leads, urn, (shown) => ({ ...shown, ...changes })) }),
233
+ () => this.client.updateLead(current, changes, options),
234
+ (records, saved) => ({ leads: replace(records.leads, urn, () => saved.lead), ...made(records, saved) }),
235
+ );
236
+ return result.lead;
237
+ }
238
+
239
+ /** A person's email and telephone, read when asked for: they aren't held in the store's state. */
240
+ contactDetails(identity: RefLike): Promise<ContactDetails> {
241
+ return this.client.contactDetails(identity);
242
+ }
243
+
244
+ /** Sets a person's email and telephone; fields left out are kept. */
245
+ setContactDetails(identity: RefLike, details: Partial<ContactDetails>): Promise<ContactDetails> {
246
+ return this.client.setContactDetails(identity, details);
247
+ }
248
+
249
+ /** Makes a person owned by the store's company: a customer's person when `company` is given. */
250
+ createContact(contact: NewContact, company?: RefLike | null): Promise<CrmPerson> {
251
+ return this.#write(
252
+ [],
253
+ () => ({}),
254
+ () => this.client.createContact(this.options.owner, contact, company),
255
+ ({ people }, person) => ({ people: [...people, person] }),
202
256
  );
203
257
  }
204
258
 
@@ -230,6 +284,46 @@ export class CrmStore {
230
284
  );
231
285
  }
232
286
 
287
+ /** Changes a deal's fields, shown at once. Moving it between stages is `moveDeal`; its outcome, `setOutcome`. */
288
+ updateDeal(deal: RefLike, changes: Partial<DealInput>): Promise<Deal> {
289
+ const urn = urnOf(deal);
290
+ return this.#write(
291
+ [urn],
292
+ ({ deals }) => ({ deals: replace(deals, urn, (current) => ({ ...current, ...changes })) }),
293
+ () => this.client.deals.update(urn, changes),
294
+ ({ deals }, stored) => ({ deals: replace(deals, urn, () => stored) }),
295
+ );
296
+ }
297
+
298
+ /**
299
+ * Changes an invoice's own fields (status, parties, dates), shown at once, making a party's new company and person
300
+ * first when `options` asks (see `CrmClient.updateInvoice`). Its lines and payments aren't changed by an update, so
301
+ * they're kept as they were when the server's answer leaves them out or has none.
302
+ */
303
+ async updateInvoice(
304
+ invoice: RefLike,
305
+ changes: Partial<InvoiceInput>,
306
+ options: InvoicePartyOptions = {},
307
+ ): Promise<Invoice> {
308
+ const urn = urnOf(invoice);
309
+ const current = this.#state.invoices.find((candidate) => candidate.urn === urn);
310
+ if (!current) throw new Error(`No invoice ${urn} in the store`);
311
+ const result = await this.#write(
312
+ [urn],
313
+ ({ invoices }) => ({ invoices: replace(invoices, urn, (shown) => ({ ...shown, ...changes })) }),
314
+ () => this.client.updateInvoice(current, changes, options),
315
+ (records, saved) => ({
316
+ invoices: replace(records.invoices, urn, (shown) => {
317
+ const lines = saved.invoice.lines?.length ? saved.invoice.lines : shown.lines;
318
+ const remittances = saved.invoice.remittances?.length ? saved.invoice.remittances : shown.remittances;
319
+ return { ...saved.invoice, ...(lines ? { lines } : {}), ...(remittances ? { remittances } : {}) };
320
+ }),
321
+ ...madeForInvoice(records, saved),
322
+ }),
323
+ );
324
+ return result.invoice;
325
+ }
326
+
233
327
  /** Wins, loses or reopens a deal, shown at once (see `CrmClient.setOutcome`). */
234
328
  setOutcome(deal: RefLike, outcome: DealOutcome, options: { lostReason?: string } = {}): Promise<Deal> {
235
329
  const urn = urnOf(deal);
@@ -275,15 +369,16 @@ export class CrmStore {
275
369
  async createInvoice(
276
370
  input: InvoiceInput,
277
371
  lines: Parameters<CrmClient['createInvoice']>[1] = [],
278
- options: { deal?: Deal } = {},
372
+ options: NewInvoiceOptions = {},
279
373
  ): Promise<Invoice> {
280
374
  const { invoice } = await this.#write(
281
375
  options.deal ? [options.deal.urn] : [],
282
376
  () => ({}),
283
377
  () => this.client.createInvoice(input, lines, options),
284
- ({ invoices, deals }, result) => ({
285
- invoices: [result.invoice, ...invoices],
286
- ...(result.deal ? { deals: replace(deals, result.deal.urn, () => result.deal!) } : {}),
378
+ (records, result) => ({
379
+ invoices: [result.invoice, ...records.invoices],
380
+ ...(result.deal ? { deals: replace(records.deals, result.deal.urn, () => result.deal!) } : {}),
381
+ ...madeForInvoice(records, result),
287
382
  }),
288
383
  );
289
384
  return invoice;
@@ -347,9 +442,18 @@ export const mainCurrency = (totals: MoneyTotals): string | undefined =>
347
442
  Object.entries(totals).sort(([, a], [, b]) => b - a)[0]?.[0];
348
443
 
349
444
  /** Everything a CRM dashboard shows for a period, compared with the period before. */
350
- export function selectDashboard(state: CrmStoreState, range: DateRange): Dashboard {
445
+ export function selectDashboard(
446
+ state: CrmStoreState,
447
+ range: DateRange,
448
+ own?: ReadonlySet<string>,
449
+ /** The user's chosen pipeline, for the pipeline card (see `defaultPipeline`). */
450
+ chosenPipeline?: string,
451
+ ): Dashboard {
351
452
  const before = previousRange(range);
352
- const { leads, deals, pipelines, remittances } = state;
453
+ const { leads, deals, pipelines } = state;
454
+ const shownPipeline = defaultPipeline(pipelines, deals, chosenPipeline);
455
+ // Revenue is what customers paid: payments the business made on its costs aren't counted.
456
+ const remittances = salesRemittances(state, own);
353
457
 
354
458
  const firstWins = new Map<string, string>();
355
459
  let openValue: MoneyTotals = {};
@@ -404,7 +508,7 @@ export function selectDashboard(state: CrmStoreState, range: DateRange): Dashboa
404
508
  ),
405
509
  target: salesTarget(leads, deals, remittances),
406
510
  leadsBySource: leadsBySource(leads),
407
- pipeline: pipelines[0] ? pipelineByStage(deals, pipelines[0]) : [],
511
+ pipeline: shownPipeline ? pipelineByStage(deals, shownPipeline) : [],
408
512
  };
409
513
  }
410
514
 
@@ -413,7 +517,12 @@ export function selectAccount(state: CrmStoreState, company: RefLike, now = new
413
517
  return companyAccount(urnOf(company), state, now);
414
518
  }
415
519
 
416
- /** Sales for a period from the store's invoices and payments (see `salesSummary`). */
417
- export function selectSales(state: CrmStoreState, range: DateRange, now = new Date()): SalesSummary {
418
- return salesSummary(state, range, now);
520
+ /** Sales for a period from the store's invoices and payments, costs apart (see `salesSummary`). */
521
+ export function selectSales(
522
+ state: CrmStoreState,
523
+ range: DateRange,
524
+ now = new Date(),
525
+ own?: ReadonlySet<string>,
526
+ ): SalesSummary {
527
+ return salesSummary(state, range, now, own);
419
528
  }
package/src/target.ts CHANGED
@@ -18,7 +18,8 @@ export interface SalesTarget {
18
18
  /**
19
19
  * The sales target and progress towards it. The target is the sum of the leads' target values, using the deal's target
20
20
  * value instead once a lead is qualified into a deal. Deals created without a lead count too, as they're qualified
21
- * already; unqualified leads don't. Lost deals stay in the target, so losing one doesn't make progress look better.
21
+ * already; unqualified leads don't, and neither do lost deals or the leads they came from (decided 27 Oct 2026, in
22
+ * place of keeping lost deals in): the target is what's still open or won.
22
23
  * Progress is the payments received against the target (decided 24 Oct 2026): money in on the invoices raised for the
23
24
  * deals (`Deal.invoices`), refunds taken off. Payments on invoices without a deal, such as repeat orders, don't count.
24
25
  */
@@ -31,8 +32,10 @@ export function salesTarget(
31
32
  let target: MoneyTotals = {};
32
33
  let won: MoneyTotals = {};
33
34
  for (const deal of deals) {
35
+ const outcome = dealOutcome(deal);
36
+ if (outcome === 'LOST') continue;
34
37
  target = addMoney(target, deal.targetValue);
35
- if (dealOutcome(deal) === 'WON') won = addMoney(won, deal.targetValue);
38
+ if (outcome === 'WON') won = addMoney(won, deal.targetValue);
36
39
  }
37
40
  for (const lead of leads) {
38
41
  if (leadOutcome(lead) === 'CLOSED') continue;
package/src/types.ts CHANGED
@@ -117,6 +117,9 @@ export type InvoiceStatus = 'DRAFT' | 'IN_REVIEW' | 'AUTHORISED' | 'PENDING' | '
117
117
  /** Whether an invoice's line prices include tax. */
118
118
  export type LineItemTax = 'EXCLUSIVE' | 'INCLUSIVE' | 'NO_TAX';
119
119
 
120
+ /** How an invoice line is taxed. */
121
+ export type Taxability = 'TAXABLE' | 'NON_TAXABLE' | 'TAX_EXEMPT' | 'PARTIALLY_TAXABLE' | 'NOT_APPLICABLE';
122
+
120
123
  /** Who pays or receives an invoice: a company, an identity, or both (a person at a company). */
121
124
  export interface InvoiceParty {
122
125
  company?: Ref | null;
@@ -136,6 +139,7 @@ export interface InvoiceLine {
136
139
  tax?: Money | null;
137
140
  discountPrice?: Money | null;
138
141
  discountRate?: number | null;
142
+ taxability?: Taxability | null;
139
143
  taxName?: string | null;
140
144
  }
141
145
 
@@ -200,3 +204,27 @@ export interface Remittance {
200
204
  export type CrmConcept = 'leads' | 'deals' | 'pipelines' | 'invoices' | 'remittances';
201
205
 
202
206
  export const CRM_CONCEPTS: readonly CrmConcept[] = ['leads', 'deals', 'pipelines', 'invoices', 'remittances'];
207
+
208
+ /**
209
+ * A new person, as the lead and customer forms make them: an identity with a person, the person's name (held
210
+ * securely, as person names are) and an email address.
211
+ */
212
+ export interface NewContact {
213
+ givenName: string;
214
+ familyName?: string | undefined;
215
+ email?: string | undefined;
216
+ telephone?: string | undefined;
217
+ }
218
+
219
+ /** How to reach a person: their email address and telephone number, each empty when they have none. */
220
+ export interface ContactDetails {
221
+ email: string;
222
+ telephone: string;
223
+ }
224
+
225
+ /** A person made through the CRM, named for showing: the identity, and the company it was made for, if any. */
226
+ export interface CrmPerson {
227
+ urn: string;
228
+ name: string;
229
+ company?: Ref | null;
230
+ }