Skip to content

Commit

Permalink
#624 - update audit fields of missing entity types that are descendan…
Browse files Browse the repository at this point in the history
…ts of subject.
  • Loading branch information
petmongrels committed Oct 19, 2023
1 parent 62645e9 commit 4c0e008
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import org.avni.server.domain.ChecklistItem;
import org.avni.server.domain.Individual;
import org.avni.server.domain.ProgramEnrolment;
import org.avni.server.framework.security.UserContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -46,4 +49,11 @@ default boolean isEntityChanged(SyncParameters syncParameters){
) > 0;
}

@Modifying(clearAutomatically = true)
@Query(value = "update checklist_item ci set last_modified_date_time = :lastModifiedDateTime, last_modified_by_id = :lastModifiedById" +
" from program_enrolment pe, checklist c where c.id = ci.checklist_id and pe.id = c.program_enrolment_id and pe.id = :individualId", nativeQuery = true)
void setChangedForSync(Long individualId, Date lastModifiedDateTime, Long lastModifiedById);
default void setChangedForSync(Individual individual) {
this.setChangedForSync(individual.getId(), new Date(), UserContextHolder.getUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import org.avni.server.domain.ChecklistDetail;
import org.avni.server.domain.Individual;
import org.avni.server.domain.ProgramEnrolment;
import org.avni.server.framework.security.UserContextHolder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -49,4 +52,11 @@ default boolean isEntityChanged(SyncParameters syncParameters){
) > 0;
}

@Modifying(clearAutomatically = true)
@Query(value = "update checklist c set last_modified_date_time = :lastModifiedDateTime, last_modified_by_id = :lastModifiedById" +
" from program_enrolment pe where pe.id = c.program_enrolment_id and pe.id = :individualId", nativeQuery = true)
void setChangedForSync(Long individualId, Date lastModifiedDateTime, Long lastModifiedById);
default void setChangedForSync(Individual individual) {
this.setChangedForSync(individual.getId(), new Date(), UserContextHolder.getUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -79,4 +80,12 @@ default boolean isEntityChanged(SyncParameters syncParameters) {
}

List<IndividualRelationship> findByIndividualaAndIndividualBAndIsVoidedFalse(Individual individualA, Individual individualB);

@Modifying(clearAutomatically = true)
@Query(value = "update individual_relationship ir set last_modified_date_time = :lastModifiedDateTime, last_modified_by_id = :lastModifiedById" +
" where ir.individual_a_id = :individualId or ir.individual_b_id = :individualId", nativeQuery = true)
void setChangedForSync(Long individualId, Date lastModifiedDateTime, Long lastModifiedById);
default void setChangedForSync(Individual individual) {
this.setChangedForSync(individual.getId(), new Date(), UserContextHolder.getUserId());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avni.server.service;

import org.avni.server.dao.*;
import org.avni.server.dao.individualRelationship.IndividualRelationshipRepository;
import org.avni.server.dao.sync.SyncEntityName;
import org.avni.server.domain.*;
import org.avni.server.framework.security.UserContextHolder;
Expand All @@ -25,6 +26,9 @@ public class SubjectMigrationService implements ScopeAwareService<SubjectMigrati
private final ProgramEncounterRepository programEncounterRepository;
private final GroupSubjectRepository groupSubjectRepository;
private final AddressLevelService addressLevelService;
private final ChecklistRepository checklistRepository;
private final ChecklistItemRepository checklistItemRepository;
private final IndividualRelationshipRepository individualRelationshipRepository;

@Autowired
public SubjectMigrationService(EntityApprovalStatusRepository entityApprovalStatusRepository,
Expand All @@ -34,7 +38,10 @@ public SubjectMigrationService(EntityApprovalStatusRepository entityApprovalStat
EncounterRepository encounterRepository,
ProgramEnrolmentRepository programEnrolmentRepository,
ProgramEncounterRepository programEncounterRepository,
GroupSubjectRepository groupSubjectRepository, AddressLevelService addressLevelService) {
GroupSubjectRepository groupSubjectRepository, AddressLevelService addressLevelService,
ChecklistRepository checklistRepository,
ChecklistItemRepository checklistItemRepository,
IndividualRelationshipRepository individualRelationshipRepository) {
this.entityApprovalStatusRepository = entityApprovalStatusRepository;
this.subjectMigrationRepository = subjectMigrationRepository;
this.subjectTypeRepository = subjectTypeRepository;
Expand All @@ -44,6 +51,9 @@ public SubjectMigrationService(EntityApprovalStatusRepository entityApprovalStat
this.programEncounterRepository = programEncounterRepository;
this.groupSubjectRepository = groupSubjectRepository;
this.addressLevelService = addressLevelService;
this.checklistRepository = checklistRepository;
this.checklistItemRepository = checklistItemRepository;
this.individualRelationshipRepository = individualRelationshipRepository;
}

@Override
Expand Down Expand Up @@ -102,6 +112,9 @@ public void markSubjectMigrationIfRequired(String individualUuid, AddressLevel n
groupSubjectRepository.updateSyncAttributesForGroupSubject(individual.getId(), newAddressLevel.getId(), newObsSingleStringValueSyncConcept1, newObsSingleStringValueSyncConcept2);
entityApprovalStatusRepository.updateSyncAttributesForIndividual(individual.getId(), newAddressLevel.getId(), newObsSingleStringValueSyncConcept1, newObsSingleStringValueSyncConcept2);
groupSubjectRepository.updateSyncAttributesForMemberSubject(individual.getId(), newAddressLevel.getId());
checklistItemRepository.setChangedForSync(individual);
checklistRepository.setChangedForSync(individual);
individualRelationshipRepository.setChangedForSync(individual);
}
}
}
Expand Down

0 comments on commit 4c0e008

Please sign in to comment.