Class CitizenSpecificRepositoryImpl
- All Implemented Interfaces:
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:
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionCitizenSpecificRepositoryImpl(org.springframework.data.mongodb.core.ReactiveMongoTemplate mongoTemplate) -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<CitizenConsent> findByFiscalCodeAndTppId(String fiscalCode, String tppId) Finds a specific TPP consent within a citizen's document.reactor.core.publisher.Mono<CitizenConsent> findByFiscalCodeWithAtLeastOneConsent(String fiscalCode) Finds a citizen with at least one enabled consent across all TPPs.reactor.core.publisher.Flux<CitizenConsent> findByTppIdEnabled(String tppId) Finds all citizens with an enabled consent for a specific TPP.
-
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
$objectToArrayto flatten the consents map and filters fortppState = true.MongoDB Aggregation Pipeline:
[ { "$match": { "fiscalCode": "<fiscalCode>" } }, { "$project": { "fiscalCode": 1, "consentsArray": { "$objectToArray": "$consents" } } }, { "$match": { "consentsArray.v.tppState": true } } ]Stages explanation:
- Stage 1 ($match): Filter by fiscal code
- Stage 2 ($project): Convert
consentsmap to array for filtering - Stage 3 ($match): Filter consents where
tppState = true
Note: The result will have
consents = nullbecause the projection transforms the original map structure intoconsentsArray.- Specified by:
findByFiscalCodeWithAtLeastOneConsentin interfaceCitizenSpecificRepository- Parameters:
fiscalCode- citizen's fiscal code- Returns:
Mono<CitizenConsent>withfiscalCodeonly (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:
- Stage 1 ($match): Filter by fiscal code
- Stage 2 ($match): Check if
consents.<tppId>exists - Stage 3 ($project): Return only
fiscalCodeand the specific consent
Example result:
{ "fiscalCode": "TESTCF00A00B000C", "consents": { "e441825b-...": { "tppState": true, "tcDate": "2025-10-29T12:04:21.251" } } }- Specified by:
findByFiscalCodeAndTppIdin interfaceCitizenSpecificRepository- Parameters:
fiscalCode- citizen's fiscal codetppId- TPP identifier (ifnull, returns emptyMono)- Returns:
Mono<CitizenConsent>with single consent, empty if not found or tppId isnull
-
findByTppIdEnabled
Finds all citizens with an enabled consent for a specific TPP.
Filters documents where
consents.<tppId>.tppState = trueand projects only fiscal code and the matching consent.MongoDB Aggregation Pipeline:
[ { "$match": { "consents.<tppId>.tppState": true } }, { "$project": { "fiscalCode": 1, "consents.<tppId>": 1 } } ]Stages explanation:
- Stage 1 ($match): Filter documents where
consents.<tppId>.tppState = true - Stage 2 ($project): Return only
fiscalCodeand 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:
findByTppIdEnabledin interfaceCitizenSpecificRepository- Parameters:
tppId- TPP identifier- Returns:
Flux<CitizenConsent>emitting all citizens with enabled consent (possibly empty)
- Stage 1 ($match): Filter documents where
-