-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
467 lines (438 loc) · 17.9 KB
/
index.html
File metadata and controls
467 lines (438 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Replicator</title>
<style>
:root {
--fg: #1f2937;
--muted: #6b7280;
--border: #e5e7eb;
--accent: #2563eb;
--accent-hover: #1d4ed8;
--ok: #16a34a;
--err: #dc2626;
--bg: #f9fafb;
--card: #ffffff;
}
* { box-sizing: border-box; }
body {
margin: 0;
padding: 24px;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
font-size: 14px;
color: var(--fg);
background: var(--bg);
}
h1 { margin: 0 0 4px; font-size: 20px; }
.subtitle { color: var(--muted); margin: 0 0 20px; }
.card {
background: var(--card);
border: 1px solid var(--border);
border-radius: 8px;
padding: 16px 20px;
margin-bottom: 16px;
}
.card h2 {
margin: 0 0 12px;
font-size: 15px;
font-weight: 600;
}
.card .step-number {
display: inline-block;
width: 22px;
height: 22px;
line-height: 22px;
text-align: center;
background: var(--accent);
color: white;
border-radius: 50%;
font-size: 12px;
margin-right: 8px;
}
label.entity {
display: flex;
align-items: center;
padding: 8px 0;
cursor: pointer;
border-bottom: 1px solid var(--border);
}
label.entity:last-child { border-bottom: none; }
label.entity input { margin-right: 10px; }
.business-row {
display: flex;
gap: 16px;
}
.business-col {
flex: 1;
}
.business-col label {
display: block;
font-weight: 600;
margin-bottom: 6px;
}
select, button {
font: inherit;
padding: 8px 12px;
border: 1px solid var(--border);
border-radius: 6px;
background: white;
}
select {
width: 100%;
}
select:disabled { background: #f3f4f6; color: var(--muted); }
button {
background: var(--accent);
color: white;
border-color: var(--accent);
cursor: pointer;
font-weight: 600;
}
button:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
.warning {
color: var(--err);
font-size: 12px;
margin-top: 8px;
}
.hidden { display: none !important; }
/* Progress visualization */
#progress { margin-top: 16px; }
.overall {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
.overall-bar {
flex: 1;
height: 10px;
background: #e5e7eb;
border-radius: 999px;
overflow: hidden;
}
.overall-fill {
height: 100%;
width: 0%;
background: var(--accent);
border-radius: 999px;
transition: width 0.3s ease;
}
.overall-fill.done { background: var(--ok); }
.overall-fill.failed { background: var(--err); }
.overall-text {
font-size: 13px;
color: var(--muted);
white-space: nowrap;
min-width: 110px;
text-align: right;
}
.entity-row {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 0;
border-bottom: 1px solid var(--border);
}
.entity-row:last-child { border-bottom: none; }
.entity-row .name {
font-weight: 600;
min-width: 160px;
}
.entity-row .status-text {
color: var(--muted);
font-size: 13px;
flex: 1;
}
.entity-row .status-text.err { color: var(--err); }
.entity-row .status-text.ok { color: var(--ok); }
.status-icon {
width: 22px;
height: 22px;
border-radius: 50%;
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 13px;
font-weight: 700;
color: white;
flex-shrink: 0;
}
.status-icon.pending {
background: transparent;
border: 2px solid #d1d5db;
}
.status-icon.active {
border: 2px solid #dbeafe;
border-top-color: var(--accent);
background: transparent;
animation: spin 0.9s linear infinite;
}
.status-icon.done { background: var(--ok); }
.status-icon.failed { background: var(--err); }
@keyframes spin { to { transform: rotate(360deg); } }
.summary {
margin-top: 16px;
padding: 12px 14px;
border-radius: 6px;
font-weight: 600;
}
.summary.ok { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.summary.err { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
</style>
</head>
<body>
<h1>Replicator</h1>
<p class="subtitle">Copy data from one Manager.io business to another.</p>
<div class="card">
<h2><span class="step-number">1</span>What do you want to copy?</h2>
<div id="entities">
<label class="entity"><input type="checkbox" value="customer"> Customers</label>
<label class="entity"><input type="checkbox" value="supplier"> Suppliers</label>
<label class="entity"><input type="checkbox" value="employee"> Employees</label>
<label class="entity"><input type="checkbox" value="inventory-item"> Inventory items</label>
<label class="entity"><input type="checkbox" value="non-inventory-item"> Non-inventory items</label>
</div>
</div>
<div class="card">
<h2><span class="step-number">2</span>Select businesses</h2>
<div class="business-row">
<div class="business-col">
<label for="source">Source</label>
<select id="source" disabled><option>Loading businesses…</option></select>
</div>
<div class="business-col">
<label for="destination">Destination</label>
<select id="destination" disabled><option>Loading businesses…</option></select>
</div>
</div>
<div id="sameBusinessWarning" class="warning hidden">Source and destination are the same business. Pick different businesses.</div>
</div>
<div class="card">
<h2><span class="step-number">3</span>Replicate</h2>
<button id="runBtn" disabled>Start replication</button>
<div id="progress" class="hidden">
<div class="overall">
<div class="overall-bar"><div class="overall-fill" id="overallFill"></div></div>
<div class="overall-text" id="overallText">0%</div>
</div>
<div id="entityList"></div>
<div id="summary" class="summary hidden"></div>
</div>
</div>
<script>
// Helper per CLAUDE.md — tags every postMessage with a requestId and resolves when Manager replies.
async function postMessageWithResponse(message) {
return new Promise((resolve) => {
const requestId = crypto.randomUUID();
function onMessage(event) {
if (event.data?.type?.endsWith('-response') && event.data?.requestId === requestId) {
window.removeEventListener('message', onMessage);
resolve(event.data.body);
}
}
window.addEventListener('message', onMessage);
window.parent.postMessage({ ...message, requestId }, '*');
});
}
// GET {batchPath}?Business=<name> lists items; PUT {batchPath} with { values, business } writes them.
const ENTITY_DEFS = {
'customer': { label: 'customers', batchPath: '/api4/customer-batch' },
'supplier': { label: 'suppliers', batchPath: '/api4/supplier-batch' },
'employee': { label: 'employees', batchPath: '/api4/employee-batch' },
'inventory-item': { label: 'inventory items', batchPath: '/api4/inventory-item-batch' },
'non-inventory-item': { label: 'non-inventory items', batchPath: '/api4/non-inventory-item-batch' },
};
const PAGE_SIZE = 50;
const sourceSel = document.getElementById('source');
const destSel = document.getElementById('destination');
const runBtn = document.getElementById('runBtn');
const progressEl = document.getElementById('progress');
const entityListEl = document.getElementById('entityList');
const overallFillEl = document.getElementById('overallFill');
const overallTextEl = document.getElementById('overallText');
const summaryEl = document.getElementById('summary');
const warnEl = document.getElementById('sameBusinessWarning');
const entityBoxes = Array.from(document.querySelectorAll('#entities input[type=checkbox]'));
const entityRows = {};
function resetProgress(selected) {
entityListEl.innerHTML = '';
summaryEl.className = 'summary hidden';
summaryEl.textContent = '';
overallFillEl.className = 'overall-fill';
overallFillEl.style.width = '0%';
overallTextEl.textContent = `0 of ${selected.length} done`;
progressEl.classList.remove('hidden');
for (const entity of selected) {
const def = ENTITY_DEFS[entity];
const row = document.createElement('div');
row.className = 'entity-row';
row.innerHTML = `
<span class="status-icon pending"></span>
<span class="name"></span>
<span class="status-text">Waiting…</span>
`;
row.querySelector('.name').textContent = capitalize(def.label);
entityListEl.appendChild(row);
entityRows[entity] = row;
}
}
function capitalize(s) { return s.charAt(0).toUpperCase() + s.slice(1); }
function setEntityStatus(entity, state, text) {
const row = entityRows[entity];
if (!row) return;
const icon = row.querySelector('.status-icon');
const txt = row.querySelector('.status-text');
icon.className = 'status-icon ' + state;
icon.textContent = state === 'done' ? '✓' : state === 'failed' ? '✕' : '';
txt.className = 'status-text' + (state === 'failed' ? ' err' : state === 'done' ? ' ok' : '');
txt.textContent = text;
}
function updateOverall(done, total) {
const pct = total ? Math.round((done / total) * 100) : 0;
overallFillEl.style.width = pct + '%';
overallTextEl.textContent = `${done} of ${total} done`;
}
function setSummary(text, ok) {
summaryEl.className = 'summary ' + (ok ? 'ok' : 'err');
summaryEl.textContent = text;
overallFillEl.classList.add(ok ? 'done' : 'failed');
}
function refreshRunButton() {
const src = sourceSel.value;
const dst = destSel.value;
const anyChecked = entityBoxes.some(cb => cb.checked);
const same = src && dst && src === dst;
warnEl.classList.toggle('hidden', !same);
runBtn.disabled = !(src && dst && !same && anyChecked);
}
entityBoxes.forEach(cb => cb.addEventListener('change', refreshRunButton));
sourceSel.addEventListener('change', refreshRunButton);
destSel.addEventListener('change', refreshRunButton);
async function loadBusinesses() {
const res = await postMessageWithResponse({
type: 'api-request',
method: 'GET',
path: '/api4/businesses'
});
const businesses = res?.businesses || [];
if (!businesses.length) {
sourceSel.innerHTML = '<option>No businesses found</option>';
destSel.innerHTML = '<option>No businesses found</option>';
return;
}
const names = businesses.map(b => b.name).filter(Boolean).sort((a, b) => a.localeCompare(b));
const opts = ['<option value="">-- Select --</option>']
.concat(names.map(n => `<option value="${encodeHtml(n)}">${encodeHtml(n)}</option>`))
.join('');
sourceSel.innerHTML = opts;
destSel.innerHTML = opts;
sourceSel.disabled = false;
destSel.disabled = false;
}
function encodeHtml(s) {
return String(s).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
}
async function fetchAllItems(entity, businessName, onProgress) {
const def = ENTITY_DEFS[entity];
const all = [];
let skip = 0;
while (true) {
const qs = new URLSearchParams({
Business: businessName,
Skip: String(skip),
PageSize: String(PAGE_SIZE),
}).toString();
const res = await postMessageWithResponse({
type: 'api-request',
method: 'GET',
path: `${def.batchPath}?${qs}`
});
const items = res?.items || [];
all.push(...items);
if (onProgress) onProgress(all.length);
if (items.length < PAGE_SIZE) break;
skip += PAGE_SIZE;
}
return all;
}
async function putBatch(entity, values, destinationBusiness) {
const def = ENTITY_DEFS[entity];
return await postMessageWithResponse({
type: 'api-request',
method: 'PUT',
path: def.batchPath,
body: { business: destinationBusiness, values }
});
}
async function replicate() {
runBtn.disabled = true;
const source = sourceSel.value;
const destination = destSel.value;
const selected = entityBoxes.filter(cb => cb.checked).map(cb => cb.value);
resetProgress(selected);
let totalOk = 0, totalFail = 0, doneCount = 0, anyFailed = false;
for (const entity of selected) {
const def = ENTITY_DEFS[entity];
let items;
try {
setEntityStatus(entity, 'active', `Reading from "${source}"…`);
items = await fetchAllItems(entity, source, (loaded) => {
setEntityStatus(entity, 'active', `Reading from "${source}"… (${loaded} loaded)`);
});
} catch (e) {
anyFailed = true;
setEntityStatus(entity, 'failed', `Couldn't read from "${source}": ${e?.message || e}`);
doneCount++;
updateOverall(doneCount, selected.length);
continue;
}
if (!items.length) {
setEntityStatus(entity, 'done', `Nothing to copy.`);
doneCount++;
updateOverall(doneCount, selected.length);
continue;
}
// GET batch returns { key, item }; PUT batch expects { key, value }.
const values = items.map(it => ({ key: it.key, value: it.item }));
setEntityStatus(entity, 'active', `Writing ${values.length} to "${destination}"…`);
try {
const result = await putBatch(entity, values, destination);
if (result === true || result === 'true') {
totalOk += values.length;
setEntityStatus(entity, 'done', `Copied ${values.length} record${values.length === 1 ? '' : 's'}.`);
} else {
anyFailed = true;
totalFail += values.length;
const msg = result?.message || 'Unknown error.';
setEntityStatus(entity, 'failed', `Failed: ${msg}`);
}
} catch (e) {
anyFailed = true;
totalFail += values.length;
setEntityStatus(entity, 'failed', `Failed: ${e?.message || e}`);
}
doneCount++;
updateOverall(doneCount, selected.length);
}
const summary = anyFailed
? `Finished with errors. ${totalOk} copied, ${totalFail} failed.`
: `All done. ${totalOk} record${totalOk === 1 ? '' : 's'} copied.`;
setSummary(summary, !anyFailed);
refreshRunButton();
}
runBtn.addEventListener('click', () => {
replicate().catch(e => {
progressEl.classList.remove('hidden');
setSummary(`Something went wrong: ${e?.message || e}`, false);
refreshRunButton();
});
});
loadBusinesses().catch(() => {
sourceSel.innerHTML = '<option>Error loading</option>';
destSel.innerHTML = '<option>Error loading</option>';
});
</script>
</body>
</html>