AladiaDocs

Sales modes and external enrollments

How a course is bought (Checkout, Request, External link, Invitation), price shown/hidden/on request, workspace default, checkout gates, enrollments API after an external sale, external payment on the course member, SSO, webhooks and migration.

Status: branch release/course-sales-mode (07/10/2026). Owner decision of 07/10 ("How this course is bought"). Guides: How you sell a course, Enrollments API. Builds on Requests, Guest checkout and Customer SSO. Paths relative to backend/ and web-next/.

1. Rules (one place)

libs/common/src/common/leads/course-sales.rules.ts, twins in web-next/lib/course-sales.ts (node --test):

FunctionLineWhat it decides
isSafeExternalUrl / isContactUrl63external link only https:// with a dotted host, no credentials, spaces or control characters, max 2048 (no javascript:, data:, http:, //host, relative); contact = that or mailto: with a valid email
selectedSalesMode96course choice → workspace default (checkout/request/invite; external is never a default) → checkout
offerModeOf133invite → invite; external with a safe link → external, else request; request → request (free and with Stripe too); checkout → free if free, checkout with a verified connect account and productId, else request (the usual fallback)
guestCheckoutAllowed151offerModeOf === 'checkout' and a Standard seller
onlineCheckoutAllowed160account checkout and "Enroll for free" only in checkout mode; the purchase link (invitation) always passes
salesErrors / cleanSales169save validation: externalUrl required and safe only in external, contactUrl safe in invite or with no mode; elsewhere an invalid link is dropped
enrollmentCycleOf258edition of an enrollment: on demand none; live the requested one if it belongs to the course, the only one if there is one; with several editions the API requires cycleId (strict), Requests take the next one not ended

lead.rules.ts re-exports offerModeOf/guestCheckoutAllowed for existing callers.

2. Data model

WhereFieldNotes
courses.billing.sales (and revisions){ mode?, price?, externalUrl?, externalLabel?, externalParams?, contactUrl?, contactLabel? }libs/common/src/common/models/microservices/course/course-sales.model.ts:18, inside BillingDocument (billing-model.ts:49): it travels wherever the price travels (Billing entity, BillingRto, cards, showcase). Missing = workspace default and price shown: no backfill
lead_settings.salesModecheckout | request | invitemodels/microservices/profile/lead-settings.model.ts:92; in LeadPublicSettingsRto (the offer uses it, the site does not receive it)
course_members.enrollment{ source: 'external-payment' | 'sso', via: 'api' | 'lead' | 'sso', externalId?, leadId?, at }models/microservices/course/course-member.model.ts:86. purchased stays false: no "buyers cannot be removed" lock, no COURSE_PURCHASED, no transactions/subscriptions; exposed as MemberGatewayRto.enrollmentSource for the members badge

3. Saving from the builder

PATCH /courses/:id with billing.sales (DTO CourseSalesGatewayDto, shape). In the course service (apps/course/src/app/courses/service/courses-upsert.service.ts): validSales (line 370) → salesErrors (400 course-sales-invalid with fields) and cleanSales; the change goes into the version like the rest of billing and is written right away on the online course ($set billing.sales, line 326), like settings.autoRecord: the storefront changes without republishing or a new review.

4. Public offer and gates

  • GET /public/courses/:id/offer (apps/gateway/.../profile/leads/leads.service.ts:114): reads billing.sales from the online course (getCourseById) falling back to the published version; answers mode, guestCheckout, price (show|hide|on-request), external { url, label, params } (only in external), invite { contactUrl, contactLabel } (safe contacts only). The workspace default is not exposed.
  • course-sales.gateway.ts: salesContextOf (30) = online course choices + default; assertSoldOnline (71) = 409 course-not-sold-online outside checkout mode.
  • Gates: account checkout CheckoutSessionsService (payment/checkout-sessions/checkout-sessions.service.ts:64, purchase links pass), "Enroll for free" POST /courses/:id/start (course/courses/courses.controller.ts:770), guest checkout sellableCourse (payment/guest-checkout/guest-checkout.service.ts:91, 409 direct-charge-not-available).

5. Course page, cards, SEO (web-next)

  • components/courses/player/purchase-box.tsx: price from offer.price (then billing.sales): hide = no price row, on-request = "Price on request"; no instalments or coupon outside show. InviteOnly (293): no button, contact (mailto or https). The external link button: icon and offerCtaLabel (lib/lead-form.ts) with the academy label.
  • app/(course)/courses/[courseId]/page.tsx:418 onBuy: external → buildExternalUrl (lib/course-sales.ts:99: chosen parameters, page UTMs, email only if chosen and signed in; an unsafe link never becomes a URL) and window.location.assign (same tab); invite → nothing.
  • CourseStructuredData (page.tsx:1096) + courseJsonLd (lib/course-sales.ts:119): schema.org Course, offers with the price only with show.
  • Cards and showcase: coursePriceLabel (lib/course-price.ts:19) and CouponBadge (components/courses/course-card-pills.tsx); social cards (feed-modules, social-rail).

6. Builder and settings

  • components/courses/builder/sales-section.tsx:59 SalesSection, under BillingSection in the Pricing panel: five choices (workspace default + four), yellow fallback notice (checkoutFallsBack: connect not verified or productId missing; reads GET /payment/connect-accounts and /leads/settings, silent without permission), external link fields (URL, label, parameters; privacy note on email), invitation contact, Show/Hide/On request. billing-section.tsx keeps sales when switching Free/Paid.
  • components/leads/sales-default-section.tsx:19: Settings › Requests and sales › "How courses are bought", saved on click (PUT /leads/settings { salesMode }, validated in apps/profile/src/app/leads/leads.service.ts:397).

7. Enrollment after an external sale

apps/gateway/src/gateway/controllers/integrations/enrollments/:

  • POST /integrations/enrollments (course-enrollments.controller.ts:27) and POST /leads/:id/enroll: ScopeAbilityGuard(UPDATE, LEADS). With an API key (apikey) the user is User.fromApiKey: workspace = the key's organization, privileges of its creator. 201 when something changes, 200 when existing.
  • CourseEnrollmentsGatewayService.enroll (course-enrollments.service.ts:103):
    1. valid email and externalId; course from getCourseById in the workspace (else 404) and online (409); edition via enrollmentCycleOf;
    2. Murlock enroll-<course>-<email> (30 s);
    3. account (191): with SSO Product only no new accounts (getProfileByEmail, else 403 workspace-sso-required with loginUrl); otherwise AccountProvisioningService.provision (single entry point, source: integration|lead; blocked account = 409);
    4. workspace Student via WorkspaceSsoGatewayService.ensureMembership (staff stays staff → internal member);
    5. course-members.enroll-external → CourseMembersUpsertService.enrollExternal (apps/course/.../members/services/course-members-upsert.service.ts:1434): new member with enrollment (through createMany, events and completions like direct adds), existing for same person/course/edition, cycle-added with $addToSet of the cycle and completion, pending invitation → enrollment. Never purchased, never COURSE_PURCHASED;
    6. if changed: enrollment.created webhook (resource EntityType.ENROLLMENT, owner = workspace) and the GUEST_PURCHASE_CONFIRMED email with origin: 'external' ("your enrollment is confirmed", set password or sign in; links on the workspace host from workspaceHosts), skipped with notify: false.
  • enrollLead (156): leads.retrieve (new ACTION.RETRIEVE in profile), same enrollment with via: lead and leadId, then leads.update → enrolled + profileId (lead.updated webhook).

SSO: enrollCourses marks enrollments from courses with source: 'sso' (workspace-sso-gateway.service.ts:628, students only). An "external payment" claim was considered and dropped: no enrollment without an Aladia purchase creates revenue; the useful distinction is who manages it (the product with replace_courses, staff for the API).

Reports: Aladia revenue only comes from payment-service transactions (checkout and instalments); these enrollments create none. numberOf.externalAttendants counts them like hand-added students. Seats (purchases, "seats left") are not consumed: with external sales the CRM governs seats.

8. Migration

scripts/migrations/2026-10-course-sales-mode.js: inserts the enrollment.created event in webhook_events (without it the webhook does not fire) and counts courses/workspaces/enrollments already touched. PHASE from process.env.PHASE or the global PHASE from mongosh.sh (const MODE, never const PHASE).

9. Tests

  • tests/unit/course-sales.spec.ts: Stripe yes/no × mode × free/paid matrix, workspace default, fallback without product, safe links (open redirect, javascript:, credentials, CRLF), public offer, checkout gate, API (leads.update guard, idempotency, key workspace, SSO product only, notify, no payment calls, no course-purchased), enrollExternal (purchased false, existing, cycle-added, other workspace).
  • web-next/lib/course-sales.test.mjs: links, parameters, notice, JSON-LD without price.
  • e2e (after release): web-next/e2e/help/flows/leads/sales-modes.spec.mjs.

On this page