@blazeo.com/appointment-client 1.0.10 → 1.0.13

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.
@@ -0,0 +1,106 @@
1
+ import { LeadModel } from "@blazeo.com/calendar-client";
2
+ import { getSnapshot, isStateTreeNode } from "mobx-state-tree";
3
+ import { ensureBlazeoHttpReady } from "../config/ensureBlazeoHttpReady.js";
4
+
5
+ export type BlazeoLeadConnection = {
6
+ baseUrl?: string;
7
+ consumer?: string;
8
+ };
9
+
10
+ /** Paging / sort options forwarded to `LeadModel.getByCompany` → `GET /lead/company/get`. */
11
+ export type LeadsByCompanyListOpts = {
12
+ skip?: number;
13
+ take?: number;
14
+ sortBy?: string;
15
+ sortOrder?: "ASC" | "DESC" | "asc" | "desc" | string;
16
+ sort?: string;
17
+ sort_column?: string;
18
+ sort_dir?: "asc" | "desc" | string;
19
+ page?: number;
20
+ page_size?: number;
21
+ searchColumn?: string;
22
+ search_column?: string;
23
+ searchText?: string;
24
+ search_text?: string;
25
+ search?: string;
26
+ };
27
+
28
+ function leadToPlain(lead: unknown): Record<string, unknown> | null {
29
+ if (lead == null) return null;
30
+ if (isStateTreeNode(lead)) {
31
+ return getSnapshot(lead) as Record<string, unknown>;
32
+ }
33
+ if (typeof lead === "object") return lead as Record<string, unknown>;
34
+ return null;
35
+ }
36
+
37
+ function leadsToPlain(list: unknown[] | null): Record<string, unknown>[] {
38
+ if (!Array.isArray(list)) return [];
39
+ return list.map((x) => leadToPlain(x)).filter((x): x is Record<string, unknown> => x != null);
40
+ }
41
+
42
+ /**
43
+ * Lead by id: `LeadModel.getRaw` / `get` → `GET /lead/get?lead_id=…`.
44
+ * Returns the mapped MST snapshot when successful, plus the raw API envelope from `getRaw`.
45
+ */
46
+ export async function fetchLeadDetails(
47
+ leadId: string,
48
+ connection: BlazeoLeadConnection = {}
49
+ ): Promise<
50
+ | { ok: true; lead: Record<string, unknown> | null; rawGet: unknown }
51
+ | { ok: false; reason: "missing_base_url"; detail: string }
52
+ > {
53
+ const ready = ensureBlazeoHttpReady(connection);
54
+ if (!ready.ok) {
55
+ return { ok: false, reason: "missing_base_url", detail: ready.error };
56
+ }
57
+ const id = String(leadId ?? "").trim();
58
+ if (!id) {
59
+ return {
60
+ ok: true,
61
+ lead: null,
62
+ rawGet: { status: "failure", message: "leadId is empty" },
63
+ };
64
+ }
65
+ const rawGet = await LeadModel.getRaw(id);
66
+ const model = await LeadModel.get(id);
67
+ return { ok: true, lead: leadToPlain(model), rawGet };
68
+ }
69
+
70
+ /**
71
+ * Single lead by email + company: `GET /lead/getbyemail`.
72
+ */
73
+ export async function fetchLeadByEmail(
74
+ email: string,
75
+ companyKey: string,
76
+ connection: BlazeoLeadConnection = {}
77
+ ): Promise<
78
+ | { ok: true; lead: Record<string, unknown> | null }
79
+ | { ok: false; reason: "missing_base_url"; detail: string }
80
+ > {
81
+ const ready = ensureBlazeoHttpReady(connection);
82
+ if (!ready.ok) {
83
+ return { ok: false, reason: "missing_base_url", detail: ready.error };
84
+ }
85
+ const model = await LeadModel.getByEmail(String(email).trim(), String(companyKey).trim());
86
+ return { ok: true, lead: leadToPlain(model) };
87
+ }
88
+
89
+ /**
90
+ * Paged list: `LeadModel.getByCompany` → `GET /lead/company/get`.
91
+ */
92
+ export async function fetchLeadsByCompany(
93
+ companyKey: string,
94
+ listOpts: LeadsByCompanyListOpts = {},
95
+ connection: BlazeoLeadConnection = {}
96
+ ): Promise<
97
+ | { ok: true; leads: Record<string, unknown>[] }
98
+ | { ok: false; reason: "missing_base_url"; detail: string }
99
+ > {
100
+ const ready = ensureBlazeoHttpReady(connection);
101
+ if (!ready.ok) {
102
+ return { ok: false, reason: "missing_base_url", detail: ready.error };
103
+ }
104
+ const models = await LeadModel.getByCompany(String(companyKey).trim(), listOpts);
105
+ return { ok: true, leads: leadsToPlain(models ?? []) };
106
+ }