|
| 1 | +// --------------------------------------------------------------------------- |
| 2 | +// The dashboard's link to Executor v2's workspace import. |
| 3 | +// |
| 4 | +// v2 imports a v1 organization only when all of these hold (v2's |
| 5 | +// `V1Import.choices`), and v1 shows the link under exactly the same rules: |
| 6 | +// 1. the signed-in user's email is verified, |
| 7 | +// 2. the organization has exactly one active member, and it is that user, |
| 8 | +// 3. the organization is not on a paid v1 plan: no Autumn subscription to a |
| 9 | +// plan outside v2's free list that has not expired. |
| 10 | +// Every failure to read one of them hides the link. Only the yes/no answer |
| 11 | +// leaves the server; memberships and billing never do. |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +import { Effect } from "effect"; |
| 15 | + |
| 16 | +import { parseV2Edge, type V2EdgeEnv } from "../edge/marketing"; |
| 17 | +import { AutumnService } from "../extensions/billing/service"; |
| 18 | +import { WorkOSClient } from "./workos"; |
| 19 | + |
| 20 | +/** v2's import page, on v2's app host. */ |
| 21 | +const V2_IMPORT_PATH = "/v1-import"; |
| 22 | + |
| 23 | +/** |
| 24 | + * The absolute URL of v2's import page, or `null` where v1 does not hand |
| 25 | + * requests to v2 (local dev without the v2 settings, test workers, or broken |
| 26 | + * settings). v2's app host is the origin of its sign-up page, the same host |
| 27 | + * the edge sends v2 dashboard links to. |
| 28 | + */ |
| 29 | +export const v2ImportUrl = (env: V2EdgeEnv): URL | null => { |
| 30 | + const edge = parseV2Edge(env); |
| 31 | + if (edge === null || typeof edge === "string") return null; |
| 32 | + return new URL(V2_IMPORT_PATH, edge.signUpUrl.origin); |
| 33 | +}; |
| 34 | + |
| 35 | +/** v1's plans that cost nothing, as v2's import counts them (v2's |
| 36 | + * `freePlans`). Every other plan, including one added later, is paid. */ |
| 37 | +export const V2_IMPORT_FREE_PLAN_IDS: ReadonlyArray<string> = ["free", "free-pay-as-you-go"]; |
| 38 | + |
| 39 | +export type V2ImportSubscription = { readonly planId: string; readonly status: string }; |
| 40 | + |
| 41 | +/** Whether v2 treats the organization as on a paid v1 plan. */ |
| 42 | +export const isPaidForV2Import = (subscriptions: ReadonlyArray<V2ImportSubscription>): boolean => |
| 43 | + subscriptions.some( |
| 44 | + (subscription) => |
| 45 | + !V2_IMPORT_FREE_PLAN_IDS.includes(subscription.planId) && subscription.status !== "expired", |
| 46 | + ); |
| 47 | + |
| 48 | +/** The reads the eligibility check makes, each against the system of record. */ |
| 49 | +export interface V2ImportSource { |
| 50 | + readonly emailVerified: (userId: string) => Effect.Effect<boolean, unknown>; |
| 51 | + /** The user ids of the organization's active members. */ |
| 52 | + readonly activeMemberIds: ( |
| 53 | + organizationId: string, |
| 54 | + ) => Effect.Effect<ReadonlyArray<string>, unknown>; |
| 55 | + readonly subscriptions: ( |
| 56 | + organizationId: string, |
| 57 | + ) => Effect.Effect<ReadonlyArray<V2ImportSubscription>, unknown>; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Whether v2 would let this user import this organization. Reads in order and |
| 62 | + * stops at the first refusal, so most refusals cost one or two calls. Never |
| 63 | + * fails: an unavailable read answers `false`. |
| 64 | + */ |
| 65 | +export const v2ImportEligible = ( |
| 66 | + source: V2ImportSource, |
| 67 | + userId: string, |
| 68 | + organizationId: string, |
| 69 | +): Effect.Effect<boolean> => |
| 70 | + Effect.gen(function* () { |
| 71 | + if (!(yield* source.emailVerified(userId))) return false; |
| 72 | + const members = yield* source.activeMemberIds(organizationId); |
| 73 | + if (members.length !== 1 || members[0] !== userId) return false; |
| 74 | + return !isPaidForV2Import(yield* source.subscriptions(organizationId)); |
| 75 | + }).pipe( |
| 76 | + Effect.orElseSucceed(() => false), |
| 77 | + Effect.withSpan("v2Import.eligible"), |
| 78 | + ); |
| 79 | + |
| 80 | +/** The reads over v1's WorkOS (live, not the membership mirror, as v2 reads |
| 81 | + * them) and Autumn. An organization Autumn has never seen is provisioned on |
| 82 | + * the free plan, the same answer v2 gives for a missing customer. */ |
| 83 | +export const liveV2ImportSource = Effect.gen(function* () { |
| 84 | + const workos = yield* WorkOSClient; |
| 85 | + const autumn = yield* AutumnService; |
| 86 | + return { |
| 87 | + emailVerified: (userId) => |
| 88 | + workos.getUser(userId).pipe(Effect.map((user) => user.emailVerified)), |
| 89 | + activeMemberIds: (organizationId) => |
| 90 | + workos |
| 91 | + .listOrgMembers(organizationId, ["active"]) |
| 92 | + .pipe( |
| 93 | + Effect.map((list) => |
| 94 | + list.data |
| 95 | + .filter((membership) => membership.status === "active") |
| 96 | + .map((membership) => membership.userId), |
| 97 | + ), |
| 98 | + ), |
| 99 | + subscriptions: (organizationId) => |
| 100 | + autumn |
| 101 | + .use((client) => client.customers.getOrCreate({ customerId: organizationId })) |
| 102 | + .pipe(Effect.map((customer) => customer.subscriptions)), |
| 103 | + } satisfies V2ImportSource; |
| 104 | +}); |
0 commit comments