(
input: SaveArtifactInput,
)
| 4057 | }); |
| 4058 | |
| 4059 | const artifactsSave = ( |
| 4060 | input: SaveArtifactInput, |
| 4061 | ): Effect.Effect<Artifact, ArtifactNotFoundError | StorageFailure> => |
| 4062 | Effect.gen(function* () { |
| 4063 | const now = new Date(); |
| 4064 | const description = input.description ?? null; |
| 4065 | // An explicit id overwrites in place (v1 keeps no version history). It |
| 4066 | // must already resolve to a visible row: minting a caller-chosen id |
| 4067 | // would let a stale client resurrect a deleted artifact silently. |
| 4068 | if (input.id !== undefined) { |
| 4069 | const where = artifactById(input.id); |
| 4070 | const existing = yield* core.findFirst("artifact", { where }); |
| 4071 | if (!existing) { |
| 4072 | return yield* new ArtifactNotFoundError({ id: ArtifactId.make(input.id) }); |
| 4073 | } |
| 4074 | // `bindings` is written on every overwrite, including back to null: |
| 4075 | // it interprets `code`, so carrying the previous value forward under |
| 4076 | // new source would bind roles the new code never declares. `preview` |
| 4077 | // is written for exactly the same reason, and the case it prevents is |
| 4078 | // visible: an image preview captured from the OLD render would |
| 4079 | // otherwise keep advertising a version of the artifact that no longer |
| 4080 | // exists. |
| 4081 | const set = { |
| 4082 | title: input.title, |
| 4083 | description, |
| 4084 | code: input.code, |
| 4085 | bindings: input.bindings ?? null, |
| 4086 | preview: input.preview ?? null, |
| 4087 | updated_at: now, |
| 4088 | }; |
| 4089 | yield* core.updateMany("artifact", { where, set }); |
| 4090 | return rowToArtifact({ ...existing, ...set }); |
| 4091 | } |
| 4092 | yield* requireUserSubject("user"); |
| 4093 | const keys = yield* Effect.try({ |
| 4094 | try: () => ownedKeys("user"), |
| 4095 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 4096 | }); |
| 4097 | const created = yield* core.create("artifact", { |
| 4098 | tenant: keys.tenant, |
| 4099 | owner: keys.owner, |
| 4100 | subject: keys.subject, |
| 4101 | id: `art_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`, |
| 4102 | title: input.title, |
| 4103 | description, |
| 4104 | code: input.code, |
| 4105 | bindings: input.bindings ?? null, |
| 4106 | preview: input.preview ?? null, |
| 4107 | created_at: now, |
| 4108 | updated_at: now, |
| 4109 | }); |
| 4110 | return rowToArtifact(created); |
| 4111 | }); |
| 4112 | |
| 4113 | /** |
| 4114 | * Replace an artifact's preview with a snapshot of a settled render. |
nothing calls this directly
no test coverage detected