@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.
@@ -499,7 +499,16 @@ function asStored(row) {
499
499
  if (!row || typeof row.document !== "object" || row.document === null) return null;
500
500
  return row;
501
501
  }
502
+ async function hasSignupControl(db, targetId, environment, active) {
503
+ const where = { targetId, environment, ...active ? { active: true } : {} };
504
+ if (db.aggregate) {
505
+ return Number((await db.aggregate("signupControls", { count: true }, { where })).count ?? 0) > 0;
506
+ }
507
+ const rows2 = (await db.query({ signupControls: {} })).signupControls;
508
+ return Array.isArray(rows2) && rows2.some((row) => row.targetId === targetId && row.environment === environment && (!active || row.active === true));
509
+ }
502
510
  async function loadLatestSignupControl(db, targetId, environment) {
511
+ if (!await hasSignupControl(db, targetId, environment)) return null;
503
512
  const rows2 = (await db.query({ signupControls: { $: {
504
513
  where: { targetId, environment },
505
514
  order: { revision: "desc" },
@@ -509,6 +518,7 @@ async function loadLatestSignupControl(db, targetId, environment) {
509
518
  return asStored(latest);
510
519
  }
511
520
  async function loadActiveSignupControl(db, targetId, environment) {
521
+ if (!await hasSignupControl(db, targetId, environment, true)) return null;
512
522
  const rows2 = (await db.query({ signupControls: { $: {
513
523
  where: { targetId, environment, active: true },
514
524
  order: { revision: "desc" },
@@ -537,6 +547,11 @@ function groupAttrs(document) {
537
547
  ...document.scheduling ? { schedulingJson: document.scheduling } : {}
538
548
  };
539
549
  }
550
+ var byPublicId = (ns, value) => ({
551
+ ns,
552
+ attr: "id",
553
+ value
554
+ });
540
555
  async function materializeSignupControl(db, envelope, previous, receivedAt) {
541
556
  const { document, digest } = envelope;
542
557
  const currentRows = (await db.query({ tiers: { $: {
@@ -548,10 +563,12 @@ async function materializeSignupControl(db, envelope, previous, receivedAt) {
548
563
  if (previous?.active) ops.push({ t: "update", ns: "signupControls", id: previous.id, attrs: { active: false } });
549
564
  for (const row of currentRows) {
550
565
  const id2 = String(row.id ?? "");
551
- if (id2 && !incoming.has(id2)) ops.push({ t: "update", ns: "tiers", id: id2, attrs: { active: false } });
566
+ if (id2 && !incoming.has(id2)) {
567
+ ops.push({ t: "update", ns: "tiers", id: byPublicId("tiers", id2), attrs: { active: false } });
568
+ }
552
569
  }
553
570
  for (const tier of document.tiers) {
554
- ops.push({ t: "update", ns: "tiers", id: tier.id, attrs: {
571
+ ops.push({ t: "update", ns: "tiers", id: byPublicId("tiers", tier.id), attrs: {
555
572
  id: tier.id,
556
573
  groupId: document.target.chapterId,
557
574
  name: tier.name,
@@ -563,7 +580,12 @@ async function materializeSignupControl(db, envelope, previous, receivedAt) {
563
580
  createdAt: receivedAt
564
581
  } });
565
582
  }
566
- ops.push({ t: "update", ns: "groups", id: document.target.chapterId, attrs: groupAttrs(document) });
583
+ ops.push({
584
+ t: "update",
585
+ ns: "groups",
586
+ id: byPublicId("groups", document.target.chapterId),
587
+ attrs: groupAttrs(document)
588
+ });
567
589
  const id = `signup-control:${document.target.environment}:${document.revision}`;
568
590
  ops.push({ t: "update", ns: "signupControls", id, attrs: {
569
591
  id,
@@ -1793,7 +1815,11 @@ var handleSignupControl = async (req, url, env, ctx) => {
1793
1815
  });
1794
1816
  }
1795
1817
  const receivedAt = Date.now();
1796
- await materializeSignupControl(db, { digest, document }, latest, receivedAt);
1818
+ try {
1819
+ await materializeSignupControl(db, { digest, document }, latest, receivedAt);
1820
+ } catch {
1821
+ return reject("materialization_failed", 503);
1822
+ }
1797
1823
  return json({
1798
1824
  ok: true,
1799
1825
  target: { chapterId: ctx.chapter.id, environment: env.ODLA_ENV },