Skip to main content

Community Fund

Source: src/data/community-fund/

Per-tenant informal community pool. Members make voluntary contributions at checkout; community leaders disburse to members in need.

Integration status: Standalone module. Order-flow checkout, Discord celebration messages, and payment capture are deferred. See docs/specs/2026-04-26-community-fund.md.


Service

createCommunityFundService / CommunityFundService

Factory: createCommunityFundService(deps: CommunityFundServiceDeps) — returns CommunityFundService.

CommunityFundServiceDeps:

  • contributions: CommunityFundContributionRepository
  • disbursements: CommunityFundDisbursementRepository
  • config: CommunityFundConfigRepository
MethodParametersReturnsDescription
recordContributionRecordContributionInputPromise<CommunityFundContribution>Creates a contribution. Throws CommunityFundDisabledError when fund is disabled.
recordDisbursementRecordDisbursementInputPromise<CommunityFundDisbursement>Creates a disbursement. Throws CommunityFundInsufficientBalanceError if balance low.
getBalancetenantId: stringPromise<FundBalance>Computes balance from aggregation; never reads a cached field.
getRecentContributionstenantId: string, limit?: numberPromise<CommunityFundContribution[]>Last N contributions, ordered by date desc. Default limit 20.
getRecentDisbursementstenantId: string, limit?: numberPromise<CommunityFundDisbursement[]>Last N disbursements, ordered by date desc. Default limit 20.
getStatstenantId: string, period: FundStatsPeriodPromise<FundStats>Contribution + disbursement counts and totals for a date range.
getOrCreateConfigtenantId: stringPromise<CommunityFundConfig>Returns existing config or creates with defaults. Idempotent.
updateConfigtenantId: string, patch: CommunityFundConfigPatchPromise<CommunityFundConfig>Partial update to tenant config. Creates with defaults if not yet present.
listContributionstenantId: string, { limit, offset }Promise<{ items, total }>Paginated contributions.
listDisbursementstenantId: string, { limit, offset }Promise<{ items, total }>Paginated disbursements.

Errors

All errors are defined in src/data/community-fund/community-fund.errors.ts.

Error classWhen thrown
CommunityFundDisabledErrorrecordContribution called when config.enabled === false
CommunityFundInsufficientBalanceErrorrecordDisbursement when balance < requested amount
CommunityFundConfigErrorInvalid or unrecoverable config state (rare)

Repositories

Three separate repository interfaces, each with a Mongoose implementation:

  • CommunityFundContributionRepository — create, findRecent, findPaginated, sumContributions, statsByPeriod
  • CommunityFundDisbursementRepository — create, findRecent, findPaginated, sumDisbursements, statsByPeriod
  • CommunityFundConfigRepository — findByTenant, upsert

Use makeCommunityFundRepositories({ ContributionModel, DisbursementModel, ConfigModel }) to build all three at once.


Deferred Integration Points

IntegrationTarget fileNotes
Order-flow checkoutsrc/data/order/order-flow.service.tsCall recordContribution after payment confirms
Celebration messageEvent bus → community-fund.events.ts (to create)Emit COMMUNITY_FUND_CONTRIBUTION_RECORDED
Bot-config guild togglesrc/data/bot-config/bot-config.types.tsAdd communityFundEnabled field
Payment capturesrc/data/payment/Add donation as separate line item in charge