@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

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