|
5 | 5 | import com.lantanagroup.link.measureeval.entities.PatientResource; |
6 | 6 | import com.lantanagroup.link.measureeval.entities.SharedResource; |
7 | 7 | import com.lantanagroup.link.measureeval.records.AbstractResourceRecord; |
| 8 | +import com.lantanagroup.link.shared.mongo.CustomAggregationOperation; |
| 9 | +import org.bson.Document; |
8 | 10 | import org.hl7.fhir.r4.model.ResourceType; |
9 | 11 | import org.springframework.data.mongodb.core.MongoOperations; |
10 | 12 | import org.springframework.data.mongodb.core.aggregation.Aggregation; |
@@ -123,7 +125,27 @@ public List<PatientResource> findPatientResources(String facilityId, String pati |
123 | 125 | .and("reports.reportTrackingId").in(reportIds))); |
124 | 126 | pipeline.add(Aggregation.unwind("resources")); |
125 | 127 | pipeline.add(Aggregation.match(Criteria.where("resources.normalizationStatus").is("NORMALIZED"))); |
126 | | - pipeline.add(Aggregation.lookup("patientResource", "resources.resourceId", "resourceId", "matchedResources")); |
| 128 | + |
| 129 | + Document lookupStage = new Document("$lookup", |
| 130 | + new Document("from", "patientResource") |
| 131 | + .append("let", new Document("localResourceId", "$resources.resourceId") |
| 132 | + .append("localResourceType", "$resources.resourceType")) |
| 133 | + .append("pipeline", List.of( |
| 134 | + new Document("$match", |
| 135 | + new Document("$expr", |
| 136 | + new Document("$and", List.of( |
| 137 | + new Document("$eq", List.of("$resourceId", "$$localResourceId")), |
| 138 | + new Document("$eq", List.of("$resourceType", "$$localResourceType")), |
| 139 | + new Document("$eq", List.of("$facilityId", facilityId)) |
| 140 | + )) |
| 141 | + ) |
| 142 | + ) |
| 143 | + )) |
| 144 | + .append("as", "matchedResources") |
| 145 | + ); |
| 146 | + |
| 147 | + pipeline.add(new CustomAggregationOperation(lookupStage)); |
| 148 | + |
127 | 149 | pipeline.add(Aggregation.unwind("matchedResources")); |
128 | 150 | pipeline.add(Aggregation.match(Criteria.where("matchedResources.facilityId").is(facilityId))); |
129 | 151 | pipeline.add(Aggregation.replaceRoot("matchedResources")); |
|
0 commit comments