sailfork / t3code-github / .repos/ alchemy-effect/ packages/ gcp/ src/ ContactCenterInsights/ AuthorizedViewSet.ts

import * as cci from "@distilled.cloud/gcp/contactcenterinsights_v1";
import { Unowned } from "alchemy/AdoptPolicy";
import { isResolved } from "alchemy/Diff";
import * as Provider from "alchemy/Provider";
import { Resource } from "alchemy/Resource";
import * as Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Stream from "effect/Stream";
import { GcpEnvironment } from "../Environment.ts";
import { createInternalLabels } from "../Labels.ts";
import type { Providers } from "../Providers.ts";
import {
  encodeOwnershipLine,
  hasOwnershipMarker,
  lastSegment,
  locationOf,
  locationParent,
  MAX_VIEW_DISPLAY_NAME_LENGTH,
  normalizeLocation,
  ownedByAlchemy,
  parseOwnership,
  toResourceId,
} from "./ownership.ts";

export type AuthorizedViewSetProps = {
  /**
   * AuthorizedViewSet id (the `{authorized_view_set}` segment of
   * `projects/{project}/locations/{location}/authorizedViewSets/{authorized_view_set}`).
   * If omitted, a unique id is generated. Must match
   * `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/gcp/src/ContactCenterInsights/AuthorizedViewSet.ts at main · sailfork/t3code-github · Sailfork
	
		
. Immutable — changing it replaces * the set. */ authorizedViewSetId?: string; /** * Location (`us-central1`, …). Immutable — changing it replaces the set. * `US-CENTRAL1` is accepted and normalized to `us-central1`. * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) */ location?: string; /** * Display name (max 64 characters including Alchemy's ownership * marker). AuthorizedViewSets have no labels field, so ownership is * stored in a `[alchemy …]` prefix and stripped from attributes. */ displayName?: string; }; export type AuthorizedViewSet = Resource< "GCP.ContactCenterInsights.AuthorizedViewSet", AuthorizedViewSetProps, { /** Full resource name `projects/{project}/locations/{location}/authorizedViewSets/{authorized_view_set}`. */ name: string; /** AuthorizedViewSet id (last path segment). */ authorizedViewSetId: string; /** Location id. */ location: string; /** Project id. */ project: string; /** User display name with the Alchemy ownership prefix stripped. */ displayName: string | undefined; /** RFC3339 creation timestamp. */ createTime: string | undefined; /** RFC3339 last-update timestamp. */ updateTime: string | undefined; }, never, Providers >; /** * A Contact Center AI Insights AuthorizedViewSet, a container for * AuthorizedView resources. * * AuthorizedViewSets have no labels field — Alchemy stamps ownership into * the display name. Location and id are immutable. Display name updates * in place. * * ### Creating an AuthorizedViewSet * **Example:** Generated id * ```typescript * const set = yield* GCP.ContactCenterInsights.AuthorizedViewSet("QaViews", { * displayName: "qa-reviewers", * }); * ``` * * **Example:** Named set * ```typescript * const set = yield* GCP.ContactCenterInsights.AuthorizedViewSet("QaViews", { * authorizedViewSetId: "qa-reviewers", * displayName: "qa-reviewers", * }); * ``` * * @resource * @category ContactCenterInsights */ export const AuthorizedViewSet = Resource<AuthorizedViewSet>( "GCP.ContactCenterInsights.AuthorizedViewSet", ); export class AuthorizedViewSetNotResolved extends Data.TaggedError( "GCP.ContactCenterInsights.AuthorizedViewSetNotResolved", )<{ resourceName: string; }> {} const resourceName = (project: string, location: string, authorizedViewSetId: string) => `${locationParent(project, location)}/authorizedViewSets/${authorizedViewSetId}`; const toAttrs = (set: cci.GoogleCloudContactcenterinsightsV1AuthorizedViewSet, project: string) => { const name = set.name ?? ""; const parsed = parseOwnership(set.displayName); return { name, authorizedViewSetId: lastSegment(name), location: locationOf(name), project, displayName: parsed.text, createTime: set.createTime, updateTime: set.updateTime, }; }; const getByName = (name: string) => name.length === 0 ? Effect.undefined : cci .getProjectsLocationsAuthorizedViewSets({ name }) .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); const listAt = (parent: string, project: string) => cci.listProjectsLocationsAuthorizedViewSets.pages({ parent, pageSize: 100 }).pipe( Stream.flatMap((page) => Stream.fromIterable(page.authorizedViewSets ?? [])), Stream.filter((set) => hasOwnershipMarker(set.displayName)), Stream.map((set) => toAttrs(set, project)), Stream.runCollect, Effect.map((chunk) => Array.from(chunk)), Effect.catchTag("NotFound", () => Effect.succeed([])), ); export const AuthorizedViewSetProvider = () => Provider.succeed(AuthorizedViewSet, { stables: ["name", "authorizedViewSetId", "location", "project", "createTime"], diff: Effect.fn(function* ({ news, olds, output }) { if (!isResolved(news)) return undefined; const previousLocation = olds?.location ?? output?.location; if ( previousLocation !== undefined && news.location !== undefined && normalizeLocation(previousLocation) !== normalizeLocation(news.location) ) { return { action: "replace" as const, deleteFirst: false }; } const previousId = olds?.authorizedViewSetId ?? output?.authorizedViewSetId; if ( previousId !== undefined && news.authorizedViewSetId !== undefined && news.authorizedViewSetId !== previousId ) { return { action: "replace" as const, deleteFirst: false }; } return undefined; }), read: Effect.fn(function* ({ id, olds, output }) { const env = yield* GcpEnvironment.current; const authorizedViewSetId = yield* toResourceId( id, olds?.authorizedViewSetId, output?.authorizedViewSetId, ); const location = normalizeLocation(olds?.location ?? output?.location ?? env.region); const name = output?.name ?? resourceName(env.project, location, authorizedViewSetId); const existing = yield* getByName(name); if (existing === undefined) return undefined; const attrs = toAttrs(existing, env.project); return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs); }), list: () => Effect.gen(function* () { const env = yield* GcpEnvironment.current; return yield* listAt(locationParent(env.project, env.region), env.project); }), reconcile: Effect.fn(function* ({ id, news, output }) { const env = yield* GcpEnvironment.current; const location = normalizeLocation(news.location ?? output?.location ?? env.region); const parent = locationParent(env.project, location); const authorizedViewSetId = yield* toResourceId( id, news.authorizedViewSetId, output?.authorizedViewSetId, ); const name = resourceName(env.project, location, authorizedViewSetId); const ownership = yield* createInternalLabels(id); const displayName = encodeOwnershipLine( ownership, news.displayName, MAX_VIEW_DISPLAY_NAME_LENGTH, ); let current = yield* getByName(output?.name ?? name); if (current === undefined) { const created = yield* cci .createProjectsLocationsAuthorizedViewSets({ parent, authorizedViewSetId, body: { displayName }, }) .pipe(Effect.catchTag("Conflict", () => getByName(name))); current = created ?? undefined; } if (current === undefined) { return yield* new AuthorizedViewSetNotResolved({ resourceName: name }); } const currentName = current.name ?? name; if ((current.displayName ?? "") !== displayName) { current = yield* cci.patchProjectsLocationsAuthorizedViewSets({ name: currentName, updateMask: "display_name", body: { name: currentName, displayName }, }); } return toAttrs(current, env.project); }), delete: Effect.fn(function* ({ output }) { yield* cci .deleteProjectsLocationsAuthorizedViewSets({ name: output.name, force: true, }) .pipe(Effect.catchTag("NotFound", () => Effect.void)); }), });
.repos/alchemy-effect/packages/gcp/src/ContactCenterInsights/AuthorizedViewSet.ts at main · sailfork/t3code-github · Sailfork
. Immutable — changing it replaces\n * the set.\n */\n authorizedViewSetId?: string;\n /**\n * Location (`us-central1`, …). Immutable — changing it replaces the set.\n * `US-CENTRAL1` is accepted and normalized to `us-central1`.\n * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)\n */\n location?: string;\n /**\n * Display name (max 64 characters including Alchemy's ownership\n * marker). AuthorizedViewSets have no labels field, so ownership is\n * stored in a `[alchemy …]` prefix and stripped from attributes.\n */\n displayName?: string;\n};\n\nexport type AuthorizedViewSet = Resource\u003c\n \"GCP.ContactCenterInsights.AuthorizedViewSet\",\n AuthorizedViewSetProps,\n {\n /** Full resource name `projects/{project}/locations/{location}/authorizedViewSets/{authorized_view_set}`. */\n name: string;\n /** AuthorizedViewSet id (last path segment). */\n authorizedViewSetId: string;\n /** Location id. */\n location: string;\n /** Project id. */\n project: string;\n /** User display name with the Alchemy ownership prefix stripped. */\n displayName: string | undefined;\n /** RFC3339 creation timestamp. */\n createTime: string | undefined;\n /** RFC3339 last-update timestamp. */\n updateTime: string | undefined;\n },\n never,\n Providers\n>;\n\n/**\n * A Contact Center AI Insights AuthorizedViewSet, a container for\n * AuthorizedView resources.\n *\n * AuthorizedViewSets have no labels field — Alchemy stamps ownership into\n * the display name. Location and id are immutable. Display name updates\n * in place.\n *\n * ### Creating an AuthorizedViewSet\n * **Example:** Generated id\n * ```typescript\n * const set = yield* GCP.ContactCenterInsights.AuthorizedViewSet(\"QaViews\", {\n * displayName: \"qa-reviewers\",\n * });\n * ```\n *\n * **Example:** Named set\n * ```typescript\n * const set = yield* GCP.ContactCenterInsights.AuthorizedViewSet(\"QaViews\", {\n * authorizedViewSetId: \"qa-reviewers\",\n * displayName: \"qa-reviewers\",\n * });\n * ```\n *\n * @resource\n * @category ContactCenterInsights\n */\nexport const AuthorizedViewSet = Resource\u003cAuthorizedViewSet>(\n \"GCP.ContactCenterInsights.AuthorizedViewSet\",\n);\n\nexport class AuthorizedViewSetNotResolved extends Data.TaggedError(\n \"GCP.ContactCenterInsights.AuthorizedViewSetNotResolved\",\n)\u003c{\n resourceName: string;\n}> {}\n\nconst resourceName = (project: string, location: string, authorizedViewSetId: string) =>\n `${locationParent(project, location)}/authorizedViewSets/${authorizedViewSetId}`;\n\nconst toAttrs = (set: cci.GoogleCloudContactcenterinsightsV1AuthorizedViewSet, project: string) => {\n const name = set.name ?? \"\";\n const parsed = parseOwnership(set.displayName);\n return {\n name,\n authorizedViewSetId: lastSegment(name),\n location: locationOf(name),\n project,\n displayName: parsed.text,\n createTime: set.createTime,\n updateTime: set.updateTime,\n };\n};\n\nconst getByName = (name: string) =>\n name.length === 0\n ? Effect.undefined\n : cci\n .getProjectsLocationsAuthorizedViewSets({ name })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.undefined));\n\nconst listAt = (parent: string, project: string) =>\n cci.listProjectsLocationsAuthorizedViewSets.pages({ parent, pageSize: 100 }).pipe(\n Stream.flatMap((page) => Stream.fromIterable(page.authorizedViewSets ?? [])),\n Stream.filter((set) => hasOwnershipMarker(set.displayName)),\n Stream.map((set) => toAttrs(set, project)),\n Stream.runCollect,\n Effect.map((chunk) => Array.from(chunk)),\n Effect.catchTag(\"NotFound\", () => Effect.succeed([])),\n );\n\nexport const AuthorizedViewSetProvider = () =>\n Provider.succeed(AuthorizedViewSet, {\n stables: [\"name\", \"authorizedViewSetId\", \"location\", \"project\", \"createTime\"],\n\n diff: Effect.fn(function* ({ news, olds, output }) {\n if (!isResolved(news)) return undefined;\n const previousLocation = olds?.location ?? output?.location;\n if (\n previousLocation !== undefined &&\n news.location !== undefined &&\n normalizeLocation(previousLocation) !== normalizeLocation(news.location)\n ) {\n return { action: \"replace\" as const, deleteFirst: false };\n }\n const previousId = olds?.authorizedViewSetId ?? output?.authorizedViewSetId;\n if (\n previousId !== undefined &&\n news.authorizedViewSetId !== undefined &&\n news.authorizedViewSetId !== previousId\n ) {\n return { action: \"replace\" as const, deleteFirst: false };\n }\n return undefined;\n }),\n\n read: Effect.fn(function* ({ id, olds, output }) {\n const env = yield* GcpEnvironment.current;\n const authorizedViewSetId = yield* toResourceId(\n id,\n olds?.authorizedViewSetId,\n output?.authorizedViewSetId,\n );\n const location = normalizeLocation(olds?.location ?? output?.location ?? env.region);\n const name = output?.name ?? resourceName(env.project, location, authorizedViewSetId);\n const existing = yield* getByName(name);\n if (existing === undefined) return undefined;\n const attrs = toAttrs(existing, env.project);\n return (yield* ownedByAlchemy(id, existing.displayName)) ? attrs : Unowned(attrs);\n }),\n\n list: () =>\n Effect.gen(function* () {\n const env = yield* GcpEnvironment.current;\n return yield* listAt(locationParent(env.project, env.region), env.project);\n }),\n\n reconcile: Effect.fn(function* ({ id, news, output }) {\n const env = yield* GcpEnvironment.current;\n const location = normalizeLocation(news.location ?? output?.location ?? env.region);\n const parent = locationParent(env.project, location);\n const authorizedViewSetId = yield* toResourceId(\n id,\n news.authorizedViewSetId,\n output?.authorizedViewSetId,\n );\n const name = resourceName(env.project, location, authorizedViewSetId);\n const ownership = yield* createInternalLabels(id);\n const displayName = encodeOwnershipLine(\n ownership,\n news.displayName,\n MAX_VIEW_DISPLAY_NAME_LENGTH,\n );\n\n let current = yield* getByName(output?.name ?? name);\n\n if (current === undefined) {\n const created = yield* cci\n .createProjectsLocationsAuthorizedViewSets({\n parent,\n authorizedViewSetId,\n body: { displayName },\n })\n .pipe(Effect.catchTag(\"Conflict\", () => getByName(name)));\n current = created ?? undefined;\n }\n\n if (current === undefined) {\n return yield* new AuthorizedViewSetNotResolved({ resourceName: name });\n }\n\n const currentName = current.name ?? name;\n if ((current.displayName ?? \"\") !== displayName) {\n current = yield* cci.patchProjectsLocationsAuthorizedViewSets({\n name: currentName,\n updateMask: \"display_name\",\n body: { name: currentName, displayName },\n });\n }\n\n return toAttrs(current, env.project);\n }),\n\n delete: Effect.fn(function* ({ output }) {\n yield* cci\n .deleteProjectsLocationsAuthorizedViewSets({\n name: output.name,\n force: true,\n })\n .pipe(Effect.catchTag(\"NotFound\", () => Effect.void));\n }),\n });\n","preview":null,"history":null,"version":"e10018e42e036ab75076dbb9a790086a1a7b6ed7"},"/_session?shell=1":null}