Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,36 @@ public interface StaffRepository extends JpaRepository<Staff, Long>, JpaSpecific

public final static String FIND_BY_OFFICE_QUERY = "select s from Staff s where s.id = :id AND s.office.id = :officeId";

public final static String IS_ANY_ACTIVE_ENTITY_ASSOCIATED_WITH_STAFF_QUERY = "select " +

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding a native query in StaffRepository, add new method in StaffReadPlatformService and use JDBC template to run this query

"case when (sum(total) > 0) then true else false end " +
"from (" +
"(select count(mg.id) as total " +
"from m_staff ms " +
"join m_group mg on mg.staff_id = ms.id " +
"where ms.id = :staffId " +
"and mg.status_enum in (100,300)) " +
"union all " +
"(select count(ml.id) as total " +
"from m_loan ml " +
"join m_staff ms on ml.loan_officer_id = ms.id " +
"where ms.id = :staffId " +
"and ml.loan_status_id in (100,200,300,700)) " +
"union all " +
"(select count(msa.id) as total " +
"from m_savings_account msa " +
"join m_staff ms on msa.field_officer_id = ms.id " +
"where ms.id = :staffId and " +
"msa.status_enum in (100,300))) as t1 ";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Client and share accounts not checked.

/**
* Find staff by officeid.
*/
@Query(FIND_BY_OFFICE_QUERY)
public Staff findByOffice(@Param("id") Long id, @Param("officeId") Long officeId);

}
/**
* Query finds if a staff is attached to active, pending group or loans or savings
*/
@Query(value=IS_ANY_ACTIVE_ENTITY_ASSOCIATED_WITH_STAFF_QUERY, nativeQuery = true)
public boolean isAnyActiveEntityAssociatedWithStaff(@Param("staffId") Long staffId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.organisation.staff.exception;

import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException;

public class StaffCannotBeDeactivatedException extends AbstractPlatformDomainRuleException {
private static final long serialVersionUID = -4991377873402585935L;

public StaffCannotBeDeactivatedException(final Long id) {
super("error.mgs.staff.cannot.be.deactivated", "Staff with identifier " + id + " cannot be deactivated.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.fineract.organisation.office.exception.OfficeNotFoundException;
import org.apache.fineract.organisation.staff.domain.Staff;
import org.apache.fineract.organisation.staff.domain.StaffRepository;
import org.apache.fineract.organisation.staff.exception.StaffCannotBeDeactivatedException;
import org.apache.fineract.organisation.staff.exception.StaffNotFoundException;
import org.apache.fineract.organisation.staff.serialization.StaffCommandFromApiJsonDeserializer;
import org.slf4j.Logger;
Expand Down Expand Up @@ -91,6 +92,17 @@ public CommandProcessingResult updateStaff(final Long staffId, final JsonCommand

final Staff staffForUpdate = this.staffRepository.findOne(staffId);
if (staffForUpdate == null) { throw new StaffNotFoundException(staffId); }

final boolean isActive = command.booleanPrimitiveValueOfParameterNamed("isActive");

if(staffForUpdate.isActive() && !isActive) {
final boolean isAnyActiveEntityAssociatedWithStaff = this.staffRepository.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use StaffReadPlatformService instead of repository

isAnyActiveEntityAssociatedWithStaff(staffId);

if (isAnyActiveEntityAssociatedWithStaff) {
throw new StaffCannotBeDeactivatedException(staffId);
}
}

final Map<String, Object> changesOnly = staffForUpdate.update(command);

Expand Down