@odla-ai/chapter 0.33.1 → 0.34.0

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.
@@ -542,11 +542,16 @@ function groupAttrs(document) {
542
542
  return {
543
543
  ...first ? { standardPriceCents: first.priceCents } : {},
544
544
  foundingDiscountCents: 0,
545
- stripePriceId: firstPaid?.stripePriceId ?? null,
545
+ ...firstPaid?.stripePriceId ? { stripePriceId: firstPaid.stripePriceId } : {},
546
546
  ...document.policy ?? {},
547
547
  ...document.scheduling ? { schedulingJson: document.scheduling } : {}
548
548
  };
549
549
  }
550
+ var byPublicId = (ns, value) => ({
551
+ ns,
552
+ attr: "id",
553
+ value
554
+ });
550
555
  async function materializeSignupControl(db, envelope, previous, receivedAt) {
551
556
  const { document, digest } = envelope;
552
557
  const currentRows = (await db.query({ tiers: { $: {
@@ -558,22 +563,40 @@ async function materializeSignupControl(db, envelope, previous, receivedAt) {
558
563
  if (previous?.active) ops.push({ t: "update", ns: "signupControls", id: previous.id, attrs: { active: false } });
559
564
  for (const row of currentRows) {
560
565
  const id2 = String(row.id ?? "");
561
- 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
+ }
562
569
  }
563
570
  for (const tier of document.tiers) {
564
- ops.push({ t: "update", ns: "tiers", id: tier.id, attrs: {
571
+ const tierRef = byPublicId("tiers", tier.id);
572
+ ops.push({ t: "update", ns: "tiers", id: tierRef, attrs: {
565
573
  id: tier.id,
566
574
  groupId: document.target.chapterId,
567
575
  name: tier.name,
568
576
  priceCents: tier.priceCents,
569
- stripePriceId: tier.stripePriceId ?? null,
570
- blurb: tier.blurb ?? null,
577
+ ...tier.stripePriceId ? { stripePriceId: tier.stripePriceId } : {},
578
+ ...tier.blurb ? { blurb: tier.blurb } : {},
571
579
  sortOrder: tier.sortOrder,
572
580
  active: tier.available,
573
581
  createdAt: receivedAt
574
582
  } });
583
+ const retract = [
584
+ ...!tier.stripePriceId ? ["stripePriceId"] : [],
585
+ ...!tier.blurb ? ["blurb"] : []
586
+ ];
587
+ if (retract.length) ops.push({ t: "retract", ns: "tiers", id: tierRef, attrs: retract });
588
+ }
589
+ const groupRef = byPublicId("groups", document.target.chapterId);
590
+ const attrs = groupAttrs(document);
591
+ ops.push({
592
+ t: "update",
593
+ ns: "groups",
594
+ id: groupRef,
595
+ attrs
596
+ });
597
+ if (!("stripePriceId" in attrs)) {
598
+ ops.push({ t: "retract", ns: "groups", id: groupRef, attrs: ["stripePriceId"] });
575
599
  }
576
- ops.push({ t: "update", ns: "groups", id: document.target.chapterId, attrs: groupAttrs(document) });
577
600
  const id = `signup-control:${document.target.environment}:${document.revision}`;
578
601
  ops.push({ t: "update", ns: "signupControls", id, attrs: {
579
602
  id,
@@ -1803,7 +1826,11 @@ var handleSignupControl = async (req, url, env, ctx) => {
1803
1826
  });
1804
1827
  }
1805
1828
  const receivedAt = Date.now();
1806
- await materializeSignupControl(db, { digest, document }, latest, receivedAt);
1829
+ try {
1830
+ await materializeSignupControl(db, { digest, document }, latest, receivedAt);
1831
+ } catch {
1832
+ return reject("materialization_failed", 503);
1833
+ }
1807
1834
  return json({
1808
1835
  ok: true,
1809
1836
  target: { chapterId: ctx.chapter.id, environment: env.ODLA_ENV },