(
input: SaveArtifactInput,
)
| 6013 | }); |
| 6014 | |
| 6015 | const artifactsSave = ( |
| 6016 | input: SaveArtifactInput, |
| 6017 | ): Effect.Effect<Artifact, ArtifactNotFoundError | StorageFailure> => |
| 6018 | Effect.gen(function* () { |
| 6019 | const now = new Date(); |
| 6020 | const description = input.description ?? null; |
| 6021 | // An explicit id overwrites in place (v1 keeps no version history). It |
| 6022 | // must already resolve to a visible row: minting a caller-chosen id |
| 6023 | // would let a stale client resurrect a deleted artifact silently. |
| 6024 | if (input.id !== undefined) { |
| 6025 | const where = artifactById(input.id); |
| 6026 | const existing = yield* core.findFirst("artifact", { where }); |
| 6027 | if (!existing) { |
| 6028 | return yield* new ArtifactNotFoundError({ |
| 6029 | id: ArtifactId.make(input.id), |
| 6030 | }); |
| 6031 | } |
| 6032 | // `bindings` is written on every overwrite, including back to null: |
| 6033 | // it interprets `code`, so carrying the previous value forward under |
| 6034 | // new source would bind roles the new code never declares. `preview` |
| 6035 | // is written for exactly the same reason, and the case it prevents is |
| 6036 | // visible: an image preview captured from the OLD render would |
| 6037 | // otherwise keep advertising a version of the artifact that no longer |
| 6038 | // exists. |
| 6039 | const set = { |
| 6040 | title: input.title, |
| 6041 | description, |
| 6042 | code: input.code, |
| 6043 | bindings: input.bindings ?? null, |
| 6044 | preview: input.preview ?? null, |
| 6045 | updated_at: now, |
| 6046 | }; |
| 6047 | yield* core.updateMany("artifact", { where, set }); |
| 6048 | return rowToArtifact({ ...existing, ...set }); |
| 6049 | } |
| 6050 | yield* requireUserSubject("user"); |
| 6051 | const keys = yield* Effect.try({ |
| 6052 | try: () => ownedKeys("user"), |
| 6053 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 6054 | }); |
| 6055 | const created = yield* core.create("artifact", { |
| 6056 | tenant: keys.tenant, |
| 6057 | owner: keys.owner, |
| 6058 | subject: keys.subject, |
| 6059 | id: `art_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`, |
| 6060 | title: input.title, |
| 6061 | description, |
| 6062 | code: input.code, |
| 6063 | bindings: input.bindings ?? null, |
| 6064 | preview: input.preview ?? null, |
| 6065 | created_at: now, |
| 6066 | updated_at: now, |
| 6067 | }); |
| 6068 | return rowToArtifact(created); |
| 6069 | }); |
| 6070 | |
| 6071 | /** |
| 6072 | * Replace an artifact's preview with a snapshot of a settled render. |
nothing calls this directly
no test coverage detected