Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added logs/admin-api.log.json.2026-02-26.gz
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,23 @@

}

@RequestMapping(value = "/getItemMappingsByFacility", headers = "Authorization", method = {

Check warning on line 231 in src/main/java/com/iemr/admin/controller/itemfacilitymapping/MItemFacilityMappingController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace "@RequestMapping(method = RequestMethod.POST)" with "@PostMapping"

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w4vHa9wv8L9j38e&open=AZ0o2w4vHa9wv8L9j38e&pullRequest=128
RequestMethod.POST }, produces = { "application/json" })
public String getItemMappingsByFacility(@RequestBody String request) {
OutputResponse response = new OutputResponse();
try {
V_fetchItemFacilityMap reqObj = InputMapper.gson().fromJson(request,
V_fetchItemFacilityMap.class);
ArrayList<V_fetchItemFacilityMap> data = M_itemfacilitymappingInter
.getItemMappingsByFacilityID(reqObj.getFacilityID());
response.setResponse(data.toString());
} catch (Exception e) {
logger.error("Unexpected error:", e);
response.setError(e);
}
return response.toString();
}

@Operation(summary = "Get item from store id")
@RequestMapping(value = "/getItemFromStoreID/{storeID}", headers = "Authorization", method = {
RequestMethod.POST }, produces = { "application/json" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public interface V_fetchItemFacilityMapRepo extends CrudRepository<V_fetchItemFa
@Query("SELECT u FROM V_fetchItemFacilityMap u where u.providerServiceMapID = :providerServiceMapID")
ArrayList<V_fetchItemFacilityMap> getAllFacilityMappedData(@Param("providerServiceMapID") Integer providerServiceMapID);

@Query("SELECT u FROM V_fetchItemFacilityMap u where u.facilityID = :facilityID")
ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityID(@Param("facilityID") Integer facilityID);

@Query(value = "SELECT u.* FROM v_fetchItemFacilityMap u WHERE u.FacilityID = :facilityID "
+ "OR u.FacilityID IN (SELECT f.FacilityID FROM m_facility f WHERE f.MainFacilityID = :facilityID AND f.Deleted = false)",
nativeQuery = true)
ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityAndSubStores(@Param("facilityID") Integer facilityID);

}
10 changes: 10 additions & 0 deletions src/main/java/com/iemr/admin/repository/store/MainStoreRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public interface MainStoreRepo extends CrudRepository<M_Facility, Integer> {

List<M_Facility> findByProviderServiceMapIDOrderByFacilityName(Integer providerServiceMapID);

@Query("SELECT f FROM M_Facility f WHERE (f.providerServiceMapID = :providerServiceMapID OR f.providerServiceMapID IS NULL) ORDER BY f.facilityName")
List<M_Facility> findByProviderServiceMapIDOrNullOrderByFacilityName(@Param("providerServiceMapID") Integer providerServiceMapID);

@Query("SELECT u FROM M_Facility u WHERE u.providerServiceMapID=:providerServiceMapID AND u.isMainFacility=:isMainFacility AND deleted=false order by u.facilityName")
ArrayList<M_Facility> getAllMainFacility(@Param("providerServiceMapID") Integer providerServiceMapID,
@Param("isMainFacility") Boolean isMainFacility);
Expand Down Expand Up @@ -90,4 +93,11 @@ ArrayList<M_Facility> findByBlockIDAndLevelValue(@Param("blockID") Integer block
int clearParentFacilityID(@Param("parentFacilityID") Integer parentFacilityID,
@Param("modifiedBy") String modifiedBy);

@Modifying
@Query(value = "UPDATE m_facility SET IsMainFacility = :isMainFacility, MainFacilityID = :mainFacilityID, StoreType = :storeType WHERE FacilityID = :facilityID", nativeQuery = true)
int updateStoreFields(@Param("facilityID") Integer facilityID,
@Param("isMainFacility") Boolean isMainFacility,
@Param("mainFacilityID") Integer mainFacilityID,
@Param("storeType") String storeType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public ArrayList<V_fetchItemFacilityMap> getAllFacilityMappedData(Integer provid
return data;
}

@Override
public ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityID(Integer facilityID) {
return v_fetchItemFacilityMapRepo.getItemMappingsByFacilityAndSubStores(facilityID);
}

@Override
public List<ItemInStore> getItemMastersFromStoreID(Integer storeID) {
// TODO Auto-generated method stub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public interface M_itemfacilitymappingInter{
ArrayList<M_itemfacilitymapping> getsubitemforsubStote(Integer providerServiceMapID, Integer facilityID);

ArrayList<V_fetchItemFacilityMap> getAllFacilityMappedData(Integer providerServiceMapID);


ArrayList<V_fetchItemFacilityMap> getItemMappingsByFacilityID(Integer facilityID);

List<ItemInStore> getItemMastersFromStoreID(Integer storeID);

Integer deleteItemStoreMapping(M_itemfacilitymapping storeID);
Expand Down
73 changes: 66 additions & 7 deletions src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
@Override
public List<M_Facility> getAllMainStore(Integer providerServiceMapID) {
// TODO Auto-generated method stub
return (List<M_Facility>) mainStoreRepo.findByProviderServiceMapIDOrderByFacilityName(providerServiceMapID);
return (List<M_Facility>) mainStoreRepo.findByProviderServiceMapIDOrNullOrderByFacilityName(providerServiceMapID);

Check warning on line 103 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unnecessary cast to "List".

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38b&open=AZ0o2w05Ha9wv8L9j38b&pullRequest=128
}

// @Override
Expand Down Expand Up @@ -267,7 +267,7 @@

@Transactional
@Override
public M_Facility createFacilityWithHierarchy(M_Facility facility, List<Integer> villageIDs,

Check failure on line 270 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 45 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38c&open=AZ0o2w05Ha9wv8L9j38c&pullRequest=128
Integer mainVillageID, List<Integer> childFacilityIDs) {
if (mainStoreRepo.existsByFacilityNameAndBlockIDAndDeletedFalse(facility.getFacilityName(), facility.getBlockID())) {
throw new RuntimeException("Facility with this name already exists in this block");
Expand Down Expand Up @@ -321,6 +321,15 @@
child.setParentFacilityID(savedFacility.getFacilityID());
child.setModifiedBy(facility.getCreatedBy());
mainStoreRepo.save(child);

// Only update store fields for NEW hierarchy facilities (PSMID is NULL)
// Existing stores (PSMID set) keep their store chain intact for inventory compatibility
if (child.getProviderServiceMapID() == null) {
if (child.getIsMainFacility() == null || child.getIsMainFacility()) {

Check warning on line 328 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38X&open=AZ0o2w05Ha9wv8L9j38X&pullRequest=128
mainStoreRepo.updateStoreFields(childID, false,
savedFacility.getFacilityID(), "SUB");
}
}
}
}
}
Expand Down Expand Up @@ -356,11 +365,16 @@
throw new Exception("Facility not found");
}
// Fix 19: clear parentFacilityID on children (unlink from hierarchy, don't block)
// Revert children to MainStore since parent is being deleted
ArrayList<M_Facility> children = mainStoreRepo.findByParentFacilityIDAndDeletedFalseOrderByFacilityName(facilityID);
for (M_Facility child : children) {
child.setParentFacilityID(null);
child.setModifiedBy(modifiedBy);
mainStoreRepo.save(child);
// Only revert store fields for new facilities (PSMID NULL)
if (child.getProviderServiceMapID() == null) {
mainStoreRepo.updateStoreFields(child.getFacilityID(), true, null, "MAIN");
}
}
facility.setDeleted(true);
facility.setModifiedBy(modifiedBy);
Expand All @@ -379,21 +393,46 @@

@Transactional
@Override
public M_Facility updateFacilityWithHierarchy(M_Facility facility, List<Integer> villageIDs,

Check failure on line 396 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 83 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38d&open=AZ0o2w05Ha9wv8L9j38d&pullRequest=128
Integer mainVillageID, List<Integer> childFacilityIDs) {
M_Facility existing = mainStoreRepo.findByFacilityID(facility.getFacilityID());
if (existing == null) {
throw new RuntimeException("Facility not found");
}

if (mainStoreRepo.existsByFacilityNameAndBlockIDAndNotFacilityID(facility.getFacilityName(), existing.getBlockID(), facility.getFacilityID())) {
throw new RuntimeException("Facility with this name already exists in this block");
if (existing.getBlockID() != null && facility.getFacilityName() != null) {
if (mainStoreRepo.existsByFacilityNameAndBlockIDAndNotFacilityID(facility.getFacilityName(), existing.getBlockID(), facility.getFacilityID())) {

Check warning on line 404 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38Y&open=AZ0o2w05Ha9wv8L9j38Y&pullRequest=128
throw new RuntimeException("Facility with this name already exists in this block");
}
}

existing.setFacilityName(facility.getFacilityName());
existing.setFacilityDesc(facility.getFacilityDesc());
existing.setFacilityCode(facility.getFacilityCode());
// Rural/Urban and FacilityType are read-only on edit β€” admin must delete and recreate to change
if (facility.getFacilityName() != null) {
existing.setFacilityName(facility.getFacilityName());
}
if (facility.getFacilityDesc() != null) {
existing.setFacilityDesc(facility.getFacilityDesc());
}
if (facility.getFacilityCode() != null) {
existing.setFacilityCode(facility.getFacilityCode());
}
// Set hierarchy fields: facilityType, ruralUrban, and location
if (facility.getFacilityTypeID() != null) {
existing.setFacilityTypeID(facility.getFacilityTypeID());
}
if (facility.getRuralUrban() != null) {
existing.setRuralUrban(facility.getRuralUrban());
}
if (facility.getStateID() != null) {
existing.setStateID(facility.getStateID());
}
if (facility.getDistrictID() != null) {
existing.setDistrictID(facility.getDistrictID());
}
if (facility.getBlockID() != null) {
existing.setBlockID(facility.getBlockID());
}
// Keep store relationships intact (isMainFacility, mainFacilityID, storeType)
// Only hierarchy columns are added. Store chain stays for inventory compatibility.
existing.setMainVillageID(mainVillageID);
existing.setModifiedBy(facility.getModifiedBy());
M_Facility savedFacility = mainStoreRepo.save(existing);
Expand Down Expand Up @@ -458,13 +497,33 @@
}
}
}
// Revert old children to MainStore before re-linking
ArrayList<M_Facility> oldChildren = mainStoreRepo
.findByParentFacilityIDAndDeletedFalseOrderByFacilityName(facility.getFacilityID());
mainStoreRepo.clearParentFacilityID(facility.getFacilityID(), facility.getModifiedBy());
for (M_Facility oldChild : oldChildren) {
if (!childFacilityIDs.contains(oldChild.getFacilityID())) {
// Child was removed β€” revert to MainStore only for new facilities (PSMID NULL)
if (oldChild.getProviderServiceMapID() == null) {

Check warning on line 507 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38Z&open=AZ0o2w05Ha9wv8L9j38Z&pullRequest=128
mainStoreRepo.updateStoreFields(oldChild.getFacilityID(), true, null, "MAIN");
}
}
}

for (Integer childID : childFacilityIDs) {
M_Facility child = mainStoreRepo.findByFacilityID(childID);
if (child != null) {
child.setParentFacilityID(savedFacility.getFacilityID());
child.setModifiedBy(facility.getModifiedBy());
mainStoreRepo.save(child);

// Only update store fields for NEW hierarchy facilities (PSMID is NULL)
if (child.getProviderServiceMapID() == null) {
if (child.getIsMainFacility() == null || child.getIsMainFacility()) {

Check warning on line 522 in src/main/java/com/iemr/admin/service/store/StoreServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge this if statement with the enclosing one.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Admin-API&issues=AZ0o2w05Ha9wv8L9j38a&open=AZ0o2w05Ha9wv8L9j38a&pullRequest=128
mainStoreRepo.updateStoreFields(childID, false,
savedFacility.getFacilityID(), "SUB");
}
}
}
}
}
Expand Down
Loading