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 healthcare from "@distilled.cloud/gcp/healthcare_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | collectPages, |
| 14 | expandParent, |
| 15 | forEachDataset, |
| 16 | hasAlchemyLabelMap, |
| 17 | normalizeLocation, |
| 18 | parseResourceName, |
| 19 | replaceOnIdentity, |
| 20 | retryTransient, |
| 21 | toPhysicalId, |
| 22 | updateMaskOf, |
| 23 | userLabels, |
| 24 | waitUntilGone, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | const DEFAULT_VERSION = "R4"; |
| 28 | |
| 29 | export type DatasetsFhirStoreProps = { |
| 30 | /** |
| 31 | * Parent dataset. Full name |
| 32 | * `projects/{project}/locations/{location}/datasets/{dataset}` or the |
| 33 | * dataset id (combined with `location`). Immutable — changing it |
| 34 | * replaces the FHIR store. |
| 35 | */ |
| 36 | dataset: string; |
| 37 | /** |
| 38 | * Region used when `dataset` is a bare id. |
| 39 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 40 | */ |
| 41 | location?: string; |
| 42 | /** |
| 43 | * FHIR store id. Must match letters, numbers, underscores, hyphens, |
| 44 | * and periods; 1-256 characters. Immutable — changing it replaces the |
| 45 | * store. |
| 46 | */ |
| 47 | fhirStoreId?: string; |
| 48 | /** |
| 49 | * Native FHIR version. Immutable — changing it replaces the store. |
| 50 | * @default "R4" |
| 51 | */ |
| 52 | version?: healthcare.FhirStoreVersionEnum | (string & {}); |
| 53 | /** |
| 54 | * User labels. Alchemy ownership labels are merged in automatically. |
| 55 | */ |
| 56 | labels?: Record<string, string>; |
| 57 | /** |
| 58 | * Allow Update to create a resource with a client-specified id. |
| 59 | * @default false |
| 60 | */ |
| 61 | enableUpdateCreate?: boolean; |
| 62 | /** |
| 63 | * Disable referential integrity checks. Immutable — changing it |
| 64 | * replaces the store. |
| 65 | * @default false |
| 66 | */ |
| 67 | disableReferentialIntegrity?: boolean; |
| 68 | /** |
| 69 | * Disable resource versioning. Immutable — changing it replaces the |
| 70 | * store. |
| 71 | * @default false |
| 72 | */ |
| 73 | disableResourceVersioning?: boolean; |
| 74 | /** |
| 75 | * Use `handling=strict` as the default FHIR search behavior. |
| 76 | * @default false |
| 77 | */ |
| 78 | defaultSearchHandlingStrict?: boolean; |
| 79 | }; |
| 80 | |
| 81 | export type DatasetsFhirStore = Resource< |
| 82 | "GCP.Healthcare.DatasetsFhirStore", |
| 83 | DatasetsFhirStoreProps, |
| 84 | { |
| 85 | /** Full resource name `.../datasets/{dataset}/fhirStores/{fhirStore}`. */ |
| 86 | name: string; |
| 87 | /** FHIR store id. */ |
| 88 | fhirStoreId: string; |
| 89 | /** Parent dataset resource name. */ |
| 90 | dataset: string; |
| 91 | /** Project id. */ |
| 92 | project: string; |
| 93 | /** Location id. */ |
| 94 | location: string; |
| 95 | /** Native FHIR version. */ |
| 96 | version: string; |
| 97 | /** User labels (Alchemy ownership labels stripped). */ |
| 98 | labels: Record<string, string>; |
| 99 | /** Whether Update can create. */ |
| 100 | enableUpdateCreate: boolean; |
| 101 | /** Whether referential integrity is disabled. */ |
| 102 | disableReferentialIntegrity: boolean; |
| 103 | /** Whether resource versioning is disabled. */ |
| 104 | disableResourceVersioning: boolean; |
| 105 | /** Whether default search handling is strict. */ |
| 106 | defaultSearchHandlingStrict: boolean; |
| 107 | }, |
| 108 | never, |
| 109 | Providers |
| 110 | >; |
| 111 | |
| 112 | /** |
| 113 | * A Cloud Healthcare FHIR store inside a dataset. |
| 114 | * |
| 115 | * Dataset, store id, location, FHIR version, referential-integrity, and |
| 116 | * versioning flags are immutable. Labels, `enableUpdateCreate`, and |
| 117 | * search-handling update in place. |
| 118 | * |
| 119 | * ### Creating a FHIR Store |
| 120 | * **Example:** R4 store with labels |
| 121 | * ```typescript |
| 122 | * const store = yield* GCP.Healthcare.DatasetsFhirStore("Records", { |
| 123 | * dataset: dataset.name, |
| 124 | * version: "R4", |
| 125 | * labels: { env: "test" }, |
| 126 | * }); |
| 127 | * ``` |
| 128 | * |
| 129 | * **Example:** Named store that allows client-assigned ids |
| 130 | * ```typescript |
| 131 | * const store = yield* GCP.Healthcare.DatasetsFhirStore("Records", { |
| 132 | * dataset: dataset.name, |
| 133 | * fhirStoreId: "clinic-fhir", |
| 134 | * version: "R4", |
| 135 | * enableUpdateCreate: true, |
| 136 | * }); |
| 137 | * ``` |
| 138 | * |
| 139 | * @resource |
| 140 | * @category Healthcare |
| 141 | */ |
| 142 | export const DatasetsFhirStore = Resource<DatasetsFhirStore>("GCP.Healthcare.DatasetsFhirStore"); |
| 143 | |
| 144 | export class DatasetsFhirStoreNotResolved extends Data.TaggedError( |
| 145 | "GCP.Healthcare.DatasetsFhirStoreNotResolved", |
| 146 | )<{ |
| 147 | resourceName: string; |
| 148 | }> {} |
| 149 | |
| 150 | const datasetOf = (dataset: string, project: string, location: string) => |
| 151 | expandParent(dataset, project, location, "datasets"); |
| 152 | |
| 153 | const resourceName = (dataset: string, fhirStoreId: string) => |
| 154 | `${dataset}/fhirStores/${fhirStoreId}`; |
| 155 | |
| 156 | const toAttrs = (store: healthcare.FhirStore, project: string, region: string) => { |
| 157 | const name = store.name ?? ""; |
| 158 | const parsed = parseResourceName(name, "fhirStores", region); |
| 159 | return { |
| 160 | name, |
| 161 | fhirStoreId: parsed.id, |
| 162 | dataset: parsed.parent, |
| 163 | project: parsed.project || project, |
| 164 | location: parsed.location, |
| 165 | version: store.version ?? DEFAULT_VERSION, |
| 166 | labels: userLabels(store.labels), |
| 167 | enableUpdateCreate: store.enableUpdateCreate === true, |
| 168 | disableReferentialIntegrity: store.disableReferentialIntegrity === true, |
| 169 | disableResourceVersioning: store.disableResourceVersioning === true, |
| 170 | defaultSearchHandlingStrict: store.defaultSearchHandlingStrict === true, |
| 171 | }; |
| 172 | }; |
| 173 | |
| 174 | const getByName = (name: string) => |
| 175 | name.length === 0 |
| 176 | ? Effect.undefined |
| 177 | : healthcare |
| 178 | .getProjectsLocationsDatasetsFhirStores({ name }) |
| 179 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 180 | |
| 181 | export const DatasetsFhirStoreProvider = () => |
| 182 | Provider.succeed(DatasetsFhirStore, { |
| 183 | stables: [ |
| 184 | "name", |
| 185 | "fhirStoreId", |
| 186 | "dataset", |
| 187 | "project", |
| 188 | "location", |
| 189 | "version", |
| 190 | "disableReferentialIntegrity", |
| 191 | "disableResourceVersioning", |
| 192 | ], |
| 193 | |
| 194 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 195 | if (!isResolved(news)) return undefined; |
| 196 | const env = yield* GcpEnvironment.current; |
| 197 | const previousVersion = olds?.version ?? output?.version; |
| 198 | const nextVersion = news.version ?? DEFAULT_VERSION; |
| 199 | const extra = |
| 200 | (previousVersion !== undefined && previousVersion !== nextVersion) || |
| 201 | ((olds?.disableReferentialIntegrity ?? output?.disableReferentialIntegrity) === true) !== |
| 202 | (news.disableReferentialIntegrity === true) || |
| 203 | ((olds?.disableResourceVersioning ?? output?.disableResourceVersioning) === true) !== |
| 204 | (news.disableResourceVersioning === true); |
| 205 | return replaceOnIdentity({ |
| 206 | previousId: olds?.fhirStoreId ?? output?.fhirStoreId, |
| 207 | nextId: news.fhirStoreId, |
| 208 | previousParent: olds?.dataset ?? output?.dataset, |
| 209 | nextParent: datasetOf( |
| 210 | news.dataset, |
| 211 | env.project, |
| 212 | normalizeLocation(news.location ?? output?.location, env.region), |
| 213 | ), |
| 214 | extra, |
| 215 | }); |
| 216 | }), |
| 217 | |
| 218 | read: Effect.fn(function* ({ id, olds, output }) { |
| 219 | const env = yield* GcpEnvironment.current; |
| 220 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 221 | const fhirStoreId = yield* toPhysicalId(id, olds?.fhirStoreId, output?.fhirStoreId); |
| 222 | const dataset = |
| 223 | olds?.dataset !== undefined |
| 224 | ? datasetOf(olds.dataset, env.project, location) |
| 225 | : (output?.dataset ?? ""); |
| 226 | const name = output?.name ?? (dataset.length > 0 ? resourceName(dataset, fhirStoreId) : ""); |
| 227 | const existing = yield* getByName(name); |
| 228 | if (existing === undefined) return undefined; |
| 229 | const attrs = toAttrs(existing, env.project, env.region); |
| 230 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 231 | }), |
| 232 | |
| 233 | list: () => |
| 234 | Effect.gen(function* () { |
| 235 | const env = yield* GcpEnvironment.current; |
| 236 | const stores = yield* forEachDataset(env.project, env.region, (parent) => |
| 237 | collectPages( |
| 238 | healthcare.listProjectsLocationsDatasetsFhirStores.pages({ |
| 239 | parent, |
| 240 | pageSize: 1000, |
| 241 | }), |
| 242 | (page) => page.fhirStores, |
| 243 | ), |
| 244 | ); |
| 245 | return stores |
| 246 | .filter((store) => hasAlchemyLabelMap(store.labels)) |
| 247 | .map((store) => toAttrs(store, env.project, env.region)); |
| 248 | }), |
| 249 | |
| 250 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 253 | const dataset = datasetOf(news.dataset, env.project, location); |
| 254 | const fhirStoreId = yield* toPhysicalId(id, news.fhirStoreId, output?.fhirStoreId); |
| 255 | const name = output?.name ?? resourceName(dataset, fhirStoreId); |
| 256 | const version = news.version ?? DEFAULT_VERSION; |
| 257 | const desiredLabels = { |
| 258 | ...toLabels(news.labels), |
| 259 | ...(yield* createInternalLabels(id)), |
| 260 | }; |
| 261 | |
| 262 | let current = yield* getByName(name); |
| 263 | |
| 264 | if (current === undefined) { |
| 265 | const created = yield* retryTransient( |
| 266 | healthcare.createProjectsLocationsDatasetsFhirStores({ |
| 267 | parent: dataset, |
| 268 | fhirStoreId, |
| 269 | body: { |
| 270 | version, |
| 271 | labels: desiredLabels, |
| 272 | enableUpdateCreate: news.enableUpdateCreate, |
| 273 | disableReferentialIntegrity: news.disableReferentialIntegrity, |
| 274 | disableResourceVersioning: news.disableResourceVersioning, |
| 275 | defaultSearchHandlingStrict: news.defaultSearchHandlingStrict, |
| 276 | }, |
| 277 | }), |
| 278 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 279 | current = created ?? undefined; |
| 280 | } |
| 281 | |
| 282 | if (current === undefined) { |
| 283 | return yield* new DatasetsFhirStoreNotResolved({ resourceName: name }); |
| 284 | } |
| 285 | |
| 286 | const currentName = current.name ?? name; |
| 287 | const observedLabels = tagRecord(current.labels); |
| 288 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 289 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 290 | const updateCreateChanged = |
| 291 | (current.enableUpdateCreate === true) !== (news.enableUpdateCreate === true); |
| 292 | const searchChanged = |
| 293 | (current.defaultSearchHandlingStrict === true) !== |
| 294 | (news.defaultSearchHandlingStrict === true); |
| 295 | |
| 296 | if (labelsChanged || updateCreateChanged || searchChanged) { |
| 297 | current = yield* retryTransient( |
| 298 | healthcare.patchProjectsLocationsDatasetsFhirStores({ |
| 299 | name: currentName, |
| 300 | updateMask: updateMaskOf( |
| 301 | labelsChanged ? "labels" : undefined, |
| 302 | updateCreateChanged ? "enableUpdateCreate" : undefined, |
| 303 | searchChanged ? "defaultSearchHandlingStrict" : undefined, |
| 304 | ), |
| 305 | body: { |
| 306 | labels: desiredLabels, |
| 307 | enableUpdateCreate: news.enableUpdateCreate, |
| 308 | defaultSearchHandlingStrict: news.defaultSearchHandlingStrict, |
| 309 | }, |
| 310 | }), |
| 311 | ); |
| 312 | } |
| 313 | |
| 314 | return toAttrs(current, env.project, env.region); |
| 315 | }), |
| 316 | |
| 317 | delete: Effect.fn(function* ({ output }) { |
| 318 | if (!output.name) return; |
| 319 | yield* retryTransient( |
| 320 | healthcare.deleteProjectsLocationsDatasetsFhirStores({ |
| 321 | name: output.name, |
| 322 | }), |
| 323 | ).pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 324 | yield* waitUntilGone(getByName(output.name), output.name); |
| 325 | }), |
| 326 | }); |