The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as turso from "@distilled.cloud/turso/turso"; |
| 2 | import * as Provider from "alchemy/Provider"; |
| 3 | import { Resource } from "alchemy/Resource"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | import { organization } from "./Credentials.ts"; |
| 7 | import type { Providers } from "./Providers.ts"; |
| 8 | |
| 9 | export interface OrganizationSettingsProps { |
| 10 | /** Allow usage beyond the plan's quota to be billed as overages. */ |
| 11 | overages?: boolean; |
| 12 | /** |
| 13 | * Require every member to enable multi-factor authentication. The |
| 14 | * deploying user must have MFA enabled first. |
| 15 | */ |
| 16 | requireMfa?: boolean; |
| 17 | /** |
| 18 | * Keep deleted databases and groups recoverable. Requires an admin or |
| 19 | * owner. |
| 20 | */ |
| 21 | restoreEnabled?: boolean; |
| 22 | } |
| 23 | |
| 24 | interface Settings { |
| 25 | overages: boolean; |
| 26 | requireMfa: boolean; |
| 27 | restoreEnabled: boolean; |
| 28 | } |
| 29 | |
| 30 | export interface OrganizationSettingsAttributes extends Settings { |
| 31 | /** The organization slug. */ |
| 32 | organization: string; |
| 33 | /** Settings observed before Alchemy first changed them; restored on destroy. */ |
| 34 | original: Settings; |
| 35 | } |
| 36 | |
| 37 | export type OrganizationSettings = Resource< |
| 38 | "Turso.OrganizationSettings", |
| 39 | OrganizationSettingsProps, |
| 40 | OrganizationSettingsAttributes, |
| 41 | never, |
| 42 | Providers |
| 43 | >; |
| 44 | |
| 45 | /** |
| 46 | * Settings of the Turso organization your credentials act on. Only the |
| 47 | * settings you set are managed; destroying the resource restores the values |
| 48 | * observed before Alchemy first changed them. |
| 49 | * @see https://docs.turso.tech/api-reference/organizations/update |
| 50 | * |
| 51 | * ### Configuring the Organization |
| 52 | * **Example:** Enable overages and require MFA |
| 53 | * ```typescript |
| 54 | * yield* Turso.OrganizationSettings("Org", { |
| 55 | * overages: true, |
| 56 | * requireMfa: true, |
| 57 | * }); |
| 58 | * ``` |
| 59 | * |
| 60 | * @resource |
| 61 | * @product Organization |
| 62 | */ |
| 63 | export const OrganizationSettings = Resource<OrganizationSettings>("Turso.OrganizationSettings"); |
| 64 | |
| 65 | const toSettings = (org: turso.Organization | undefined): Settings => ({ |
| 66 | overages: org?.overages ?? false, |
| 67 | requireMfa: org?.require_mfa ?? false, |
| 68 | restoreEnabled: org?.restore_enabled ?? false, |
| 69 | }); |
| 70 | |
| 71 | export const OrganizationSettingsProvider = () => |
| 72 | Provider.effect( |
| 73 | OrganizationSettings, |
| 74 | Effect.gen(function* () { |
| 75 | const observe = (slug: string) => |
| 76 | turso |
| 77 | .getOrganization({ organizationSlug: slug }) |
| 78 | .pipe(Effect.map(({ organization }) => toSettings(organization))); |
| 79 | |
| 80 | // `restore_enabled` must be updated on its own (per the API). |
| 81 | const apply = (slug: string, observed: Settings, desired: Partial<Settings>) => |
| 82 | Effect.gen(function* () { |
| 83 | const patch: { overages?: boolean; require_mfa?: boolean } = {}; |
| 84 | if (desired.overages !== undefined && desired.overages !== observed.overages) { |
| 85 | patch.overages = desired.overages; |
| 86 | } |
| 87 | if (desired.requireMfa !== undefined && desired.requireMfa !== observed.requireMfa) { |
| 88 | patch.require_mfa = desired.requireMfa; |
| 89 | } |
| 90 | if (Object.keys(patch).length > 0) { |
| 91 | yield* turso.updateOrganization({ organizationSlug: slug, ...patch }); |
| 92 | } |
| 93 | if ( |
| 94 | desired.restoreEnabled !== undefined && |
| 95 | desired.restoreEnabled !== observed.restoreEnabled |
| 96 | ) { |
| 97 | yield* turso.updateOrganization({ |
| 98 | organizationSlug: slug, |
| 99 | restore_enabled: desired.restoreEnabled, |
| 100 | }); |
| 101 | } |
| 102 | }); |
| 103 | |
| 104 | return { |
| 105 | stables: ["organization", "original"], |
| 106 | read: Effect.fn(function* ({ output }) { |
| 107 | if (!output) return undefined; |
| 108 | return { ...output, ...(yield* observe(output.organization)) }; |
| 109 | }), |
| 110 | reconcile: Effect.fn(function* ({ news, output }) { |
| 111 | const slug = output?.organization ?? (yield* organization); |
| 112 | const observed = yield* observe(slug); |
| 113 | yield* apply(slug, observed, news); |
| 114 | // Organization reads can briefly lag a PATCH; wait until the |
| 115 | // settings we set are visible. |
| 116 | const settled = yield* observe(slug).pipe( |
| 117 | Effect.repeat({ |
| 118 | schedule: Schedule.spaced("1 second"), |
| 119 | until: (current) => |
| 120 | (news.overages === undefined || current.overages === news.overages) && |
| 121 | (news.requireMfa === undefined || current.requireMfa === news.requireMfa) && |
| 122 | (news.restoreEnabled === undefined || |
| 123 | current.restoreEnabled === news.restoreEnabled), |
| 124 | times: 10, |
| 125 | }), |
| 126 | ); |
| 127 | return { |
| 128 | organization: slug, |
| 129 | original: output?.original ?? observed, |
| 130 | ...settled, |
| 131 | }; |
| 132 | }), |
| 133 | delete: Effect.fn(function* ({ olds, output }) { |
| 134 | const observed = yield* observe(output.organization); |
| 135 | // Restore only the settings this resource managed. |
| 136 | yield* apply(output.organization, observed, { |
| 137 | overages: olds.overages !== undefined ? output.original.overages : undefined, |
| 138 | requireMfa: olds.requireMfa !== undefined ? output.original.requireMfa : undefined, |
| 139 | restoreEnabled: |
| 140 | olds.restoreEnabled !== undefined ? output.original.restoreEnabled : undefined, |
| 141 | }); |
| 142 | }), |
| 143 | }; |
| 144 | }), |
| 145 | ); |