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 { withRequestOptions } from "@distilled.cloud/stripe"; |
| 2 | import { |
| 3 | DeleteProductFeature, |
| 4 | GetProducts, |
| 5 | GetProductFeatures, |
| 6 | GetProductFeature, |
| 7 | CreateProductFeature, |
| 8 | type Product as StripeProduct, |
| 9 | type ProductFeature as StripeProductFeature, |
| 10 | } from "@distilled.cloud/stripe/stripe"; |
| 11 | import { isResolved } from "alchemy/Diff"; |
| 12 | import * as Provider from "alchemy/Provider"; |
| 13 | import { Resource } from "alchemy/Resource"; |
| 14 | import { tagRecord } from "alchemy/Tags"; |
| 15 | import * as Effect from "effect/Effect"; |
| 16 | import { alchemyMetadataKeys } from "./Metadata.ts"; |
| 17 | import { isMissingStripeResource } from "./missing.ts"; |
| 18 | import type { Providers } from "./Providers.ts"; |
| 19 | |
| 20 | const LIST_PAGE_SIZE = 100; |
| 21 | const LIST_MAX_PAGES = 100; |
| 22 | const LIST_CONCURRENCY = 10; |
| 23 | |
| 24 | export interface ProductFeatureProps { |
| 25 | /** |
| 26 | * Id of the Stripe Product to attach the feature to (`prod_…`). |
| 27 | * Changing it replaces the attachment. |
| 28 | */ |
| 29 | product: string; |
| 30 | /** |
| 31 | * Id of the Entitlements Feature to attach (`feat_…`). Changing it |
| 32 | * replaces the attachment. |
| 33 | */ |
| 34 | entitlementFeature: string; |
| 35 | } |
| 36 | |
| 37 | export type ProductFeature = Resource< |
| 38 | "Stripe.ProductFeature", |
| 39 | ProductFeatureProps, |
| 40 | { |
| 41 | /** Stripe product-feature id (`prodft_…`). */ |
| 42 | id: string; |
| 43 | /** Id of the product this feature is attached to (`prod_…`). */ |
| 44 | product: string; |
| 45 | /** Id of the attached entitlements feature (`feat_…`). */ |
| 46 | entitlementFeature: string; |
| 47 | /** Whether the attachment exists in live mode. */ |
| 48 | livemode: boolean; |
| 49 | }, |
| 50 | never, |
| 51 | Providers |
| 52 | >; |
| 53 | |
| 54 | /** |
| 55 | * A Stripe Product Feature — the attachment of an Entitlements Feature |
| 56 | * to a Product. When a customer purchases a product that has a feature |
| 57 | * attached, Stripe creates an entitlement to that feature. Existence-only: |
| 58 | * there is nothing to update in place; changing `product` or |
| 59 | * `entitlementFeature` replaces the attachment. Destroy deletes it. |
| 60 | * |
| 61 | * Product features have no metadata of their own. Ownership for |
| 62 | * account-wide `list()` (nuke) is inferred from the parent Product's |
| 63 | * Alchemy metadata. |
| 64 | * |
| 65 | * @see https://docs.stripe.com/api/product-feature |
| 66 | * |
| 67 | * ### Attaching a Feature |
| 68 | * **Example:** Attach a feature to a product |
| 69 | * ```typescript |
| 70 | * const product = yield* Stripe.Product("pro-plan", { name: "Pro Plan" }); |
| 71 | * const analytics = yield* Stripe.EntitlementsFeature("analytics", { |
| 72 | * lookupKey: "analytics", |
| 73 | * name: "Analytics", |
| 74 | * }); |
| 75 | * const attachment = yield* Stripe.ProductFeature("pro-analytics", { |
| 76 | * product: product.id, |
| 77 | * entitlementFeature: analytics.id, |
| 78 | * }); |
| 79 | * ``` |
| 80 | * |
| 81 | * ### Replacing an Attachment |
| 82 | * **Example:** Point the product at a different feature |
| 83 | * ```typescript |
| 84 | * const reporting = yield* Stripe.EntitlementsFeature("reporting", { |
| 85 | * lookupKey: "reporting", |
| 86 | * name: "Reporting", |
| 87 | * }); |
| 88 | * const attachment = yield* Stripe.ProductFeature("pro-analytics", { |
| 89 | * product: product.id, |
| 90 | * entitlementFeature: reporting.id, |
| 91 | * }); |
| 92 | * ``` |
| 93 | * |
| 94 | * @resource |
| 95 | * @product Product |
| 96 | */ |
| 97 | export const ProductFeature = Resource<ProductFeature>("Stripe.ProductFeature"); |
| 98 | |
| 99 | type ProductFeatureAttributes = ProductFeature["Attributes"]; |
| 100 | |
| 101 | const toAttrs = (product: string, feature: StripeProductFeature): ProductFeatureAttributes => ({ |
| 102 | id: feature.id, |
| 103 | product, |
| 104 | entitlementFeature: feature.entitlement_feature.id, |
| 105 | livemode: feature.livemode, |
| 106 | }); |
| 107 | |
| 108 | const isMissing = isMissingStripeResource; |
| 109 | |
| 110 | const getById = (product: string, id: string) => |
| 111 | GetProductFeature({ product, id }).pipe(Effect.catchIf(isMissing, () => Effect.undefined)); |
| 112 | |
| 113 | const listFeatures = Effect.fn(function* (product: string) { |
| 114 | const features: StripeProductFeature[] = []; |
| 115 | let startingAfter: string | undefined; |
| 116 | for (let page = 0; page < LIST_MAX_PAGES; page++) { |
| 117 | const response = yield* GetProductFeatures({ |
| 118 | product, |
| 119 | limit: LIST_PAGE_SIZE, |
| 120 | ...(startingAfter !== undefined ? { starting_after: startingAfter } : {}), |
| 121 | }).pipe(Effect.catchIf(isMissing, () => Effect.undefined)); |
| 122 | if (response === undefined) { |
| 123 | break; |
| 124 | } |
| 125 | features.push(...response.data); |
| 126 | if (!response.has_more || response.data.length === 0) { |
| 127 | break; |
| 128 | } |
| 129 | startingAfter = response.data[response.data.length - 1]?.id; |
| 130 | if (startingAfter === undefined) { |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | return features; |
| 135 | }); |
| 136 | |
| 137 | const listProductsByActive = Effect.fn(function* (active: boolean) { |
| 138 | const products: StripeProduct[] = []; |
| 139 | let startingAfter: string | undefined; |
| 140 | for (let page = 0; page < LIST_MAX_PAGES; page++) { |
| 141 | const response = yield* GetProducts({ |
| 142 | active, |
| 143 | limit: LIST_PAGE_SIZE, |
| 144 | ...(startingAfter !== undefined ? { starting_after: startingAfter } : {}), |
| 145 | }); |
| 146 | products.push(...response.data); |
| 147 | if (!response.has_more || response.data.length === 0) { |
| 148 | break; |
| 149 | } |
| 150 | startingAfter = response.data[response.data.length - 1]?.id; |
| 151 | if (startingAfter === undefined) { |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | return products; |
| 156 | }); |
| 157 | |
| 158 | const listAlchemyProducts = Effect.fn(function* () { |
| 159 | const [active, inactive] = yield* Effect.all( |
| 160 | [listProductsByActive(true), listProductsByActive(false)], |
| 161 | { concurrency: 2 }, |
| 162 | ); |
| 163 | const seen = new Set<string>(); |
| 164 | const products: StripeProduct[] = []; |
| 165 | for (const product of [...active, ...inactive]) { |
| 166 | if (seen.has(product.id)) continue; |
| 167 | seen.add(product.id); |
| 168 | if (tagRecord(product.metadata)[alchemyMetadataKeys.stack] !== undefined) { |
| 169 | products.push(product); |
| 170 | } |
| 171 | } |
| 172 | return products; |
| 173 | }); |
| 174 | |
| 175 | const observe = Effect.fn(function* (input: { |
| 176 | product?: string; |
| 177 | id?: string; |
| 178 | entitlementFeature?: string; |
| 179 | }) { |
| 180 | if (input.product !== undefined && input.id !== undefined) { |
| 181 | const byId = yield* getById(input.product, input.id); |
| 182 | if (byId !== undefined) return byId; |
| 183 | } |
| 184 | if (input.product !== undefined && input.entitlementFeature !== undefined) { |
| 185 | const features = yield* listFeatures(input.product); |
| 186 | return features.find((feature) => feature.entitlement_feature.id === input.entitlementFeature); |
| 187 | } |
| 188 | return undefined; |
| 189 | }); |
| 190 | |
| 191 | const shouldReplace = ( |
| 192 | news: ProductFeatureProps, |
| 193 | output: ProductFeatureAttributes | undefined, |
| 194 | ): boolean => { |
| 195 | if (output === undefined) return false; |
| 196 | if (news.product !== output.product) return true; |
| 197 | if (news.entitlementFeature !== output.entitlementFeature) return true; |
| 198 | return false; |
| 199 | }; |
| 200 | |
| 201 | export const ProductFeatureProvider = () => |
| 202 | Provider.succeed(ProductFeature, { |
| 203 | stables: ["id", "product", "entitlementFeature", "livemode"], |
| 204 | nuke: { dependsOn: ["Stripe.Product"] }, |
| 205 | |
| 206 | diff: Effect.fn(function* ({ news, output }) { |
| 207 | if (!isResolved(news)) return undefined; |
| 208 | if (shouldReplace(news, output)) { |
| 209 | return { action: "replace" } as const; |
| 210 | } |
| 211 | return undefined; |
| 212 | }), |
| 213 | |
| 214 | read: Effect.fn(function* ({ output, olds }) { |
| 215 | const product = |
| 216 | output?.product ?? (typeof olds?.product === "string" ? olds.product : undefined); |
| 217 | const entitlementFeature = |
| 218 | output?.entitlementFeature ?? |
| 219 | (typeof olds?.entitlementFeature === "string" ? olds.entitlementFeature : undefined); |
| 220 | const existing = yield* observe({ |
| 221 | product, |
| 222 | id: output?.id, |
| 223 | entitlementFeature, |
| 224 | }); |
| 225 | if (existing === undefined || product === undefined) return undefined; |
| 226 | return toAttrs(product, existing); |
| 227 | }), |
| 228 | |
| 229 | list: Effect.fn(function* () { |
| 230 | // Product features have no metadata. Fan out from alchemy-owned |
| 231 | // products so nuke only tears down attachments we created. |
| 232 | const products = yield* listAlchemyProducts(); |
| 233 | const rows = yield* Effect.forEach( |
| 234 | products, |
| 235 | (product) => |
| 236 | listFeatures(product.id).pipe( |
| 237 | Effect.map((features) => features.map((feature) => toAttrs(product.id, feature))), |
| 238 | ), |
| 239 | { concurrency: LIST_CONCURRENCY }, |
| 240 | ); |
| 241 | return rows.flat(); |
| 242 | }), |
| 243 | |
| 244 | reconcile: Effect.fn(function* ({ news, output, instanceId }) { |
| 245 | let current = yield* observe({ |
| 246 | product: news.product, |
| 247 | id: output?.id, |
| 248 | entitlementFeature: news.entitlementFeature, |
| 249 | }); |
| 250 | if (current !== undefined && shouldReplace(news, toAttrs(news.product, current))) { |
| 251 | current = undefined; |
| 252 | } |
| 253 | |
| 254 | if (current === undefined) { |
| 255 | current = yield* CreateProductFeature({ |
| 256 | product: news.product, |
| 257 | entitlement_feature: news.entitlementFeature, |
| 258 | }).pipe( |
| 259 | withRequestOptions({ |
| 260 | idempotencyKey: `alchemy-product-feature-${instanceId}`, |
| 261 | }), |
| 262 | Effect.catchIf( |
| 263 | (e) => e._tag === "InvalidRequestError" || e._tag === "Conflict", |
| 264 | (e) => |
| 265 | observe({ |
| 266 | product: news.product, |
| 267 | entitlementFeature: news.entitlementFeature, |
| 268 | }).pipe( |
| 269 | Effect.flatMap((found) => |
| 270 | found !== undefined ? Effect.succeed(found) : Effect.fail(e), |
| 271 | ), |
| 272 | ), |
| 273 | ), |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | return toAttrs(news.product, current); |
| 278 | }), |
| 279 | |
| 280 | delete: Effect.fn(function* ({ output }) { |
| 281 | yield* DeleteProductFeature({ |
| 282 | product: output.product, |
| 283 | id: output.id, |
| 284 | }).pipe(Effect.catchIf(isMissing, () => Effect.void)); |
| 285 | }), |
| 286 | }); |