Class CitizenSpecificRepositoryImpl

java.lang.Object
it.gov.pagopa.onboarding.citizen.repository.CitizenSpecificRepositoryImpl
All Implemented Interfaces:
CitizenSpecificRepository

@Repository public class CitizenSpecificRepositoryImpl extends Object implements CitizenSpecificRepository

Implementation of custom MongoDB aggregation queries for CitizenConsent.

This repository handles complex queries that require dynamic field projection and nested document filtering within the consents map.

All methods use MongoDB Aggregation Framework to optimize query performance by projecting only required fields and filtering at database level.

See Also:
  • Constructor Details

    • CitizenSpecificRepositoryImpl

      public CitizenSpecificRepositoryImpl(org.springframework.data.mongodb.core.ReactiveMongoTemplate mongoTemplate)
  • Method Details

    • findByFiscalCodeWithAtLeastOneConsent

      public reactor.core.publisher.Mono<CitizenConsent> findByFiscalCodeWithAtLeastOneConsent(String fiscalCode)

      Finds a citizen with at least one enabled consent across all TPPs.

      Uses $objectToArray to flatten the consents map and filters for tppState = true.

      MongoDB Aggregation Pipeline:

       [
         { "$match": { "fiscalCode": "<fiscalCode>" } },
         { "$project": { "fiscalCode": 1, "consentsArray": { "$objectToArray": "$consents" } } },
         { "$match": { "consentsArray.v.tppState": true } }
       ]
       

      Stages explanation:

      1. Stage 1 ($match): Filter by fiscal code
      2. Stage 2 ($project): Convert consents map to array for filtering
      3. Stage 3 ($match): Filter consents where tppState = true

      Note: The result will have consents = null because the projection transforms the original map structure into consentsArray.

      Specified by:
      findByFiscalCodeWithAtLeastOneConsent in interface CitizenSpecificRepository
      Parameters:
      fiscalCode - citizen's fiscal code
      Returns:
      Mono<CitizenConsent> with fiscalCode only (consents is null), empty if no enabled consents
    • findByFiscalCodeAndTppId

      public reactor.core.publisher.Mono<CitizenConsent> findByFiscalCodeAndTppId(String fiscalCode, String tppId)

      Finds a specific TPP consent within a citizen's document.

      Projects only the requested consent field (consents.<tppId>) using MongoDB aggregation.

      MongoDB Aggregation Pipeline:

       [
         { "$match": { "fiscalCode": "<fiscalCode>" } },
         { "$match": { "consents.<tppId>": { "$exists": true } } },
         { "$project": { "fiscalCode": 1, "consents.<tppId>": 1 } }
       ]
       

      Stages explanation:

      1. Stage 1 ($match): Filter by fiscal code
      2. Stage 2 ($match): Check if consents.<tppId> exists
      3. Stage 3 ($project): Return only fiscalCode and the specific consent

      Example result:

       {
         "fiscalCode": "TESTCF00A00B000C",
         "consents": {
           "e441825b-...": { "tppState": true, "tcDate": "2025-10-29T12:04:21.251" }
         }
       }
       
      Specified by:
      findByFiscalCodeAndTppId in interface CitizenSpecificRepository
      Parameters:
      fiscalCode - citizen's fiscal code
      tppId - TPP identifier (if null, returns empty Mono)
      Returns:
      Mono<CitizenConsent> with single consent, empty if not found or tppId is null
    • findByTppIdEnabled

      public reactor.core.publisher.Flux<CitizenConsent> findByTppIdEnabled(String tppId)

      Finds all citizens with an enabled consent for a specific TPP.

      Filters documents where consents.<tppId>.tppState = true and projects only fiscal code and the matching consent.

      MongoDB Aggregation Pipeline:

       [
         { "$match": { "consents.<tppId>.tppState": true } },
         { "$project": { "fiscalCode": 1, "consents.<tppId>": 1 } }
       ]
       

      Stages explanation:

      1. Stage 1 ($match): Filter documents where consents.<tppId>.tppState = true
      2. Stage 2 ($project): Return only fiscalCode and the matching consent

      Performance note: This query can return multiple documents (one per citizen with enabled consent for the given TPP). Consider adding pagination for production use.

      Example result:

       [
         {
           "fiscalCode": "TESTCF00A00B000C",
           "consents": {
             "e441825b-...": { "tppState": true, "tcDate": "2025-10-29T12:04:21.369" }
           }
         },
         { ... }
       ]
       
      Specified by:
      findByTppIdEnabled in interface CitizenSpecificRepository
      Parameters:
      tppId - TPP identifier
      Returns:
      Flux<CitizenConsent> emitting all citizens with enabled consent (possibly empty)