Coverage Summary for Class: ListPendingInvitesForCurrentUserUseCase (com.georgeci.moneysurfer.domain.usecase)

Class Method, % Branch, % Line, % Instruction, %
ListPendingInvitesForCurrentUserUseCase 100% (2/2) 100% (5/5) 100% (18/18)
ListPendingInvitesForCurrentUserUseCase$invoke$1 100% (1/1) 20% (2/10) 33.3% (3/9) 31.2% (43/138)
ListPendingInvitesForCurrentUserUseCase$invoke$1$1 0% (0/1) 0% (0/2) 0% (0/3) 0% (0/22)
Total 75% (3/4) 16.7% (2/12) 47.1% (8/17) 34.3% (61/178)


 package com.georgeci.moneysurfer.domain.usecase
 
 import com.georgeci.moneysurfer.domain.auth.SessionPointers
 import com.georgeci.moneysurfer.domain.model.InviteStatus
 import com.georgeci.moneysurfer.domain.model.WorkspaceInvite
 import com.georgeci.moneysurfer.domain.repositories.UserRepository
 import com.georgeci.moneysurfer.domain.repositories.WorkspaceInviteRepository
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.flow.combine
 import kotlinx.coroutines.flow.emitAll
 import kotlinx.coroutines.flow.first
 import kotlinx.coroutines.flow.flow
 import kotlinx.coroutines.flow.flowOf
 import org.koin.core.annotation.Single
 
 /**
  * Streams pending invitations addressed to the current user — by `email` (lowercase match)
  * AND by `targetUserId`. Expired-but-still-PENDING rows are surfaced as PENDING; UI tags
  * them as EXPIRED for display when `now > expiresAt`.
  *
  * Empty when no current user — caller usually doesn't render anything in that state.
  */
 @Single
 class ListPendingInvitesForCurrentUserUseCase(
     private val inviteRepository: WorkspaceInviteRepository,
     private val userRepository: UserRepository,
     private val session: SessionPointers,
 ) {
     operator fun invoke(): Flow<List<WorkspaceInvite>> = flow {
         val callerId = session.currentUserId.first()
         if (callerId == null) {
             emit(emptyList())
             return@flow
         }
         val caller = userRepository.getById(callerId)
         val email = caller?.email?.lowercase()
 
         val byEmail = if (email != null) inviteRepository.getByEmail(email) else flowOf(emptyList())
         val byUid = inviteRepository.getByTargetUserId(callerId)
 
         emitAll(
             byEmail.combine(byUid) { a, b ->
                 (a + b)
                     .distinctBy { it.id.value }
                     .filter { it.status == InviteStatus.PENDING }
             },
         )
     }
 }