{"draft":null,"from":2,"to":318,"files":[{"path":"client/src/orgs.ts","added":186,"removed":0,"diff":"--- /dev/null\n+++ b/client/src/orgs.ts\n@@ -0,0 +1,186 @@\n+// Organizations from the CLI: abso orgs and abso org <name>. They read as the\n+// person who approved this box's sign-in (abso login). abso org new makes one, and\n+// abso init --org makes a project in one, from a box approved for all its\n+// person's projects (abso 663). Changing an org (invite, role, remove, leave,\n+// settings) stays on the website: a box sits where agents run, so the server takes\n+// no other org change from it.\n+\n+import { type Parsed, str, UsageError } from \"./args.ts\";\n+import { type Answer, boxCreateOrg, boxGet, originOf, readBox } from \"./box.ts\";\n+import type { Output } from \"./commands.ts\";\n+\n+/** The website for an API server: api.<site> serves <site>; any other server serves its own pages. */\n+export function siteOf(server: string): string {\n+\ttry {\n+\t\tconst url = new URL(server);\n+\t\tif (url.hostname.startsWith(\"api.\")) url.hostname = url.hostname.slice(\"api.\".length);\n+\t\treturn url.origin;\n+\t} catch {\n+\t\treturn server;\n+\t}\n+}\n+\n+/** The flags that change an org, in the order Design wrote them; each names where the website does it. */\n+const CHANGES: readonly { flag: string; tab: string }[] = [\n+\t{ flag: \"invite\", tab: \"People\" },\n+\t{ flag: \"role\", tab: \"People\" },\n+\t{ flag: \"remove\", tab: \"People\" },\n+\t{ flag: \"leave\", tab: \"People\" },\n+\t{ flag: \"members\", tab: \"Settings\" },\n+\t{ flag: \"projects-by\", tab: \"Settings\" },\n+];\n+\n+const WEBSITE_ONLY =\n+\t\"Organizations are changed on the website; from the CLI they can be read, and made with abso org new, as the person who approved this box's sign-in\";\n+\n+/** Where the box's sign-in is read from and how the server is reached; tests pass their own. */\n+export interface OrgIo {\n+\treadonly home?: string;\n+\treadonly fetch?: typeof fetch;\n+}\n+\n+async function read(server: string, path: string, io: OrgIo): Promise<Answer> {\n+\tconst record = io.home === undefined ? readBox(server) : readBox(server, io.home);\n+\tif (!record)\n+\t\tthrow new UsageError(\n+\t\t\t`this box isn't signed in to ${originOf(server)}. abso login signs it in; org reads answer as the person who approves it`,\n+\t\t);\n+\treturn boxGet(record, path, io.fetch);\n+}\n+\n+/** What a refusal from an org read means, in words. */\n+function refused(a: Answer, what: string, server: string): UsageError {\n+\tconst said = typeof a.body.error === \"string\" ? a.body.error : `status ${a.status}`;\n+\tif (a.status === 401)\n+\t\treturn new UsageError(`${originOf(server)} didn't take this box's sign-in (${said}). abso login renews it`);\n+\tif (a.status === 404 && /aren't on here/.test(said)) return new UsageError(`${originOf(server)}: ${said}`);\n+\tif (a.status === 404) return new UsageError(`${what}: ${said}`);\n+\treturn new UsageError(`${originOf(server)} refused ${what} (${a.status}): ${said}`);\n+}\n+\n+const when = (iso: unknown) => (typeof iso === \"string\" ? `${iso.slice(0, 16).replace(\"T\", \" \")} UTC` : \"?\");\n+\n+interface OrgRow {\n+\tname: string;\n+\tdisplayName: string;\n+\trole: string;\n+}\n+interface Invitation {\n+\tid: string;\n+\torg: string;\n+\trole: string;\n+\texpiresAt: string;\n+}\n+\n+/** abso orgs: your organizations, your role in each, and invitations waiting for you. */\n+export async function runOrgs(server: string, io: OrgIo = {}): Promise<Output> {\n+\tconst a = await read(server, \"/orgs\", io);\n+\tif (a.status !== 200) throw refused(a, \"the list of your organizations\", server);\n+\tconst orgs = (a.body.orgs ?? []) as OrgRow[];\n+\tconst invitations = (a.body.invitations ?? []) as Invitation[];\n+\tconst lines =\n+\t\torgs.length === 0\n+\t\t\t? [\"You're in no organizations.\"]\n+\t\t\t: [\n+\t\t\t\t\t`Your organizations (${orgs.length}):`,\n+\t\t\t\t\t...orgs.map(\n+\t\t\t\t\t\t(o) => `  ${o.name}${o.displayName && o.displayName !== o.name ? ` (${o.displayName})` : \"\"}: ${o.role}`,\n+\t\t\t\t\t),\n+\t\t\t\t];\n+\tif (invitations.length > 0)\n+\t\tlines.push(\n+\t\t\t\"Invited to:\",\n+\t\t\t...invitations.map((i) => `  ${i.org} as ${i.role}, until ${when(i.expiresAt)}`),\n+\t\t\t`Accept them on the website, signed in: ${siteOf(server)}.`,\n+\t\t);\n+\treturn { text: lines.join(\"\\n\"), json: { orgs, invitations } };\n+}\n+\n+interface Member {\n+\tid: string;\n+\trole: string;\n+\tname: string | null;\n+\traised?: { project: string; role: string }[];\n+}\n+interface Invite {\n+\tid: string;\n+\trole: string;\n+\tkind: \"link\" | \"person\";\n+\texpiresAt: string;\n+}\n+\n+/** abso org <name>, abso org new <name>, and the change flags, which say where the website does each. */\n+export async function runOrg(args: Parsed, server: string, io: OrgIo = {}): Promise<Output> {\n+\tconst [first, second, ...more] = args.positionals;\n+\tconst asked = CHANGES.filter((c) => args.flags.has(c.flag));\n+\tif (first === \"new\") {\n+\t\tif (second === undefined || more.length > 0) throw new UsageError('abso org new <name> [--name \"<shown name>\"]');\n+\t\tif (asked.length > 0) throw new UsageError(\"abso org new takes --name only\");\n+\t\tconst record = io.home === undefined ? readBox(server) : readBox(server, io.home);\n+\t\tif (!record)\n+\t\t\tthrow new UsageError(\n+\t\t\t\t`this box isn't signed in to ${originOf(server)}. abso login signs it in; approve all your projects to make organizations from here`,\n+\t\t\t);\n+\t\tconst made = await boxCreateOrg(record, second, str(args, \"name\"), io.fetch);\n+\t\tconst name = typeof made.name === \"string\" ? made.name : second;\n+\t\treturn {\n+\t\t\ttext: `Made the organization ${name}; you own it. Its page: ${siteOf(server)}/${name}. abso init --org ${name} --project <name> makes a project in it.`,\n+\t\t\tjson: { org: made },\n+\t\t};\n+\t}\n+\tif (first === undefined || second !== undefined)\n+\t\tthrow new UsageError(\"abso org <name> [one change], or abso org new <name>\");\n+\tif (args.flags.has(\"as\") && !args.flags.has(\"invite\")) throw new UsageError(\"--as goes with --invite\");\n+\tif (asked.length > 1)\n+\t\tthrow new UsageError(`one change at a time: --${asked.map((c) => c.flag).join(\" and --\")} were given together`);\n+\tif (asked.length === 1)\n+\t\tthrow new UsageError(\n+\t\t\t`${WEBSITE_ONLY}. --${asked[0]!.flag} is on ${first}'s ${asked[0]!.tab} tab: ${siteOf(server)}/${first}.`,\n+\t\t);\n+\n+\tconst a = await read(server, `/orgs/${encodeURIComponent(first)}`, io);\n+\tif (a.status !== 200) throw refused(a, first, server);\n+\tconst org = a.body as {\n+\t\tname: string;\n+\t\tdisplayName: string;\n+\t\tdescription: string | null;\n+\t\trole?: string;\n+\t\tmembersCan?: string;\n+\t\twhoMakesProjects?: string;\n+\t\tmembers?: Member[];\n+\t\tprojects?: string[];\n+\t\tinvites?: Invite[];\n+\t};\n+\tconst lines = [`${org.name}${org.displayName && org.displayName !== org.name ? ` (${org.displayName})` : \"\"}`];\n+\tif (org.description) lines.push(`  ${org.description.replace(/\\r?\\n/g, \"\\n  \")}`);\n+\tif (org.role === undefined) {\n+\t\tlines.push(\"You aren't a member, so its people and settings aren't shown.\");\n+\t\treturn { text: lines.join(\"\\n\"), json: { org } };\n+\t}\n+\tlines.push(\n+\t\t`You're ${org.role === \"owner\" ? \"an owner\" : \"a member\"}. Members can ${org.membersCan ?? \"?\"} every project; ${org.whoMakesProjects === \"owners\" ? \"only owners make\" : \"members make\"} projects.`,\n+\t);\n+\tconst projects = org.projects ?? [];\n+\tlines.push(projects.length === 0 ? \"Projects: none yet.\" : `Projects (${projects.length}): ${projects.join(\", \")}`);\n+\tconst members = org.members ?? [];\n+\tlines.push(`People (${members.length}):`);\n+\tfor (const m of members) {\n+\t\tconst raised = (m.raised ?? []).map((r) => `${r.role} in ${r.project}`);\n+\t\tlines.push(\n+\t\t\t`  ${m.name ?? \"(no name yet)\"}: ${m.role}${raised.length > 0 ? `, raised to ${raised.join(\", \")}` : \"\"}`,\n+\t\t);\n+\t}\n+\tif (org.invites !== undefined && org.invites.length > 0) {\n+\t\tlines.push(\"Open invites:\");\n+\t\tfor (const i of org.invites)\n+\t\t\tlines.push(`  ${i.kind === \"link\" ? \"a link\" : \"a person, by name\"}, as ${i.role}, until ${when(i.expiresAt)}`);\n+\t}\n+\treturn { text: lines.join(\"\\n\"), json: { org } };\n+}\n+\n+/** abso init --org with no box signed in to the server: an org's project is made as the person who approved one (abso 663). */\n+export function initInOrg(org: string, server: string): never {\n+\tthrow new UsageError(\n+\t\t`this box isn't signed in to ${originOf(server)}, and a project in ${org} is made as a signed-in person. abso login, approving all your projects, then abso init --org ${org} again; or New project at ${siteOf(server)}.`,\n+\t);\n+}"}],"unmatched":[]}