Service Layer high complexity backend
3
Dependencies
1
Dependents
7
Entities
0
Integrations

Description

Server-side Supabase Edge Function that evaluates badge criteria whenever an activity is saved. Checks all configured badge definitions for the organization against the peer mentor's cumulative activity data, including Blindeforbundet's 3rd and 15th assignment honorar thresholds.

Feature: Achievement Badges & Status Recognition

badge-evaluation-service

Summaries

The Badge Evaluation Service is the intelligence engine behind the entire recognition system. Every time a peer mentor logs an activity, this service automatically determines whether they have crossed any achievement thresholds — including Blindeforbundet's specific 3rd and 15th assignment honorar milestones — and triggers the appropriate awards. By automating this evaluation server-side, the organization eliminates manual tracking of recognition criteria, reduces administrative overhead, and ensures that no earned badge is ever missed. This reliability is critical to maintaining trust in the recognition system and the motivational value it provides.

This is the highest-complexity component in the badge system and sits on the critical path for the entire recognition feature. It depends on badge-repository, badge-definition-repository, and peer-mentor-stats-aggregator — all of which must be available and tested before end-to-end evaluation can be validated. As a Supabase Edge Function, it requires backend deployment infrastructure and appropriate database permissions. Testing must cover all supported criteria types: threshold-based (3rd, 15th assignment), streak-based, and training completion.

Performance testing is recommended since this runs on every activity save. Plan for staged rollout and monitor Edge Function execution times post-launch.

Implemented as a Supabase Edge Function in TypeScript/Deno. evaluateBadgesForPeerMentor() is the entry point, triggered via a Postgres webhook or direct invocation after activity insert. It fetches all BadgeDefinitions for the organisation, calls checkCriteria() for each against the aggregated PeerMentorStats, and passes newly satisfied definitions to triggerAwardForBadge(). checkCriteria() must handle multiple criterion types — use a strategy pattern or type-discriminated union.

detectNewlyEarned() diffs current satisfied criteria against already-earned badges from badge-repository to avoid re-triggering. Idempotency is enforced downstream by badge-award-service, but this service should still short-circuit on already-earned badges for performance. Stats are sourced from peer-mentor-stats-aggregator which should use materialized views or cached aggregates to keep latency acceptable.

Responsibilities

  • Evaluate all badge criteria for a peer mentor on activity save
  • Detect newly satisfied criteria and trigger badge award
  • Support threshold-based criteria (e.g., 3rd, 15th assignment)
  • Support streak-based and training completion criteria

Interfaces

evaluateBadgesForPeerMentor(String peerMentorId, String organisationId)
checkCriteria(BadgeDefinition badge, PeerMentorStats stats) -> bool
detectNewlyEarned(List<BadgeDefinition> definitions, PeerMentorStats stats) -> List<BadgeDefinition>
triggerAwardForBadge(String badgeDefinitionId, String peerMentorId)

Relationships

Dependencies (3)

Components this component depends on

Dependents (1)

Components that depend on this component

API Contract

View full contract →
REST /api/v1/badge-evaluations 3 endpoints
POST /api/v1/badge-evaluations
POST /api/v1/badge-evaluations/check-criteria
GET /api/v1/badge-evaluations/:peer_mentor_id