Skip to content

Commit

Permalink
OAM-251: Updated DELETE facilities endpoint for wards/services (#118)
Browse files Browse the repository at this point in the history
* OAM-251: Updated DELETE facilities endpoint for wards/services

* OAM-251: Added requisition group members removing for delete facility
  • Loading branch information
sradziszewski authored Oct 21, 2024
1 parent 261bc37 commit 92fb74a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

public interface RequisitionGroupRepository
extends JpaRepository<RequisitionGroup, UUID>, RequisitionGroupRepositoryCustom,
BaseAuditableRepository<RequisitionGroup, UUID> {

<S extends RequisitionGroup> S findByCode(String code);

@Transactional
@Modifying
@Query(value = "DELETE FROM referencedata.requisition_group_members "
+ "WHERE facilityid = :facilityId", nativeQuery = true)
void deleteRequisitionGroupMembersByFacilityId(UUID facilityId);

@Query(value = "SELECT\n"
+ " r.*\n"
+ "FROM\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.openlmis.referencedata.repository.FacilityTypeApprovedProductRepository;
import org.openlmis.referencedata.repository.GeographicZoneRepository;
import org.openlmis.referencedata.repository.OrderableRepository;
import org.openlmis.referencedata.repository.RequisitionGroupRepository;
import org.openlmis.referencedata.service.FacilityBuilder;
import org.openlmis.referencedata.service.FacilityService;
import org.openlmis.referencedata.service.RightAssignmentService;
Expand Down Expand Up @@ -113,6 +114,9 @@ public class FacilityController extends BaseController {
@Autowired
private GeographicZoneRepository geographicZoneRepository;

@Autowired
private RequisitionGroupRepository requisitionGroupRepository;

/**
* Allows creating new facilities. If the id is specified, it will be ignored.
*
Expand Down Expand Up @@ -439,13 +443,22 @@ public void deleteFacility(@PathVariable("id") UUID facilityId) {
checkAdminRight(RightName.FACILITIES_MANAGE_RIGHT, profiler);

Facility facility = findFacility(facilityId, profiler);

profiler.start("DELETE_REQUISITION_GROUP_MEMBERS");
requisitionGroupRepository.deleteRequisitionGroupMembersByFacilityId(facility.getId());

profiler.start("DELETE_FACILITY");
facilityRepository.delete(facility);

if (!facility.getType().getCode().equals(WARD_SERVICE_TYPE_CODE)) {
GeographicZone geographicZone = facility.getGeographicZone();
List<Facility> wardsAndServices = facilityRepository.findByGeographicZone(geographicZone);

profiler.start("DELETE_REQUISITION_GROUP_MEMBERS");
for (Facility ward : wardsAndServices) {
requisitionGroupRepository.deleteRequisitionGroupMembersByFacilityId(ward.getId());
}

profiler.start("DELETE_WARDS_AND_SERVICES");
facilityRepository.deleteAll(wardsAndServices);

Expand Down

0 comments on commit 92fb74a

Please sign in to comment.