The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as turso from "@distilled.cloud/turso/turso"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import { organization } from "./Credentials.ts"; |
| 7 | import type { MemberRole } from "./OrganizationInvite.ts"; |
| 8 | import type { Providers } from "./Providers.ts"; |
| 9 | |
| 10 | export interface OrganizationMemberProps { |
| 11 | /** Username of an existing Turso user. Changing it replaces the member. */ |
| 12 | username: string; |
| 13 | /** |
| 14 | * Role of the member in the organization. |
| 15 | * @default "member" |
| 16 | */ |
| 17 | role?: MemberRole; |
| 18 | } |
| 19 | |
| 20 | export interface OrganizationMemberAttributes { |
| 21 | /** Username of the member. */ |
| 22 | username: string; |
| 23 | /** The organization slug. */ |
| 24 | organization: string; |
| 25 | /** Role of the member. */ |
| 26 | role: string; |
| 27 | /** Email of the member. */ |
| 28 | email: string | undefined; |
| 29 | } |
| 30 | |
| 31 | export type OrganizationMember = Resource< |
| 32 | "Turso.OrganizationMember", |
| 33 | OrganizationMemberProps, |
| 34 | OrganizationMemberAttributes, |
| 35 | never, |
| 36 | Providers |
| 37 | >; |
| 38 | |
| 39 | /** |
| 40 | * Adds an existing Turso user to your organization with a role, and keeps |
| 41 | * that role in sync. Destroying the resource removes the member. Use |
| 42 | * {@link OrganizationInvite} for people who don't have a Turso account |
| 43 | * yet. Requires a team organization on a paid plan. |
| 44 | * @see https://docs.turso.tech/api-reference/organizations/members/add |
| 45 | * |
| 46 | * ### Managing Members |
| 47 | * **Example:** Add a user as a viewer |
| 48 | * ```typescript |
| 49 | * yield* Turso.OrganizationMember("Bob", { |
| 50 | * username: "bob", |
| 51 | * role: "viewer", |
| 52 | * }); |
| 53 | * ``` |
| 54 | * |
| 55 | * @resource |
| 56 | * @product Organization |
| 57 | */ |
| 58 | export const OrganizationMember = Resource<OrganizationMember>("Turso.OrganizationMember"); |
| 59 | |
| 60 | export const OrganizationMemberProvider = () => |
| 61 | Provider.effect( |
| 62 | OrganizationMember, |
| 63 | Effect.gen(function* () { |
| 64 | const observe = (org: string, username: string) => |
| 65 | turso.getOrganizationMember({ organizationSlug: org, username }).pipe( |
| 66 | Effect.map(({ member }) => member), |
| 67 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 68 | ); |
| 69 | |
| 70 | return { |
| 71 | stables: ["username", "organization"], |
| 72 | diff: Effect.fn(function* ({ olds, news }) { |
| 73 | if (!isResolved(news)) return undefined; |
| 74 | if (news.username !== olds?.username) return { action: "replace" } as const; |
| 75 | }), |
| 76 | read: Effect.fn(function* ({ olds, output }) { |
| 77 | const org = output?.organization ?? (yield* organization); |
| 78 | const member = yield* observe(org, output?.username ?? olds.username); |
| 79 | if (!member?.username) return undefined; |
| 80 | return { |
| 81 | username: member.username, |
| 82 | organization: org, |
| 83 | role: member.role ?? "member", |
| 84 | email: member.email, |
| 85 | }; |
| 86 | }), |
| 87 | reconcile: Effect.fn(function* ({ news, output }) { |
| 88 | const org = output?.organization ?? (yield* organization); |
| 89 | const role = news.role ?? "member"; |
| 90 | |
| 91 | // Observe |
| 92 | let member = yield* observe(org, news.username); |
| 93 | |
| 94 | // Ensure |
| 95 | if (!member) { |
| 96 | yield* turso |
| 97 | .addOrganizationMember({ organizationSlug: org, username: news.username, role }) |
| 98 | .pipe(Effect.catchTag("Conflict", () => Effect.void)); |
| 99 | member = yield* observe(org, news.username); |
| 100 | } |
| 101 | |
| 102 | // Sync role |
| 103 | if (member?.role !== role) { |
| 104 | yield* turso.updateMemberRole({ |
| 105 | organizationSlug: org, |
| 106 | username: news.username, |
| 107 | role, |
| 108 | }); |
| 109 | } |
| 110 | return { username: news.username, organization: org, role, email: member?.email }; |
| 111 | }), |
| 112 | delete: Effect.fn(function* ({ output }) { |
| 113 | yield* turso |
| 114 | .removeOrganizationMember({ |
| 115 | organizationSlug: output.organization, |
| 116 | username: output.username, |
| 117 | }) |
| 118 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 119 | }), |
| 120 | }; |
| 121 | }), |
| 122 | ); |