@odla-ai/chapter 0.33.0 → 0.33.2

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.
@@ -527,7 +527,16 @@ function asStored(row) {
527
527
  if (!row || typeof row.document !== "object" || row.document === null) return null;
528
528
  return row;
529
529
  }
530
+ async function hasSignupControl(db, targetId, environment, active) {
531
+ const where = { targetId, environment, ...active ? { active: true } : {} };
532
+ if (db.aggregate) {
533
+ return Number((await db.aggregate("signupControls", { count: true }, { where })).count ?? 0) > 0;
534
+ }
535
+ const rows2 = (await db.query({ signupControls: {} })).signupControls;
536
+ return Array.isArray(rows2) && rows2.some((row) => row.targetId === targetId && row.environment === environment && (!active || row.active === true));
537
+ }
530
538
  async function loadLatestSignupControl(db, targetId, environment) {
539
+ if (!await hasSignupControl(db, targetId, environment)) return null;
531
540
  const rows2 = (await db.query({ signupControls: { $: {
532
541
  where: { targetId, environment },
533
542
  order: { revision: "desc" },
@@ -537,6 +546,7 @@ async function loadLatestSignupControl(db, targetId, environment) {
537
546
  return asStored(latest);
538
547
  }
539
548
  async function loadActiveSignupControl(db, targetId, environment) {
549
+ if (!await hasSignupControl(db, targetId, environment, true)) return null;
540
550
  const rows2 = (await db.query({ signupControls: { $: {
541
551
  where: { targetId, environment, active: true },
542
552
  order: { revision: "desc" },
@@ -565,6 +575,11 @@ function groupAttrs(document) {
565
575
  ...document.scheduling ? { schedulingJson: document.scheduling } : {}
566
576
  };
567
577
  }
578
+ var byPublicId = (ns, value) => ({
579
+ ns,
580
+ attr: "id",
581
+ value
582
+ });
568
583
  async function materializeSignupControl(db, envelope, previous, receivedAt) {
569
584
  const { document, digest } = envelope;
570
585
  const currentRows = (await db.query({ tiers: { $: {
@@ -576,10 +591,12 @@ async function materializeSignupControl(db, envelope, previous, receivedAt) {
576
591
  if (previous?.active) ops.push({ t: "update", ns: "signupControls", id: previous.id, attrs: { active: false } });
577
592
  for (const row of currentRows) {
578
593
  const id2 = String(row.id ?? "");
579
- if (id2 && !incoming.has(id2)) ops.push({ t: "update", ns: "tiers", id: id2, attrs: { active: false } });
594
+ if (id2 && !incoming.has(id2)) {
595
+ ops.push({ t: "update", ns: "tiers", id: byPublicId("tiers", id2), attrs: { active: false } });
596
+ }
580
597
  }
581
598
  for (const tier of document.tiers) {
582
- ops.push({ t: "update", ns: "tiers", id: tier.id, attrs: {
599
+ ops.push({ t: "update", ns: "tiers", id: byPublicId("tiers", tier.id), attrs: {
583
600
  id: tier.id,
584
601
  groupId: document.target.chapterId,
585
602
  name: tier.name,
@@ -591,7 +608,12 @@ async function materializeSignupControl(db, envelope, previous, receivedAt) {
591
608
  createdAt: receivedAt
592
609
  } });
593
610
  }
594
- ops.push({ t: "update", ns: "groups", id: document.target.chapterId, attrs: groupAttrs(document) });
611
+ ops.push({
612
+ t: "update",
613
+ ns: "groups",
614
+ id: byPublicId("groups", document.target.chapterId),
615
+ attrs: groupAttrs(document)
616
+ });
595
617
  const id = `signup-control:${document.target.environment}:${document.revision}`;
596
618
  ops.push({ t: "update", ns: "signupControls", id, attrs: {
597
619
  id,
@@ -1816,7 +1838,11 @@ var handleSignupControl = async (req, url, env, ctx) => {
1816
1838
  });
1817
1839
  }
1818
1840
  const receivedAt = Date.now();
1819
- await materializeSignupControl(db, { digest, document }, latest, receivedAt);
1841
+ try {
1842
+ await materializeSignupControl(db, { digest, document }, latest, receivedAt);
1843
+ } catch {
1844
+ return reject("materialization_failed", 503);
1845
+ }
1820
1846
  return json({
1821
1847
  ok: true,
1822
1848
  target: { chapterId: ctx.chapter.id, environment: env.ODLA_ENV },