(
input: SaveArtifactInput,
)
| 4129 | }); |
| 4130 | |
| 4131 | const artifactsSave = ( |
| 4132 | input: SaveArtifactInput, |
| 4133 | ): Effect.Effect<Artifact, ArtifactNotFoundError | StorageFailure> => |
| 4134 | Effect.gen(function* () { |
| 4135 | const now = new Date(); |
| 4136 | const description = input.description ?? null; |
| 4137 | // An explicit id overwrites in place (v1 keeps no version history). It |
| 4138 | // must already resolve to a visible row: minting a caller-chosen id |
| 4139 | // would let a stale client resurrect a deleted artifact silently. |
| 4140 | if (input.id !== undefined) { |
| 4141 | const where = artifactById(input.id); |
| 4142 | const existing = yield* core.findFirst("artifact", { where }); |
| 4143 | if (!existing) { |
| 4144 | return yield* new ArtifactNotFoundError({ id: ArtifactId.make(input.id) }); |
| 4145 | } |
| 4146 | // `bindings` is written on every overwrite, including back to null: |
| 4147 | // it interprets `code`, so carrying the previous value forward under |
| 4148 | // new source would bind roles the new code never declares. `preview` |
| 4149 | // is written for exactly the same reason, and the case it prevents is |
| 4150 | // visible: an image preview captured from the OLD render would |
| 4151 | // otherwise keep advertising a version of the artifact that no longer |
| 4152 | // exists. |
| 4153 | const set = { |
| 4154 | title: input.title, |
| 4155 | description, |
| 4156 | code: input.code, |
| 4157 | bindings: input.bindings ?? null, |
| 4158 | preview: input.preview ?? null, |
| 4159 | updated_at: now, |
| 4160 | }; |
| 4161 | yield* core.updateMany("artifact", { where, set }); |
| 4162 | return rowToArtifact({ ...existing, ...set }); |
| 4163 | } |
| 4164 | yield* requireUserSubject("user"); |
| 4165 | const keys = yield* Effect.try({ |
| 4166 | try: () => ownedKeys("user"), |
| 4167 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 4168 | }); |
| 4169 | const created = yield* core.create("artifact", { |
| 4170 | tenant: keys.tenant, |
| 4171 | owner: keys.owner, |
| 4172 | subject: keys.subject, |
| 4173 | id: `art_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`, |
| 4174 | title: input.title, |
| 4175 | description, |
| 4176 | code: input.code, |
| 4177 | bindings: input.bindings ?? null, |
| 4178 | preview: input.preview ?? null, |
| 4179 | created_at: now, |
| 4180 | updated_at: now, |
| 4181 | }); |
| 4182 | return rowToArtifact(created); |
| 4183 | }); |
| 4184 | |
| 4185 | /** |
| 4186 | * Replace an artifact's preview with a snapshot of a settled render. |
nothing calls this directly
no test coverage detected