@nectarcrm/ccx 0.5.2 → 0.5.4
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/bin/ccx.js +23 -4
- package/package.json +1 -1
package/bin/ccx.js
CHANGED
|
@@ -677,9 +677,22 @@ function when(d) {
|
|
|
677
677
|
const dd = Math.floor(mins / 1440), hh = Math.floor((mins % 1440) / 60), mm = mins % 60
|
|
678
678
|
return dd ? `${dd}d ${hh}h` : hh ? `${hh}h${String(mm).padStart(2, '0')}` : `${mm}min`
|
|
679
679
|
}
|
|
680
|
+
// O servidor diz quem dá pra usar (available); app antigo não manda: vale como disponível
|
|
681
|
+
const usableNow = a => a.available !== false
|
|
682
|
+
// Conta no limite (97% ou mais, a regra do painel): vale o limite que estiver mais alto
|
|
683
|
+
function limitText(a) {
|
|
684
|
+
const week = (a.weekly_pct ?? 0) >= (a.five_hour_pct ?? 0)
|
|
685
|
+
const at = week ? a.weekly_resets_at : a.five_hour_resets_at
|
|
686
|
+
const d = at && new Date(at)
|
|
687
|
+
const back = !d ? '' : d.toDateString() === new Date().toDateString()
|
|
688
|
+
? ` · volta ${d.toLocaleTimeString('pt-BR', { hour: '2-digit', minute: '2-digit' })}`
|
|
689
|
+
: ` · volta ${d.toLocaleDateString('pt-BR', { weekday: 'short' }).replace('.', '')} ${d.toLocaleTimeString('pt-BR', { hour: '2-digit', minute: '2-digit' })}`
|
|
690
|
+
return `no limite ${week ? 'da semana' : 'da sessão de 5h'} (${week ? a.weekly_pct : a.five_hour_pct}%)${back}`
|
|
691
|
+
}
|
|
680
692
|
function line(a) {
|
|
681
693
|
const name = a.slug.padEnd(14)
|
|
682
694
|
if (a.five_hour_pct === null && a.weekly_pct === null) return `${name} ${c(90, a.status === 'ok' ? 'sem leitura ainda' : 'desconectada — avise um admin')}`
|
|
695
|
+
if (!usableNow(a) && (a.status === 'ok' || a.status === 'erro_leitura')) return `${name} ${c(31, limitText(a))}`
|
|
683
696
|
const at = s => s && new Date(s)
|
|
684
697
|
return `${name} 5h ${colorPct(a.five_hour_pct ?? 0).padEnd(isTTY ? 13 : 4)} (reinicia em ${when(at(a.five_hour_resets_at))}) semana ${colorPct(a.weekly_pct ?? 0).padEnd(isTTY ? 13 : 4)} (reinicia em ${when(at(a.weekly_resets_at))})`
|
|
685
698
|
}
|
|
@@ -693,7 +706,8 @@ async function api(app, key, method, path, body, timeoutMs = 60_000) {
|
|
|
693
706
|
try {
|
|
694
707
|
res = await fetch(app.url + path, {
|
|
695
708
|
method,
|
|
696
|
-
|
|
709
|
+
// x-ccx-version: o painel mostra a versão de cada computador
|
|
710
|
+
headers: { 'content-type': 'application/json', 'x-ccx-version': myVersion(), ...(key ? { authorization: `Bearer ${key}` } : {}) },
|
|
697
711
|
body: body && JSON.stringify(body),
|
|
698
712
|
signal: AbortSignal.timeout(timeoutMs),
|
|
699
713
|
})
|
|
@@ -742,15 +756,20 @@ async function chooseAccount(app, key) {
|
|
|
742
756
|
if (!list.length) die('Nenhuma conta liberada pra você. Peça a um admin.')
|
|
743
757
|
if (list.length === 1) return list[0].slug
|
|
744
758
|
if (!process.stdin.isTTY) return recommended ?? die('Nenhuma conta disponível agora.')
|
|
745
|
-
|
|
746
|
-
|
|
759
|
+
// Só as que dá pra usar ganham número; as no limite aparecem embaixo, sem número
|
|
760
|
+
const options = list.filter(usableNow)
|
|
761
|
+
const current = a => (a.id === app.account?.id ? c(90, ' (atual)') : '')
|
|
762
|
+
console.error(options.length ? 'Qual conta este computador deve usar?\n' : 'Nenhuma conta com folga agora.\n')
|
|
763
|
+
options.forEach((a, i) => console.error(` ${c(1, String(i + 1))} ${line(a)}${a.slug === recommended ? c(36, ' ← recomendada') : ''}${current(a)}`))
|
|
764
|
+
for (const a of list.filter(a => !usableNow(a))) console.error(` ${c(90, '–')} ${line(a)}${current(a)}`)
|
|
765
|
+
if (!options.length) die('\nPra usar uma delas mesmo assim: ccx <conta>')
|
|
747
766
|
const { createInterface } = await import('node:readline/promises')
|
|
748
767
|
const rl = createInterface({ input: process.stdin, output: process.stderr })
|
|
749
768
|
for (;;) {
|
|
750
769
|
const ans = (await rl.question(`\nNúmero${recommended ? ' (Enter = recomendada)' : ''}: `).catch(() => die('\nCancelado.'))).trim()
|
|
751
770
|
if (!ans && recommended) { rl.close(); return recommended }
|
|
752
771
|
const n = Number(ans)
|
|
753
|
-
if (Number.isInteger(n) && n >= 1 && n <=
|
|
772
|
+
if (Number.isInteger(n) && n >= 1 && n <= options.length) { rl.close(); return options[n - 1].slug }
|
|
754
773
|
console.error('Digite um dos números da lista.')
|
|
755
774
|
}
|
|
756
775
|
}
|