{
  "schema_version": 2,
  "diagram_type": "workflow",
  "meta": {
    "title": "07 — Discounts, promotions and quote tokens",
    "subtitle": "Inspected working tree, 7 Oct 2026. Detailed HTML uses nine separate inline-SVG figures; candidate records their actual paths, not a committed-snapshot verification.",
    "output": ".archify/deep-20260930/07-discounts.html",
    "locale": "en",
    "visual_preset": "editorial",
    "quality_profile": "showcase",
    "animation": "none"
  },
  "lanes": [
    {
      "id": "rates",
      "label": "Plan discounts and the full-cycle price",
      "variant": "normal"
    },
    {
      "id": "coupons",
      "label": "Manual coupons: public code, account use and limits",
      "variant": "normal"
    },
    {
      "id": "promotions",
      "label": "Promotions: automatic offers without a code",
      "variant": "normal"
    },
    {
      "id": "quote-create",
      "label": "Quote tokens: contact link, custom plan and linked coupon",
      "variant": "normal"
    },
    {
      "id": "quote-entry",
      "label": "Quote link: customer routing, plan visibility and enforcement gaps",
      "variant": "normal"
    },
    {
      "id": "selection",
      "label": "Quote coupon vs typed coupon vs promotion",
      "variant": "normal"
    },
    {
      "id": "cycles",
      "label": "Stored discount: signup, renewals, retries and expiry",
      "variant": "normal"
    },
    {
      "id": "redemption-race",
      "label": "D11: atomic booking, settlement and expiry",
      "variant": "normal"
    },
    {
      "id": "settlement-race",
      "label": "D11: one settlement, one discount cycle",
      "variant": "normal"
    }
  ],
  "nodes": [
    {
      "id": "rate-form",
      "lane": "rates",
      "col": 0,
      "type": "frontend",
      "label": "Admin sets interval rate",
      "sublabel": "Base rate + interval discount · USD and INR are separate",
      "sources": [
        {
          "path": "siem-admin-new/app/dashboard/plans/page.tsx",
          "line": 202
        }
      ]
    },
    {
      "id": "rate-store",
      "lane": "rates",
      "col": 1,
      "type": "backend",
      "label": "Discount, then tax",
      "sublabel": "Rate × (1 − plan discount) · Then × (1 + tax); round 2dp",
      "sources": [
        {
          "path": "siem-admin-new/app/dashboard/plans/page.tsx",
          "line": 207
        }
      ]
    },
    {
      "id": "cycle-base",
      "lane": "rates",
      "col": 2,
      "type": "backend",
      "label": "Build full-cycle base",
      "sublabel": "Stored rate × assets × months · month 1 · quarter 3 · year 12",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 52
        }
      ]
    },
    {
      "id": "amount",
      "lane": "rates",
      "col": 3,
      "type": "backend",
      "label": "Calculate discount",
      "sublabel": "Percentage of full-cycle base · Or fixed once; cap at base",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 120
        }
      ]
    },
    {
      "id": "coupon-admin",
      "lane": "coupons",
      "col": 0,
      "type": "frontend",
      "label": "Create manual coupon",
      "sublabel": "Code, type, value, duration · Dates, plans, currency, cap",
      "sources": [
        {
          "path": "siem-server-new/src/services/discount.service.ts",
          "line": 11
        }
      ]
    },
    {
      "id": "coupon-preview",
      "lane": "coupons",
      "col": 1,
      "type": "frontend",
      "label": "Customer enters code",
      "sublabel": "Normalize uppercase · Preview validates; no reservation",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 808
        }
      ]
    },
    {
      "id": "coupon-check",
      "lane": "coupons",
      "col": 2,
      "type": "security",
      "label": "Checkout revalidates",
      "sublabel": "Dates, active, plan, currency · Global cap + prior user use",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 204
        }
      ]
    },
    {
      "id": "coupon-redeem",
      "lane": "coupons",
      "col": 3,
      "type": "database",
      "label": "Book winning coupon",
      "sublabel": "UNPAID + BOOKED together · 30-minute UTC deadline",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 657
        }
      ]
    },
    {
      "id": "promo-admin",
      "lane": "promotions",
      "col": 0,
      "type": "frontend",
      "label": "Super Admin creates offer",
      "sublabel": "Name, occasion, type, value · Dates, plans, currency, cycles",
      "sources": [
        {
          "path": "siem-server-new/src/services/discount.service.ts",
          "line": 77
        }
      ]
    },
    {
      "id": "promo-filter",
      "lane": "promotions",
      "col": 1,
      "type": "backend",
      "label": "Find running promotions",
      "sublabel": "isActive and start ≤ now ≤ end · Match plan and fixed currency",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 159
        }
      ]
    },
    {
      "id": "promo-best",
      "lane": "promotions",
      "col": 2,
      "type": "backend",
      "label": "Choose largest amount",
      "sublabel": "Full-cycle amount comparison · Equal amounts keep first seen",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 175
        }
      ]
    },
    {
      "id": "promo-store",
      "lane": "promotions",
      "col": 3,
      "type": "database",
      "label": "Compete, then copy winner",
      "sublabel": "Store promotionId and amount · No global redemption counter",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 633
        }
      ]
    },
    {
      "id": "quote-compose",
      "lane": "quote-create",
      "col": 0,
      "type": "frontend",
      "label": "Admin sends quote",
      "sublabel": "Pick plan and contact · Optional discount on custom plan",
      "sources": [
        {
          "path": "siem-server-new/src/services/plan.service.ts",
          "line": 736
        }
      ]
    },
    {
      "id": "quote-jwt",
      "lane": "quote-create",
      "col": 1,
      "type": "security",
      "label": "Sign contact JWT",
      "sublabel": "contact_id + custom_quote · Default expiry 7 days",
      "sources": [
        {
          "path": "siem-server-new/src/utils/auth.ts",
          "line": 83
        }
      ]
    },
    {
      "id": "quote-coupon",
      "lane": "quote-create",
      "col": 2,
      "type": "database",
      "label": "Create linked QUOTE code",
      "sublabel": "Only when discount > 0 · One redemption; quoted plan only",
      "sources": [
        {
          "path": "siem-server-new/src/services/plan.service.ts",
          "line": 796
        }
      ]
    },
    {
      "id": "quote-email",
      "lane": "quote-create",
      "col": 3,
      "type": "external",
      "label": "Email link and save SENT",
      "sublabel": "Link includes ?token=JWT · Contact stores plan + couponId",
      "sources": [
        {
          "path": "siem-server-new/src/services/plan.service.ts",
          "line": 846
        }
      ]
    },
    {
      "id": "quote-open",
      "lane": "quote-entry",
      "col": 0,
      "type": "frontend",
      "label": "Customer opens link",
      "sublabel": "Registered subscriber → sign in · New buyer → onboarding",
      "sources": [
        {
          "path": "siem-client-new/app/portal/(me)/buy-new-subscription/page.tsx",
          "line": 43
        }
      ]
    },
    {
      "id": "quote-list",
      "lane": "quote-entry",
      "col": 1,
      "type": "security",
      "label": "Load quoted plan",
      "sublabel": "JWT + current stored token · Reject ACCEPTED / EXPIRED",
      "sources": [
        {
          "path": "siem-server-new/src/services/plan.service.ts",
          "line": 516
        }
      ]
    },
    {
      "id": "quote-checkout",
      "lane": "quote-entry",
      "col": 2,
      "type": "backend",
      "label": "Verify quote on server",
      "sublabel": "JWT, current link, owner email · SENT quote + selected plan",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 572
        }
      ]
    },
    {
      "id": "quote-accept",
      "lane": "quote-entry",
      "col": 3,
      "type": "database",
      "label": "Accept after settlement",
      "sublabel": "First successful payment · Includes internal ₹0 settlement",
      "sources": [
        {
          "path": "siem-server-new/src/payment/processor/PaymentEventProcessor.ts",
          "line": 439
        }
      ]
    },
    {
      "id": "select-base",
      "lane": "selection",
      "col": 0,
      "type": "backend",
      "label": "Same cycle base",
      "sublabel": "₹1,000 illustrative cycle · All candidates use this base",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 593
        }
      ]
    },
    {
      "id": "select-coupons",
      "lane": "selection",
      "col": 1,
      "type": "backend",
      "label": "Compare both coupons",
      "sublabel": "Quote ₹250 vs typed ₹200 · Quote wins equal coupon ties",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 603
        }
      ]
    },
    {
      "id": "select-promo",
      "lane": "selection",
      "col": 2,
      "type": "backend",
      "label": "Compare best promotion",
      "sublabel": "Promotion ₹300 wins here · Coupon wins equal promo ties",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 633
        }
      ]
    },
    {
      "id": "select-final",
      "lane": "selection",
      "col": 3,
      "type": "database",
      "label": "Subtract winner once",
      "sublabel": "₹1,000 − ₹300 = ₹700 · Store winning ID + duration",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 669
        }
      ]
    },
    {
      "id": "cycle-store",
      "lane": "cycles",
      "col": 0,
      "type": "database",
      "label": "Copy selected discount",
      "sublabel": "Amount + cyclesTotal + ID · Initial cyclesUsed = 0",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 671
        }
      ]
    },
    {
      "id": "cycle-charge",
      "lane": "cycles",
      "col": 1,
      "type": "backend",
      "label": "Resolve amount owed",
      "sublabel": "Pending price or locked price · Subtract while used < total",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 98
        }
      ]
    },
    {
      "id": "cycle-success",
      "lane": "cycles",
      "col": 2,
      "type": "backend",
      "label": "Successful settlement",
      "sublabel": "Including ₹0 internal settlement · Increment used by one",
      "sources": [
        {
          "path": "siem-server-new/src/payment/processor/PaymentEventProcessor.ts",
          "line": 423
        }
      ]
    },
    {
      "id": "cycle-end",
      "lane": "cycles",
      "col": 3,
      "type": "backend",
      "label": "Allowance exhausted",
      "sublabel": "Finite: restore full cycle charge · null: discount continues",
      "sources": [
        {
          "path": "siem-server-new/src/services/subscription.service.ts",
          "line": 106
        }
      ]
    },
    {
      "id": "redeem-start",
      "lane": "redemption-race",
      "col": 0,
      "type": "backend",
      "label": "Lock coupon row",
      "sublabel": "Check dates and remaining slots · No gateway call inside lock",
      "sources": [
        {
          "path": "siem-server-new/src/services/couponBooking.service.ts",
          "line": 16
        }
      ]
    },
    {
      "id": "redeem-count",
      "lane": "redemption-race",
      "col": 1,
      "type": "database",
      "label": "Reserve one slot",
      "sublabel": "UNPAID + BOOKED together · Successful count stays unchanged",
      "sources": [
        {
          "path": "siem-server-new/src/services/couponBooking.service.ts",
          "line": 16
        }
      ]
    },
    {
      "id": "redeem-money",
      "lane": "redemption-race",
      "col": 2,
      "type": "external",
      "label": "Pay or close checkout",
      "sublabel": "30-minute server deadline · Keep unresolved payments booked",
      "sources": [
        {
          "path": "siem-server-new/src/services/couponBooking.service.ts",
          "line": 150
        }
      ]
    },
    {
      "id": "redeem-cleanup",
      "lane": "redemption-race",
      "col": 3,
      "type": "security",
      "label": "Redeem or release",
      "sublabel": "Success: redeem once · Closed and unpaid: release slot",
      "sources": [
        {
          "path": "siem-server-new/src/services/couponBooking.service.ts",
          "line": 193
        }
      ]
    },
    {
      "id": "settle-read",
      "lane": "settlement-race",
      "col": 0,
      "type": "backend",
      "label": "Lock and reload",
      "sublabel": "Coupon then subscription · Read current cycle + invoice",
      "sources": [
        {
          "path": "siem-server-new/src/payment/processor/PaymentEventProcessor.ts",
          "line": 360
        }
      ]
    },
    {
      "id": "settle-a",
      "lane": "settlement-race",
      "col": 1,
      "type": "database",
      "label": "Apply settlement once",
      "sublabel": "Invoice, payment, redemption · Count actual discount once",
      "sources": [
        {
          "path": "siem-server-new/src/payment/processor/PaymentEventProcessor.ts",
          "line": 360
        }
      ]
    },
    {
      "id": "settle-b",
      "lane": "settlement-race",
      "col": 2,
      "type": "database",
      "label": "Recheck next delivery",
      "sublabel": "Same payment: no second write · Settled cycle: no extra use",
      "sources": [
        {
          "path": "siem-server-new/src/payment/processor/PaymentEventProcessor.ts",
          "line": 360
        }
      ]
    },
    {
      "id": "settle-result",
      "lane": "settlement-race",
      "col": 3,
      "type": "security",
      "label": "Recover follow-up jobs",
      "sublabel": "Provisioning, emails, next job · Retry without recounting discount",
      "sources": [
        {
          "path": "siem-server-new/src/payment/processor/PaymentEventProcessor.ts",
          "line": 494
        }
      ]
    }
  ],
  "edges": [
    {
      "id": "rates-e0",
      "from": "rate-form",
      "to": "rate-store",
      "role": "main"
    },
    {
      "id": "rates-e1",
      "from": "rate-store",
      "to": "cycle-base",
      "role": "main"
    },
    {
      "id": "rates-e2",
      "from": "cycle-base",
      "to": "amount",
      "role": "main"
    },
    {
      "id": "coupons-e0",
      "from": "coupon-admin",
      "to": "coupon-preview",
      "role": "main"
    },
    {
      "id": "coupons-e1",
      "from": "coupon-preview",
      "to": "coupon-check",
      "role": "main"
    },
    {
      "id": "coupons-e2",
      "from": "coupon-check",
      "to": "coupon-redeem",
      "role": "main"
    },
    {
      "id": "promotions-e0",
      "from": "promo-admin",
      "to": "promo-filter",
      "role": "main"
    },
    {
      "id": "promotions-e1",
      "from": "promo-filter",
      "to": "promo-best",
      "role": "main"
    },
    {
      "id": "promotions-e2",
      "from": "promo-best",
      "to": "promo-store",
      "role": "main"
    },
    {
      "id": "quote-create-e0",
      "from": "quote-compose",
      "to": "quote-jwt",
      "role": "main"
    },
    {
      "id": "quote-create-e1",
      "from": "quote-jwt",
      "to": "quote-coupon",
      "role": "main"
    },
    {
      "id": "quote-create-e2",
      "from": "quote-coupon",
      "to": "quote-email",
      "role": "main"
    },
    {
      "id": "quote-entry-e0",
      "from": "quote-open",
      "to": "quote-list",
      "role": "main"
    },
    {
      "id": "quote-entry-e1",
      "from": "quote-list",
      "to": "quote-checkout",
      "role": "main"
    },
    {
      "id": "quote-entry-e2",
      "from": "quote-checkout",
      "to": "quote-accept",
      "role": "main"
    },
    {
      "id": "selection-e0",
      "from": "select-base",
      "to": "select-coupons",
      "role": "main"
    },
    {
      "id": "selection-e1",
      "from": "select-coupons",
      "to": "select-promo",
      "role": "main"
    },
    {
      "id": "selection-e2",
      "from": "select-promo",
      "to": "select-final",
      "role": "main"
    },
    {
      "id": "cycles-e0",
      "from": "cycle-store",
      "to": "cycle-charge",
      "role": "main"
    },
    {
      "id": "cycles-e1",
      "from": "cycle-charge",
      "to": "cycle-success",
      "role": "main"
    },
    {
      "id": "cycles-e2",
      "from": "cycle-success",
      "to": "cycle-end",
      "role": "main"
    },
    {
      "id": "redemption-race-e0",
      "from": "redeem-start",
      "to": "redeem-count",
      "role": "main"
    },
    {
      "id": "redemption-race-e1",
      "from": "redeem-count",
      "to": "redeem-money",
      "role": "main"
    },
    {
      "id": "redemption-race-e2",
      "from": "redeem-money",
      "to": "redeem-cleanup",
      "role": "main"
    },
    {
      "id": "settlement-race-e0",
      "from": "settle-read",
      "to": "settle-a",
      "role": "main"
    },
    {
      "id": "settlement-race-e1",
      "from": "settle-a",
      "to": "settle-b",
      "role": "main"
    },
    {
      "id": "settlement-race-e2",
      "from": "settle-b",
      "to": "settle-result",
      "role": "main"
    }
  ]
}
