u
This commit is contained in:
@@ -40,6 +40,16 @@ async function askInt(message, placeholder = '1') {
|
||||
});
|
||||
}
|
||||
|
||||
async function askNumber(message, placeholder = '19') {
|
||||
const value = await askText(message, {
|
||||
placeholder,
|
||||
validate: (v) =>
|
||||
v?.trim() === '' || Number.isNaN(Number(v)) ? 'Number required' : undefined,
|
||||
});
|
||||
if (value == null) return null;
|
||||
return Number(value);
|
||||
}
|
||||
|
||||
async function shutdown(pool, code = 0) {
|
||||
try {
|
||||
await pool.close();
|
||||
@@ -60,8 +70,10 @@ const MENU = {
|
||||
company: {
|
||||
label: 'company',
|
||||
items: [
|
||||
{ value: 'company.create', label: 'create', hint: '{name} → id' },
|
||||
{ value: 'company.create', label: 'create', hint: 'iso639,name,taxrate,reducedtaxrate → id' },
|
||||
{ value: 'company.update', label: 'update', hint: 'id + same fields' },
|
||||
{ value: 'company.list', label: 'list', hint: '→ rows' },
|
||||
{ value: 'company.getById', label: 'getById', hint: 'id → row' },
|
||||
{ value: 'company.setDefaultLang', label: 'setDefaultLang', hint: 'companyId, langId' },
|
||||
{ value: 'company.setRootCategory', label: 'setRootCategory', hint: 'companyId, categoryId' },
|
||||
{ value: 'company.delete', label: 'delete', hint: 'companyId' },
|
||||
@@ -76,6 +88,14 @@ const MENU = {
|
||||
{ value: 'lang.delete', label: 'delete', hint: 'id' },
|
||||
],
|
||||
},
|
||||
tax: {
|
||||
label: 'tax',
|
||||
items: [
|
||||
{ value: 'tax.create', label: 'create', hint: 'rate → id' },
|
||||
{ value: 'tax.list', label: 'list', hint: '→ rows' },
|
||||
{ value: 'tax.ensure', label: 'ensure', hint: 'rate → id' },
|
||||
],
|
||||
},
|
||||
category: {
|
||||
label: 'category',
|
||||
items: [
|
||||
@@ -99,6 +119,7 @@ const MENU = {
|
||||
{ value: 'google_auth.add', label: 'add', hint: 'email → authId' },
|
||||
{ value: 'google_auth.getById', label: 'getById', hint: 'id → row' },
|
||||
{ value: 'google_auth.getByEmail', label: 'getByEmail', hint: 'email → row' },
|
||||
{ value: 'google_auth.setSelectedCompany', label: 'setSelectedCompany', hint: 'authId, companyId' },
|
||||
{ value: 'google_auth.list', label: 'list', hint: '→ rows' },
|
||||
{ value: 'google_auth.del', label: 'del', hint: 'id' },
|
||||
],
|
||||
@@ -157,15 +178,17 @@ async function runMethod(client, action) {
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (iso639 == null) return;
|
||||
const taxrate = await askNumber('Tax rate', '19');
|
||||
if (taxrate == null) return;
|
||||
const reducedtaxrate = await askNumber('Reduced tax rate', '7');
|
||||
if (reducedtaxrate == null) return;
|
||||
|
||||
const langs = await client.lang.list();
|
||||
const existingLang = langs.find((row) => row.iso639 === iso639);
|
||||
const langId = existingLang
|
||||
? existingLang.id
|
||||
: await client.lang.create(iso639);
|
||||
|
||||
const companyId = await client.company.create({ name });
|
||||
await client.company.setDefaultLang(companyId, langId);
|
||||
const companyId = await client.company.create({
|
||||
iso639,
|
||||
name,
|
||||
taxrate,
|
||||
reducedtaxrate,
|
||||
});
|
||||
|
||||
const auths = await client.google_auth.list();
|
||||
const existingAuth = auths.find((row) => row.email === email);
|
||||
@@ -174,6 +197,7 @@ async function runMethod(client, action) {
|
||||
: await client.google_auth.add(email);
|
||||
|
||||
const companyUserId = await client.companyuser.add(companyId, authId);
|
||||
await client.google_auth.setSelectedCompany(authId, companyId);
|
||||
|
||||
return [
|
||||
'created',
|
||||
@@ -181,11 +205,11 @@ async function runMethod(client, action) {
|
||||
companyId,
|
||||
companyUserId,
|
||||
authId,
|
||||
langId,
|
||||
email,
|
||||
name,
|
||||
iso639,
|
||||
langCreated: !existingLang,
|
||||
taxrate,
|
||||
reducedtaxrate,
|
||||
authCreated: !existingAuth,
|
||||
},
|
||||
];
|
||||
@@ -196,11 +220,53 @@ async function runMethod(client, action) {
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (name == null) return;
|
||||
return ['id', await client.company.create({ name })];
|
||||
const iso639 = await askText('iso639', {
|
||||
placeholder: 'de',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (iso639 == null) return;
|
||||
const taxrate = await askNumber('taxrate', '19');
|
||||
if (taxrate == null) return;
|
||||
const reducedtaxrate = await askNumber('reducedtaxrate', '7');
|
||||
if (reducedtaxrate == null) return;
|
||||
return [
|
||||
'id',
|
||||
await client.company.create({ iso639, name, taxrate, reducedtaxrate }),
|
||||
];
|
||||
}
|
||||
case 'company.update': {
|
||||
const id = await askInt('Company id');
|
||||
if (id == null) return;
|
||||
const name = await askText('Name', {
|
||||
placeholder: 'Acme',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (name == null) return;
|
||||
const iso639 = await askText('iso639', {
|
||||
placeholder: 'de',
|
||||
validate: (v) => (!v?.trim() ? 'Required' : undefined),
|
||||
});
|
||||
if (iso639 == null) return;
|
||||
const taxrate = await askNumber('taxrate', '19');
|
||||
if (taxrate == null) return;
|
||||
const reducedtaxrate = await askNumber('reducedtaxrate', '7');
|
||||
if (reducedtaxrate == null) return;
|
||||
await client.company.update(Number(id), {
|
||||
iso639,
|
||||
name,
|
||||
taxrate,
|
||||
reducedtaxrate,
|
||||
});
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'company.list': {
|
||||
return ['rows', await client.company.list()];
|
||||
}
|
||||
case 'company.getById': {
|
||||
const id = await askInt('Company id');
|
||||
if (id == null) return;
|
||||
return ['row', await client.company.getById(Number(id))];
|
||||
}
|
||||
case 'company.setDefaultLang': {
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
@@ -231,6 +297,19 @@ async function runMethod(client, action) {
|
||||
await client.company.getByCompanyUserId(Number(id)),
|
||||
];
|
||||
}
|
||||
case 'tax.create': {
|
||||
const rate = await askNumber('rate', '19');
|
||||
if (rate == null) return;
|
||||
return ['id', await client.tax.create(rate)];
|
||||
}
|
||||
case 'tax.list': {
|
||||
return ['rows', await client.tax.list()];
|
||||
}
|
||||
case 'tax.ensure': {
|
||||
const rate = await askNumber('rate', '19');
|
||||
if (rate == null) return;
|
||||
return ['id', await client.tax.ensure(rate)];
|
||||
}
|
||||
case 'lang.create': {
|
||||
const iso639 = await askText('ISO 639', {
|
||||
placeholder: 'de',
|
||||
@@ -326,6 +405,14 @@ async function runMethod(client, action) {
|
||||
if (email == null) return;
|
||||
return ['row', await client.google_auth.getByEmail(email)];
|
||||
}
|
||||
case 'google_auth.setSelectedCompany': {
|
||||
const id = await askInt('Auth id');
|
||||
if (id == null) return;
|
||||
const companyId = await askInt('Company id');
|
||||
if (companyId == null) return;
|
||||
await client.google_auth.setSelectedCompany(Number(id), Number(companyId));
|
||||
return ['ok', true];
|
||||
}
|
||||
case 'google_auth.list': {
|
||||
return ['rows', await client.google_auth.list()];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user