kassza 0.12.0 → 0.14.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/README.en.md +48 -0
- package/README.md +427 -32
- package/agents/README.md +26 -6
- package/agents/api.md +218 -3
- package/agents/pitfalls.md +27 -3
- package/agents/recipes.md +280 -51
- package/agents/skills/kassza/SKILL.md +14 -8
- package/agents/skills/migrate-from-szamlazz-js/SKILL.md +39 -0
- package/dist/batch/index.cjs +346 -0
- package/dist/batch/index.d.cts +91 -0
- package/dist/batch/index.d.ts +91 -0
- package/dist/batch/index.js +339 -0
- package/dist/cli.cjs +643 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +643 -0
- package/dist/client-CLFvJCVW.cjs +1452 -0
- package/dist/{client-CL3fuH6A.js → client-Cvf-hTz_.js} +306 -515
- package/dist/client-DeWWQ-RZ.d.ts +341 -0
- package/dist/client-SVZu25MB.d.cts +341 -0
- package/dist/context-CFNKzStE.d.cts +449 -0
- package/dist/context-kb33TgzE.d.ts +449 -0
- package/dist/cookie-stores/index.cjs +21 -150
- package/dist/cookie-stores/index.d.cts +4 -63
- package/dist/cookie-stores/index.d.ts +4 -63
- package/dist/cookie-stores/index.js +3 -144
- package/dist/create-CflXrPip.js +204 -0
- package/dist/create-CvUvLUaT.cjs +257 -0
- package/dist/{create-once-D0pwuenl.cjs → create-once-9yxR_Er_.cjs} +291 -33
- package/dist/create-once-Bh6fOWkW.d.cts +20 -0
- package/dist/{create-once-CqdYmQD8.js → create-once-Ca2gMk1h.js} +250 -34
- package/dist/create-once-VKp0tVow.d.ts +20 -0
- package/dist/{crypto-B8lERvR1.cjs → crypto-Dpn4FHQ6.cjs} +9 -0
- package/dist/{crypto-DK_osBVy.js → crypto-usDg8L2K.js} +4 -1
- package/dist/data-link/index.cjs +232 -0
- package/dist/data-link/index.d.cts +29 -0
- package/dist/data-link/index.d.ts +29 -0
- package/dist/data-link/index.js +224 -0
- package/dist/delegation/index.cjs +35 -34
- package/dist/delegation/index.d.cts +2 -1
- package/dist/delegation/index.d.ts +2 -1
- package/dist/delegation/index.js +4 -3
- package/dist/{errors-DLrb0g64.js → errors-DtN4g5Z9.js} +9 -1
- package/dist/{errors-DxeuTslP.cjs → errors-EE96_6D_.cjs} +20 -0
- package/dist/get-BdMTseh4.cjs +271 -0
- package/dist/get-Byc9T5WO.d.cts +151 -0
- package/dist/get-Byc9T5WO.d.ts +151 -0
- package/dist/get-CZEShCcj.js +260 -0
- package/dist/index-BhUPY_EH.d.cts +109 -0
- package/dist/index-BqGgQ4t3.d.ts +109 -0
- package/dist/{index-CYAGCdKJ.d.cts → index-C-7yWV7L.d.cts} +24 -1
- package/dist/{index-CYAGCdKJ.d.ts → index-C-7yWV7L.d.ts} +24 -1
- package/dist/index.cjs +19 -5
- package/dist/index.d.cts +11 -19
- package/dist/index.d.ts +11 -19
- package/dist/index.js +6 -6
- package/dist/ipn/index.cjs +26 -5
- package/dist/ipn/index.d.cts +12 -1
- package/dist/ipn/index.d.ts +12 -1
- package/dist/ipn/index.js +26 -6
- package/dist/issue-Bin4tKD_.cjs +988 -0
- package/dist/issue-DMA1Nbr1.js +905 -0
- package/dist/journal/index.cjs +454 -0
- package/dist/journal/index.d.cts +18 -0
- package/dist/journal/index.d.ts +18 -0
- package/dist/journal/index.js +445 -0
- package/dist/journal-DvI81Kt7.d.cts +114 -0
- package/dist/journal-Y9qrIB8G.d.ts +114 -0
- package/dist/mcp/index.cjs +10 -0
- package/dist/mcp/index.d.cts +71 -0
- package/dist/mcp/index.d.ts +71 -0
- package/dist/mcp/index.js +2 -0
- package/dist/money/index.cjs +2 -1
- package/dist/money/index.d.cts +2 -2
- package/dist/money/index.d.ts +2 -2
- package/dist/money/index.js +2 -2
- package/dist/{money-DAA7OwiO.js → money-Cw457SD6.js} +120 -12
- package/dist/{money-DrhGkWHW.cjs → money-K9j0wPit.cjs} +124 -10
- package/dist/nav/index.cjs +1046 -0
- package/dist/nav/index.d.cts +247 -0
- package/dist/nav/index.d.ts +247 -0
- package/dist/nav/index.js +1012 -0
- package/dist/nav-BNbc8U0R.cjs +203 -0
- package/dist/nav-BcCerwWS.js +156 -0
- package/dist/node/index.cjs +124 -0
- package/dist/node/index.d.cts +35 -0
- package/dist/node/index.d.ts +35 -0
- package/dist/node/index.js +120 -0
- package/dist/observe/index.cjs +311 -0
- package/dist/observe/index.d.cts +53 -0
- package/dist/observe/index.d.ts +53 -0
- package/dist/observe/index.js +303 -0
- package/dist/once-BN_3B5OF.cjs +103 -0
- package/dist/once-CqqZxsXG.js +74 -0
- package/dist/parse-Cr7yQ3_z.js +291 -0
- package/dist/parse-DOA2XQ3e.cjs +380 -0
- package/dist/payments/barion.cjs +26 -2
- package/dist/payments/barion.d.cts +2 -2
- package/dist/payments/barion.d.ts +2 -2
- package/dist/payments/barion.js +26 -2
- package/dist/payments/index.cjs +7 -2
- package/dist/payments/index.d.cts +4 -4
- package/dist/payments/index.d.ts +4 -4
- package/dist/payments/index.js +3 -3
- package/dist/payments/paypal.cjs +16 -2
- package/dist/payments/paypal.d.cts +2 -2
- package/dist/payments/paypal.d.ts +2 -2
- package/dist/payments/paypal.js +16 -2
- package/dist/payments/revolut.cjs +17 -3
- package/dist/payments/revolut.d.cts +2 -2
- package/dist/payments/revolut.d.ts +2 -2
- package/dist/payments/revolut.js +17 -3
- package/dist/payments/simplepay.cjs +37 -5
- package/dist/payments/simplepay.d.cts +2 -2
- package/dist/payments/simplepay.d.ts +2 -2
- package/dist/payments/simplepay.js +37 -5
- package/dist/payments/stripe.cjs +20 -3
- package/dist/payments/stripe.d.cts +2 -2
- package/dist/payments/stripe.d.ts +2 -2
- package/dist/payments/stripe.js +20 -3
- package/dist/push-BXj0Sf0Z.d.ts +52 -0
- package/dist/push-D4nNbCNV.d.cts +52 -0
- package/dist/reference-B5TATqb5.js +124 -0
- package/dist/reference-BIdYMK0t.cjs +183 -0
- package/dist/reports/index.cjs +11 -164
- package/dist/reports/index.d.cts +1 -1
- package/dist/reports/index.d.ts +1 -1
- package/dist/reports/index.js +1 -154
- package/dist/request-body-CH6ZGOrq.cjs +34 -0
- package/dist/request-body-uZHMRlQr.js +29 -0
- package/dist/serialize-B2eFh3D-.js +219 -0
- package/dist/serialize-CquIsxA-.cjs +290 -0
- package/dist/server-BrJUbXs7.js +1505 -0
- package/dist/server-Dw9j8Mnd.cjs +1558 -0
- package/dist/session-Ckl5eJhV.d.ts +7 -0
- package/dist/{session-CKOuE6Jc.cjs → session-DeM5vjvC.cjs} +69 -13
- package/dist/{session-rMKaJcmq.js → session-SaEEXu2N.js} +58 -14
- package/dist/session-YbChChx7.d.cts +7 -0
- package/dist/storage/fs.cjs +1 -1
- package/dist/storage/fs.js +1 -1
- package/dist/storage/index.cjs +1 -1
- package/dist/storage/index.js +1 -1
- package/dist/store-C1rHd9DB.d.cts +19 -0
- package/dist/store-C1rHd9DB.d.ts +19 -0
- package/dist/stores/index.cjs +24 -0
- package/dist/stores/index.d.cts +4 -0
- package/dist/stores/index.d.ts +4 -0
- package/dist/stores/index.js +3 -0
- package/dist/stores-BNrS0dWl.cjs +506 -0
- package/dist/stores-DhjGBrl_.js +441 -0
- package/dist/testing/index.cjs +1500 -15
- package/dist/testing/index.d.cts +198 -3
- package/dist/testing/index.d.ts +198 -3
- package/dist/testing/index.js +1498 -15
- package/dist/{types-COf1LAD-.d.ts → types-B1A8mUIE.d.cts} +14 -2
- package/dist/{types-COf1LAD-.d.cts → types-ByAEvGsZ.d.ts} +14 -2
- package/dist/{types-k--zHf4l.d.ts → types-D1zDOH_F.d.ts} +1 -1
- package/dist/{types-DH8QoNZK.d.cts → types-lnKkQdot.d.cts} +1 -1
- package/dist/warnings-B3DXnveC.d.cts +31 -0
- package/dist/warnings-B3DXnveC.d.ts +31 -0
- package/dist/warnings-DeIw5RFj.cjs +20 -0
- package/dist/warnings-DwTl5J-2.js +15 -0
- package/dist/{webhook-D4lvtex1.js → webhook-CQVShAfI.js} +47 -26
- package/dist/{webhook-C76bV-4R.cjs → webhook-Ckj-_hPD.cjs} +70 -25
- package/dist/webhook-CmdhGiH5.d.cts +11 -0
- package/dist/webhook-D6lEejob.d.ts +11 -0
- package/llms.txt +6 -5
- package/package.json +134 -4
- package/dist/client-CickRRFr.d.ts +0 -838
- package/dist/client-CsmGIFRS.cjs +0 -1637
- package/dist/client-DvWZoX_E.d.cts +0 -838
- package/dist/issue-BsyYdrIG.cjs +0 -1531
- package/dist/issue-C6Fcjvln.js +0 -1274
- package/dist/session-Dm_45x8K.d.cts +0 -11
- package/dist/session-Dm_45x8K.d.ts +0 -11
- package/dist/webhook-CfaDgcL0.d.cts +0 -6
- package/dist/webhook-q2OzNwd7.d.ts +0 -6
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { o as isSzamlazzError, r as SzamlazzError } from "../errors-DtN4g5Z9.js";
|
|
2
|
+
import { n as toAgentDate, t as addBudapestDays } from "../dates-CXyj7Azy.js";
|
|
3
|
+
import { n as calculateReceiptItems } from "../create-CflXrPip.js";
|
|
4
|
+
//#region src/batch/recurring.ts
|
|
5
|
+
const INTERVALS = /* @__PURE__ */ new Set([
|
|
6
|
+
"week",
|
|
7
|
+
"month",
|
|
8
|
+
"quarter",
|
|
9
|
+
"year"
|
|
10
|
+
]);
|
|
11
|
+
const MONTHS = {
|
|
12
|
+
month: 1,
|
|
13
|
+
quarter: 3,
|
|
14
|
+
year: 12
|
|
15
|
+
};
|
|
16
|
+
function invalid$1(message) {
|
|
17
|
+
return new SzamlazzError(message, { category: "validation" });
|
|
18
|
+
}
|
|
19
|
+
function parts(day) {
|
|
20
|
+
const [year, month, date] = day.split("-").map(Number);
|
|
21
|
+
return [
|
|
22
|
+
year,
|
|
23
|
+
month,
|
|
24
|
+
date
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
function format(year, month, day) {
|
|
28
|
+
return `${String(year).padStart(4, "0")}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
|
|
29
|
+
}
|
|
30
|
+
function daysInMonth(year, month) {
|
|
31
|
+
return new Date(Date.UTC(year, month, 0)).getUTCDate();
|
|
32
|
+
}
|
|
33
|
+
function addDays(day, days) {
|
|
34
|
+
const [year, month, date] = parts(day);
|
|
35
|
+
return new Date(Date.UTC(year, month - 1, date + days)).toISOString().slice(0, 10);
|
|
36
|
+
}
|
|
37
|
+
function shift(anchor, interval, count) {
|
|
38
|
+
if (interval === "week") return addDays(anchor, count * 7);
|
|
39
|
+
const [year, month, day] = parts(anchor);
|
|
40
|
+
const total = year * 12 + (month - 1) + count * MONTHS[interval];
|
|
41
|
+
const targetYear = Math.floor(total / 12);
|
|
42
|
+
const targetMonth = total % 12 + 1;
|
|
43
|
+
return format(targetYear, targetMonth, Math.min(day, daysInMonth(targetYear, targetMonth)));
|
|
44
|
+
}
|
|
45
|
+
function resolve(options) {
|
|
46
|
+
if (!INTERVALS.has(options.interval)) throw invalid$1(`Ismeretlen számlázási időszak: ${String(options.interval)}`);
|
|
47
|
+
let anchor;
|
|
48
|
+
try {
|
|
49
|
+
anchor = toAgentDate(options.anchor);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
throw invalid$1(error instanceof Error ? error.message : String(error));
|
|
52
|
+
}
|
|
53
|
+
const due = options.paymentDueInDays;
|
|
54
|
+
if (due !== void 0 && (!Number.isInteger(due) || due < 0)) throw invalid$1(`A paymentDueInDays nemnegatív egész legyen, kapott: ${due}`);
|
|
55
|
+
return {
|
|
56
|
+
anchor,
|
|
57
|
+
interval: options.interval
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function billingPeriod(options, index) {
|
|
61
|
+
if (!Number.isInteger(index) || index < 0) throw invalid$1(`Az időszak sorszáma nemnegatív egész legyen, kapott: ${index}`);
|
|
62
|
+
const { anchor, interval } = resolve(options);
|
|
63
|
+
const start = shift(anchor, interval, index);
|
|
64
|
+
const end = addDays(shift(anchor, interval, index + 1), -1);
|
|
65
|
+
const due = options.paymentDueInDays;
|
|
66
|
+
return {
|
|
67
|
+
index,
|
|
68
|
+
interval,
|
|
69
|
+
start,
|
|
70
|
+
end,
|
|
71
|
+
...due === void 0 ? {} : { dueDate: addDays(start, due) },
|
|
72
|
+
key: `${interval}:${start}`
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function billingPeriodAt(options, date) {
|
|
76
|
+
const { anchor, interval } = resolve(options);
|
|
77
|
+
let day;
|
|
78
|
+
try {
|
|
79
|
+
day = toAgentDate(date);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw invalid$1(error instanceof Error ? error.message : String(error));
|
|
82
|
+
}
|
|
83
|
+
if (day < anchor) throw invalid$1(`A(z) ${day} dátum korábbi, mint az első időszak kezdete (${anchor}).`);
|
|
84
|
+
const [anchorYear, anchorMonth] = parts(anchor);
|
|
85
|
+
const [year, month] = parts(day);
|
|
86
|
+
let index = interval === "week" ? Math.floor((Date.parse(`${day}T00:00:00Z`) - Date.parse(`${anchor}T00:00:00Z`)) / 6048e5) : Math.floor(((year - anchorYear) * 12 + (month - anchorMonth)) / MONTHS[interval]);
|
|
87
|
+
while (index > 0 && shift(anchor, interval, index) > day) index -= 1;
|
|
88
|
+
while (shift(anchor, interval, index + 1) <= day) index += 1;
|
|
89
|
+
return billingPeriod(options, index);
|
|
90
|
+
}
|
|
91
|
+
function billingPeriodsBetween(options, from, to) {
|
|
92
|
+
const first = billingPeriodAt(options, from);
|
|
93
|
+
const last = billingPeriodAt(options, to);
|
|
94
|
+
const periods = [];
|
|
95
|
+
for (let index = first.index; index <= last.index; index++) periods.push(billingPeriod(options, index));
|
|
96
|
+
return periods;
|
|
97
|
+
}
|
|
98
|
+
function todayPeriod(options, now = /* @__PURE__ */ new Date()) {
|
|
99
|
+
return billingPeriodAt(options, addBudapestDays(now, 0));
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/batch/run.ts
|
|
103
|
+
const MAX_BATCH_CONCURRENCY = 4;
|
|
104
|
+
const DEFAULT_BATCH_RATE_PER_MINUTE = 30;
|
|
105
|
+
const STOPPING_CATEGORIES = /* @__PURE__ */ new Set([
|
|
106
|
+
"rate_limit",
|
|
107
|
+
"auth",
|
|
108
|
+
"account",
|
|
109
|
+
"configuration",
|
|
110
|
+
"maintenance",
|
|
111
|
+
"attempt_limit",
|
|
112
|
+
"store_unavailable"
|
|
113
|
+
]);
|
|
114
|
+
function invalid(message) {
|
|
115
|
+
return new SzamlazzError(message, { category: "validation" });
|
|
116
|
+
}
|
|
117
|
+
function resolveConcurrency(value) {
|
|
118
|
+
if (value === void 0) return 1;
|
|
119
|
+
if (!Number.isInteger(value) || value < 1 || value > 4) throw new SzamlazzError(`A concurrency 1 és 4 közötti egész legyen, kapott: ${value}. A Számlázz.hu a párhuzamos kérések kerülését kéri.`, { category: "configuration" });
|
|
120
|
+
return value;
|
|
121
|
+
}
|
|
122
|
+
function resolveRate(value) {
|
|
123
|
+
if (value === void 0) return 30;
|
|
124
|
+
if (!Number.isFinite(value) || value <= 0) throw new SzamlazzError(`A ratePerMinute pozitív szám legyen, kapott: ${value}`, { category: "configuration" });
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
function assertKeys(items) {
|
|
128
|
+
const seen = /* @__PURE__ */ new Set();
|
|
129
|
+
for (const item of items) {
|
|
130
|
+
const key = item.key?.trim();
|
|
131
|
+
if (!key) throw invalid("Minden tételhez adj meg kulcsot (key), ez lesz a rendelésszám.");
|
|
132
|
+
if (seen.has(key)) throw invalid(`A(z) ${key} kulcs kétszer szerepel a listában.`);
|
|
133
|
+
seen.add(key);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
function withOrderNumber(input, key) {
|
|
137
|
+
const own = input.orderNumber?.trim();
|
|
138
|
+
if (own && own !== key) throw invalid(`A(z) ${key} tétel rendelésszáma (${own}) eltér a kulcstól. A batch a kulcsot használja rendelésszámként, hogy az újrafuttatás ne állítson ki duplát.`);
|
|
139
|
+
return {
|
|
140
|
+
...input,
|
|
141
|
+
orderNumber: key
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
function wait(ms, signal) {
|
|
145
|
+
if (ms <= 0) return Promise.resolve();
|
|
146
|
+
return new Promise((resolve, reject) => {
|
|
147
|
+
if (signal?.aborted) {
|
|
148
|
+
reject(signal.reason);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
const timer = setTimeout(() => {
|
|
152
|
+
signal?.removeEventListener("abort", onAbort);
|
|
153
|
+
resolve();
|
|
154
|
+
}, ms);
|
|
155
|
+
const onAbort = () => {
|
|
156
|
+
clearTimeout(timer);
|
|
157
|
+
reject(signal?.reason);
|
|
158
|
+
};
|
|
159
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
function invoiceGross(result) {
|
|
163
|
+
return result.invoice?.grossTotal ?? result.details?.totals.grossAmount;
|
|
164
|
+
}
|
|
165
|
+
async function previewDocument(api, document, key, signal) {
|
|
166
|
+
if (document.kind === "invoice") return {
|
|
167
|
+
key,
|
|
168
|
+
status: "previewed",
|
|
169
|
+
kind: "invoice",
|
|
170
|
+
grossTotal: (await api.invoices.preview(withOrderNumber(document.input, key), { signal })).grossTotal
|
|
171
|
+
};
|
|
172
|
+
const input = withOrderNumber(document.input, key);
|
|
173
|
+
if (input.items.length === 0) throw invalid(`A(z) ${key} nyugtán nincs tétel.`);
|
|
174
|
+
const gross = calculateReceiptItems(input.items, input.currency ?? "HUF").reduce((sum, item) => sum + item.amounts.grossAmount, 0);
|
|
175
|
+
return {
|
|
176
|
+
key,
|
|
177
|
+
status: "previewed",
|
|
178
|
+
kind: "receipt",
|
|
179
|
+
grossTotal: Math.round(gross * 100) / 100
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
async function issueDocument(api, document, key, options) {
|
|
183
|
+
const onceOptions = {
|
|
184
|
+
signal: options.signal,
|
|
185
|
+
recoveryDelayMs: options.recoveryDelayMs
|
|
186
|
+
};
|
|
187
|
+
if (document.kind === "invoice") {
|
|
188
|
+
const input = withOrderNumber(document.input, key);
|
|
189
|
+
const run = () => api.invoices.createOnce(input, onceOptions);
|
|
190
|
+
const result = options.journal ? await options.journal.trackInvoice(key, run) : await run();
|
|
191
|
+
return {
|
|
192
|
+
key,
|
|
193
|
+
status: result.created ? "created" : "existing",
|
|
194
|
+
kind: "invoice",
|
|
195
|
+
number: result.number,
|
|
196
|
+
grossTotal: invoiceGross(result)
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const input = withOrderNumber(document.input, key);
|
|
200
|
+
const run = () => api.receipts.createOnce(input, onceOptions);
|
|
201
|
+
const result = options.journal ? await options.journal.trackReceipt(key, run) : await run();
|
|
202
|
+
return {
|
|
203
|
+
key,
|
|
204
|
+
status: result.created ? "created" : "existing",
|
|
205
|
+
kind: "receipt",
|
|
206
|
+
number: result.receipt.number,
|
|
207
|
+
grossTotal: result.receipt.totals.grossAmount
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
function stopReason(error) {
|
|
211
|
+
if (!isSzamlazzError(error) || !STOPPING_CATEGORIES.has(error.category)) return void 0;
|
|
212
|
+
return `A(z) ${error.category} hiba miatt a batch leállt, a hátralévő tételek nem futottak le: ${error.message}`;
|
|
213
|
+
}
|
|
214
|
+
async function runBatch(api, options) {
|
|
215
|
+
const concurrency = resolveConcurrency(options.concurrency);
|
|
216
|
+
const intervalMs = 6e4 / resolveRate(options.ratePerMinute);
|
|
217
|
+
assertKeys(options.items);
|
|
218
|
+
const dryRun = options.dryRun === true;
|
|
219
|
+
const results = new Array(options.items.length);
|
|
220
|
+
const counts = {
|
|
221
|
+
created: 0,
|
|
222
|
+
existing: 0,
|
|
223
|
+
previewed: 0,
|
|
224
|
+
failed: 0,
|
|
225
|
+
skipped: 0
|
|
226
|
+
};
|
|
227
|
+
let done = 0;
|
|
228
|
+
let cursor = 0;
|
|
229
|
+
let nextStart = Date.now();
|
|
230
|
+
const control = { stopped: void 0 };
|
|
231
|
+
const currentStop = () => control.stopped;
|
|
232
|
+
const record = (index, result) => {
|
|
233
|
+
results[index] = result;
|
|
234
|
+
counts[result.status] += 1;
|
|
235
|
+
done += 1;
|
|
236
|
+
try {
|
|
237
|
+
options.onProgress?.({
|
|
238
|
+
total: options.items.length,
|
|
239
|
+
done,
|
|
240
|
+
...counts,
|
|
241
|
+
key: result.key,
|
|
242
|
+
status: result.status
|
|
243
|
+
});
|
|
244
|
+
} catch {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
const processItem = async (index, item) => {
|
|
249
|
+
const key = item.key.trim();
|
|
250
|
+
try {
|
|
251
|
+
const document = await item.document();
|
|
252
|
+
const result = dryRun ? await previewDocument(api, document, key, options.signal) : await issueDocument(api, document, key, options);
|
|
253
|
+
record(index, result);
|
|
254
|
+
} catch (error) {
|
|
255
|
+
if (options.signal?.aborted && error === options.signal.reason) {
|
|
256
|
+
control.stopped ??= {
|
|
257
|
+
reason: "A batch futását megszakították.",
|
|
258
|
+
error
|
|
259
|
+
};
|
|
260
|
+
record(index, {
|
|
261
|
+
key,
|
|
262
|
+
status: "skipped",
|
|
263
|
+
reason: "Megszakítva."
|
|
264
|
+
});
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const reason = stopReason(error);
|
|
268
|
+
if (reason) control.stopped ??= {
|
|
269
|
+
reason,
|
|
270
|
+
error
|
|
271
|
+
};
|
|
272
|
+
record(index, {
|
|
273
|
+
key,
|
|
274
|
+
status: "failed",
|
|
275
|
+
error
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
const worker = async () => {
|
|
280
|
+
for (;;) {
|
|
281
|
+
if (control.stopped || options.signal?.aborted) return;
|
|
282
|
+
const index = cursor;
|
|
283
|
+
cursor += 1;
|
|
284
|
+
const item = options.items[index];
|
|
285
|
+
if (!item) return;
|
|
286
|
+
const delay = nextStart - Date.now();
|
|
287
|
+
nextStart = Math.max(nextStart, Date.now()) + intervalMs;
|
|
288
|
+
try {
|
|
289
|
+
await wait(delay, options.signal);
|
|
290
|
+
} catch (error) {
|
|
291
|
+
control.stopped ??= {
|
|
292
|
+
reason: "A batch futását megszakították.",
|
|
293
|
+
error
|
|
294
|
+
};
|
|
295
|
+
record(index, {
|
|
296
|
+
key: item.key.trim(),
|
|
297
|
+
status: "skipped",
|
|
298
|
+
reason: "Megszakítva."
|
|
299
|
+
});
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
const stop = currentStop();
|
|
303
|
+
if (stop) {
|
|
304
|
+
record(index, {
|
|
305
|
+
key: item.key.trim(),
|
|
306
|
+
status: "skipped",
|
|
307
|
+
reason: stop.reason
|
|
308
|
+
});
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
await processItem(index, item);
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
await Promise.all(Array.from({ length: Math.min(concurrency, options.items.length) }, worker));
|
|
315
|
+
if (options.signal?.aborted) control.stopped ??= { reason: "A batch futását megszakították." };
|
|
316
|
+
options.items.forEach((item, index) => {
|
|
317
|
+
if (results[index]) return;
|
|
318
|
+
record(index, {
|
|
319
|
+
key: item.key.trim(),
|
|
320
|
+
status: "skipped",
|
|
321
|
+
reason: control.stopped?.reason ?? "Nem futott le."
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
const by = (status) => results.filter((result) => result.status === status);
|
|
325
|
+
const grossTotal = results.filter((result) => result.status !== "failed" && result.status !== "skipped").reduce((sum, result) => sum + (result.grossTotal ?? 0), 0);
|
|
326
|
+
return {
|
|
327
|
+
dryRun,
|
|
328
|
+
items: results,
|
|
329
|
+
created: by("created"),
|
|
330
|
+
existing: by("existing"),
|
|
331
|
+
previewed: by("previewed"),
|
|
332
|
+
failed: by("failed"),
|
|
333
|
+
skipped: by("skipped"),
|
|
334
|
+
stopped: control.stopped,
|
|
335
|
+
grossTotal: Math.round(grossTotal * 100) / 100
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
//#endregion
|
|
339
|
+
export { DEFAULT_BATCH_RATE_PER_MINUTE, MAX_BATCH_CONCURRENCY, billingPeriod, billingPeriodAt, billingPeriodsBetween, runBatch, todayPeriod };
|