@ai-matrx/records 0.98.10 → 0.98.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5148,20 +5148,22 @@ function tableRenderSchema(facts) {
5148
5148
  function refuse(code, message, hint) {
5149
5149
  return { ok: false, error: { code, message, hint, ours: true } };
5150
5150
  }
5151
- function alternatePatch(document, field_key, alternate_index) {
5151
+ function alternatePatch(document, field_key, alternate_index, rank) {
5152
5152
  const block = document._values ?? {};
5153
5153
  const envelope = block[field_key];
5154
5154
  const alternates = Array.isArray(envelope?.alternates) ? envelope.alternates : [];
5155
- const chosen = alternates[alternate_index];
5155
+ const at = rank === void 0 ? alternate_index : alternates.findIndex((a) => a.rank === rank);
5156
+ alternate_index = at;
5157
+ const chosen = alternates[at];
5156
5158
  if (!envelope || !chosen) return { ok: false, message: "That other value is no longer on this record." };
5157
5159
  const kept = alternates.filter((_, i) => i !== alternate_index);
5158
5160
  const current = document[field_key];
5159
5161
  const next = [...kept];
5160
5162
  if (current !== null && current !== void 0) {
5161
5163
  const taken = new Set(kept.map((a) => a.rank));
5162
- let rank = 1;
5163
- while (taken.has(rank)) rank += 1;
5164
- next.push({ value: current, ...envelope.src ? { src: envelope.src } : {}, rank });
5164
+ let rank2 = 1;
5165
+ while (taken.has(rank2)) rank2 += 1;
5166
+ next.push({ value: current, ...envelope.src ? { src: envelope.src } : {}, rank: rank2 });
5165
5167
  }
5166
5168
  const { ver: _ver, actor: _actor, at: _at, on_behalf_of: _on, absent: _absent, alternates: _alts, src: _src, ...rest } = envelope;
5167
5169
  const changed = { ...rest, ...chosen.src ? { src: chosen.src } : {} };
@@ -5175,14 +5177,20 @@ function alternatePatch(document, field_key, alternate_index) {
5175
5177
  }
5176
5178
  };
5177
5179
  }
5178
- async function chooseAlternate(client, { record_id, field_key, alternate_index }) {
5180
+ async function chooseAlternate(client, { record_id, field_key, alternate_index, rank }) {
5179
5181
  const read = await client.recordRead({ record_id, withVersion: true });
5180
5182
  if (!read.ok) return read;
5183
+ if (Object.keys(read.data.hidden ?? {}).length > 0) {
5184
+ return refuse("refused_by_rule", "Some fields here are hidden from you, so this choice can't be saved safely.", "Ask someone who can see every field.");
5185
+ }
5181
5186
  const version = read.data.version;
5182
5187
  if (version === null || version === void 0) {
5183
5188
  return refuse("stale_read", "Nothing was saved: this record's latest changes could not be checked.", "Reload, then try again.");
5184
5189
  }
5185
- const built = alternatePatch(read.data.document, field_key, alternate_index);
5190
+ const raw = await client.recordAsOf({ record_id, recorded_at: (/* @__PURE__ */ new Date()).toISOString() });
5191
+ if (!raw.ok) return raw;
5192
+ if (raw.data === null) return refuse("not_found", "That record is no longer here.", "Reload, then look again.");
5193
+ const built = alternatePatch(raw.data, field_key, alternate_index, rank);
5186
5194
  if (!built.ok) return refuse("not_found", built.message, "Reload, then look again.");
5187
5195
  return client.recordUpdate({ record_id, patch: built.patch, expectedVersion: version });
5188
5196
  }