medium priority low complexity documentation pending documentor Tier 8

Acceptance Criteria

Documentation covers all four badge backend services: PeerMentorStatsAggregator, RecognitionTierService, BadgeAwardService, and BadgeConfigurationService
Each service section includes: class-level purpose statement, all public method signatures with parameter names, types, and return types, and a one-paragraph description of behavior
Riverpod provider dependency graph is rendered as an ASCII diagram or Mermaid chart showing provider names and dependency arrows
Idempotency guarantees are explicitly stated per service method (e.g., 'calling awardBadge twice with the same arguments is safe — returns existing record')
Honorar threshold counting rules for Blindeforbundet are documented with exact numbers (3rd assignment = office honorar trigger, 15th = higher rate trigger) and the counting unit is defined
Tier eligibility formula is documented with input variables, formula, and an example calculation
Known edge cases section lists at least five scenarios with expected system behavior (e.g., peer mentor with 0 assignments, peer mentor crossing two thresholds in a single batch)
Documentation is written in English and is accessible to both backend and UI engineers without requiring access to production code
Document is committed to the repository under docs/ or an equivalent location agreed with the team

Technical Requirements

frameworks
Flutter
Riverpod
apis
Supabase REST API
data models
PeerMentorStats
RecognitionTier
EarnedBadge
BadgeDefinition
HonorarRecord
security requirements
Documentation must not contain real API keys, Supabase project URLs, or any production credentials
Examples using peer mentor data must use synthetic identifiers only

Execution Context

Execution Tier
Tier 8

Tier 8 - 48 tasks

Can start after Tier 7 completes

Implementation Notes

Use Dart doc-comment style (///) for inline documentation on service classes and methods so that dart doc can generate HTML output. Supplement with a standalone BADGE_SERVICES.md file for the provider dependency graph and the cross-cutting rules (idempotency, honorar thresholds, eligibility formula). For the Riverpod graph, list providers in dependency order top-to-bottom; use Mermaid graph TD syntax if the repo supports it in markdown. Document the honorar counting rules using a table with columns: Threshold Number, Honorar Type, Trigger Condition.

For the eligibility formula, use the format: tierScore = f(activityCount, honorarCount, periodDays) with variable definitions below. Cross-reference the integration test file so readers can verify the documented behavior against executable tests.

Testing Requirements

No automated tests for documentation itself. However, a peer review by one backend engineer and one UI engineer is required before the documentation is considered complete.

Reviewers must confirm that: (1) they can understand the provider graph without reading source code, (2) idempotency guarantees match the integration tests written in task-017, (3) the honorar threshold rules match the test fixture values used in task-017.

Component
Badge Configuration Service
service medium
Epic Risks (3)
high impact medium prob technical

peer-mentor-stats-aggregator must compute streaks and threshold counts across potentially hundreds of activity records per peer mentor. Naive queries (full table scans or N+1 patterns) will cause slow badge evaluation, especially when triggered on every activity save for all active peer mentors.

Mitigation & Contingency

Mitigation: Design aggregation queries using Supabase RPCs with window functions or materialised views from the start. Add database indexes on (peer_mentor_id, activity_date, activity_type) before writing any service code. Profile all aggregation queries against a dataset of 500+ activities during development.

Contingency: If query performance is insufficient at launch, implement incremental stat caching: maintain a peer_mentor_stats snapshot table updated on each activity insert via a database trigger, so the aggregator reads from pre-computed values rather than scanning raw activity rows.

medium impact low prob technical

badge-award-service must be idempotent, but if two concurrent edge function invocations evaluate the same peer mentor simultaneously (e.g., from a rapid double-save), both could pass the uniqueness check before either commits, resulting in duplicate badge records.

Mitigation & Contingency

Mitigation: Rely on the database-level uniqueness constraint (peer_mentor_id, badge_definition_id) as the final guard. In the service layer, use an upsert with ON CONFLICT DO NOTHING and return the existing record. Add a Postgres advisory lock or serialisable transaction for the award sequence during the edge function integration epic.

Contingency: If duplicate records are discovered in production, run a deduplication migration to remove extras (keeping earliest earned_at) and add a unique index if not already present. Alert engineering via Supabase database webhook on constraint violations.

medium impact medium prob scope

The badge-configuration-service must validate org admin-supplied criteria JSON on save, but the full range of valid criteria types (threshold, streak, training-completion, tier-based) may not be fully enumerated during development, leading to either over-permissive or over-restrictive validation that frustrates admins.

Mitigation & Contingency

Mitigation: Define a versioned Dart sealed class hierarchy for CriteriaType before writing the validation logic. Review the hierarchy with product against all known badge types across NHF, Blindeforbundet, and HLF before implementation. Build the validator against the sealed class so new criteria types require an explicit code addition.

Contingency: If admins encounter validation rejections for legitimate criteria, expose a 'criteria_raw' escape hatch (JSON passthrough, admin-only) with a product warning, and schedule a sprint to formalise the new criteria type properly.