(
input: SaveArtifactInput,
)
| 6224 | }); |
| 6225 | |
| 6226 | const artifactsSave = ( |
| 6227 | input: SaveArtifactInput, |
| 6228 | ): Effect.Effect<Artifact, ArtifactNotFoundError | StorageFailure> => |
| 6229 | Effect.gen(function* () { |
| 6230 | const now = new Date(); |
| 6231 | const description = input.description ?? null; |
| 6232 | // An explicit id overwrites in place (v1 keeps no version history). It |
| 6233 | // must already resolve to a visible row: minting a caller-chosen id |
| 6234 | // would let a stale client resurrect a deleted artifact silently. |
| 6235 | if (input.id !== undefined) { |
| 6236 | const where = artifactById(input.id); |
| 6237 | const existing = yield* core.findFirst("artifact", { where }); |
| 6238 | if (!existing) { |
| 6239 | return yield* new ArtifactNotFoundError({ |
| 6240 | id: ArtifactId.make(input.id), |
| 6241 | }); |
| 6242 | } |
| 6243 | // `bindings` is written on every overwrite, including back to null: |
| 6244 | // it interprets `code`, so carrying the previous value forward under |
| 6245 | // new source would bind roles the new code never declares. `preview` |
| 6246 | // is written for exactly the same reason, and the case it prevents is |
| 6247 | // visible: an image preview captured from the OLD render would |
| 6248 | // otherwise keep advertising a version of the artifact that no longer |
| 6249 | // exists. |
| 6250 | const set = { |
| 6251 | title: input.title, |
| 6252 | description, |
| 6253 | code: input.code, |
| 6254 | bindings: input.bindings ?? null, |
| 6255 | preview: input.preview ?? null, |
| 6256 | updated_at: now, |
| 6257 | }; |
| 6258 | yield* core.updateMany("artifact", { where, set }); |
| 6259 | return rowToArtifact({ ...existing, ...set }); |
| 6260 | } |
| 6261 | yield* requireUserSubject("user"); |
| 6262 | const keys = yield* Effect.try({ |
| 6263 | try: () => ownedKeys("user"), |
| 6264 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 6265 | }); |
| 6266 | const created = yield* core.create("artifact", { |
| 6267 | tenant: keys.tenant, |
| 6268 | owner: keys.owner, |
| 6269 | subject: keys.subject, |
| 6270 | id: `art_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`, |
| 6271 | title: input.title, |
| 6272 | description, |
| 6273 | code: input.code, |
| 6274 | bindings: input.bindings ?? null, |
| 6275 | preview: input.preview ?? null, |
| 6276 | created_at: now, |
| 6277 | updated_at: now, |
| 6278 | }); |
| 6279 | return rowToArtifact(created); |
| 6280 | }); |
| 6281 | |
| 6282 | /** |
| 6283 | * Replace an artifact's preview with a snapshot of a settled render. |
nothing calls this directly
no test coverage detected