This commit is contained in:
seb
2026-07-31 00:35:02 +02:00
parent a47c598f76
commit fdd9e684d8
10 changed files with 169 additions and 2 deletions

View File

@@ -50,6 +50,11 @@ async function shutdown(pool, code = 0) {
}
const METHODS = [
{
value: 'setup.company',
label: chalk.cyan('setup company'),
hint: 'email + name + lang',
},
{ value: 'company.create', label: 'company.create', hint: '{name} → id' },
{ value: 'company.list', label: 'company.list', hint: '→ rows' },
{ value: 'company.setDefaultLang', label: 'company.setDefaultLang', hint: 'companyId, langId' },
@@ -60,8 +65,11 @@ const METHODS = [
{ value: 'lang.list', label: 'lang.list', hint: '→ rows' },
{ value: 'lang.delete', label: 'lang.delete', hint: 'id' },
{ value: 'category.create', label: 'category.create', hint: '{parent_id, sort} → id' },
{ value: 'category.updateParentId', label: 'category.updateParentId', hint: 'id, parentId' },
{ value: 'category.delete', label: 'category.delete', hint: 'id' },
{ value: 'category.getTree', label: 'category.getTree', hint: 'rootId → rows' },
{ value: 'companyuser.add', label: 'companyuser.add', hint: 'companyId, authId → id' },
{ value: 'companyuser.delete', label: 'companyuser.delete', hint: 'id' },
{ value: 'companyuser.getByEmail', label: 'companyuser.getByEmail', hint: 'email → ids' },
{ value: 'google_auth.add', label: 'google_auth.add', hint: 'email → authId' },
{ value: 'google_auth.list', label: 'google_auth.list', hint: '→ rows' },
@@ -73,6 +81,55 @@ const METHODS = [
async function runMethod(client, action) {
switch (action) {
case 'setup.company': {
const email = await askText('Email', {
placeholder: 'user@example.com',
validate: (v) => (!v?.trim() ? 'Required' : undefined),
});
if (email == null) return;
const name = await askText('Company name', {
placeholder: 'Acme',
validate: (v) => (!v?.trim() ? 'Required' : undefined),
});
if (name == null) return;
const iso639 = await askText('Default language (iso639)', {
placeholder: 'de',
validate: (v) => (!v?.trim() ? 'Required' : undefined),
});
if (iso639 == 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 auths = await client.google_auth.list();
const existingAuth = auths.find((row) => row.email === email);
const authId = existingAuth
? existingAuth.id
: await client.google_auth.add(email);
const companyUserId = await client.companyuser.add(companyId, authId);
return [
'created',
{
companyId,
companyUserId,
authId,
langId,
email,
name,
iso639,
langCreated: !existingLang,
authCreated: !existingAuth,
},
];
}
case 'company.create': {
const name = await askText('Name', {
placeholder: 'Acme',
@@ -143,6 +200,25 @@ async function runMethod(client, action) {
const parent_id = parentRaw === '' ? null : Number(parentRaw);
return ['id', await client.category.create({ parent_id, sort: Number(sortRaw) })];
}
case 'category.updateParentId': {
const id = await askInt('Category id');
if (id == null) return;
const parentRaw = await askText('parent_id (null as empty)', {
placeholder: '',
validate: (v) =>
v?.trim() && !/^\d+$/.test(v.trim()) ? 'Integer or empty' : undefined,
});
if (parentRaw == null) return;
const parentId = parentRaw === '' ? null : Number(parentRaw);
await client.category.updateParentId(Number(id), parentId);
return ['ok', true];
}
case 'category.delete': {
const id = await askInt('Category id');
if (id == null) return;
await client.category.delete(Number(id));
return ['ok', true];
}
case 'category.getTree': {
const rootId = await askInt('Root category id');
if (rootId == null) return;
@@ -155,6 +231,12 @@ async function runMethod(client, action) {
if (authId == null) return;
return ['id', await client.companyuser.add(Number(companyId), Number(authId))];
}
case 'companyuser.delete': {
const id = await askInt('Company user id');
if (id == null) return;
await client.companyuser.delete(Number(id));
return ['ok', true];
}
case 'companyuser.getByEmail': {
const email = await askText('Email', {
placeholder: 'user@example.com',
@@ -237,6 +319,24 @@ async function main() {
}
if (action === 'purge') {
const confirm1 = await p.confirm({
message: chalk.red('Drop ALL tables?'),
initialValue: false,
});
if (p.isCancel(confirm1) || !confirm1) {
p.log.warn('Purge cancelled');
continue;
}
const confirm2 = await p.confirm({
message: chalk.red('Really purge? This cannot be undone.'),
initialValue: false,
});
if (p.isCancel(confirm2) || !confirm2) {
p.log.warn('Purge cancelled');
continue;
}
const run = p.spinner();
run.start('Purging…');
await client.purge();