import type { CorpConfig } from "../../core/config/types.js"; import type { createSealClient } from "../../core/http/factory.js"; import { confirmationRequired, usageError } from "../../core/errors.js"; import { runCapability } from "../helpers.js"; import { booleanOption, dryRunOption, parseDelegationExpireHours, stringOption } from "../parse/parser.js"; import { outputOptions } from "../output/printer.js"; import type { ParsedArgs } from "../types.js"; export async function handleUsersCommand( action: string | undefined, maybeName: string | undefined, client: Awaited>, corp: CorpConfig, args: ParsedArgs ) { if (action === "create") { const name = stringOption(args.options.name) ?? maybeName; const phone = stringOption(args.options.phone); if (!name || !phone) throw usageError("Usage: seal-use users create --name --phone [--dept ] [--dry-run]"); const deptId = stringOption(args.options.dept) ?? stringOption(args.options.departmentId); const departmentIds = deptId ? [deptId] : undefined; await runCapability( "sealUserCreate", client, corp, { name, phone, departmentIds, dryRun: dryRunOption(args) }, outputOptions(args.options) ); return; } if (action === "reset-password") { const userId = maybeName ?? stringOption(args.options.userId); if (!userId) throw usageError("Usage: seal-use users reset-password [--dry-run] --yes"); const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; const dryRun = dryRunOption(args); if (!confirmed && !dryRun) { throw confirmationRequired( `Refusing to reset password of user '${userId}' without --yes.`, `seal-use users reset-password ${userId} --corp ${corp.id} --yes` ); } await runCapability("sealUserResetPassword", client, corp, { userId, dryRun }, outputOptions(args.options)); return; } if (action === "search" || action === "list") { const keyword = maybeName ?? stringOption(args.options.keyword) ?? stringOption(args.options.query) ?? ""; await runCapability("sealUserSearch", client, corp, { keyword }, outputOptions(args.options)); return; } if (action === "get") { const userId = maybeName ?? stringOption(args.options.userId); if (!userId) throw usageError("Usage: seal-use users get "); await runCapability("sealUserGet", client, corp, { userId }, outputOptions(args.options)); return; } if (action === "delete") { const userId = maybeName ?? stringOption(args.options.userId); if (!userId) throw usageError("Usage: seal-use users delete [--dry-run] --yes"); const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; const dryRun = dryRunOption(args); if (!confirmed && !dryRun) { throw confirmationRequired( `Refusing to delete user '${userId}' without --yes.`, `seal-use users delete ${userId} --corp ${corp.id} --yes` ); } await runCapability("sealUserDelete", client, corp, { userId, dryRun }, outputOptions(args.options)); return; } if (action === "activate") { const userId = maybeName ?? stringOption(args.options.userId); if (!userId) throw usageError("Usage: seal-use users activate [--dry-run] --yes"); const confirmed = booleanOption(args.options.yes) === true || booleanOption(args.options.confirm) === true; const dryRun = dryRunOption(args); if (!confirmed && !dryRun) { throw confirmationRequired( `Refusing to activate user '${userId}' without --yes.`, `seal-use users activate ${userId} --corp ${corp.id} --yes` ); } await runCapability("sealUserActivate", client, corp, { userId, dryRun }, outputOptions(args.options)); return; } if (action === "delegation" || action === "impersonate") { const userIdOrPhone = maybeName ?? stringOption(args.options.userId) ?? stringOption(args.options.phone); if (!userIdOrPhone) throw usageError("Usage: seal-use users delegation [--note ] [--expire ]"); const note = stringOption(args.options.note); const expireOption = args.options.expire ?? args.options["expires-in"] ?? args.options.expiresIn; const expiresInHours = parseDelegationExpireHours(expireOption, 24); await runCapability( "sealUserDelegationCreate", client, corp, { userIdOrPhone, note, expiresInHours, dryRun: dryRunOption(args) }, outputOptions(args.options) ); return; } throw usageError(`Unknown users command: ${action ?? ""}. Run 'seal-use help' for available commands.`); }