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 cloudchannel from "@distilled.cloud/gcp/cloudchannel_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | defaultInvoiceMonth, |
| 11 | desiredRepricingConfig, |
| 12 | findCustomerRepricing, |
| 13 | jsonEqual, |
| 14 | normalizeDate, |
| 15 | normalizeName, |
| 16 | replaceOnIdentity, |
| 17 | toCustomerRepricingAttrs, |
| 18 | toRepricingConfigName, |
| 19 | } from "./internal.ts"; |
| 20 | |
| 21 | export type CustomersCustomerRepricingConfigProps = { |
| 22 | /** |
| 23 | * Customer that receives this repricing config. Full name |
| 24 | * `accounts/{account}/customers/{customer}`. Immutable — changing it |
| 25 | * replaces the config. |
| 26 | */ |
| 27 | parent: string; |
| 28 | /** |
| 29 | * Config id (last segment of the resource name). Server-assigned on |
| 30 | * create. Immutable — changing it replaces the config. |
| 31 | */ |
| 32 | configId?: string; |
| 33 | /** |
| 34 | * Year/month when the adjustment activates. Day must be `0`. You can |
| 35 | * only create or update configs for a future month. Immutable — |
| 36 | * changing it replaces the config. |
| 37 | */ |
| 38 | effectiveInvoiceMonth?: cloudchannel.GoogleTypeDate; |
| 39 | /** |
| 40 | * Rebilling basis used for the bill. |
| 41 | * @default "COST_AT_LIST" |
| 42 | */ |
| 43 | rebillingBasis?: |
| 44 | | cloudchannel.GoogleCloudChannelV1RepricingConfigRebillingBasisEnum |
| 45 | | (string & {}); |
| 46 | /** |
| 47 | * Markup or markdown percentage (`"1.00"` is +1%, `"-1.00"` is -1%, |
| 48 | * `"0.00"` is pass-through). Ignored when `adjustment` is set. |
| 49 | * @default "0.00" |
| 50 | */ |
| 51 | adjustmentPercentage?: string; |
| 52 | /** |
| 53 | * Full adjustment. Defaults to a percentage adjustment of |
| 54 | * `adjustmentPercentage`. |
| 55 | */ |
| 56 | adjustment?: cloudchannel.GoogleCloudChannelV1RepricingAdjustment; |
| 57 | /** |
| 58 | * Entitlement this config applies to |
| 59 | * (`accounts/{account}/customers/{customer}/entitlements/{entitlement}`). |
| 60 | */ |
| 61 | entitlement?: string; |
| 62 | /** |
| 63 | * Entitlement granularity. Takes precedence over `entitlement`. |
| 64 | */ |
| 65 | entitlementGranularity?: cloudchannel.GoogleCloudChannelV1RepricingConfigEntitlementGranularity; |
| 66 | /** |
| 67 | * Conditional overrides applied before the default adjustment. |
| 68 | */ |
| 69 | conditionalOverrides?: cloudchannel.GoogleCloudChannelV1ConditionalOverrideList; |
| 70 | }; |
| 71 | |
| 72 | export type CustomersCustomerRepricingConfig = Resource< |
| 73 | "GCP.CloudChannel.CustomersCustomerRepricingConfig", |
| 74 | CustomersCustomerRepricingConfigProps, |
| 75 | { |
| 76 | /** Resource name `accounts/{account}/customers/{customer}/customerRepricingConfigs/{id}`. */ |
| 77 | name: string; |
| 78 | /** Config id (last path segment). */ |
| 79 | configId: string; |
| 80 | /** Parent customer resource name. */ |
| 81 | parent: string; |
| 82 | /** RFC3339 last-update timestamp. */ |
| 83 | updateTime: string | undefined; |
| 84 | /** Repricing configuration. */ |
| 85 | repricingConfig: cloudchannel.GoogleCloudChannelV1RepricingConfig | undefined; |
| 86 | }, |
| 87 | never, |
| 88 | Providers |
| 89 | >; |
| 90 | |
| 91 | /** |
| 92 | * A Cloud Channel customer repricing config. |
| 93 | * |
| 94 | * Repricing configs have no labels field: `read` reports a config it |
| 95 | * finds without prior state as unowned (adopt it with `--adopt`). Parent |
| 96 | * customer and effective invoice month are identity — changing either |
| 97 | * replaces the config. Adjustment, rebilling basis, entitlement, and |
| 98 | * overrides update in place for a future month. |
| 99 | * |
| 100 | * Creating configs requires Cloud Channel reseller access and a |
| 101 | * provisioned entitlement. |
| 102 | * |
| 103 | * ### Creating a Customer Repricing Config |
| 104 | * **Example:** Pass-through for a future month |
| 105 | * ```typescript |
| 106 | * const config = yield* GCP.CloudChannel.CustomersCustomerRepricingConfig( |
| 107 | * "AcmeBill", |
| 108 | * { |
| 109 | * parent: customer.name, |
| 110 | * effectiveInvoiceMonth: { year: 2099, month: 1, day: 0 }, |
| 111 | * entitlement: `${customer.name}/entitlements/ent-1`, |
| 112 | * }, |
| 113 | * ); |
| 114 | * ``` |
| 115 | * |
| 116 | * **Example:** One percent markup |
| 117 | * ```typescript |
| 118 | * const config = yield* GCP.CloudChannel.CustomersCustomerRepricingConfig( |
| 119 | * "AcmeBill", |
| 120 | * { |
| 121 | * parent: customer.name, |
| 122 | * effectiveInvoiceMonth: { year: 2099, month: 1, day: 0 }, |
| 123 | * adjustmentPercentage: "1.00", |
| 124 | * entitlement: `${customer.name}/entitlements/ent-1`, |
| 125 | * }, |
| 126 | * ); |
| 127 | * ``` |
| 128 | * |
| 129 | * @resource |
| 130 | * @category CloudChannel |
| 131 | */ |
| 132 | export const CustomersCustomerRepricingConfig = Resource<CustomersCustomerRepricingConfig>( |
| 133 | "GCP.CloudChannel.CustomersCustomerRepricingConfig", |
| 134 | ); |
| 135 | |
| 136 | export class CustomersCustomerRepricingConfigNotResolved extends Data.TaggedError( |
| 137 | "GCP.CloudChannel.CustomersCustomerRepricingConfigNotResolved", |
| 138 | )<{ |
| 139 | resourceName: string; |
| 140 | }> {} |
| 141 | |
| 142 | const monthOf = ( |
| 143 | news: CustomersCustomerRepricingConfigProps, |
| 144 | outputMonth?: cloudchannel.GoogleTypeDate, |
| 145 | ) => |
| 146 | Effect.gen(function* () { |
| 147 | return ( |
| 148 | normalizeDate(news.effectiveInvoiceMonth) ?? |
| 149 | normalizeDate(outputMonth) ?? |
| 150 | (yield* defaultInvoiceMonth()) |
| 151 | ); |
| 152 | }); |
| 153 | |
| 154 | export const CustomersCustomerRepricingConfigProvider = () => |
| 155 | Provider.succeed(CustomersCustomerRepricingConfig, { |
| 156 | stables: ["name", "configId", "parent"], |
| 157 | |
| 158 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 159 | if (!isResolved(news)) return undefined; |
| 160 | const previousMonth = normalizeDate( |
| 161 | olds?.effectiveInvoiceMonth ?? output?.repricingConfig?.effectiveInvoiceMonth, |
| 162 | ); |
| 163 | const nextMonth = normalizeDate(news.effectiveInvoiceMonth); |
| 164 | return replaceOnIdentity({ |
| 165 | previousId: olds?.configId ?? output?.configId, |
| 166 | nextId: news.configId, |
| 167 | previousParent: olds?.parent ?? output?.parent, |
| 168 | nextParent: news.parent, |
| 169 | extra: |
| 170 | previousMonth !== undefined && |
| 171 | nextMonth !== undefined && |
| 172 | !jsonEqual(previousMonth, nextMonth), |
| 173 | }); |
| 174 | }), |
| 175 | |
| 176 | read: Effect.fn(function* ({ olds, output }) { |
| 177 | const parent = normalizeName(olds?.parent ?? output?.parent ?? ""); |
| 178 | const name = toRepricingConfigName( |
| 179 | parent, |
| 180 | olds?.configId ?? output?.configId ?? output?.name, |
| 181 | "customerRepricingConfigs", |
| 182 | ); |
| 183 | const existing = yield* findCustomerRepricing( |
| 184 | parent, |
| 185 | output?.name ?? name, |
| 186 | olds?.effectiveInvoiceMonth ?? output?.repricingConfig?.effectiveInvoiceMonth, |
| 187 | ); |
| 188 | if (existing === undefined) return undefined; |
| 189 | const attrs = toCustomerRepricingAttrs(existing); |
| 190 | // No labels field: without prior state the config may not be ours. |
| 191 | return output !== undefined ? attrs : Unowned(attrs); |
| 192 | }), |
| 193 | |
| 194 | reconcile: Effect.fn(function* ({ news, output }) { |
| 195 | const parent = normalizeName(news.parent); |
| 196 | const month = yield* monthOf(news, output?.repricingConfig?.effectiveInvoiceMonth); |
| 197 | const repricingConfig = desiredRepricingConfig({ |
| 198 | effectiveInvoiceMonth: month, |
| 199 | rebillingBasis: news.rebillingBasis, |
| 200 | adjustmentPercentage: news.adjustmentPercentage, |
| 201 | adjustment: news.adjustment, |
| 202 | entitlementGranularity: |
| 203 | news.entitlementGranularity ?? |
| 204 | (news.entitlement |
| 205 | ? { entitlement: news.entitlement } |
| 206 | : output?.repricingConfig?.entitlementGranularity), |
| 207 | conditionalOverrides: news.conditionalOverrides, |
| 208 | }); |
| 209 | const name = toRepricingConfigName( |
| 210 | parent, |
| 211 | news.configId ?? output?.configId ?? output?.name, |
| 212 | "customerRepricingConfigs", |
| 213 | ); |
| 214 | |
| 215 | let current = yield* findCustomerRepricing(parent, output?.name ?? name, month); |
| 216 | |
| 217 | if (current === undefined) { |
| 218 | const created = yield* cloudchannel |
| 219 | .createAccountsCustomersCustomerRepricingConfigs({ |
| 220 | parent, |
| 221 | body: { repricingConfig }, |
| 222 | }) |
| 223 | .pipe(Effect.catchTag("Conflict", () => findCustomerRepricing(parent, name, month))); |
| 224 | current = created ?? undefined; |
| 225 | } |
| 226 | |
| 227 | if (current === undefined) { |
| 228 | return yield* new CustomersCustomerRepricingConfigNotResolved({ |
| 229 | resourceName: name || `${parent}/customerRepricingConfigs`, |
| 230 | }); |
| 231 | } |
| 232 | |
| 233 | const currentName = current.name ?? name; |
| 234 | const observed = current.repricingConfig; |
| 235 | const changed = |
| 236 | !jsonEqual( |
| 237 | normalizeDate(observed?.effectiveInvoiceMonth), |
| 238 | normalizeDate(repricingConfig.effectiveInvoiceMonth), |
| 239 | ) || |
| 240 | (observed?.rebillingBasis ?? "") !== (repricingConfig.rebillingBasis ?? "") || |
| 241 | !jsonEqual(observed?.adjustment, repricingConfig.adjustment) || |
| 242 | !jsonEqual(observed?.entitlementGranularity, repricingConfig.entitlementGranularity) || |
| 243 | !jsonEqual(observed?.conditionalOverrides, repricingConfig.conditionalOverrides); |
| 244 | |
| 245 | if (changed && currentName.length > 0) { |
| 246 | current = yield* cloudchannel.patchAccountsCustomersCustomerRepricingConfigs({ |
| 247 | name: currentName, |
| 248 | body: { name: currentName, repricingConfig }, |
| 249 | }); |
| 250 | } |
| 251 | |
| 252 | return toCustomerRepricingAttrs(current); |
| 253 | }), |
| 254 | |
| 255 | delete: Effect.fn(function* ({ output }) { |
| 256 | const name = output.name; |
| 257 | if (name.length === 0) return; |
| 258 | yield* cloudchannel |
| 259 | .deleteAccountsCustomersCustomerRepricingConfigs({ name }) |
| 260 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 261 | }), |
| 262 | }); |