adamsaghy commented on code in PR #6021:
URL: https://github.com/apache/fineract/pull/6021#discussion_r3481281785
##########
fineract-provider/src/main/java/org/apache/fineract/portfolio/group/service/GroupingTypesWritePlatformServiceJpaRepositoryImpl.java:
##########
@@ -231,6 +239,181 @@ private CommandProcessingResult createGroupingType(final
JsonCommand command, fi
}
}
+ @Transactional
+ @Override
+ public GroupActivateResponse activateGroup(final GroupActivateRequest
request) {
+ try {
+ final AppUser currentUser = this.context.authenticatedUser();
+ currentUser.validateHasPermissionTo("ACTIVATE_GROUP");
+ final Group group =
this.groupRepository.findOneWithNotFoundDetection(request.getGroupId());
+
+ if (group.isGroup()) {
+ validateGroupRulesBeforeActivation(group);
+ }
+
+ final LocalDate activationDate =
parseDate(request.getActivationDate(), request.getDateFormat(),
request.getLocale());
+
validateOfficeOpeningDateisAfterGroupOrCenterOpeningDate(group.getOffice(),
group.getGroupLevel(), activationDate);
+
+ group.activate(currentUser, activationDate);
+ this.groupRepository.saveAndFlush(group);
+
+ return
GroupActivateResponse.builder().resourceId(group.getId()).officeId(group.officeId()).groupId(group.getId()).build();
+
+ } catch (final JpaSystemException | DataIntegrityViolationException
dve) {
+ handleGroupDataIntegrityIssuesTyped(null, null,
dve.getMostSpecificCause(), dve, GroupTypes.GROUP);
+ return GroupActivateResponse.builder().build();
+ } catch (final PersistenceException dve) {
+ final Throwable throwable =
ExceptionUtils.getRootCause(dve.getCause());
+ handleGroupDataIntegrityIssuesTyped(null, null, throwable, dve,
GroupTypes.GROUP);
+ return GroupActivateResponse.builder().build();
+ }
+ }
+
+ @Transactional
+ @Override
+ public GroupCloseResponse closeGroup(final GroupCloseRequest request) {
+ final AppUser currentUser = this.context.authenticatedUser();
+ currentUser.validateHasPermissionTo("CLOSE_GROUP");
+ final Group group =
this.groupRepository.findOneWithNotFoundDetection(request.getGroupId());
+
+ final LocalDate closureDate = parseDate(request.getClosureDate(),
request.getDateFormat(), request.getLocale());
+
+ final CodeValue closureReason = this.codeValueRepository
+
.findOneByCodeNameAndIdWithNotFoundDetection(GroupingTypesApiConstants.GROUP_CLOSURE_REASON,
request.getClosureReasonId());
+
+ if (group.hasActiveClients()) {
+ final String errorMessage = group.getGroupLevel().getLevelName()
+ + " cannot be closed because of active clients associated
with it.";
+ throw new
InvalidGroupStateTransitionException(group.getGroupLevel().getLevelName(),
"close", "active.clients.exist",
+ errorMessage);
+ }
+
+ validateLoansAndSavingsForGroupOrCenterClose(group, closureDate);
+
+
entityDatatableChecksWritePlatformService.runTheCheck(request.getGroupId(),
EntityTables.GROUP.getName(),
+ StatusEnum.CLOSE.getValue(),
EntityTables.GROUP.getForeignKeyColumnNameOnDatatable(), null);
+
+ group.close(currentUser, closureReason, closureDate);
+ this.groupRepository.saveAndFlush(group);
+
+ return
GroupCloseResponse.builder().resourceId(group.getId()).groupId(group.getId()).build();
+ }
+
+ @Transactional
+ @Override
+ public GroupAssignStaffResponse assignGroupStaff(final
GroupAssignStaffRequest request) {
+
this.context.authenticatedUser().validateHasPermissionTo("ASSIGNSTAFF_GROUP");
+
+ final Map<String, Object> actualChanges = new LinkedHashMap<>(5);
+
+ final Group groupForUpdate =
this.groupRepository.findOneWithNotFoundDetection(request.getGroupId());
+
+ final Staff staff =
this.staffRepository.findByOfficeHierarchyWithNotFoundDetection(request.getStaffId(),
+ groupForUpdate.getOffice().getHierarchy());
+ groupForUpdate.updateStaff(staff);
+
+ if (Boolean.TRUE.equals(request.getInheritStaffForClientAccounts())) {
+ LocalDate loanOfficerReassignmentDate =
DateUtils.getBusinessLocalDate();
+ Set<Client> clients = groupForUpdate.getClientMembers();
+ if (clients != null) {
+ for (Client client : clients) {
+ client.updateStaff(staff);
+ if
(this.loanRepositoryWrapper.doNonClosedLoanAccountsExistForClient(client.getId()))
{
+ for (final Loan loan :
this.loanRepositoryWrapper.findLoanByClientId(client.getId())) {
+ if (loan.isDisbursed() && !loan.isClosed()) {
+ loanOfficerService.reassignLoanOfficer(loan,
staff, loanOfficerReassignmentDate);
+ }
+ }
+ }
+ if
(this.savingsAccountRepositoryWrapper.doNonClosedSavingAccountsExistForClient(client.getId()))
{
+ for (final SavingsAccount savingsAccount :
this.savingsAccountRepositoryWrapper
+ .findSavingAccountByClientId(client.getId())) {
+ if (!savingsAccount.isClosed()) {
+ savingsAccount.reassignSavingsOfficer(staff,
loanOfficerReassignmentDate);
+ }
+ }
+ }
+ }
+ }
+ }
+ this.groupRepository.saveAndFlush(groupForUpdate);
+
+ actualChanges.put(GroupingTypesApiConstants.staffIdParamName,
request.getStaffId());
+
+ return
GroupAssignStaffResponse.builder().resourceId(groupForUpdate.getId()).officeId(groupForUpdate.officeId())
+ .groupId(request.getGroupId()).changes(actualChanges).build();
+ }
+
+ @Transactional
+ @Override
+ public GroupUnassignStaffResponse unassignGroupStaff(final
GroupUnassignStaffRequest request) {
+
this.context.authenticatedUser().validateHasPermissionTo("UNASSIGNSTAFF_GROUP");
+
+ final Map<String, Object> actualChanges = new LinkedHashMap<>(9);
+
+ final Group groupForUpdate =
this.groupRepository.findOneWithNotFoundDetection(request.getGroupId());
+ final Staff presentStaff = groupForUpdate.getStaff();
+ if (presentStaff == null) {
+ throw new GroupHasNoStaffException(request.getGroupId());
+ }
+
+ final Long presentStaffId = presentStaff.getId();
+ if (request.getStaffId() != null &&
request.getStaffId().equals(presentStaffId)) {
+ groupForUpdate.unassignStaff();
+ }
+
+ this.groupRepository.saveAndFlush(groupForUpdate);
+
+ actualChanges.put(GroupingTypesApiConstants.staffIdParamName, null);
+
+ return
GroupUnassignStaffResponse.builder().resourceId(groupForUpdate.getId()).officeId(groupForUpdate.officeId())
+ .groupId(request.getGroupId()).changes(actualChanges).build();
+ }
+
+ private LocalDate parseDate(final String value, final String dateFormat,
final String locale) {
+ if (value == null || value.isBlank()) {
+ return null;
+ }
+ final String fmt = (dateFormat == null || dateFormat.isBlank()) ? "dd
MMMM yyyy" : dateFormat;
+ final java.util.Locale loc = (locale == null || locale.isBlank()) ?
java.util.Locale.ENGLISH
+ : java.util.Locale.forLanguageTag(locale);
+ try {
+ return LocalDate.parse(value,
java.time.format.DateTimeFormatter.ofPattern(fmt, loc));
+ } catch (java.time.format.DateTimeParseException ex) {
+ throw ErrorHandler.getMappable(ex,
"error.msg.invalid.date.format", "Invalid date `" + value + "` with format `" +
fmt + "`");
Review Comment:
I dont think we need this. Our error mapping logic would catch it eventually
and do the same...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]