Skip to main content

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.

Words used on this page​

WordMeaning
Root companyAn Org::Company with parent_company_id = NULL
Child companyAn Org::Company with a parent_company_id, for example a FairPrice division
Own accountThe Billing::Account whose billing_accounts.org_company_id is this company
DeliverableThe record that spends credits, for example an Ads::CampaignPlacement, and later a Careers::Job
The service ResolveServiceBilling::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
CompanyAccount it spends today
Root companyIts own account
Child companyIts 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::CreateService creates an own account for every company made in Jod, children included.
  • JodGig::Companies::BillingAccountUpsertAllService skips 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​

RuleApplies toExample
Every reserve uses the own account of the deliverable's companyUS-2, US-5North's campaign reserves on North's account
A child company never spends its parent's accountUS-2North's submit never reads FairPrice's account
The submit credit check reads the same account as the reserveUS-2, US-5North's validator checks North's balance
Consume and release use the account on the reserve ledger entryalwaysA campaign that reserved on FairPrice's account keeps consuming from FairPrice's account
A company owns exactly one accountalwaysNorth owns acct_N and no other account
An account belongs to exactly one companyalwaysacct_N belongs to North only
Every company has an account before it spends creditsUS-4A child synced from JodGig gets its account in the same sync
A company with no account cannot spend credits, and the error names the companyUS-5"The company 'FairPrice North' has no billing account yet"
The employer balance shows the company's own accountUS-3North's HQ manager sees North's balance
An invoice or a credit action for a division goes to the division's own accountUS-1Finance posts North's invoice to acct_N
Credits never move between accounts in this phasealwaysFairPrice'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​

CompanyOwns accountSpends
NTUC FairPrice (root)acct_FPacct_FP
FairPrice Supermarkets (North)acct_Nacct_N
FairPrice Supermarkets (East)acct_Eacct_E
FairPrice Finestacct_FINacct_FIN

A client that wants one shared pool of credits is one company.

  • Its stores are Org::Outlet records of that one company.
  • It does not use child companies.

Database​

No column changes.

  • billing_accounts.org_company_id already says which company owns an account.
  • billing_accounts.org_company_id gets 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​

PathFileChange
Account choiceapp/domains/billing/accounts/shared/resolve_service.rbReturns org_company.billing_account, and raises the 422 when it is missing
JodGig company syncapp/domains/jod_gig/companies/billing_account_upsert_all_service.rbCreates an account for child companies too
BackfillNew rake taskCreates the missing account for each existing company, with a dry run
Submit validatorsapp/domains/ads/campaigns/team_submit_validator.rb, employers_submit_validator.rbCode comment only, the logic already calls ResolveService
Employer balance readapp/domains/billing/entitlement_balances/employers_index_manager.rbCode comment only
Team Portal campaign showapp/domains/ads/campaigns/team_show_manager.rb, app/domains/ads/team_campaign_detail_serializer.rbCode comment only
Company create in Jodapp/domains/org/companies/shared/create_service.rbNone, it already creates an account for every company
Reserve, consume, release, expireapp/domains/billing/entitlement_lot_service.rbNone

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_id would 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::Membership on each division.
  • Outlet budgets.
    • With one account per company, an outlet budget lives inside the account of the outlet's company.

Rollout​

Existing records​

CompanyOwns an account todayWhat the rollout does
Root companyYesNothing
Child company created in JodYes, created by Org::Companies::Shared::CreateServiceNothing
Child company synced from JodGigNoThe backfill creates its account
Root company created before jodapp-api#1993 with no accountNoThe backfill creates its account

Reserve ledger entries that a child's campaign wrote on its parent's account stay correct.

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=1 writes only missing accounts, and prints the same report again.

After release​

The report after the live run lists no company without an account.

  • Then ResolveService can 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.

  1. NTUC invoice name. Should North's invoice show the legal entity name or the division name?
    • Either works, because it is the Billing::BillToProfile on North's own account.
  2. 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.
  3. 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.
  4. Parent HQ visibility. Is an Org::Membership on each division enough for group HQ to see each balance?
    • This phase builds no combined view.
  5. 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.
  6. Gig credit expiry. Will gig credits always stay non-expiring?