@infaris/calendar-cli 0.9.0 → 0.12.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/README.md +181 -13
- package/dist/api.js +147 -26
- package/dist/auth.js +24 -1
- package/dist/crm-api.js +5 -1
- package/dist/crm-index.js +4 -2
- package/dist/crm-quote.js +174 -0
- package/dist/index.js +536 -23
- package/dist/mail-draft.js +357 -0
- package/dist/mail-send.js +97 -0
- package/dist/voice.js +321 -0
- package/package.json +1 -1
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { Option } from "commander";
|
|
5
|
+
import { crmFetch, crmFetchRaw } from "./crm-api.js";
|
|
6
|
+
/**
|
|
7
|
+
* `infaris-crm quote`: Guard quotes as DRAFTs in Support, priced from the
|
|
8
|
+
* live Infaris catalogue, with the PDF Support renders. Support owns the
|
|
9
|
+
* numbering, totals and acceptance; this only sends the draft.
|
|
10
|
+
*/
|
|
11
|
+
const MAX_LOGO_BYTES = 256 * 1024;
|
|
12
|
+
/** PNG or JPEG file as a data URI; Support renders it on white in the PDF header. */
|
|
13
|
+
export function logoDataUri(path) {
|
|
14
|
+
const bytes = readFileSync(path);
|
|
15
|
+
if (bytes.length > MAX_LOGO_BYTES)
|
|
16
|
+
throw new Error(`Logo ${path} is larger than 256 kB; crop or compress it`);
|
|
17
|
+
const png = bytes.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]));
|
|
18
|
+
const jpeg = bytes[0] === 0xff && bytes[1] === 0xd8;
|
|
19
|
+
if (!png && !jpeg)
|
|
20
|
+
throw new Error(`Logo ${path} must be a PNG or JPEG (convert an SVG first)`);
|
|
21
|
+
return `data:image/${png ? "png" : "jpeg"};base64,${bytes.toString("base64")}`;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The quote JSON with its logo inlined and an idempotency key: the same file
|
|
25
|
+
* for the same relation always gives the same key, so a repeated command
|
|
26
|
+
* returns the quote it already made instead of a second one.
|
|
27
|
+
*/
|
|
28
|
+
export function readQuoteFile(path, overrides = {}) {
|
|
29
|
+
const text = readFileSync(path, "utf8");
|
|
30
|
+
const quote = JSON.parse(text);
|
|
31
|
+
if (!quote || typeof quote !== "object" || Array.isArray(quote))
|
|
32
|
+
throw new Error(`${path} must hold a JSON object`);
|
|
33
|
+
if (overrides.relation)
|
|
34
|
+
quote.relation = overrides.relation;
|
|
35
|
+
if (overrides.organization)
|
|
36
|
+
quote.organization = overrides.organization;
|
|
37
|
+
const relation = typeof quote.relation === "string" ? quote.relation.trim() : "";
|
|
38
|
+
const organization = typeof quote.organization === "string" ? quote.organization.trim() : "";
|
|
39
|
+
// With only the organization, Support uses the relation it is linked to (by its trial).
|
|
40
|
+
if (!relation && !organization)
|
|
41
|
+
throw new Error("Give the relation (--customer) or the organization (--organization)");
|
|
42
|
+
const logo = overrides.logo ?? (typeof quote.logo === "string" && !quote.logo.startsWith("data:") ? resolve(dirname(path), quote.logo) : undefined);
|
|
43
|
+
if (logo)
|
|
44
|
+
quote.logo = logoDataUri(logo);
|
|
45
|
+
quote.key =
|
|
46
|
+
overrides.key ??
|
|
47
|
+
(typeof quote.key === "string" && quote.key
|
|
48
|
+
? quote.key
|
|
49
|
+
: `cli:${createHash("sha256").update(`${relation || `org:${organization}`}\n${text}`).digest("hex").slice(0, 40)}`);
|
|
50
|
+
return quote;
|
|
51
|
+
}
|
|
52
|
+
const json = (body) => ({
|
|
53
|
+
method: "POST",
|
|
54
|
+
headers: { "content-type": "application/json" },
|
|
55
|
+
body: JSON.stringify(body),
|
|
56
|
+
});
|
|
57
|
+
function output(value) {
|
|
58
|
+
process.stdout.write(`${JSON.stringify(value, null, 2)}\n`);
|
|
59
|
+
}
|
|
60
|
+
const enc = encodeURIComponent;
|
|
61
|
+
/** A Support without the quote API answers 404 on its collection routes. */
|
|
62
|
+
async function quoteApi(call) {
|
|
63
|
+
try {
|
|
64
|
+
return await call();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
if (error instanceof Error && /\(404\): Not Found$|\(404\): $/.test(error.message))
|
|
68
|
+
throw new Error("Support has no quote API yet (deploy Support with /api/crm/quotes first)");
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export function registerQuoteCommands(program) {
|
|
73
|
+
const quote = program
|
|
74
|
+
.command("quote")
|
|
75
|
+
.description("Guard quotes in Support: draft from the catalogue, PDF, send, decline");
|
|
76
|
+
quote
|
|
77
|
+
.command("catalog")
|
|
78
|
+
.description("Quotable Infaris components with live prices (ids for the quote file)")
|
|
79
|
+
.option("--json", "Machine-readable output (default)")
|
|
80
|
+
.action(async () => output(await quoteApi(() => crmFetch("/quotes/catalog"))));
|
|
81
|
+
quote
|
|
82
|
+
.command("create")
|
|
83
|
+
.description("Create a DRAFT quote from a JSON file; repeating returns the same quote")
|
|
84
|
+
.requiredOption("--file <path>", "Quote JSON (see the guard-quote skill)")
|
|
85
|
+
.option("--customer <relation>", "Relation number or id; leave out to use the relation the organization is linked to")
|
|
86
|
+
.option("--organization <slug>", "Organization the license belongs to (overrides the file)")
|
|
87
|
+
.option("--logo <path>", "Customer logo, PNG or JPEG on white")
|
|
88
|
+
.option("--key <key>", "Idempotency key (default: derived from relation and file)")
|
|
89
|
+
.option("--json", "Machine-readable output (default)")
|
|
90
|
+
.action(async (opts) => {
|
|
91
|
+
const body = readQuoteFile(opts.file, {
|
|
92
|
+
relation: opts.customer,
|
|
93
|
+
organization: opts.organization,
|
|
94
|
+
key: opts.key,
|
|
95
|
+
logo: opts.logo,
|
|
96
|
+
});
|
|
97
|
+
output(await quoteApi(() => crmFetch("/quotes", json(body))));
|
|
98
|
+
});
|
|
99
|
+
quote
|
|
100
|
+
.command("update <number>")
|
|
101
|
+
.description("Replace the lines and texts of a DRAFT quote")
|
|
102
|
+
.requiredOption("--file <path>", "Quote JSON")
|
|
103
|
+
.option("--customer <relation>")
|
|
104
|
+
.option("--organization <slug>")
|
|
105
|
+
.option("--logo <path>")
|
|
106
|
+
.option("--json", "Machine-readable output (default)")
|
|
107
|
+
.action(async (number, opts) => {
|
|
108
|
+
const body = readQuoteFile(opts.file, {
|
|
109
|
+
relation: opts.customer,
|
|
110
|
+
organization: opts.organization,
|
|
111
|
+
logo: opts.logo,
|
|
112
|
+
});
|
|
113
|
+
output(await crmFetch(`/quotes/${enc(number)}`, { ...json(body), method: "PUT" }));
|
|
114
|
+
});
|
|
115
|
+
quote
|
|
116
|
+
.command("show <number>")
|
|
117
|
+
.description("Status, totals, lines and the --components value for the trial")
|
|
118
|
+
.option("--json", "Machine-readable output (default)")
|
|
119
|
+
.action(async (number) => output(await crmFetch(`/quotes/${enc(number)}`)));
|
|
120
|
+
quote
|
|
121
|
+
.command("list <customer>")
|
|
122
|
+
.description("Quotes of a relation, newest first")
|
|
123
|
+
.option("--json", "Machine-readable output (default)")
|
|
124
|
+
.action(async (customer) => output(await crmFetch(`/quotes?${new URLSearchParams({ relation: customer })}`)));
|
|
125
|
+
quote
|
|
126
|
+
.command("pdf <number>")
|
|
127
|
+
.description("Download the PDF Support renders for the quote")
|
|
128
|
+
.requiredOption("--out <path>", "Where to write the PDF")
|
|
129
|
+
.action(async (number, opts) => {
|
|
130
|
+
const res = await crmFetchRaw(`/quotes/${enc(number)}/pdf`);
|
|
131
|
+
if (!res.headers.get("content-type")?.includes("application/pdf"))
|
|
132
|
+
throw new Error("Support did not return a PDF");
|
|
133
|
+
const bytes = Buffer.from(await res.arrayBuffer());
|
|
134
|
+
writeFileSync(opts.out, bytes);
|
|
135
|
+
output({ pdf: resolve(opts.out), bytes: bytes.length });
|
|
136
|
+
});
|
|
137
|
+
quote
|
|
138
|
+
.command("send <number>")
|
|
139
|
+
.description("Mark the quote SENT once the mail draft with the PDF is ready")
|
|
140
|
+
.option("--json", "Machine-readable output (default)")
|
|
141
|
+
.action(async (number) => output(await crmFetch(`/quotes/${enc(number)}/send`, json({}))));
|
|
142
|
+
quote
|
|
143
|
+
.command("accept <number>")
|
|
144
|
+
.description("Record the customer's acceptance, as on the quote page; starts the license and billing in Infaris")
|
|
145
|
+
.requiredOption("--by <name>", "Who agreed on behalf of the customer")
|
|
146
|
+
.addOption(new Option("--via <channel>", "How the customer agreed")
|
|
147
|
+
.choices(["email", "signed_pdf", "verbal"])
|
|
148
|
+
.makeOptionMandatory())
|
|
149
|
+
.addOption(new Option("--variant <variant>", "base (Guard) or option (e.g. Guard met support); required with an option").choices([
|
|
150
|
+
"base",
|
|
151
|
+
"option",
|
|
152
|
+
]))
|
|
153
|
+
.option("--document-url <url>", "https link to the signed copy or the acceptance mail")
|
|
154
|
+
.option("--dpa-signed", "The data processing agreement is signed")
|
|
155
|
+
.option("--yes", "Confirm: this starts real billing")
|
|
156
|
+
.option("--json", "Machine-readable output (default)")
|
|
157
|
+
.action(async (number, opts) => {
|
|
158
|
+
if (!opts.yes)
|
|
159
|
+
throw new Error("Accepting starts the license and billing in Infaris. Only with the customer's confirmed acceptance, then repeat with --yes.");
|
|
160
|
+
output(await crmFetch(`/quotes/${enc(number)}/accept`, json({
|
|
161
|
+
by: opts.by,
|
|
162
|
+
via: opts.via,
|
|
163
|
+
variant: opts.variant,
|
|
164
|
+
documentUrl: opts.documentUrl,
|
|
165
|
+
dpaSigned: Boolean(opts.dpaSigned),
|
|
166
|
+
confirm: true,
|
|
167
|
+
})));
|
|
168
|
+
});
|
|
169
|
+
quote
|
|
170
|
+
.command("decline <number>")
|
|
171
|
+
.description("Record that the customer declined")
|
|
172
|
+
.option("--json", "Machine-readable output (default)")
|
|
173
|
+
.action(async (number) => output(await crmFetch(`/quotes/${enc(number)}/decline`, json({}))));
|
|
174
|
+
}
|