grant-admin.mjs 835 Bytes
#!/usr/bin/env node
/**
 * 把一个已注册用户设为运营管理员(role='admin'),用于登录 /admin 审核加入申请。
 * 用法:pnpm --filter @voko/server admin:grant you@example.com
 */
import { createDb } from '@drama/db'

import { databaseUrlFromArgs } from '../src/db/env-file.js'

const email = process.argv[2]
if (!email || email.startsWith('--')) throw new Error('usage: admin:grant <email> [--env file]')
const db = createDb({ url: databaseUrlFromArgs(process.argv.slice(3)), pool: { min: 1, max: 1 } })

try {
  const updated = await db('users').where({ email: email.trim().toLowerCase() }).update({ role: 'admin', updated_at: new Date() })
  if (updated !== 1) throw new Error(`user not found: ${email}`)
  console.log(JSON.stringify({ ok: true, email, role: 'admin' }))
} finally {
  await db.destroy()
}