diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryImportServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryImportServiceImpl.java index e77f703fa..5e6355a30 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryImportServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryImportServiceImpl.java @@ -144,6 +144,7 @@ public MotherImportRejection importMotherRecord(Map record, Subs String action = ""; Boolean flagForMcts = true; String ashaId ; + Long caseNo; if (importOrigin.equals(SubscriptionOrigin.MCTS_IMPORT)) { action = actionFinderService.motherActionFinder(convertMapToMother(record)); beneficiaryId = (String) record.get(KilkariConstants.BENEFICIARY_ID); @@ -153,6 +154,7 @@ public MotherImportRejection importMotherRecord(Map record, Subs stillBirth = (Boolean) record.get(KilkariConstants.STILLBIRTH); lastUpdatedDateNic = (LocalDate) record.get(KilkariConstants.LAST_UPDATE_DATE); ashaId = (String) record.get(KilkariConstants.KILKARI_ASHA_ID); + caseNo = (Long) record.get(KilkariConstants.CASE_NO); } else { flagForMcts = false; action = actionFinderService.rchMotherActionFinder((convertMapToRchMother(record))); @@ -164,6 +166,7 @@ public MotherImportRejection importMotherRecord(Map record, Subs stillBirth = (Boolean) record.get(KilkariConstants.DELIVERY_OUTCOMES); lastUpdatedDateNic = (LocalDate) record.get(KilkariConstants.EXECUTION_DATE); ashaId = (String) record.get(KilkariConstants.KILKARI_ASHA_ID); + caseNo = (Long) record.get(KilkariConstants.CASE_NO); } LOGGER.trace("MotherImportRejection::importMotherRecord Start " + beneficiaryId) ; @@ -203,7 +206,7 @@ public MotherImportRejection importMotherRecord(Map record, Subs //new rejection reason less_than_12_week boolean isServiceable=validateIsServiceable(lmp, SubscriptionPackType.PREGNANCY, msisdn, beneficiaryId, importOrigin); - if (!isServiceable) { + if (!isServiceable && (action.equals("CREATE") || mother.getMaxCaseNo() < caseNo)) { return createUpdateMotherRejections(flagForMcts, record, action, RejectionReasons.LESS_THAN_12_WEEK, false); } @@ -289,7 +292,6 @@ public MotherImportRejection importMotherRecord(Map record, Subs return createUpdateMotherRejections(flagForMcts, record, action, RejectionReasons.ACTIVE_CHILD_PRESENT, false); } - Long caseNo = (Long) record.get(KilkariConstants.CASE_NO); // validate caseNo if (!validateCaseNo(caseNo, mother)) { LOGGER.debug("MotherImportRejection::importMotherRecord End synchronized block " + beneficiaryId); @@ -463,7 +465,7 @@ public ChildImportRejection importChildRecord(Map record, Subscr //new rejection reason less_than_12_week boolean isServiceable=validateIsServiceable(dob, SubscriptionPackType.CHILD, msisdn, childId, importOrigin); - if (!isServiceable) { + if (!isServiceable && action.equals("CREATE")) { return createUpdateChildRejections(flagForMcts, record, action, RejectionReasons.LESS_THAN_12_WEEK, false); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 5dc6319ec..5588ae925 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -584,6 +584,17 @@ private void liveBirthChildDeathCheck(Subscription subscription, Map record, String action) { //NOPMD NcssMethodCount District district = childUpdate.getDistrict(); // district should never be null here since we validate upstream on setLocation @@ -614,10 +625,23 @@ public ChildImportRejection updateRchChildSubscriber(Long msisdn, MctsChild chil } } + if(subscriberByRchId!=null && childUpdate.getMother()!=null && childUpdate.getMother().getId() != null && getSubscriberListByMother(childUpdate.getMother().getId()) != null){ + Subscriber motherSubscriberByRchId = getSubscriberListByMother(childUpdate.getMother().getId()); + Subscription subscription = subscriptionService.getActiveSubscription(motherSubscriberByRchId, pack.getType()); + if(!Objects.equals(motherSubscriberByRchId.getId(), subscriberByRchId.getId()) && subscription!=null){ + return childRejectionRch(convertMapToRchChild(record), false, RejectionReasons.ALREADY_SUBSCRIBED.toString(), action); + } + } + if (subscriberByRchId != null) { //subscriber exists with the provided RCH id if (subscribersByMsisdn.isEmpty()) { //no subscriber with provided msisdn //subscriber's number has changed //update msisdn in subscriber and delete msisdn from blocked list + if(subscriberByRchId.getMother()==null && childUpdate.getMother()!= null){ + Subscriber motherSubscriber = getSubscriberListByMother(childUpdate.getMother().getId()); + Subscription motherSubscription = subscriptionService.getActiveSubscription(motherSubscriber,SubscriptionPackType.PREGNANCY); + liveBirthChildCheck(motherSubscription); + } subscriptionService.deleteBlockedMsisdn(childUpdate.getId(), subscriberByRchId.getCallingNumber(), msisdn); subscriberByRchId.setCallingNumber(msisdn); if (subscriberByRchId.getMother() == null) { @@ -631,7 +655,43 @@ public ChildImportRejection updateRchChildSubscriber(Long msisdn, MctsChild chil finalSubscription = updateOrCreateSubscription(subscriberByRchId, subscription, dob, pack, language, circle, SubscriptionOrigin.RCH_IMPORT, false); } else { //subscriber found with provided msisdn - if(childUpdate.getMother()!=null){ + if(subscriberByRchId.getMother()==null && childUpdate.getMother()!= null){ + Boolean isSameSubscriber = true; + Subscriber motherSubscriber = getSubscriberListByMother(childUpdate.getMother().getId()); + Subscription motherSubscription = subscriptionService.getActiveSubscription(motherSubscriber,SubscriptionPackType.PREGNANCY); + liveBirthChildCheck(motherSubscription); + + for (Subscriber subscriber : subscribersByMsisdn) { + + if (subscriber.getId().equals(subscriberByRchId.getId())) { + Subscription subscription = subscriptionService.getActiveSubscription(subscriberByRchId, pack.getType()); + if (subscriberByRchId.getMother() == null) { + subscriberByRchId.setMother(childUpdate.getMother()); + } + subscriberByRchId.setDateOfBirth(dob); + subscriberByRchId.setModificationDate(DateTime.now()); + finalSubscription = updateOrCreateSubscription(subscriberByRchId, subscription, dob, pack, language, circle, SubscriptionOrigin.RCH_IMPORT, false); + } else { + //A different subscriber found with same mobile number + isSameSubscriber = false; + } + } + if (!isSameSubscriber) { + if (subscriptionService.activeSubscriptionByMsisdnRch(subscribersByMsisdn, msisdn, SubscriptionPackType.CHILD, motherRchId, childUpdate.getRchId())) { + return childRejectionRch(convertMapToRchChild(record), false, RejectionReasons.MOBILE_NUMBER_ALREADY_SUBSCRIBED.toString(), action); + } else { + subscriberByRchId.setCallingNumber(msisdn); + if (subscriberByRchId.getMother() == null) { + subscriberByRchId.setMother(childUpdate.getMother()); + } + Subscription subscription = subscriptionService.getActiveSubscription(subscriberByRchId, pack.getType()); + subscriberByRchId.setDateOfBirth(dob); + subscriberByRchId.setModificationDate(DateTime.now()); + finalSubscription = updateOrCreateSubscription(subscriberByRchId, subscription, dob, pack, language, circle, SubscriptionOrigin.RCH_IMPORT, false); + } + } + } + else if(childUpdate.getMother()!=null){ Subscriber subscriber = getSubscriberListByMother(childUpdate.getMother().getId()); Subscription subscription = subscriptionService.getActiveSubscription(subscriber, pack.getType()); if ((!Objects.equals(subscriber.getCallingNumber(), msisdn) || subscriber.getDateOfBirth().getDayOfYear() != dob.getDayOfYear()) && subscription!=null){ diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index 4c2b6ad34..c568cb71d 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -60,6 +60,7 @@ public class SubscriptionServiceImpl implements SubscriptionService { private DeactivatedBeneficiaryDataService deactivatedBeneficiaryDataService; private SubscriberMsisdnTrackerDataService subscriberMsisdnTrackerDataService; public static AtomicBoolean isCapacityAvailable = new AtomicBoolean(true); + private static final String HIGH_PRIORITY_BLOCK = "kilkari.highPriority.blockId"; @Autowired @@ -311,7 +312,7 @@ public Boolean activeSubscriptionByMsisdnRch(List subscribers,Long m return (subscriber.getMother() != null && subscriber.getMother().getRchId() != null && !motherRchId.equals(subscriber.getMother().getRchId())); } } else { - return (subscriber.getMother() != null && !motherRchId.equals(subscriber.getMother().getRchId())); + return (subscriber.getMother() == null || !motherRchId.equals(subscriber.getMother().getRchId())); } } } @@ -326,7 +327,7 @@ public Boolean activeSubscriptionByMsisdnRch(List subscribers,Long m subscriptionsSize = subscriptions.size(); if (subscriptionsSize != 0) { if (subscriptionsSize == 1) { - if (SubscriptionPackType.PREGNANCY.equals(subscriptions.get(0).getSubscriptionPack().getType()) && subscriber.getMother() != null && subscriber.getMother().getRchId() != null && motherRchId != null && !motherRchId.equals(subscriber.getMother().getRchId())) { + if (SubscriptionPackType.PREGNANCY.equals(subscriptions.get(0).getSubscriptionPack().getType()) && subscriber.getMother() != null && subscriber.getMother().getRchId() != null && (motherRchId == null || !motherRchId.equals(subscriber.getMother().getRchId()))) { return true; } else if (SubscriptionPackType.PREGNANCY.equals(subscriptions.get(0).getSubscriptionPack().getType()) && subscriber.getMother() != null && subscriber.getMother().getRchId() != null && motherRchId != null && motherRchId.equals(subscriber.getMother().getRchId())) { return false; @@ -886,7 +887,7 @@ public String getSqlQuery() { "JOIN nms_subscription_packs AS sp ON ss.subscriptionPack_id_OID = sp.id " + "JOIN nms_subscribers AS s ON ss.subscriber_id_OID = s.id " + "JOIN nms_mcts_mothers m ON s.mother_id_OID = m.id " + - "WHERE ss.status = 'HOLD' AND m.healthBlock_id_OID IN (3122, 3139, 3144, 1669, 3027, 3240, 3251, 3252, 3266, 3322, 3331, 3340) AND origin in ('MCTS_IMPORT', 'RCH_IMPORT')) AS res1 " + + "WHERE ss.status = 'HOLD' AND m.healthBlock_id_OID IN ( " + highPriorityBlocks() + " ) AND origin in ('MCTS_IMPORT', 'RCH_IMPORT')) AS res1 " + "UNION " + "SELECT res.id as id, res.activationDate, res.deactivationReason, res.endDate, res.firstMessageDayOfWeek, res.needsWelcomeMessageViaObd, " + "res.origin, res.secondMessageDayOfWeek, res.startDate, res.status, res.subscriber_id_OID, res.subscriptionId, res.subscriptionPack_id_OID, " + @@ -1320,5 +1321,12 @@ private Set getWhatsAppStateFilter(){ } return states; } + + private String highPriorityBlocks(){ + if (settingsFacade.getProperty(HIGH_PRIORITY_BLOCK)==null || settingsFacade.getProperty(HIGH_PRIORITY_BLOCK).trim().isEmpty()){ + return "0"; + } + return settingsFacade.getProperty(HIGH_PRIORITY_BLOCK); + } } diff --git a/kilkari/src/main/resources/kilkari.properties b/kilkari/src/main/resources/kilkari.properties index afc8ad018..4e81f6c75 100644 --- a/kilkari/src/main/resources/kilkari.properties +++ b/kilkari/src/main/resources/kilkari.properties @@ -12,3 +12,5 @@ kilkari.accept_new_subscription_for_blocked_msisdn=true kilkari.chunk.size=1000 kilkari.thread.size=10000 + +kilkari.highPriority.blockId=3257999, 3258000, 3258001, 3258002, 3258003, 3258004, 3258005, 3258006, 3258007, 3258008, 3258009, 3258010, 3258011, 3258012, 3258013, 3258014, 3258015, 3258016, 3258017, 3258018, 3258019, 3258020, 3258021, 3258022, 3258023, 3258024, 3258025, 3258026, 3258027, 3258028, 3258029, 3258030, 3258031, 3258032, 3258033, 3258034, 3258035, 3258036, 3258037, 3258038, 3258039, 3258040, 3258041, 3258042, 3258043, 3258044, 3258045, 3258046, 3258047, 3258048, 3258049, 3258050, 3258051, 3258052, 3258053, 3258054, 3258055, 3258056, 3258057, 3258058, 3258059, 3258060, 3258061, 3258062, 3258063, 3258064, 3258065, 3258066, 3258067, 3258068, 3258069, 3258070, 3258071, 3258072, 3258073, 3258074, 3258075, 3258076, 3258077, 3258078, 3258079, 3258080, 3258081, 3258082, 3258083, 3258084, 3258085, 3258086, 3258087, 3258088, 3258089, 3258090, 3258091, 3258092, 3258093, 3258094, 3258095, 3258096, 3258097, 3258098, 3258099, 3258100, 3258101, 3258102, 3258103, 3258104, 3258105, 3258106, 3258107, 3258108, 3258109, 3258110, 3258111, 3258112, 3258113, 3258114, 3258115, 3258116, 3258117, 3258118, 3258119, 3258120, 3258121, 3258122, 3258123, 3258124, 3258125, 3258126, 3258127, 3258128, 3258129, 3258130, 3258131, 3258132, 3258133, 3258134, 3258135, 3258136, 3258137, 3258138, 3258139, 3258140, 3258141, 3258142, 3258143, 3258144, 3258145, 3258146, 3258147, 3258148, 3258149, 3258150, 3258151, 3258152, 3258153, 3258154, 3258155, 3258156, 3258157, 3258158, 3258159, 3258160, 3258161, 3258162, 3258163, 3258164, 3258165, 3258166, 3258167, 3258168, 3258169, 3258170, 3258171, 3258172, 3258173, 3258174, 3258175, 3258176, 3258177, 3258178, 3258179, 3258180, 3257859, 3257860, 3257861, 3257862, 3257863, 3257864, 3257865, 3257866, 3257867, 3257868, 3257869, 3257870, 3257871, 3257872, 3257873, 3257874, 3257875, 3257876, 3122, 3139, 3144, 1669, 3027, 3240, 3251, 3252, 3266, 3322, 3331, 3340 \ No newline at end of file