Spreadsheet-UX op een echte relationele engine

main

Deze repository wordt als momentopname gepubliceerd: een clone bevat de volledige inhoud, maar geen commitgeschiedenis.

3.1 kB Ruw bestand
import { isPlanId, PLAN_IDS, planOf } from '@wellformed/shared';
import { effectivePlan, getSubscription, setSubscription } from '../control/billing.js';
import { getWorkspaceBySlug, listAllWorkspaces } from '../control/workspaces.js';
import { openInstallation } from './install.js';

/**
 * Move a workspace between plans from the command line.
 *
 * The operator's lever, and on a self-hosted install the *only* one — there is
 * no provider there to send a webhook. It is also what makes the paid tier
 * operable before any payment integration exists: sell however you like, then
 * run this.
 *
 *   npm run plan                                     # list every workspace
 *   npm run plan -- --workspace acme --plan team
 *   npm run plan -- --workspace acme --plan free
 */
function flag(name: string): string | undefined {
  const index = process.argv.indexOf(`--${name}`);
  if (index === -1) return undefined;
  return process.argv[index + 1];
}

const slug = flag('workspace');
const plan = flag('plan');

const install = openInstallation();

try {
  if (!slug) {
    // No arguments: say what is here, which is what you actually want first.
    const rows = listAllWorkspaces(install.control).map((workspace) => {
      const subscription = getSubscription(install.control, workspace.id);
      return {
        workspace: workspace.slug,
        plan: subscription?.plan ?? 'free',
        status: subscription?.status ?? 'active',
        applies: effectivePlan(subscription),
      };
    });
    if (rows.length === 0) console.log('[wellformed] no workspaces yet.');
    else console.table(rows);
    console.log('');
    console.log(`  npm run plan -- --workspace <slug> --plan <${PLAN_IDS.join('|')}>`);
    console.log('');
    process.exit(0);
  }

  const workspace = getWorkspaceBySlug(install.control, slug);
  if (!workspace) {
    console.error(`[wellformed] no workspace at /w/${slug}`);
    process.exit(1);
  }

  if (!plan) {
    const subscription = getSubscription(install.control, workspace.id);
    console.log(`[wellformed] ${workspace.slug} is on "${subscription?.plan ?? 'free'}" (${subscription?.status ?? 'active'}).`);
    console.log(`[wellformed] the limits that apply right now are "${effectivePlan(subscription)}".`);
    process.exit(0);
  }

  if (!isPlanId(plan)) {
    console.error(`[wellformed] "${plan}" is not a plan. Try one of: ${PLAN_IDS.join(', ')}`);
    process.exit(2);
  }

  setSubscription(install.control, workspace.id, {
    plan,
    status: 'active',
    // Set by hand, so no provider owns it and no period runs out on its own.
    periodEnd: null,
    cancelAtPeriodEnd: false,
    provider: null,
  });

  const { rowLimit, tableLimit } = planOf(plan);
  console.log(`[wellformed] ${workspace.slug} is now on "${plan}".`);
  console.log(
    `[wellformed] limits: ${rowLimit === null ? 'unlimited' : rowLimit.toLocaleString('en-US')} rows, ` +
      `${tableLimit === null ? 'unlimited' : tableLimit} tables.`,
  );
  if (plan === 'free') {
    console.log('[wellformed] nothing is deleted. A workspace over the limit simply stops growing.');
  }
} finally {
  install.close();
}