@ai-matrx/records 0.58.117 → 0.58.118

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.
@@ -39907,6 +39907,13 @@ function createRecordsClient(config) {
39907
39907
  }
39908
39908
  return last;
39909
39909
  }
39910
+ const spans = (filter) => Boolean(filter) || (config.organizationIds?.length ?? 0) > 0;
39911
+ const metadataSearchDoor = () => {
39912
+ const refusal = doorAbsent(DOORS.metadataSearch);
39913
+ scream({ code: refusal.code, message: refusal.message, ...refusal.hint ? { hint: refusal.hint } : {}, where: "metadataSearch" });
39914
+ return err(refusal);
39915
+ };
39916
+ const unavailableReason = (error) => `${error.message}${error.hint ? ` ${error.hint}` : ""}`;
39910
39917
  const client = {
39911
39918
  config,
39912
39919
  async recordWrite({ table_id, data }) {
@@ -41236,7 +41243,23 @@ function createRecordsClient(config) {
41236
41243
  "dashboardDelete"
41237
41244
  );
41238
41245
  },
41239
- async recordAggregate({ table_id, groupBy, measures, bucket, filter, limit, compare }) {
41246
+ async recordAggregate({ table_id, organization_id, groupBy, measures, bucket, filter, limit, compare }) {
41247
+ if (spans(organization_id)) {
41248
+ const owner = await client.ownerOrganization({ table_id, ...organization_id ? { organization_id } : {} });
41249
+ const target = owner.ok && owner.data ? owner.data : organization_id ?? org;
41250
+ if (target !== org) {
41251
+ const answered = await clientFor(target).recordAggregate({
41252
+ table_id,
41253
+ ...groupBy ? { groupBy } : {},
41254
+ ...measures ? { measures } : {},
41255
+ ...bucket !== void 0 ? { bucket } : {},
41256
+ ...filter ? { filter } : {},
41257
+ ...limit !== void 0 ? { limit } : {},
41258
+ ...compare !== void 0 ? { compare } : {}
41259
+ });
41260
+ return answered.ok ? ok(answered.data.map((row) => ({ ...row, organization_id: target }))) : answered;
41261
+ }
41262
+ }
41240
41263
  return callDoor(
41241
41264
  DOORS.recordAggregate,
41242
41265
  {
@@ -42152,10 +42175,80 @@ function createRecordsClient(config) {
42152
42175
  "ruleDeclare"
42153
42176
  );
42154
42177
  },
42155
- async metadataSearch() {
42156
- const refusal = doorAbsent(DOORS.metadataSearch);
42157
- scream({ code: refusal.code, message: refusal.message, ...refusal.hint ? { hint: refusal.hint } : {}, where: "metadataSearch" });
42158
- return err(refusal);
42178
+ async metadataSearch({ text, organization_id }) {
42179
+ if (spans(organization_id) && readOrgs(organization_id).length > 0) {
42180
+ const targets = readOrgs(organization_id).filter((t) => t !== null);
42181
+ const unavailable = [];
42182
+ const answers = await Promise.all(
42183
+ targets.map(async (t) => {
42184
+ const sub = clientFor(t);
42185
+ const open = await sub.storeIsOpen();
42186
+ if (!open.ok) {
42187
+ unavailable.push({ organization_id: t, reason: unavailableReason(open.error) });
42188
+ return null;
42189
+ }
42190
+ if (!open.data) {
42191
+ unavailable.push({ organization_id: t, reason: "The custom data store is switched off for this organization." });
42192
+ return null;
42193
+ }
42194
+ const found = metadataSearchDoor();
42195
+ if (!found.ok) {
42196
+ unavailable.push({ organization_id: t, reason: unavailableReason(found.error) });
42197
+ return { t, found };
42198
+ }
42199
+ return { t, found };
42200
+ })
42201
+ );
42202
+ const matches = [];
42203
+ const covered = [];
42204
+ let firstRefusal = null;
42205
+ for (const answer of answers) {
42206
+ if (!answer) continue;
42207
+ if (!answer.found.ok) {
42208
+ firstRefusal ??= answer.found.error;
42209
+ continue;
42210
+ }
42211
+ covered.push(answer.t);
42212
+ for (const m of answer.found.data.matches) matches.push({ ...m, organization_id: answer.t });
42213
+ }
42214
+ const inOrder = (a, b) => targets.indexOf(a.organization_id) - targets.indexOf(b.organization_id);
42215
+ unavailable.sort(inOrder);
42216
+ if (covered.length === 0 && firstRefusal) return err(firstRefusal);
42217
+ return ok({ matches, organizations_covered: covered, unavailable });
42218
+ }
42219
+ return metadataSearchDoor();
42220
+ },
42221
+ async organizationsOpen(args) {
42222
+ const targets = readOrgs(args?.organization_id).filter((t) => t !== null);
42223
+ const open = [];
42224
+ const unavailable = [];
42225
+ await Promise.all(
42226
+ targets.map(async (t) => {
42227
+ const result = await clientFor(t).storeIsOpen();
42228
+ if (!result.ok) unavailable.push({ organization_id: t, reason: unavailableReason(result.error) });
42229
+ else if (!result.data)
42230
+ unavailable.push({ organization_id: t, reason: "The custom data store is switched off for this organization." });
42231
+ else open.push(t);
42232
+ })
42233
+ );
42234
+ const position = (id) => targets.indexOf(id);
42235
+ open.sort((a, b) => position(a) - position(b));
42236
+ unavailable.sort((a, b) => position(a.organization_id) - position(b.organization_id));
42237
+ return ok({ open, unavailable });
42238
+ },
42239
+ async ownerOrganization({ table_id, record_id, organization_id }) {
42240
+ if (table_id) {
42241
+ const read = await client.tableRead({ table_id, ...organization_id ? { organization_id } : {} });
42242
+ if (!read.ok) return err(read.error);
42243
+ return ok(read.data ? read.data.organization_id ?? null : null);
42244
+ }
42245
+ if (record_id) {
42246
+ const read = await client.recordRead({ record_id, ...organization_id ? { organization_id } : {} });
42247
+ if (!read.ok) return ok(null);
42248
+ const targets = readOrgs(organization_id);
42249
+ return ok(recordOrgs.get(record_id) ?? organization_id ?? (targets.length === 1 ? targets[0] ?? null : null));
42250
+ }
42251
+ return ok(null);
42159
42252
  },
42160
42253
  async fieldPropose() {
42161
42254
  const refusal = doorAbsent(DOORS.fieldPropose);