|
| 1 | +import { |
| 2 | + defineCommand, |
| 3 | + detectOutputFormat, |
| 4 | + type Config, |
| 5 | + type GlobalFlags, |
| 6 | + BailianError, |
| 7 | + ExitCode, |
| 8 | +} from "bailian-cli-core"; |
| 9 | +import { emitResult, emitBare } from "../../output/output.ts"; |
| 10 | +import { padEnd } from "../../output/cjk-width.ts"; |
| 11 | +import type { AddOrganizationMemberResponse } from "./types.ts"; |
| 12 | +import { |
| 13 | + TOKEN_PLAN_AK_OPTIONS, |
| 14 | + TOKEN_PLAN_COMMON_QUERY_OPTIONS, |
| 15 | + appendCommonQueryParams, |
| 16 | + callTokenPlanApi, |
| 17 | + prepareTokenPlanRequest, |
| 18 | + resolveTokenPlanCredentials, |
| 19 | + type TokenPlanQueryParams, |
| 20 | +} from "./utils.ts"; |
| 21 | + |
| 22 | +const API_ACTION = "AddOrganizationMember"; |
| 23 | +const API_PATH = "/tokenplan/organization/member-additions"; |
| 24 | + |
| 25 | +const DEFAULT_ORG_ROLE = "ORG_MEMBER"; |
| 26 | + |
| 27 | +export default defineCommand({ |
| 28 | + name: "tokenplan add-member", |
| 29 | + description: "Add a member to a Token Plan organization", |
| 30 | + usage: "bl tokenplan add-member --account-name <name> --org-id <id> [flags]", |
| 31 | + options: [ |
| 32 | + { flag: "--account-name <name>", description: "Member display name", required: true }, |
| 33 | + { flag: "--org-id <id>", description: "Organization ID", required: true }, |
| 34 | + { |
| 35 | + flag: "--org-role-code <code>", |
| 36 | + description: "Organization role: ORG_ADMIN or ORG_MEMBER (default: ORG_MEMBER)", |
| 37 | + }, |
| 38 | + { |
| 39 | + flag: "--spec-type <type>", |
| 40 | + description: "Seat tier to assign on creation: standard, pro, or max", |
| 41 | + }, |
| 42 | + ...TOKEN_PLAN_COMMON_QUERY_OPTIONS, |
| 43 | + ...TOKEN_PLAN_AK_OPTIONS, |
| 44 | + ], |
| 45 | + examples: [ |
| 46 | + "bl tokenplan add-member --account-name dev_user --org-id org_123", |
| 47 | + "bl tokenplan add-member --account-name admin_user --org-id org_123 --org-role-code ORG_ADMIN", |
| 48 | + "bl tokenplan add-member --account-name member1 --org-id org_123 --spec-type standard", |
| 49 | + ], |
| 50 | + async run(config: Config, flags: GlobalFlags) { |
| 51 | + const format = detectOutputFormat(config.output); |
| 52 | + const credentials = resolveTokenPlanCredentials(config, flags); |
| 53 | + |
| 54 | + const accountName = flags.accountName as string | undefined; |
| 55 | + const orgId = flags.orgId as string | undefined; |
| 56 | + if (!accountName) { |
| 57 | + throw new BailianError("Missing required argument --account-name.", ExitCode.USAGE); |
| 58 | + } |
| 59 | + if (!orgId) { |
| 60 | + throw new BailianError("Missing required argument --org-id.", ExitCode.USAGE); |
| 61 | + } |
| 62 | + |
| 63 | + const queryParams = buildQueryParams(flags); |
| 64 | + |
| 65 | + if (config.dryRun) { |
| 66 | + const { endpoint, queryParams: query } = prepareTokenPlanRequest( |
| 67 | + config, |
| 68 | + API_PATH, |
| 69 | + queryParams, |
| 70 | + ); |
| 71 | + emitResult({ endpoint, query }, format); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + const data = await callTokenPlanApi<AddOrganizationMemberResponse>({ |
| 76 | + config, |
| 77 | + credentials, |
| 78 | + action: API_ACTION, |
| 79 | + path: API_PATH, |
| 80 | + method: "POST", |
| 81 | + queryParams, |
| 82 | + }); |
| 83 | + |
| 84 | + if (config.quiet || format === "text") { |
| 85 | + emitTextMember(data); |
| 86 | + } else { |
| 87 | + emitResult(data, format); |
| 88 | + } |
| 89 | + }, |
| 90 | +}); |
| 91 | + |
| 92 | +function buildQueryParams(flags: GlobalFlags): TokenPlanQueryParams { |
| 93 | + const params: TokenPlanQueryParams = {}; |
| 94 | + |
| 95 | + if (flags.accountName) params.AccountName = flags.accountName as string; |
| 96 | + if (flags.orgId) params.OrgId = flags.orgId as string; |
| 97 | + params.OrgRoleCode = |
| 98 | + typeof flags.orgRoleCode === "string" && flags.orgRoleCode.length > 0 |
| 99 | + ? flags.orgRoleCode |
| 100 | + : DEFAULT_ORG_ROLE; |
| 101 | + if (flags.specType) params.SpecType = flags.specType as string; |
| 102 | + appendCommonQueryParams(params, flags); |
| 103 | + |
| 104 | + return params; |
| 105 | +} |
| 106 | + |
| 107 | +function emitTextMember(data: AddOrganizationMemberResponse): void { |
| 108 | + const item = data.Data; |
| 109 | + if (!item) { |
| 110 | + emitBare("Member added."); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + emitBare(`${padEnd("AccountId", 14)} ${item.AccountId ?? "-"}`); |
| 115 | + emitBare(`${padEnd("SeatAssigned", 14)} ${String(item.SeatAssigned ?? "-")}`); |
| 116 | +} |
0 commit comments