The three readers of SourceSummaryService.read: the dialog, the cancel and the daily consume
This page lists the three readers of LedgerEntries::SourceSummaryService.read, and what each one takes from it.
- The objects, the rules and the raises are on Reserved units of one deliverable.
- The spends endpoint: Billing interface for product domains — A product page reads Billing data from a Billing endpoint.
Words used in this page
| Word | Meaning |
|---|---|
| Deliverable | The product record that one spend pays for, here one Ads::CampaignPlacement, called the source in the code. |
| Marketing | The one person at Jod who runs Ads campaigns for clients in the Team Portal. |
| The header | One LedgerEntries::SourceSummary: the totals of one deliverable, with the list of its lines. |
| A line | One EntitlementLotAllocations::ReservedBalance: the units one deliverable still has reserved in one EntitlementLot. |
| Expired lot | An EntitlementLot whose expires_at is at or earlier than the expired_as_of: of the read. |
| Live lot | An EntitlementLot with no expires_at, or with an expires_at later than the expired_as_of: of the read. |
| The balance row lock | lock('FOR UPDATE') on the billing_entitlement_balances row, which consume and release take while they write. |
Every use case below follows Acme's placement 901 through five moments.
- 901 costs 30 units and runs 30 days from 20 Nov 2026.
- So the daily consume job takes 1 unit a day.
- The inputs and each step are in Reserved units of one deliverable — Worked example: placement 901 on 2 December.
EntitlementLot10 expires on 30 Nov 2026, and 901 reserved 20 units from it.EntitlementLot11 expires on 31 Dec 2026, and 901 reserved 10 units from it.
| Moment | Who acts | What is read | The answer |
|---|---|---|---|
| 20 Nov to 29 Nov, the daily consume | The daily consume job | The lines of 901 | Lot 10 expires first, so the job takes 1 unit a day from lot 10, 10 units in all |
| 30 Nov, the lot expiry | Billing::ExpireEntitlementLotsJob | Nothing of 901 | The job expires only the units that no deliverable reserved, and lot 10 has none, so 901 still has 10 units reserved in it |
| 30 Nov to 2 Dec, the daily consume | The daily consume job | The lines of 901 | Lot 10 still sorts first after it expires, so the job takes 3 more units from it |
| 2 Dec, the dialog | Marketing, through GET /team/billing/spends | The header of 901 | 17 units reserved, 13 consumed, and 7 of the 17 in an expired lot |
| 2 Dec, the confirm | EntitlementLotService.release | The lines of 901 | 10 units go back to lot 11, and 7 units in lot 10 expire |
Summary
Three readers call the same SourceSummaryService.read, and each one takes a different part of the header.
- US-1: As Marketing, I want the cancel dialog to show the units that will expire, so I decide first.
- US-2: As Marketing, I want a cancel to give back units in live lots, so the company spends them again.
- US-3: As the daily consume job, I want each day's units from the soonest-expiring lot, so no credit expires unused.
| ID | Use case | Actor | Story |
|---|---|---|---|
| UC-1 | Marketing sees the units in expired lots in the cancel dialog | Marketing (Identities::Admin) | US-1 |
| UC-2 | A confirmed cancel gives back the units in live lots, and expires the rest | Marketing, then EntitlementLotService.release | US-2 |
| UC-3 | The daily consume takes units from the soonest-expiring lot first | Ads::CampaignPlacements::ConsumeReservedCreditsJob | US-3 |
Legend: The orange node is the one builder, and each lavender node is a reader.
UC-1: Marketing sees the units in expired lots in the cancel dialog
| Field | Details |
|---|---|
| Actor | Marketing (Identities::Admin) |
| Trigger | Marketing opens the cancel dialog of the campaign of placement 901 on 2 Dec |
System behaviour:
- The campaign page calls
GET /team/billing/spends?source_type=Ads::CampaignPlacement&source_ids[]=901. Billing::Spends::TeamIndexManageropens a:repeatable_readtransaction.- It calls
SourceSummaryService.readwithsource_ids: [901]andexpired_as_of: Time.current. - The serializer writes five fields of the header, and no line.
- The dialog shows the row of 901: 10 units go back, 7 expire and 13 are already used.
- The web works out the units that go back:
units_reservedminusunits_reserved_in_expired_lots, 17 − 7 = 10. - The dialog warns that the cancel will expire 7 reserved units of the campaign.
- The web works out the units that go back:
{ "billing_spends": [{ "source_type": "Ads::CampaignPlacement", "source_id": 901, "units_reserved": 17, "units_consumed": 13, "units_reserved_in_expired_lots": 7 }] }
Business rules:
- The
:repeatable_readtransaction runs all three queries on the same committed data.- So a consume that commits between two queries cannot make the numbers disagree.
- The lines stay in Ruby, because no user story asks for a table per lot.
- A later page that wants one changes only the serializer.
- Marketing rejects a campaign in review, or cancels a campaign that runs.
- Billing D15 — An admin can cancel a running ad campaign allows the cancel of a campaign that runs.
- Each dialog shows one row for each placement of the campaign.
- The cancel dialog shows the units that go back, the units that expire and the units already used.
- The reject dialog shows only the first two, because a campaign in review has used no units.
- A failed read keeps the confirm button of the dialog disabled.
UC-2: A confirmed cancel gives back the units in live lots, and expires the rest
| Field | Details |
|---|---|
| Actor | Marketing confirms, and EntitlementLotService.release acts |
| Trigger | Marketing confirms the cancel of the campaign of placement 901 on 2 Dec |
System behaviour (one transaction, under the balance row lock):
releasecallsSourceSummaryService.readwithsource_ids: [901]andexpired_as_of: Time.current, and takes the first header.releasegives the units of every line back first, expired or not.- Then
releasepicks the lines withlot_expired?(as_of: expired_as_of)of the header, and expires their units.
| Step | Lines on 2 Dec | What release writes |
|---|---|---|
| 2, every lot | Lot 10 → 7 Lot 11 → 10 | One release entry with available_delta: 17 and reserved_delta: -17, and one release allocation for each lot |
| 3, the expired lots | Lot 10 → 7 | One expire entry with available_delta: -7 for lot 10, with the EntitlementLot as its source |
After both writes, lot 11 has 10 more units available, and the 7 units of lot 10 are expired.
Business rules:
- Released credits in an expired lot expire at once.
- Marketing can cancel the campaign although placement 901 runs.
releasealready runs under the balance row lock, so it needs no:repeatable_readtransaction of its own.releasedoes nothing when the read returns no header, or a header withunits_reservedat 0.- A total below 0 never reaches
release, because the read raises first. - The lines are all 1 or more, so they cannot add up to a total below 0.
- The first cancel wrote
reserved_delta: -17, so a second cancel findsunits_reserved: 0. releasethen returns before it reads any line.
- A total below 0 never reaches
UC-3: The daily consume takes units from the soonest-expiring lot first
| Field | Details |
|---|---|
| Actor | Ads::CampaignPlacements::ConsumeReservedCreditsJob, through EntitlementLotService.consume |
| Trigger | The job runs for placement 901 on 29 Nov at 00:20, with target_units_consumed: 10 |
System behaviour (one transaction, under the balance row lock):
consumecallsSourceSummaryService.readwithsource_ids: [901]andexpired_as_of: Time.current, and takes the first header.- 901 has consumed 9 units so far, so
consumemust take 10 − 9 = 1 unit. - The lines of the header come in the spend order of Billing, so
consumesorts nothing.- The order is the soonest
expires_atof the lot, then the earliestpurchased_at, then the lowest lotid. - Billing D2 — All stored-value credits use entitlement lots; placement is not pooled decides it, for every spend.
- The order is the soonest
- The lines are lot 10 → 11, then lot 11 → 10.
consumetakes the 1 unit from lot 10, and writes oneconsumeledger entry.
Business rules:
- A lot with no
expires_atsorts last. - The job passes a total to reach, not the units of one day.
- A second run on 29 Nov finds 10 units consumed already, so it writes nothing.
consumeraises when the lines run out and some units it must take are still not covered.consumeraises when the read returns no header, because the placement has no reserve entry.