conduyt 1.42.0 → 1.43.0
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/index.js +117 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -180,6 +180,123 @@ contacts
|
|
|
180
180
|
return client.del(`/api/v1/contacts/${encodeURIComponent(id)}`);
|
|
181
181
|
}));
|
|
182
182
|
// ---- deals ----
|
|
183
|
+
contacts
|
|
184
|
+
.command("delete-matching")
|
|
185
|
+
.description("Soft-delete EVERY contact a list filter matches, across pages (#73): the contacts page's \"Select all N matching\" action. Give the same filter you would give `contacts list` (or --filter with a JSON object of list query parameters) plus --expected, the total that filter showed; the server re-counts and refuses (409) when the set moved, so you never delete rows you did not look at. Contacts with an upcoming appointment are kept and reported. The command repeats the call until the server reports done. Requires contacts:delete.")
|
|
186
|
+
.option("--search <text>", "free-text search")
|
|
187
|
+
.option("--tag <name>", "one tag")
|
|
188
|
+
.option("--tags <names>", "comma-separated tags")
|
|
189
|
+
.option("--source <source>", "lead source")
|
|
190
|
+
.option("--assigned-to <userId>", "assignee user id")
|
|
191
|
+
.option("--smart-view <id>", "Smart View id")
|
|
192
|
+
.option("--smart-list <id>", "Smart List id")
|
|
193
|
+
.option("--import-id <id>", "import job id")
|
|
194
|
+
.option("--master-status <statuses>", "comma-separated lead statuses to include")
|
|
195
|
+
.option("--filter <json>", "a JSON object of list query parameters (merged over the flags above)")
|
|
196
|
+
.requiredOption("--expected <n>", "the total the list showed for this filter")
|
|
197
|
+
.option("--yes", "skip the typed confirmation (scripts)")
|
|
198
|
+
.action(run(async (client, opts) => {
|
|
199
|
+
const filter = {};
|
|
200
|
+
if (typeof opts.search === "string")
|
|
201
|
+
filter.search = opts.search;
|
|
202
|
+
if (typeof opts.tag === "string")
|
|
203
|
+
filter.tag = opts.tag;
|
|
204
|
+
if (typeof opts.tags === "string")
|
|
205
|
+
filter.tags = opts.tags;
|
|
206
|
+
if (typeof opts.source === "string")
|
|
207
|
+
filter.source = opts.source;
|
|
208
|
+
if (typeof opts.assignedTo === "string")
|
|
209
|
+
filter.assigned_to = opts.assignedTo;
|
|
210
|
+
if (typeof opts.smartView === "string")
|
|
211
|
+
filter.smart_view = opts.smartView;
|
|
212
|
+
if (typeof opts.smartList === "string")
|
|
213
|
+
filter.smartListId = opts.smartList;
|
|
214
|
+
if (typeof opts.importId === "string")
|
|
215
|
+
filter.import_id = opts.importId;
|
|
216
|
+
if (typeof opts.masterStatus === "string")
|
|
217
|
+
filter.master_status = opts.masterStatus.split(",").map((s) => s.trim()).filter(Boolean);
|
|
218
|
+
if (typeof opts.filter === "string") {
|
|
219
|
+
let parsed;
|
|
220
|
+
try {
|
|
221
|
+
parsed = JSON.parse(opts.filter);
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
throw new Error("--filter must be a JSON object of list query parameters.");
|
|
225
|
+
}
|
|
226
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
227
|
+
throw new Error("--filter must be a JSON object of list query parameters.");
|
|
228
|
+
for (const [k, v] of Object.entries(parsed)) {
|
|
229
|
+
if (typeof v === "string" || (Array.isArray(v) && v.every((x) => typeof x === "string")))
|
|
230
|
+
filter[k] = v;
|
|
231
|
+
else
|
|
232
|
+
throw new Error(`--filter.${k} must be a string or an array of strings.`);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
const hasValue = Object.values(filter).some((v) => (Array.isArray(v) ? v.some((x) => x.trim()) : v.trim()));
|
|
236
|
+
if (!hasValue)
|
|
237
|
+
throw new Error("Narrow the delete with at least one filter value; deleting every contact is not a bulk action.");
|
|
238
|
+
const expected = Number(opts.expected);
|
|
239
|
+
if (!Number.isInteger(expected) || expected < 1)
|
|
240
|
+
throw new Error("--expected must be the positive whole number the list showed for this filter.");
|
|
241
|
+
if (!opts.yes) {
|
|
242
|
+
const { createInterface } = await import("node:readline/promises");
|
|
243
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
244
|
+
const answer = (await rl.question(`Delete all ${expected} contacts matching this filter? Type the number ${expected} to confirm: `)).trim();
|
|
245
|
+
rl.close();
|
|
246
|
+
if (answer !== String(expected))
|
|
247
|
+
throw new Error("Cancelled: the typed number did not match.");
|
|
248
|
+
}
|
|
249
|
+
// The first call freezes the set on the server (409 when the count moved or when shared rows are not
|
|
250
|
+
// yours to delete); every later pass sends back the ids the server returned, never the filter again.
|
|
251
|
+
let body = { filter, expectedTotal: expected };
|
|
252
|
+
let deleted = 0;
|
|
253
|
+
let skipped = 0;
|
|
254
|
+
let kept = 0;
|
|
255
|
+
let last = {};
|
|
256
|
+
// A continuation that fails for a retryable reason (429 honouring retryAfter, a 5xx, a network error) is
|
|
257
|
+
// retried with the SAME ids so committed progress is never stranded.
|
|
258
|
+
const postWithRetry = async (payload) => {
|
|
259
|
+
for (let attempt = 0;; attempt++) {
|
|
260
|
+
try {
|
|
261
|
+
return (await client.post("/api/v1/bulk/contacts/delete-matching", payload));
|
|
262
|
+
}
|
|
263
|
+
catch (err) {
|
|
264
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
265
|
+
const status = Number(/^Conduyt API (\d{3})/.exec(msg)?.[1] ?? 0);
|
|
266
|
+
const retryable = "ids" in payload && (status === 429 || status >= 500 || status === 0);
|
|
267
|
+
if (!retryable || attempt >= 5)
|
|
268
|
+
throw err;
|
|
269
|
+
const retryAfter = Number(/"retryAfter":(\d+)/.exec(msg)?.[1] ?? 0);
|
|
270
|
+
await new Promise((r) => setTimeout(r, status === 429 ? Math.max(5, Math.min(60, retryAfter)) * 1000 : 2000 * (attempt + 1)));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
for (let pass = 0; pass < 200; pass++) {
|
|
275
|
+
const res = await postWithRetry(body);
|
|
276
|
+
const d = (res?.data ?? res);
|
|
277
|
+
last = d;
|
|
278
|
+
deleted += Number(d.deleted ?? 0);
|
|
279
|
+
skipped += Number(d.skipped ?? 0);
|
|
280
|
+
kept += Number(d.keptWithAppointments ?? 0);
|
|
281
|
+
const remainingIds = Array.isArray(d.remainingIds) ? d.remainingIds : [];
|
|
282
|
+
if (d.done || remainingIds.length === 0)
|
|
283
|
+
break;
|
|
284
|
+
if (!d.busy && Number(d.deleted ?? 0) === 0)
|
|
285
|
+
break;
|
|
286
|
+
if (d.busy)
|
|
287
|
+
await new Promise((r) => setTimeout(r, 1500));
|
|
288
|
+
body = { ids: remainingIds, continuation: d.continuation ?? null };
|
|
289
|
+
}
|
|
290
|
+
const remaining = Number(last.remaining ?? 0);
|
|
291
|
+
return {
|
|
292
|
+
deleted,
|
|
293
|
+
skipped,
|
|
294
|
+
keptWithAppointments: kept,
|
|
295
|
+
done: last.done === true,
|
|
296
|
+
remaining,
|
|
297
|
+
...(remaining > 0 ? { note: "the run stopped before finishing; run the same command again (with the new --expected) to delete the rest" } : {}),
|
|
298
|
+
};
|
|
299
|
+
}));
|
|
183
300
|
const deals = program.command("deals").description("Manage deals");
|
|
184
301
|
deals
|
|
185
302
|
.command("list")
|
package/package.json
CHANGED