Design Plan: every company spends only its own credits
Goal
Every Org::Company, a child company included, spends only the credits on its own Billing::Account.
- The decision is Billing D16 — A company spends only its own billing account, never its parent's.
- The user stories are on the Overview.
- This page shows the rules, the code that changes and the rollout.
Words used on this page
| Word | Meaning |
|---|---|
| Root company | An Org::Company with parent_company_id = NULL |
| Child company | An Org::Company with a parent_company_id, for example a FairPrice division |
| Own account | The Billing::Account whose billing_accounts.org_company_id is this company |
| Deliverable | The record that spends credits, for example an Ads::CampaignPlacement, and later a Careers::Job |
The service ResolveService | Billing::Accounts::Shared::ResolveService, the one place that decides which account a company spends |
How it works today
ResolveService sends a child company to its parent's account.
# jodapp-api app/domains/billing/accounts/shared/resolve_service.rb:33-35 (feature/backend-billing-phase-3)
is_child_company = org_company.parent_company_id.present?
paying_org_company = is_child_company ? T.must(org_company.parent_company) : org_company
paying_billing_account = paying_org_company.billing_account
| Company | Account it spends today |
|---|---|
| Root company | Its own account |
| Child company | Its parent's own account, even when the child owns an account |
Four paths call ResolveService:
- Ads submit, in the team and employer submit managers.
- The two submit validators, which check that the company has enough credits.
- The employer balance read,
Billing::EntitlementBalances::EmployersIndexManager. - The Team Portal campaign show,
Ads::Campaigns::TeamShowManager.
Consume and release do not call ResolveService.
- They read the account from the reserve ledger entry of the same deliverable.
Two paths create accounts today, and only one of them covers child companies.
Org::Companies::Shared::CreateServicecreates an own account for every company made in Jod, children included.JodGig::Companies::BillingAccountUpsertAllServiceskips child companies.
# jodapp-api app/domains/jod_gig/companies/billing_account_upsert_all_service.rb:18-19 (feature/backend-billing-phase-3)
billing_account_upserts = org_companies.filter_map do |org_company|
next if org_company.parent_company_id.present?
So a child company synced from JodGig owns no account today.
Rules
| Rule | Applies to | Example |
|---|---|---|
| Every reserve uses the own account of the deliverable's company | US-2, US-5 | North's campaign reserves on North's account |
| A child company never spends its parent's account | US-2 | North's submit never reads FairPrice's account |
| The submit credit check reads the same account as the reserve | US-2, US-5 | North's validator checks North's balance |
| Consume and release use the account on the reserve ledger entry | always | A campaign that reserved on FairPrice's account keeps consuming from FairPrice's account |
| A company owns exactly one account | always | North owns acct_N and no other account |
| An account belongs to exactly one company | always | acct_N belongs to North only |
| Every company has an account before it spends credits | US-4 | A child synced from JodGig gets its account in the same sync |
| A company with no account cannot spend credits, and the error names the company | US-5 | "The company 'FairPrice North' has no billing account yet" |
| The employer balance shows the company's own account | US-3 | North's HQ manager sees North's balance |
| An invoice or a credit action for a division goes to the division's own account | US-1 | Finance posts North's invoice to acct_N |
| Credits never move between accounts in this phase | always | FairPrice's leftover credits stay on FairPrice's account |
Direction
ResolveService returns the company's own account, and every company gets one.
- A child company spends its own credits.
- The employer balance and the Team Portal campaign page show the company's own account.
- No column and no table change.
Who spends what: FairPrice example
| Company | Owns account | Spends |
|---|---|---|
| NTUC FairPrice (root) | acct_FP | acct_FP |
| FairPrice Supermarkets (North) | acct_N | acct_N |
| FairPrice Supermarkets (East) | acct_E | acct_E |
| FairPrice Finest | acct_FIN | acct_FIN |
A client that wants one shared pool of credits is one company.
- Its stores are
Org::Outletrecords of that one company. - It does not use child companies.
Database
No column changes.
billing_accounts.org_company_idalready says which company owns an account.billing_accounts.org_company_idgets a unique index in jodapp-api#2132.- The index makes "an account belongs to exactly one company" a database rule.
- Schema truth stays
jodapp-api/docs/db/billing.dbml.
Backend
| Path | File | Change |
|---|---|---|
| Account choice | app/domains/billing/accounts/shared/resolve_service.rb | Returns org_company.billing_account, and raises the 422 when it is missing |
| JodGig company sync | app/domains/jod_gig/companies/billing_account_upsert_all_service.rb | Creates an account for child companies too |
| Backfill | New rake task | Creates the missing account for each existing company, with a dry run |
| Submit validators | app/domains/ads/campaigns/team_submit_validator.rb, employers_submit_validator.rb | Code comment only, the logic already calls ResolveService |
| Employer balance read | app/domains/billing/entitlement_balances/employers_index_manager.rb | Code comment only |
| Team Portal campaign show | app/domains/ads/campaigns/team_show_manager.rb, app/domains/ads/team_campaign_detail_serializer.rb | Code comment only |
| Company create in Jod | app/domains/org/companies/shared/create_service.rb | None, it already creates an account for every company |
| Reserve, consume, release, expire | app/domains/billing/entitlement_lot_service.rb | None |
The 422 from ResolveService names only the company.
- A missing account has one cause after this phase: the company has no account yet.
Team Portal
The Ads campaign detail page shows the account that pays, as today.
- The paying account is always the company's own account.
- So the note "uses the credits of its parent company" no longer appears.
Employer Dashboard
The navbar balance shows the company's own account.
- No screen change is needed.
Rejected options
- A pointer
org_companies.billing_account_idwould let a child spend its own or its parent's account.- It lost, because it adds a column, an admin switch and a lock for a choice clients do not need.
- A budget per child inside the parent's account would work like
Billing::OutletBudget.- It lost, because placement lots expire and a budget cannot tell whose credits expired.
Out of scope
- Moving credits between two accounts.
- A later phase plans a transfer with a ledger entry on both accounts.
Org::LegalEntity, a model that groups companies under one registered legal entity.- It groups companies only, and every company keeps its own account.
- A parent HQ manager's combined view of every division's balance.
- A person who manages many divisions has an
Org::Membershipon each division.
- A person who manages many divisions has an
- Outlet budgets.
- With one account per company, an outlet budget lives inside the account of the outlet's company.
Rollout
Existing records
| Company | Owns an account today | What the rollout does |
|---|---|---|
| Root company | Yes | Nothing |
| Child company created in Jod | Yes, created by Org::Companies::Shared::CreateService | Nothing |
| Child company synced from JodGig | No | The backfill creates its account |
| Root company created before jodapp-api#1993 with no account | No | The backfill creates its account |
Reserve ledger entries that a child's campaign wrote on its parent's account stay correct.
- Consume and release follow the reserve ledger entry, so those campaigns keep using the parent's account until they end.
- Phase 1-3 billing data is reset before launch, so production should hold none (Billing D13 — No Phase-3 grant backfill: Phase 1-3 billing data is reset).
Dry run
The backfill runs as a dry run first, and a person approves its report before any write.
- The report counts companies by row of the table above.
- The report lists every company that owns no account, with id, name and parent id.
- The report lists every open reserve whose account owner is not the deliverable's company.
- The live run with
APPLY=1writes only missing accounts, and prints the same report again.
After release
The report after the live run lists no company without an account.
- Then
ResolveServicecan change, because every company has an account to return. - A child's submit then reserves on its own account.
Open questions
These do not block the design.
- NTUC invoice name. Should North's invoice show the legal entity name or the division name?
- Either works, because it is the
Billing::BillToProfileon North's own account.
- Either works, because it is the
- Expiry warnings for the parent. Should FairPrice group HQ also get the expiry warning emails for North's account?
- Today only the account owner's HQ managers get them.
- Trial credits for divisions. Does each child company get trial credits, or only the root?
- Every division now has its own account, so each one could look like a new buyer.
- Parent HQ visibility. Is an
Org::Membershipon each division enough for group HQ to see each balance?- This phase builds no combined view.
- Gig credits from JodGig. Do JodGig gig credits sit on the parent company today?
- If yes, moving gig credits into Billing must split them per division.
- Gig credit expiry. Will gig credits always stay non-expiring?
- Outlet budgets work only because gig lots have no
expires_at. - Gig credits are refundable stored value, so expiry needs a legal review first (Billing D3 — Credit expiry mechanics).
- Outlet budgets work only because gig lots have no