From b5c9101fa45219246caa5736adfe7ed8633afbfa Mon Sep 17 00:00:00 2001 From: keshav898966 Date: Wed, 24 Jun 2026 12:47:03 +0530 Subject: [PATCH] feat: implement teller creation limit and soft delete with cashier check bypass --- .../organisation/teller/domain/Teller.java | 11 +++++++ .../teller/domain/TellerRepository.java | 2 ++ .../domain/TellerRepositoryWrapper.java | 7 +++++ .../TellerAlreadyExistsException.java | 31 +++++++++++++++++++ ...llerManagementReadPlatformServiceImpl.java | 4 +-- .../TellerWritePlatformServiceJpaImpl.java | 19 +++++------- .../db/changelog/tenant/changelog-tenant.xml | 1 + .../parts/0338_add_soft_delete_to_tellers.xml | 31 +++++++++++++++++++ 8 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 fineract-branch/src/main/java/org/apache/fineract/organisation/teller/exception/TellerAlreadyExistsException.java create mode 100644 fineract-provider/src/main/resources/db/changelog/tenant/parts/0338_add_soft_delete_to_tellers.xml diff --git a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/Teller.java b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/Teller.java index 91e22d10e..e41e7bc27 100644 --- a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/Teller.java +++ b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/Teller.java @@ -76,6 +76,9 @@ public class Teller extends AbstractPersistableCustom { @Column(name = "state", nullable = false) private Integer status; + @Column(name = "is_deleted", nullable = false) + private boolean deleted = false; + @OneToMany(mappedBy = "teller", fetch = FetchType.LAZY) private Set cashiers; @@ -161,4 +164,12 @@ public class Teller extends AbstractPersistableCustom { this.office.getId(); this.cashiers.size(); } + + public void delete() { + this.deleted = true; + } + + public boolean isDeleted() { + return this.deleted; + } } diff --git a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepository.java b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepository.java index 2fdea2a68..6b4c43e63 100644 --- a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepository.java +++ b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepository.java @@ -31,4 +31,6 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface TellerRepository extends JpaRepository, JpaSpecificationExecutor { Collection findTellerByOfficeId(Long officeId); + + long countByDeletedFalse(); } diff --git a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepositoryWrapper.java b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepositoryWrapper.java index f0b335d1e..494c4069c 100644 --- a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepositoryWrapper.java +++ b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/domain/TellerRepositoryWrapper.java @@ -36,6 +36,9 @@ public class TellerRepositoryWrapper { @Transactional(readOnly = true) public Teller findOneWithNotFoundDetection(final Long id) { final Teller teller = this.repository.findById(id).orElseThrow(() -> new TellerNotFoundException(id)); + if (teller.isDeleted()) { + throw new TellerNotFoundException(id); + } teller.initializeLazyCollections(); return teller; } @@ -51,4 +54,8 @@ public class TellerRepositoryWrapper { public void delete(final Teller teller) { this.repository.delete(teller); } + + public boolean existsAny() { + return this.repository.countByDeletedFalse() > 0; + } } diff --git a/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/exception/TellerAlreadyExistsException.java b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/exception/TellerAlreadyExistsException.java new file mode 100644 index 000000000..a465aa44d --- /dev/null +++ b/fineract-branch/src/main/java/org/apache/fineract/organisation/teller/exception/TellerAlreadyExistsException.java @@ -0,0 +1,31 @@ +/** + * 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.teller.exception; + +import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException; + +public class TellerAlreadyExistsException extends AbstractPlatformDomainRuleException { + + private static final String ERROR_MESSAGE_CODE = "error.msg.teller.already.exists"; + private static final String DEFAULT_ERROR_MESSAGE = "Cannot create teller as a teller is already present."; + + public TellerAlreadyExistsException() { + super(ERROR_MESSAGE_CODE, DEFAULT_ERROR_MESSAGE); + } +} diff --git a/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerManagementReadPlatformServiceImpl.java b/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerManagementReadPlatformServiceImpl.java index aa694751a..2f05b3d4a 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerManagementReadPlatformServiceImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerManagementReadPlatformServiceImpl.java @@ -120,7 +120,7 @@ public class TellerManagementReadPlatformServiceImpl implements TellerManagement try { final TellerMapper tm = new TellerMapper(); - final String sql = "select " + tm.schema() + " where t.id = ?"; + final String sql = "select " + tm.schema() + " where t.id = ? and t.is_deleted = false"; return this.jdbcTemplate.queryForObject(sql, tm, new Object[] { tellerId }); // NOSONAR } catch (final EmptyResultDataAccessException e) { @@ -198,7 +198,7 @@ public class TellerManagementReadPlatformServiceImpl implements TellerManagement hierarchySearchString = hierarchy + "%"; } final TellerMapper tm = new TellerMapper(); - final String sql = "select " + tm.schema() + "where o.hierarchy like ? order by o.hierarchy"; + final String sql = "select " + tm.schema() + "where o.hierarchy like ? and t.is_deleted = false order by o.hierarchy"; return this.jdbcTemplate.query(sql, tm, new Object[] { hierarchySearchString }); // NOSONAR } diff --git a/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerWritePlatformServiceJpaImpl.java b/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerWritePlatformServiceJpaImpl.java index 0561cca45..a2475b4c2 100644 --- a/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerWritePlatformServiceJpaImpl.java +++ b/fineract-provider/src/main/java/org/apache/fineract/organisation/teller/service/TellerWritePlatformServiceJpaImpl.java @@ -46,6 +46,7 @@ import org.apache.fineract.organisation.teller.domain.Teller; import org.apache.fineract.organisation.teller.domain.TellerRepositoryWrapper; import org.apache.fineract.organisation.teller.exception.CashierExistForTellerException; import org.apache.fineract.organisation.teller.exception.CashierNotFoundException; +import org.apache.fineract.organisation.teller.exception.TellerAlreadyExistsException; import org.apache.fineract.organisation.teller.serialization.TellerCommandFromApiJsonDeserializer; import org.apache.fineract.useradministration.domain.AppUser; import org.springframework.dao.DataIntegrityViolationException; @@ -71,6 +72,10 @@ public class TellerWritePlatformServiceJpaImpl implements TellerWritePlatformSer try { this.context.authenticatedUser(); + if (this.tellerRepositoryWrapper.existsAny()) { + throw new TellerAlreadyExistsException(); + } + final Long officeId = command.longValueOfParameterNamed("officeId"); this.fromApiJsonDeserializer.validateForCreateAndUpdateTeller(command.json()); @@ -152,22 +157,12 @@ public class TellerWritePlatformServiceJpaImpl implements TellerWritePlatformSer @Override @Transactional public CommandProcessingResult deleteTeller(Long tellerId) { - // TODO Auto-generated method stub - Teller teller = tellerRepositoryWrapper.findOneWithNotFoundDetection(tellerId); - Set isTellerIdPresentInCashier = teller.getCashiers(); - - for (final Cashier tellerIdInCashier : isTellerIdPresentInCashier) { - if (tellerIdInCashier.getTeller().getId().toString().equalsIgnoreCase(tellerId.toString())) { - throw new CashierExistForTellerException(tellerId); - } - - } - tellerRepositoryWrapper.delete(teller); + teller.delete(); + tellerRepositoryWrapper.saveAndFlush(teller); return new CommandProcessingResultBuilder() // .withEntityId(teller.getId()) // .build(); - } /* diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml index 7500d4c10..b25b0f775 100644 --- a/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml +++ b/fineract-provider/src/main/resources/db/changelog/tenant/changelog-tenant.xml @@ -365,4 +365,5 @@ + diff --git a/fineract-provider/src/main/resources/db/changelog/tenant/parts/0338_add_soft_delete_to_tellers.xml b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0338_add_soft_delete_to_tellers.xml new file mode 100644 index 000000000..f544cbb71 --- /dev/null +++ b/fineract-provider/src/main/resources/db/changelog/tenant/parts/0338_add_soft_delete_to_tellers.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + +